QueryBuilder.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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\InvalidArgumentException;
  9. use yii\db\Constraint;
  10. use yii\db\Expression;
  11. use yii\db\ExpressionInterface;
  12. use yii\db\Query;
  13. use yii\db\PdoValue;
  14. use yii\helpers\StringHelper;
  15. /**
  16. * QueryBuilder is the query builder for PostgreSQL databases.
  17. *
  18. * @author Gevik Babakhani <gevikb@gmail.com>
  19. * @since 2.0
  20. */
  21. class QueryBuilder extends \yii\db\QueryBuilder
  22. {
  23. /**
  24. * Defines a UNIQUE index for [[createIndex()]].
  25. * @since 2.0.6
  26. */
  27. const INDEX_UNIQUE = 'unique';
  28. /**
  29. * Defines a B-tree index for [[createIndex()]].
  30. * @since 2.0.6
  31. */
  32. const INDEX_B_TREE = 'btree';
  33. /**
  34. * Defines a hash index for [[createIndex()]].
  35. * @since 2.0.6
  36. */
  37. const INDEX_HASH = 'hash';
  38. /**
  39. * Defines a GiST index for [[createIndex()]].
  40. * @since 2.0.6
  41. */
  42. const INDEX_GIST = 'gist';
  43. /**
  44. * Defines a GIN index for [[createIndex()]].
  45. * @since 2.0.6
  46. */
  47. const INDEX_GIN = 'gin';
  48. /**
  49. * @var array mapping from abstract column types (keys) to physical column types (values).
  50. */
  51. public $typeMap = [
  52. Schema::TYPE_PK => 'serial NOT NULL PRIMARY KEY',
  53. Schema::TYPE_UPK => 'serial NOT NULL PRIMARY KEY',
  54. Schema::TYPE_BIGPK => 'bigserial NOT NULL PRIMARY KEY',
  55. Schema::TYPE_UBIGPK => 'bigserial NOT NULL PRIMARY KEY',
  56. Schema::TYPE_CHAR => 'char(1)',
  57. Schema::TYPE_STRING => 'varchar(255)',
  58. Schema::TYPE_TEXT => 'text',
  59. Schema::TYPE_TINYINT => 'smallint',
  60. Schema::TYPE_SMALLINT => 'smallint',
  61. Schema::TYPE_INTEGER => 'integer',
  62. Schema::TYPE_BIGINT => 'bigint',
  63. Schema::TYPE_FLOAT => 'double precision',
  64. Schema::TYPE_DOUBLE => 'double precision',
  65. Schema::TYPE_DECIMAL => 'numeric(10,0)',
  66. Schema::TYPE_DATETIME => 'timestamp(0)',
  67. Schema::TYPE_TIMESTAMP => 'timestamp(0)',
  68. Schema::TYPE_TIME => 'time(0)',
  69. Schema::TYPE_DATE => 'date',
  70. Schema::TYPE_BINARY => 'bytea',
  71. Schema::TYPE_BOOLEAN => 'boolean',
  72. Schema::TYPE_MONEY => 'numeric(19,4)',
  73. Schema::TYPE_JSON => 'jsonb',
  74. ];
  75. /**
  76. * {@inheritdoc}
  77. */
  78. protected function defaultConditionClasses()
  79. {
  80. return array_merge(parent::defaultConditionClasses(), [
  81. 'ILIKE' => 'yii\db\conditions\LikeCondition',
  82. 'NOT ILIKE' => 'yii\db\conditions\LikeCondition',
  83. 'OR ILIKE' => 'yii\db\conditions\LikeCondition',
  84. 'OR NOT ILIKE' => 'yii\db\conditions\LikeCondition',
  85. ]);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. protected function defaultExpressionBuilders()
  91. {
  92. return array_merge(parent::defaultExpressionBuilders(), [
  93. 'yii\db\ArrayExpression' => 'yii\db\pgsql\ArrayExpressionBuilder',
  94. 'yii\db\JsonExpression' => 'yii\db\pgsql\JsonExpressionBuilder',
  95. ]);
  96. }
  97. /**
  98. * Builds a SQL statement for creating a new index.
  99. * @param string $name the name of the index. The name will be properly quoted by the method.
  100. * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method.
  101. * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns,
  102. * separate them with commas or use an array to represent them. Each column name will be properly quoted
  103. * by the method, unless a parenthesis is found in the name.
  104. * @param bool|string $unique whether to make this a UNIQUE index constraint. You can pass `true` or [[INDEX_UNIQUE]] to create
  105. * a unique index, `false` to make a non-unique index using the default index type, or one of the following constants to specify
  106. * the index method to use: [[INDEX_B_TREE]], [[INDEX_HASH]], [[INDEX_GIST]], [[INDEX_GIN]].
  107. * @return string the SQL statement for creating a new index.
  108. * @see http://www.postgresql.org/docs/8.2/static/sql-createindex.html
  109. */
  110. public function createIndex($name, $table, $columns, $unique = false)
  111. {
  112. if ($unique === self::INDEX_UNIQUE || $unique === true) {
  113. $index = false;
  114. $unique = true;
  115. } else {
  116. $index = $unique;
  117. $unique = false;
  118. }
  119. return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') .
  120. $this->db->quoteTableName($name) . ' ON ' .
  121. $this->db->quoteTableName($table) .
  122. ($index !== false ? " USING $index" : '') .
  123. ' (' . $this->buildColumns($columns) . ')';
  124. }
  125. /**
  126. * Builds a SQL statement for dropping an index.
  127. * @param string $name the name of the index to be dropped. The name will be properly quoted by the method.
  128. * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method.
  129. * @return string the SQL statement for dropping an index.
  130. */
  131. public function dropIndex($name, $table)
  132. {
  133. if (strpos($table, '.') !== false && strpos($name, '.') === false) {
  134. if (strpos($table, '{{') !== false) {
  135. $table = preg_replace('/\\{\\{(.*?)\\}\\}/', '\1', $table);
  136. list($schema, $table) = explode('.', $table);
  137. if (strpos($schema, '%') === false)
  138. $name = $schema.'.'.$name;
  139. else
  140. $name = '{{'.$schema.'.'.$name.'}}';
  141. } else {
  142. list($schema) = explode('.', $table);
  143. $name = $schema.'.'.$name;
  144. }
  145. }
  146. return 'DROP INDEX ' . $this->db->quoteTableName($name);
  147. }
  148. /**
  149. * Builds a SQL statement for renaming a DB table.
  150. * @param string $oldName the table to be renamed. The name will be properly quoted by the method.
  151. * @param string $newName the new table name. The name will be properly quoted by the method.
  152. * @return string the SQL statement for renaming a DB table.
  153. */
  154. public function renameTable($oldName, $newName)
  155. {
  156. return 'ALTER TABLE ' . $this->db->quoteTableName($oldName) . ' RENAME TO ' . $this->db->quoteTableName($newName);
  157. }
  158. /**
  159. * Creates a SQL statement for resetting the sequence value of a table's primary key.
  160. * The sequence will be reset such that the primary key of the next new row inserted
  161. * will have the specified value or 1.
  162. * @param string $tableName the name of the table whose primary key sequence will be reset
  163. * @param mixed $value the value for the primary key of the next new row inserted. If this is not set,
  164. * the next new row's primary key will have a value 1.
  165. * @return string the SQL statement for resetting sequence
  166. * @throws InvalidArgumentException if the table does not exist or there is no sequence associated with the table.
  167. */
  168. public function resetSequence($tableName, $value = null)
  169. {
  170. $table = $this->db->getTableSchema($tableName);
  171. if ($table !== null && $table->sequenceName !== null) {
  172. // c.f. http://www.postgresql.org/docs/8.1/static/functions-sequence.html
  173. $sequence = $this->db->quoteTableName($table->sequenceName);
  174. $tableName = $this->db->quoteTableName($tableName);
  175. if ($value === null) {
  176. $key = $this->db->quoteColumnName(reset($table->primaryKey));
  177. $value = "(SELECT COALESCE(MAX({$key}),0) FROM {$tableName})+1";
  178. } else {
  179. $value = (int) $value;
  180. }
  181. return "SELECT SETVAL('$sequence',$value,false)";
  182. } elseif ($table === null) {
  183. throw new InvalidArgumentException("Table not found: $tableName");
  184. }
  185. throw new InvalidArgumentException("There is not sequence associated with table '$tableName'.");
  186. }
  187. /**
  188. * Builds a SQL statement for enabling or disabling integrity check.
  189. * @param bool $check whether to turn on or off the integrity check.
  190. * @param string $schema the schema of the tables.
  191. * @param string $table the table name.
  192. * @return string the SQL statement for checking integrity
  193. */
  194. public function checkIntegrity($check = true, $schema = '', $table = '')
  195. {
  196. $enable = $check ? 'ENABLE' : 'DISABLE';
  197. $schema = $schema ?: $this->db->getSchema()->defaultSchema;
  198. $tableNames = $table ? [$table] : $this->db->getSchema()->getTableNames($schema);
  199. $viewNames = $this->db->getSchema()->getViewNames($schema);
  200. $tableNames = array_diff($tableNames, $viewNames);
  201. $command = '';
  202. foreach ($tableNames as $tableName) {
  203. $tableName = $this->db->quoteTableName("{$schema}.{$tableName}");
  204. $command .= "ALTER TABLE $tableName $enable TRIGGER ALL; ";
  205. }
  206. // enable to have ability to alter several tables
  207. $this->db->getMasterPdo()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true);
  208. return $command;
  209. }
  210. /**
  211. * Builds a SQL statement for truncating a DB table.
  212. * Explicitly restarts identity for PGSQL to be consistent with other databases which all do this by default.
  213. * @param string $table the table to be truncated. The name will be properly quoted by the method.
  214. * @return string the SQL statement for truncating a DB table.
  215. */
  216. public function truncateTable($table)
  217. {
  218. return 'TRUNCATE TABLE ' . $this->db->quoteTableName($table) . ' RESTART IDENTITY';
  219. }
  220. /**
  221. * Builds a SQL statement for changing the definition of a column.
  222. * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method.
  223. * @param string $column the name of the column to be changed. The name will be properly quoted by the method.
  224. * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract
  225. * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept
  226. * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null'
  227. * will become 'varchar(255) not null'. You can also use PostgreSQL-specific syntax such as `SET NOT NULL`.
  228. * @return string the SQL statement for changing the definition of a column.
  229. */
  230. public function alterColumn($table, $column, $type)
  231. {
  232. $columnName = $this->db->quoteColumnName($column);
  233. $tableName = $this->db->quoteTableName($table);
  234. // https://github.com/yiisoft/yii2/issues/4492
  235. // http://www.postgresql.org/docs/9.1/static/sql-altertable.html
  236. if (preg_match('/^(DROP|SET|RESET)\s+/i', $type)) {
  237. return "ALTER TABLE {$tableName} ALTER COLUMN {$columnName} {$type}";
  238. }
  239. $type = 'TYPE ' . $this->getColumnType($type);
  240. $multiAlterStatement = [];
  241. $constraintPrefix = preg_replace('/[^a-z0-9_]/i', '', $table . '_' . $column);
  242. if (preg_match('/\s+DEFAULT\s+(["\']?\w+["\']?)/i', $type, $matches)) {
  243. $type = preg_replace('/\s+DEFAULT\s+(["\']?\w+["\']?)/i', '', $type);
  244. $multiAlterStatement[] = "ALTER COLUMN {$columnName} SET DEFAULT {$matches[1]}";
  245. } else {
  246. // safe to drop default even if there was none in the first place
  247. $multiAlterStatement[] = "ALTER COLUMN {$columnName} DROP DEFAULT";
  248. }
  249. $type = preg_replace('/\s+NOT\s+NULL/i', '', $type, -1, $count);
  250. if ($count) {
  251. $multiAlterStatement[] = "ALTER COLUMN {$columnName} SET NOT NULL";
  252. } else {
  253. // remove additional null if any
  254. $type = preg_replace('/\s+NULL/i', '', $type);
  255. // safe to drop not null even if there was none in the first place
  256. $multiAlterStatement[] = "ALTER COLUMN {$columnName} DROP NOT NULL";
  257. }
  258. if (preg_match('/\s+CHECK\s+\((.+)\)/i', $type, $matches)) {
  259. $type = preg_replace('/\s+CHECK\s+\((.+)\)/i', '', $type);
  260. $multiAlterStatement[] = "ADD CONSTRAINT {$constraintPrefix}_check CHECK ({$matches[1]})";
  261. }
  262. $type = preg_replace('/\s+UNIQUE/i', '', $type, -1, $count);
  263. if ($count) {
  264. $multiAlterStatement[] = "ADD UNIQUE ({$columnName})";
  265. }
  266. // add what's left at the beginning
  267. array_unshift($multiAlterStatement, "ALTER COLUMN {$columnName} {$type}");
  268. return 'ALTER TABLE ' . $tableName . ' ' . implode(', ', $multiAlterStatement);
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function insert($table, $columns, &$params)
  274. {
  275. return parent::insert($table, $this->normalizeTableRowData($table, $columns), $params);
  276. }
  277. /**
  278. * {@inheritdoc}
  279. * @see https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT
  280. * @see https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql/8702291#8702291
  281. */
  282. public function upsert($table, $insertColumns, $updateColumns, &$params)
  283. {
  284. $insertColumns = $this->normalizeTableRowData($table, $insertColumns);
  285. if (!is_bool($updateColumns)) {
  286. $updateColumns = $this->normalizeTableRowData($table, $updateColumns);
  287. }
  288. if (version_compare($this->db->getServerVersion(), '9.5', '<')) {
  289. return $this->oldUpsert($table, $insertColumns, $updateColumns, $params);
  290. }
  291. return $this->newUpsert($table, $insertColumns, $updateColumns, $params);
  292. }
  293. /**
  294. * [[upsert()]] implementation for PostgreSQL 9.5 or higher.
  295. * @param string $table
  296. * @param array|Query $insertColumns
  297. * @param array|bool $updateColumns
  298. * @param array $params
  299. * @return string
  300. */
  301. private function newUpsert($table, $insertColumns, $updateColumns, &$params)
  302. {
  303. $insertSql = $this->insert($table, $insertColumns, $params);
  304. list($uniqueNames, , $updateNames) = $this->prepareUpsertColumns($table, $insertColumns, $updateColumns);
  305. if (empty($uniqueNames)) {
  306. return $insertSql;
  307. }
  308. if ($updateColumns === false) {
  309. return "$insertSql ON CONFLICT DO NOTHING";
  310. }
  311. if ($updateColumns === true) {
  312. $updateColumns = [];
  313. foreach ($updateNames as $name) {
  314. $updateColumns[$name] = new Expression('EXCLUDED.' . $this->db->quoteColumnName($name));
  315. }
  316. }
  317. list($updates, $params) = $this->prepareUpdateSets($table, $updateColumns, $params);
  318. return $insertSql . ' ON CONFLICT (' . implode(', ', $uniqueNames) . ') DO UPDATE SET ' . implode(', ', $updates);
  319. }
  320. /**
  321. * [[upsert()]] implementation for PostgreSQL older than 9.5.
  322. * @param string $table
  323. * @param array|Query $insertColumns
  324. * @param array|bool $updateColumns
  325. * @param array $params
  326. * @return string
  327. */
  328. private function oldUpsert($table, $insertColumns, $updateColumns, &$params)
  329. {
  330. /** @var Constraint[] $constraints */
  331. list($uniqueNames, $insertNames, $updateNames) = $this->prepareUpsertColumns($table, $insertColumns, $updateColumns, $constraints);
  332. if (empty($uniqueNames)) {
  333. return $this->insert($table, $insertColumns, $params);
  334. }
  335. /** @var Schema $schema */
  336. $schema = $this->db->getSchema();
  337. if (!$insertColumns instanceof Query) {
  338. $tableSchema = $schema->getTableSchema($table);
  339. $columnSchemas = $tableSchema !== null ? $tableSchema->columns : [];
  340. foreach ($insertColumns as $name => $value) {
  341. // NULLs and numeric values must be type hinted in order to be used in SET assigments
  342. // NVM, let's cast them all
  343. if (isset($columnSchemas[$name])) {
  344. $phName = self::PARAM_PREFIX . count($params);
  345. $params[$phName] = $value;
  346. $insertColumns[$name] = new Expression("CAST($phName AS {$columnSchemas[$name]->dbType})");
  347. }
  348. }
  349. }
  350. list(, $placeholders, $values, $params) = $this->prepareInsertValues($table, $insertColumns, $params);
  351. $updateCondition = ['or'];
  352. $insertCondition = ['or'];
  353. $quotedTableName = $schema->quoteTableName($table);
  354. foreach ($constraints as $constraint) {
  355. $constraintUpdateCondition = ['and'];
  356. $constraintInsertCondition = ['and'];
  357. foreach ($constraint->columnNames as $name) {
  358. $quotedName = $schema->quoteColumnName($name);
  359. $constraintUpdateCondition[] = "$quotedTableName.$quotedName=\"EXCLUDED\".$quotedName";
  360. $constraintInsertCondition[] = "\"upsert\".$quotedName=\"EXCLUDED\".$quotedName";
  361. }
  362. $updateCondition[] = $constraintUpdateCondition;
  363. $insertCondition[] = $constraintInsertCondition;
  364. }
  365. $withSql = 'WITH "EXCLUDED" (' . implode(', ', $insertNames)
  366. . ') AS (' . (!empty($placeholders) ? 'VALUES (' . implode(', ', $placeholders) . ')' : ltrim($values, ' ')) . ')';
  367. if ($updateColumns === false) {
  368. $selectSubQuery = (new Query())
  369. ->select(new Expression('1'))
  370. ->from($table)
  371. ->where($updateCondition);
  372. $insertSelectSubQuery = (new Query())
  373. ->select($insertNames)
  374. ->from('EXCLUDED')
  375. ->where(['not exists', $selectSubQuery]);
  376. $insertSql = $this->insert($table, $insertSelectSubQuery, $params);
  377. return "$withSql $insertSql";
  378. }
  379. if ($updateColumns === true) {
  380. $updateColumns = [];
  381. foreach ($updateNames as $name) {
  382. $quotedName = $this->db->quoteColumnName($name);
  383. if (strrpos($quotedName, '.') === false) {
  384. $quotedName = '"EXCLUDED".' . $quotedName;
  385. }
  386. $updateColumns[$name] = new Expression($quotedName);
  387. }
  388. }
  389. list($updates, $params) = $this->prepareUpdateSets($table, $updateColumns, $params);
  390. $updateSql = 'UPDATE ' . $this->db->quoteTableName($table) . ' SET ' . implode(', ', $updates)
  391. . ' FROM "EXCLUDED" ' . $this->buildWhere($updateCondition, $params)
  392. . ' RETURNING ' . $this->db->quoteTableName($table) .'.*';
  393. $selectUpsertSubQuery = (new Query())
  394. ->select(new Expression('1'))
  395. ->from('upsert')
  396. ->where($insertCondition);
  397. $insertSelectSubQuery = (new Query())
  398. ->select($insertNames)
  399. ->from('EXCLUDED')
  400. ->where(['not exists', $selectUpsertSubQuery]);
  401. $insertSql = $this->insert($table, $insertSelectSubQuery, $params);
  402. return "$withSql, \"upsert\" AS ($updateSql) $insertSql";
  403. }
  404. /**
  405. * {@inheritdoc}
  406. */
  407. public function update($table, $columns, $condition, &$params)
  408. {
  409. return parent::update($table, $this->normalizeTableRowData($table, $columns), $condition, $params);
  410. }
  411. /**
  412. * Normalizes data to be saved into the table, performing extra preparations and type converting, if necessary.
  413. *
  414. * @param string $table the table that data will be saved into.
  415. * @param array|Query $columns the column data (name => value) to be saved into the table or instance
  416. * of [[yii\db\Query|Query]] to perform INSERT INTO ... SELECT SQL statement.
  417. * Passing of [[yii\db\Query|Query]] is available since version 2.0.11.
  418. * @return array normalized columns
  419. * @since 2.0.9
  420. */
  421. private function normalizeTableRowData($table, $columns)
  422. {
  423. if ($columns instanceof Query) {
  424. return $columns;
  425. }
  426. if (($tableSchema = $this->db->getSchema()->getTableSchema($table)) !== null) {
  427. $columnSchemas = $tableSchema->columns;
  428. foreach ($columns as $name => $value) {
  429. if (isset($columnSchemas[$name]) && $columnSchemas[$name]->type === Schema::TYPE_BINARY && is_string($value)) {
  430. $columns[$name] = new PdoValue($value, \PDO::PARAM_LOB); // explicitly setup PDO param type for binary column
  431. }
  432. }
  433. }
  434. return $columns;
  435. }
  436. /**
  437. * {@inheritdoc}
  438. */
  439. public function batchInsert($table, $columns, $rows, &$params = [])
  440. {
  441. if (empty($rows)) {
  442. return '';
  443. }
  444. $schema = $this->db->getSchema();
  445. if (($tableSchema = $schema->getTableSchema($table)) !== null) {
  446. $columnSchemas = $tableSchema->columns;
  447. } else {
  448. $columnSchemas = [];
  449. }
  450. $values = [];
  451. foreach ($rows as $row) {
  452. $vs = [];
  453. foreach ($row as $i => $value) {
  454. if (isset($columns[$i], $columnSchemas[$columns[$i]])) {
  455. $value = $columnSchemas[$columns[$i]]->dbTypecast($value);
  456. }
  457. if (is_string($value)) {
  458. $value = $schema->quoteValue($value);
  459. } elseif (is_float($value)) {
  460. // ensure type cast always has . as decimal separator in all locales
  461. $value = StringHelper::floatToString($value);
  462. } elseif ($value === true) {
  463. $value = 'TRUE';
  464. } elseif ($value === false) {
  465. $value = 'FALSE';
  466. } elseif ($value === null) {
  467. $value = 'NULL';
  468. } elseif ($value instanceof ExpressionInterface) {
  469. $value = $this->buildExpression($value, $params);
  470. }
  471. $vs[] = $value;
  472. }
  473. $values[] = '(' . implode(', ', $vs) . ')';
  474. }
  475. if (empty($values)) {
  476. return '';
  477. }
  478. foreach ($columns as $i => $name) {
  479. $columns[$i] = $schema->quoteColumnName($name);
  480. }
  481. return 'INSERT INTO ' . $schema->quoteTableName($table)
  482. . ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values);
  483. }
  484. }