DBOracle.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * Oracle adapter.
  11. *
  12. * @author David Giffin <david@giffin.org> (Propel)
  13. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  14. * @author Jon S. Stevens <jon@clearink.com> (Torque)
  15. * @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
  16. * @author Bill Schneider <bschneider@vecna.com> (Torque)
  17. * @author Daniel Rall <dlr@finemaltcoding.com> (Torque)
  18. * @version $Revision: 1669 $
  19. * @package propel.runtime.adapter
  20. */
  21. class DBOracle extends DBAdapter
  22. {
  23. /**
  24. * This method is called after a connection was created to run necessary
  25. * post-initialization queries or code.
  26. * Removes the charset query and adds the date queries
  27. *
  28. * @param PDO A PDO connection instance.
  29. * @see parent::initConnection()
  30. */
  31. public function initConnection(PDO $con, array $settings)
  32. {
  33. $con->exec("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
  34. $con->exec("ALTER SESSION SET NLS_TIMESTAMP_FORMAT='YYYY-MM-DD HH24:MI:SS'");
  35. if (isset($settings['queries']) && is_array($settings['queries'])) {
  36. foreach ($settings['queries'] as $queries) {
  37. foreach ((array)$queries as $query) {
  38. $con->exec($query);
  39. }
  40. }
  41. }
  42. }
  43. /**
  44. * This method is used to ignore case.
  45. *
  46. * @param string $in The string to transform to upper case.
  47. * @return string The upper case string.
  48. */
  49. public function toUpperCase($in)
  50. {
  51. return "UPPER(" . $in . ")";
  52. }
  53. /**
  54. * This method is used to ignore case.
  55. *
  56. * @param string $in The string whose case to ignore.
  57. * @return string The string in a case that can be ignored.
  58. */
  59. public function ignoreCase($in)
  60. {
  61. return "UPPER(" . $in . ")";
  62. }
  63. /**
  64. * Returns SQL which concatenates the second string to the first.
  65. *
  66. * @param string String to concatenate.
  67. * @param string String to append.
  68. * @return string
  69. */
  70. public function concatString($s1, $s2)
  71. {
  72. return "CONCAT($s1, $s2)";
  73. }
  74. /**
  75. * Returns SQL which extracts a substring.
  76. *
  77. * @param string String to extract from.
  78. * @param int Offset to start from.
  79. * @param int Number of characters to extract.
  80. * @return string
  81. */
  82. public function subString($s, $pos, $len)
  83. {
  84. return "SUBSTR($s, $pos, $len)";
  85. }
  86. /**
  87. * Returns SQL which calculates the length (in chars) of a string.
  88. *
  89. * @param string String to calculate length of.
  90. * @return string
  91. */
  92. public function strLength($s)
  93. {
  94. return "LENGTH($s)";
  95. }
  96. /**
  97. * @see DBAdapter::applyLimit()
  98. */
  99. public function applyLimit(&$sql, $offset, $limit, $criteria = null)
  100. {
  101. if (BasePeer::needsSelectAliases($criteria)) {
  102. $selectSql = BasePeer::createSelectSqlPart($criteria, $params, true);
  103. $sql = $selectSql . substr($sql, strpos('FROM', $sql));
  104. }
  105. $sql = 'SELECT B.* FROM ('
  106. . 'SELECT A.*, rownum AS PROPEL_ROWNUM FROM (' . $sql . ') A '
  107. . ') B WHERE ';
  108. if ( $offset > 0 ) {
  109. $sql .= ' B.PROPEL_ROWNUM > ' . $offset;
  110. if ( $limit > 0 ) {
  111. $sql .= ' AND B.PROPEL_ROWNUM <= ' . ( $offset + $limit );
  112. }
  113. } else {
  114. $sql .= ' B.PROPEL_ROWNUM <= ' . $limit;
  115. }
  116. }
  117. protected function getIdMethod()
  118. {
  119. return DBAdapter::ID_METHOD_SEQUENCE;
  120. }
  121. public function getId(PDO $con, $name = null)
  122. {
  123. if ($name === null) {
  124. throw new PropelException("Unable to fetch next sequence ID without sequence name.");
  125. }
  126. $stmt = $con->query("SELECT " . $name . ".nextval FROM dual");
  127. $row = $stmt->fetch(PDO::FETCH_NUM);
  128. return $row[0];
  129. }
  130. public function random($seed=NULL)
  131. {
  132. return 'dbms_random.value';
  133. }
  134. }