QueryBuilder.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\cubrid;
  8. use yii\base\InvalidParamException;
  9. use yii\db\Exception;
  10. /**
  11. * QueryBuilder is the query builder for CUBRID databases (version 9.3.x and higher).
  12. *
  13. * @author Carsten Brandt <mail@cebe.cc>
  14. * @since 2.0
  15. */
  16. class QueryBuilder extends \yii\db\QueryBuilder
  17. {
  18. /**
  19. * @var array mapping from abstract column types (keys) to physical column types (values).
  20. */
  21. public $typeMap = [
  22. Schema::TYPE_PK => 'int NOT NULL AUTO_INCREMENT PRIMARY KEY',
  23. Schema::TYPE_UPK => 'int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
  24. Schema::TYPE_BIGPK => 'bigint NOT NULL AUTO_INCREMENT PRIMARY KEY',
  25. Schema::TYPE_UBIGPK => 'bigint UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY',
  26. Schema::TYPE_CHAR => 'char(1)',
  27. Schema::TYPE_STRING => 'varchar(255)',
  28. Schema::TYPE_TEXT => 'varchar',
  29. Schema::TYPE_SMALLINT => 'smallint',
  30. Schema::TYPE_INTEGER => 'int',
  31. Schema::TYPE_BIGINT => 'bigint',
  32. Schema::TYPE_FLOAT => 'float(7)',
  33. Schema::TYPE_DOUBLE => 'double(15)',
  34. Schema::TYPE_DECIMAL => 'decimal(10,0)',
  35. Schema::TYPE_DATETIME => 'datetime',
  36. Schema::TYPE_TIMESTAMP => 'timestamp',
  37. Schema::TYPE_TIME => 'time',
  38. Schema::TYPE_DATE => 'date',
  39. Schema::TYPE_BINARY => 'blob',
  40. Schema::TYPE_BOOLEAN => 'smallint',
  41. Schema::TYPE_MONEY => 'decimal(19,4)',
  42. ];
  43. /**
  44. * @inheritdoc
  45. */
  46. protected $likeEscapeCharacter = '!';
  47. /**
  48. * @inheritdoc
  49. */
  50. protected $likeEscapingReplacements = [
  51. '%' => '!%',
  52. '_' => '!_',
  53. '!' => '!!',
  54. ];
  55. /**
  56. * Creates a SQL statement for resetting the sequence value of a table's primary key.
  57. * The sequence will be reset such that the primary key of the next new row inserted
  58. * will have the specified value or 1.
  59. * @param string $tableName the name of the table whose primary key sequence will be reset
  60. * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
  61. * the next new row's primary key will have a value 1.
  62. * @return string the SQL statement for resetting sequence
  63. * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table.
  64. */
  65. public function resetSequence($tableName, $value = null)
  66. {
  67. $table = $this->db->getTableSchema($tableName);
  68. if ($table !== null && $table->sequenceName !== null) {
  69. $tableName = $this->db->quoteTableName($tableName);
  70. if ($value === null) {
  71. $key = reset($table->primaryKey);
  72. $value = (int) $this->db->createCommand("SELECT MAX(`$key`) FROM " . $this->db->schema->quoteTableName($tableName))->queryScalar() + 1;
  73. } else {
  74. $value = (int) $value;
  75. }
  76. return 'ALTER TABLE ' . $this->db->schema->quoteTableName($tableName) . " AUTO_INCREMENT=$value;";
  77. } elseif ($table === null) {
  78. throw new InvalidParamException("Table not found: $tableName");
  79. } else {
  80. throw new InvalidParamException("There is not sequence associated with table '$tableName'.");
  81. }
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function buildLimit($limit, $offset)
  87. {
  88. $sql = '';
  89. // limit is not optional in CUBRID
  90. // http://www.cubrid.org/manual/90/en/LIMIT%20Clause
  91. // "You can specify a very big integer for row_count to display to the last row, starting from a specific row."
  92. if ($this->hasLimit($limit)) {
  93. $sql = 'LIMIT ' . $limit;
  94. if ($this->hasOffset($offset)) {
  95. $sql .= ' OFFSET ' . $offset;
  96. }
  97. } elseif ($this->hasOffset($offset)) {
  98. $sql = "LIMIT 9223372036854775807 OFFSET $offset"; // 2^63-1
  99. }
  100. return $sql;
  101. }
  102. /**
  103. * @inheritdoc
  104. * @since 2.0.8
  105. */
  106. public function selectExists($rawSql)
  107. {
  108. return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END';
  109. }
  110. /**
  111. * @inheritdoc
  112. * @since 2.0.8
  113. */
  114. public function addCommentOnColumn($table, $column, $comment)
  115. {
  116. $definition = $this->getColumnDefinition($table, $column);
  117. $definition = trim(preg_replace("/COMMENT '(.*?)'/i", '', $definition));
  118. return 'ALTER TABLE ' . $this->db->quoteTableName($table)
  119. . ' CHANGE ' . $this->db->quoteColumnName($column)
  120. . ' ' . $this->db->quoteColumnName($column)
  121. . (empty($definition) ? '' : ' ' . $definition)
  122. . ' COMMENT ' . $this->db->quoteValue($comment);
  123. }
  124. /**
  125. * @inheritdoc
  126. * @since 2.0.8
  127. */
  128. public function addCommentOnTable($table, $comment)
  129. {
  130. return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' COMMENT ' . $this->db->quoteValue($comment);
  131. }
  132. /**
  133. * @inheritdoc
  134. * @since 2.0.8
  135. */
  136. public function dropCommentFromColumn($table, $column)
  137. {
  138. return $this->addCommentOnColumn($table, $column, '');
  139. }
  140. /**
  141. * @inheritdoc
  142. * @since 2.0.8
  143. */
  144. public function dropCommentFromTable($table)
  145. {
  146. return $this->addCommentOnTable($table, '');
  147. }
  148. /**
  149. * Gets column definition.
  150. *
  151. * @param string $table table name
  152. * @param string $column column name
  153. * @return null|string the column definition
  154. * @throws Exception in case when table does not contain column
  155. * @since 2.0.8
  156. */
  157. private function getColumnDefinition($table, $column)
  158. {
  159. $row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->db->quoteTableName($table))->queryOne();
  160. if ($row === false) {
  161. throw new Exception("Unable to find column '$column' in table '$table'.");
  162. }
  163. if (isset($row['Create Table'])) {
  164. $sql = $row['Create Table'];
  165. } else {
  166. $row = array_values($row);
  167. $sql = $row[1];
  168. }
  169. $sql = preg_replace('/^[^(]+\((.*)\).*$/', '\1', $sql);
  170. $sql = str_replace(', [', ",\n[", $sql);
  171. if (preg_match_all('/^\s*\[(.*?)\]\s+(.*?),?$/m', $sql, $matches)) {
  172. foreach ($matches[1] as $i => $c) {
  173. if ($c === $column) {
  174. return $matches[2][$i];
  175. }
  176. }
  177. }
  178. return null;
  179. }
  180. }