QueryBuilder.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /**
  10. * QueryBuilder is the query builder for Oracle databases.
  11. *
  12. */
  13. class QueryBuilder extends \yii\db\QueryBuilder
  14. {
  15. private $sql;
  16. public function build($query)
  17. {
  18. $params = $query->params;
  19. $clauses = [
  20. $this->buildSelect($query->select, $query->distinct, $query->selectOption),
  21. $this->buildFrom($query->from),
  22. $this->buildJoin($query->join, $params),
  23. $this->buildWhere($query->where, $params),
  24. $this->buildGroupBy($query->groupBy),
  25. $this->buildHaving($query->having, $params),
  26. $this->buildUnion($query->union, $params),
  27. $this->buildOrderBy($query->orderBy),
  28. ];
  29. $this->sql = implode($this->separator, array_filter($clauses));
  30. if ($query->limit !== null || $query->offset !== null) {
  31. $this->sql = $this->buildLimit($query->limit, $query->offset);
  32. }
  33. return [$this->sql, $params];
  34. }
  35. public function buildLimit($limit, $offset)
  36. {
  37. if (($limit < 0) && ($offset < 0)) {
  38. return $this->sql;
  39. }
  40. $filters = [];
  41. if ($offset > 0) {
  42. $filters[] = 'rowNumId > ' . (int)$offset;
  43. }
  44. if ($limit >= 0) {
  45. $filters[] = 'rownum <= ' . (int)$limit;
  46. }
  47. if (count($filters) > 0) {
  48. $filter = implode(' and ', $filters);
  49. $filter = " WHERE " . $filter;
  50. } else {
  51. $filter = '';
  52. }
  53. $sql = <<<EOD
  54. WITH USER_SQL AS ({$this->sql}),
  55. PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL)
  56. SELECT *
  57. FROM PAGINATION
  58. {$filter}
  59. EOD;
  60. return $sql;
  61. }
  62. /**
  63. * Builds a SQL statement for renaming a DB table.
  64. *
  65. * @param string $table the table to be renamed. The name will be properly quoted by the method.
  66. * @param string $newName the new table name. The name will be properly quoted by the method.
  67. * @return string the SQL statement for renaming a DB table.
  68. */
  69. public function renameTable($table, $newName)
  70. {
  71. return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' RENAME TO ' . $this->db->quoteTableName($newName);
  72. }
  73. /**
  74. * Builds a SQL statement for changing the definition of a column.
  75. *
  76. * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  77. * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  78. * @param string $type the new column type. The {@link getColumnType} method will be invoked to convert abstract column type (if any)
  79. * into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL.
  80. * For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'.
  81. * @return string the SQL statement for changing the definition of a column.
  82. */
  83. public function alterColumn($table, $column, $type)
  84. {
  85. $type = $this->getColumnType($type);
  86. return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' MODIFY ' . $this->db->quoteColumnName($column) . ' ' . $this->getColumnType($type);
  87. }
  88. /**
  89. * Builds a SQL statement for dropping an index.
  90. *
  91. * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
  92. * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
  93. * @return string the SQL statement for dropping an index.
  94. */
  95. public function dropIndex($name, $table)
  96. {
  97. return 'DROP INDEX ' . $this->db->quoteTableName($name);
  98. }
  99. /**
  100. * @inheritdoc
  101. */
  102. public function resetSequence($table, $value = null)
  103. {
  104. $tableSchema = $this->db->getTableSchema($table);
  105. if ($tableSchema === null) {
  106. throw new InvalidParamException("Unknown table: $table");
  107. }
  108. if ($tableSchema->sequenceName === null) {
  109. return '';
  110. }
  111. if ($value !== null) {
  112. $value = (int)$value;
  113. } else {
  114. $value = (int)$this->db->createCommand("SELECT MAX(\"{$tableSchema->primaryKey}\") FROM \"{$tableSchema->name}\"")->queryScalar();
  115. $value++;
  116. }
  117. return "DROP SEQUENCE \"{$tableSchema->name}_SEQ\";"
  118. . "CREATE SEQUENCE \"{$tableSchema->name}_SEQ\" START WITH {$value} INCREMENT BY 1 NOMAXVALUE NOCACHE";
  119. }
  120. }