QueryBuilder.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\pgsql;
  8. use yii\base\InvalidParamException;
  9. /**
  10. * QueryBuilder is the query builder for PostgreSQL databases.
  11. *
  12. * @author Gevik Babakhani <[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 => 'serial NOT NULL PRIMARY KEY',
  22. Schema::TYPE_BIGPK => 'bigserial NOT NULL PRIMARY KEY',
  23. Schema::TYPE_STRING => 'varchar(255)',
  24. Schema::TYPE_TEXT => 'text',
  25. Schema::TYPE_SMALLINT => 'smallint',
  26. Schema::TYPE_INTEGER => 'integer',
  27. Schema::TYPE_BIGINT => 'bigint',
  28. Schema::TYPE_FLOAT => 'double precision',
  29. Schema::TYPE_DECIMAL => 'numeric(10,0)',
  30. Schema::TYPE_DATETIME => 'timestamp',
  31. Schema::TYPE_TIMESTAMP => 'timestamp',
  32. Schema::TYPE_TIME => 'time',
  33. Schema::TYPE_DATE => 'date',
  34. Schema::TYPE_BINARY => 'bytea',
  35. Schema::TYPE_BOOLEAN => 'boolean',
  36. Schema::TYPE_MONEY => 'numeric(19,4)',
  37. ];
  38. /**
  39. * Builds a SQL statement for dropping an index.
  40. * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
  41. * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
  42. * @return string the SQL statement for dropping an index.
  43. */
  44. public function dropIndex($name, $table)
  45. {
  46. return 'DROP INDEX ' . $this->db->quoteTableName($name);
  47. }
  48. /**
  49. * Builds a SQL statement for renaming a DB table.
  50. * @param string $oldName the table to be renamed. The name will be properly quoted by the method.
  51. * @param string $newName the new table name. The name will be properly quoted by the method.
  52. * @return string the SQL statement for renaming a DB table.
  53. */
  54. public function renameTable($oldName, $newName)
  55. {
  56. return 'ALTER TABLE ' . $this->db->quoteTableName($oldName) . ' RENAME TO ' . $this->db->quoteTableName($newName);
  57. }
  58. /**
  59. * Creates a SQL statement for resetting the sequence value of a table's primary key.
  60. * The sequence will be reset such that the primary key of the next new row inserted
  61. * will have the specified value or 1.
  62. * @param string $tableName the name of the table whose primary key sequence will be reset
  63. * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
  64. * the next new row's primary key will have a value 1.
  65. * @return string the SQL statement for resetting sequence
  66. * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table.
  67. */
  68. public function resetSequence($tableName, $value = null)
  69. {
  70. $table = $this->db->getTableSchema($tableName);
  71. if ($table !== null && $table->sequenceName !== null) {
  72. $sequence = '"' . $table->sequenceName . '"';
  73. if (strpos($sequence, '.') !== false) {
  74. $sequence = str_replace('.', '"."', $sequence);
  75. }
  76. $tableName = $this->db->quoteTableName($tableName);
  77. if ($value === null) {
  78. $key = reset($table->primaryKey);
  79. $value = "(SELECT COALESCE(MAX(\"{$key}\"),0) FROM {$tableName})+1";
  80. } else {
  81. $value = (int)$value;
  82. }
  83. return "SELECT SETVAL('$sequence',$value,false)";
  84. } elseif ($table === null) {
  85. throw new InvalidParamException("Table not found: $tableName");
  86. } else {
  87. throw new InvalidParamException("There is not sequence associated with table '$tableName'.");
  88. }
  89. }
  90. /**
  91. * Builds a SQL statement for enabling or disabling integrity check.
  92. * @param boolean $check whether to turn on or off the integrity check.
  93. * @param string $schema the schema of the tables.
  94. * @param string $table the table name.
  95. * @return string the SQL statement for checking integrity
  96. */
  97. public function checkIntegrity($check = true, $schema = '', $table = '')
  98. {
  99. $enable = $check ? 'ENABLE' : 'DISABLE';
  100. $schema = $schema ? $schema : $this->db->schema->defaultSchema;
  101. $tableNames = $table ? [$table] : $this->db->schema->getTableNames($schema);
  102. $command = '';
  103. foreach ($tableNames as $tableName) {
  104. $tableName = '"' . $schema . '"."' . $tableName . '"';
  105. $command .= "ALTER TABLE $tableName $enable TRIGGER ALL; ";
  106. }
  107. #enable to have ability to alter several tables
  108. $this->db->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
  109. return $command;
  110. }
  111. /**
  112. * Builds a SQL statement for changing the definition of a column.
  113. * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  114. * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  115. * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract
  116. * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept
  117. * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null'
  118. * will become 'varchar(255) not null'.
  119. * @return string the SQL statement for changing the definition of a column.
  120. */
  121. public function alterColumn($table, $column, $type)
  122. {
  123. return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ALTER COLUMN '
  124. . $this->db->quoteColumnName($column) . ' TYPE '
  125. . $this->getColumnType($type);
  126. }
  127. }