Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

95 lines
3.5KB

  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.3.x and higher).
  11. *
  12. * @author Carsten Brandt <mail@cebe.cc>
  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_DOUBLE => 'double(15)',
  30. Schema::TYPE_DECIMAL => 'decimal(10,0)',
  31. Schema::TYPE_DATETIME => 'datetime',
  32. Schema::TYPE_TIMESTAMP => 'timestamp',
  33. Schema::TYPE_TIME => 'time',
  34. Schema::TYPE_DATE => 'date',
  35. Schema::TYPE_BINARY => 'blob',
  36. Schema::TYPE_BOOLEAN => 'smallint',
  37. Schema::TYPE_MONEY => 'decimal(19,4)',
  38. ];
  39. /**
  40. * Creates a SQL statement for resetting the sequence value of a table's primary key.
  41. * The sequence will be reset such that the primary key of the next new row inserted
  42. * will have the specified value or 1.
  43. * @param string $tableName the name of the table whose primary key sequence will be reset
  44. * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
  45. * the next new row's primary key will have a value 1.
  46. * @return string the SQL statement for resetting sequence
  47. * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table.
  48. */
  49. public function resetSequence($tableName, $value = null)
  50. {
  51. $table = $this->db->getTableSchema($tableName);
  52. if ($table !== null && $table->sequenceName !== null) {
  53. $tableName = $this->db->quoteTableName($tableName);
  54. if ($value === null) {
  55. $key = reset($table->primaryKey);
  56. $value = (int) $this->db->createCommand("SELECT MAX(`$key`) FROM " . $this->db->schema->quoteTableName($tableName))->queryScalar() + 1;
  57. } else {
  58. $value = (int) $value;
  59. }
  60. return "ALTER TABLE " . $this->db->schema->quoteTableName($tableName) . " AUTO_INCREMENT=$value;";
  61. } elseif ($table === null) {
  62. throw new InvalidParamException("Table not found: $tableName");
  63. } else {
  64. throw new InvalidParamException("There is not sequence associated with table '$tableName'.");
  65. }
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public function buildLimit($limit, $offset)
  71. {
  72. $sql = '';
  73. // limit is not optional in CUBRID
  74. // http://www.cubrid.org/manual/90/en/LIMIT%20Clause
  75. // "You can specify a very big integer for row_count to display to the last row, starting from a specific row."
  76. if ($this->hasLimit($limit)) {
  77. $sql = 'LIMIT ' . $limit;
  78. if ($this->hasOffset($offset)) {
  79. $sql .= ' OFFSET ' . $offset;
  80. }
  81. } elseif ($this->hasOffset($offset)) {
  82. $sql = "LIMIT 9223372036854775807 OFFSET $offset"; // 2^63-1
  83. }
  84. return $sql;
  85. }
  86. }