You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.7KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\db\oci;
  8. use yii\base\InvalidParamException;
  9. use yii\db\Connection;
  10. use yii\db\Exception;
  11. /**
  12. * QueryBuilder is the query builder for Oracle databases.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @since 2.0
  16. */
  17. class QueryBuilder extends \yii\db\QueryBuilder
  18. {
  19. /**
  20. * @var array mapping from abstract column types (keys) to physical column types (values).
  21. */
  22. public $typeMap = [
  23. Schema::TYPE_PK => 'NUMBER(10) NOT NULL PRIMARY KEY',
  24. Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY',
  25. Schema::TYPE_STRING => 'VARCHAR2(255)',
  26. Schema::TYPE_TEXT => 'CLOB',
  27. Schema::TYPE_SMALLINT => 'NUMBER(5)',
  28. Schema::TYPE_INTEGER => 'NUMBER(10)',
  29. Schema::TYPE_BIGINT => 'NUMBER(20)',
  30. Schema::TYPE_FLOAT => 'NUMBER',
  31. Schema::TYPE_DOUBLE => 'NUMBER',
  32. Schema::TYPE_DECIMAL => 'NUMBER',
  33. Schema::TYPE_DATETIME => 'TIMESTAMP',
  34. Schema::TYPE_TIMESTAMP => 'TIMESTAMP',
  35. Schema::TYPE_TIME => 'TIMESTAMP',
  36. Schema::TYPE_DATE => 'DATE',
  37. Schema::TYPE_BINARY => 'BLOB',
  38. Schema::TYPE_BOOLEAN => 'NUMBER(1)',
  39. Schema::TYPE_MONEY => 'NUMBER(19,4)',
  40. ];
  41. /**
  42. * @inheritdoc
  43. */
  44. public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset)
  45. {
  46. $orderBy = $this->buildOrderBy($orderBy);
  47. if ($orderBy !== '') {
  48. $sql .= $this->separator . $orderBy;
  49. }
  50. $filters = [];
  51. if ($this->hasOffset($offset)) {
  52. $filters[] = 'rowNumId > ' . $offset;
  53. }
  54. if ($this->hasLimit($limit)) {
  55. $filters[] = 'rownum <= ' . $limit;
  56. }
  57. if (empty($filters)) {
  58. return $sql;
  59. }
  60. $filter = implode(' AND ', $filters);
  61. return <<<EOD
  62. WITH USER_SQL AS ($sql),
  63. PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
  64. SELECT *
  65. FROM PAGINATION
  66. WHERE $filter
  67. EOD;
  68. }
  69. /**
  70. * Builds a SQL statement for renaming a DB table.
  71. *
  72. * @param string $table the table to be renamed. The name will be properly quoted by the method.
  73. * @param string $newName the new table name. The name will be properly quoted by the method.
  74. * @return string the SQL statement for renaming a DB table.
  75. */
  76. public function renameTable($table, $newName)
  77. {
  78. return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' RENAME TO ' . $this->db->quoteTableName($newName);
  79. }
  80. /**
  81. * Builds a SQL statement for changing the definition of a column.
  82. *
  83. * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  84. * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  85. * @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any)
  86. * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  87. * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  88. * @return string the SQL statement for changing the definition of a column.
  89. */
  90. public function alterColumn($table, $column, $type)
  91. {
  92. $type = $this->getColumnType($type);
  93. return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' MODIFY ' . $this->db->quoteColumnName($column) . ' ' . $this->getColumnType($type);
  94. }
  95. /**
  96. * Builds a SQL statement for dropping an index.
  97. *
  98. * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
  99. * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
  100. * @return string the SQL statement for dropping an index.
  101. */
  102. public function dropIndex($name, $table)
  103. {
  104. return 'DROP INDEX ' . $this->db->quoteTableName($name);
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function resetSequence($table, $value = null)
  110. {
  111. $tableSchema = $this->db->getTableSchema($table);
  112. if ($tableSchema === null) {
  113. throw new InvalidParamException("Unknown table: $table");
  114. }
  115. if ($tableSchema->sequenceName === null) {
  116. return '';
  117. }
  118. if ($value !== null) {
  119. $value = (int) $value;
  120. } else {
  121. // use master connection to get the biggest PK value
  122. $value = $this->db->useMaster(function (Connection $db) use ($tableSchema) {
  123. return $db->createCommand("SELECT MAX(\"{$tableSchema->primaryKey}\") FROM \"{$tableSchema->name}\"")->queryScalar();
  124. }) + 1;
  125. }
  126. return "DROP SEQUENCE \"{$tableSchema->name}_SEQ\";"
  127. . "CREATE SEQUENCE \"{$tableSchema->name}_SEQ\" START WITH {$value} INCREMENT BY 1 NOMAXVALUE NOCACHE";
  128. }
  129. /**
  130. * @inheritdoc
  131. */
  132. public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
  133. {
  134. $sql = 'ALTER TABLE ' . $this->db->quoteTableName($table)
  135. . ' ADD CONSTRAINT ' . $this->db->quoteColumnName($name)
  136. . ' FOREIGN KEY (' . $this->buildColumns($columns) . ')'
  137. . ' REFERENCES ' . $this->db->quoteTableName($refTable)
  138. . ' (' . $this->buildColumns($refColumns) . ')';
  139. if ($delete !== null) {
  140. $sql .= ' ON DELETE ' . $delete;
  141. }
  142. if ($update !== null) {
  143. throw new Exception('Oracle does not support ON UPDATE clause.');
  144. }
  145. return $sql;
  146. }
  147. }