QueryBuilder.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  10. * QueryBuilder is the query builder for CUBRID databases (version 9.1.x and higher).
  11. *
  12. * @author Carsten Brandt <[email protected]>
  13. * @since 2.0
  14. */
  15. class QueryBuilder extends \yii\db\QueryBuilder
  16. {
  17. /**
  18. * @var array mapping from abstract column types (keys) to physical column types (values).
  19. */
  20. public $typeMap = [
  21. Schema::TYPE_PK => 'int NOT NULL AUTO_INCREMENT PRIMARY KEY',
  22. Schema::TYPE_BIGPK => 'bigint NOT NULL AUTO_INCREMENT PRIMARY KEY',
  23. Schema::TYPE_STRING => 'varchar(255)',
  24. Schema::TYPE_TEXT => 'varchar',
  25. Schema::TYPE_SMALLINT => 'smallint',
  26. Schema::TYPE_INTEGER => 'int',
  27. Schema::TYPE_BIGINT => 'bigint',
  28. Schema::TYPE_FLOAT => 'float(7)',
  29. Schema::TYPE_DECIMAL => 'decimal(10,0)',
  30. Schema::TYPE_DATETIME => 'datetime',
  31. Schema::TYPE_TIMESTAMP => 'timestamp',
  32. Schema::TYPE_TIME => 'time',
  33. Schema::TYPE_DATE => 'date',
  34. Schema::TYPE_BINARY => 'blob',
  35. Schema::TYPE_BOOLEAN => 'smallint',
  36. Schema::TYPE_MONEY => 'decimal(19,4)',
  37. ];
  38. /**
  39. * Creates a SQL statement for resetting the sequence value of a table's primary key.
  40. * The sequence will be reset such that the primary key of the next new row inserted
  41. * will have the specified value or 1.
  42. * @param string $tableName the name of the table whose primary key sequence will be reset
  43. * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
  44. * the next new row's primary key will have a value 1.
  45. * @return string the SQL statement for resetting sequence
  46. * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table.
  47. */
  48. public function resetSequence($tableName, $value = null)
  49. {
  50. $table = $this->db->getTableSchema($tableName);
  51. if ($table !== null && $table->sequenceName !== null) {
  52. $tableName = $this->db->quoteTableName($tableName);
  53. if ($value === null) {
  54. $key = reset($table->primaryKey);
  55. $value = (int)$this->db->createCommand("SELECT MAX(`$key`) FROM " . $this->db->schema->quoteTableName($tableName))->queryScalar() + 1;
  56. } else {
  57. $value = (int)$value;
  58. }
  59. return "ALTER TABLE " . $this->db->schema->quoteTableName($tableName) . " AUTO_INCREMENT=$value;";
  60. } elseif ($table === null) {
  61. throw new InvalidParamException("Table not found: $tableName");
  62. } else {
  63. throw new InvalidParamException("There is not sequence associated with table '$tableName'.");
  64. }
  65. }
  66. /**
  67. * @inheritdoc
  68. */
  69. public function buildLimit($limit, $offset)
  70. {
  71. $sql = '';
  72. // limit is not optional in CUBRID
  73. // http://www.cubrid.org/manual/90/en/LIMIT%20Clause
  74. // "You can specify a very big integer for row_count to display to the last row, starting from a specific row."
  75. if ($limit !== null && $limit >= 0) {
  76. $sql = 'LIMIT ' . (int)$limit;
  77. if ($offset > 0) {
  78. $sql .= ' OFFSET ' . (int)$offset;
  79. }
  80. } elseif ($offset > 0) {
  81. $sql = 'LIMIT 9223372036854775807 OFFSET ' . (int)$offset; // 2^63-1
  82. }
  83. return $sql;
  84. }
  85. }