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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * Creates a SQL statement for resetting the sequence value of a table's primary key.
  45. * The sequence will be reset such that the primary key of the next new row inserted
  46. * will have the specified value or 1.
  47. * @param string $tableName the name of the table whose primary key sequence will be reset
  48. * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
  49. * the next new row's primary key will have a value 1.
  50. * @return string the SQL statement for resetting sequence
  51. * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table.
  52. */
  53. public function resetSequence($tableName, $value = null)
  54. {
  55. $table = $this->db->getTableSchema($tableName);
  56. if ($table !== null && $table->sequenceName !== null) {
  57. $tableName = $this->db->quoteTableName($tableName);
  58. if ($value === null) {
  59. $key = reset($table->primaryKey);
  60. $value = (int) $this->db->createCommand("SELECT MAX(`$key`) FROM " . $this->db->schema->quoteTableName($tableName))->queryScalar() + 1;
  61. } else {
  62. $value = (int) $value;
  63. }
  64. return 'ALTER TABLE ' . $this->db->schema->quoteTableName($tableName) . " AUTO_INCREMENT=$value;";
  65. } elseif ($table === null) {
  66. throw new InvalidParamException("Table not found: $tableName");
  67. } else {
  68. throw new InvalidParamException("There is not sequence associated with table '$tableName'.");
  69. }
  70. }
  71. /**
  72. * @inheritdoc
  73. */
  74. public function buildLimit($limit, $offset)
  75. {
  76. $sql = '';
  77. // limit is not optional in CUBRID
  78. // http://www.cubrid.org/manual/90/en/LIMIT%20Clause
  79. // "You can specify a very big integer for row_count to display to the last row, starting from a specific row."
  80. if ($this->hasLimit($limit)) {
  81. $sql = 'LIMIT ' . $limit;
  82. if ($this->hasOffset($offset)) {
  83. $sql .= ' OFFSET ' . $offset;
  84. }
  85. } elseif ($this->hasOffset($offset)) {
  86. $sql = "LIMIT 9223372036854775807 OFFSET $offset"; // 2^63-1
  87. }
  88. return $sql;
  89. }
  90. /**
  91. * @inheritdoc
  92. * @since 2.0.8
  93. */
  94. public function selectExists($rawSql)
  95. {
  96. return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END';
  97. }
  98. /**
  99. * @inheritdoc
  100. * @since 2.0.8
  101. */
  102. public function addCommentOnColumn($table, $column, $comment)
  103. {
  104. $definition = $this->getColumnDefinition($table, $column);
  105. $definition = trim(preg_replace("/COMMENT '(.*?)'/i", '', $definition));
  106. return 'ALTER TABLE ' . $this->db->quoteTableName($table)
  107. . ' CHANGE ' . $this->db->quoteColumnName($column)
  108. . ' ' . $this->db->quoteColumnName($column)
  109. . (empty($definition) ? '' : ' ' . $definition)
  110. . ' COMMENT ' . $this->db->quoteValue($comment);
  111. }
  112. /**
  113. * @inheritdoc
  114. * @since 2.0.8
  115. */
  116. public function addCommentOnTable($table, $comment)
  117. {
  118. return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' COMMENT ' . $this->db->quoteValue($comment);
  119. }
  120. /**
  121. * @inheritdoc
  122. * @since 2.0.8
  123. */
  124. public function dropCommentFromColumn($table, $column)
  125. {
  126. return $this->addCommentOnColumn($table, $column, '');
  127. }
  128. /**
  129. * @inheritdoc
  130. * @since 2.0.8
  131. */
  132. public function dropCommentFromTable($table)
  133. {
  134. return $this->addCommentOnTable($table, '');
  135. }
  136. /**
  137. * Gets column definition.
  138. *
  139. * @param string $table table name
  140. * @param string $column column name
  141. * @return null|string the column definition
  142. * @throws Exception in case when table does not contain column
  143. * @since 2.0.8
  144. */
  145. private function getColumnDefinition($table, $column)
  146. {
  147. $row = $this->db->createCommand('SHOW CREATE TABLE ' . $this->db->quoteTableName($table))->queryOne();
  148. if ($row === false) {
  149. throw new Exception("Unable to find column '$column' in table '$table'.");
  150. }
  151. if (isset($row['Create Table'])) {
  152. $sql = $row['Create Table'];
  153. } else {
  154. $row = array_values($row);
  155. $sql = $row[1];
  156. }
  157. $sql = preg_replace('/^[^(]+\((.*)\).*$/', '\1', $sql);
  158. $sql = str_replace(', [', ",\n[", $sql);
  159. if (preg_match_all('/^\s*\[(.*?)\]\s+(.*?),?$/m', $sql, $matches)) {
  160. foreach ($matches[1] as $i => $c) {
  161. if ($c === $column) {
  162. return $matches[2][$i];
  163. }
  164. }
  165. }
  166. return null;
  167. }
  168. }