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
|
|
|
|
8
|
|
|
namespace yii\db\pgsql; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidParamException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* QueryBuilder is the query builder for PostgreSQL databases. |
14
|
|
|
* |
15
|
|
|
* @author Gevik Babakhani <[email protected]> |
16
|
|
|
* @since 2.0 |
17
|
|
|
*/ |
18
|
|
|
class QueryBuilder extends \yii\db\QueryBuilder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Defines a UNIQUE index for [[createIndex()]]. |
22
|
|
|
* @since 2.0.6 |
23
|
|
|
*/ |
24
|
|
|
const INDEX_UNIQUE = 'unique'; |
25
|
|
|
/** |
26
|
|
|
* Defines a B-tree index for [[createIndex()]]. |
27
|
|
|
* @since 2.0.6 |
28
|
|
|
*/ |
29
|
|
|
const INDEX_B_TREE = 'btree'; |
30
|
|
|
/** |
31
|
|
|
* Defines a hash index for [[createIndex()]]. |
32
|
|
|
* @since 2.0.6 |
33
|
|
|
*/ |
34
|
|
|
const INDEX_HASH = 'hash'; |
35
|
|
|
/** |
36
|
|
|
* Defines a GiST index for [[createIndex()]]. |
37
|
|
|
* @since 2.0.6 |
38
|
|
|
*/ |
39
|
|
|
const INDEX_GIST = 'gist'; |
40
|
|
|
/** |
41
|
|
|
* Defines a GIN index for [[createIndex()]]. |
42
|
|
|
* @since 2.0.6 |
43
|
|
|
*/ |
44
|
|
|
const INDEX_GIN = 'gin'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array mapping from abstract column types (keys) to physical column types (values). |
48
|
|
|
*/ |
49
|
|
|
public $typeMap = [ |
50
|
|
|
Schema::TYPE_PK => 'serial NOT NULL PRIMARY KEY', |
51
|
|
|
Schema::TYPE_BIGPK => 'bigserial NOT NULL PRIMARY KEY', |
52
|
|
|
Schema::TYPE_STRING => 'varchar(255)', |
53
|
|
|
Schema::TYPE_TEXT => 'text', |
54
|
|
|
Schema::TYPE_SMALLINT => 'smallint', |
55
|
|
|
Schema::TYPE_INTEGER => 'integer', |
56
|
|
|
Schema::TYPE_BIGINT => 'bigint', |
57
|
|
|
Schema::TYPE_FLOAT => 'double precision', |
58
|
|
|
Schema::TYPE_DOUBLE => 'double precision', |
59
|
|
|
Schema::TYPE_DECIMAL => 'numeric(10,0)', |
60
|
|
|
Schema::TYPE_DATETIME => 'timestamp(0)', |
61
|
|
|
Schema::TYPE_TIMESTAMP => 'timestamp(0)', |
62
|
|
|
Schema::TYPE_TIME => 'time(0)', |
63
|
|
|
Schema::TYPE_DATE => 'date', |
64
|
|
|
Schema::TYPE_BINARY => 'bytea', |
65
|
|
|
Schema::TYPE_BOOLEAN => 'boolean', |
66
|
|
|
Schema::TYPE_MONEY => 'numeric(19,4)', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var array map of query condition to builder methods. |
71
|
|
|
* These methods are used by [[buildCondition]] to build SQL conditions from array syntax. |
72
|
|
|
*/ |
73
|
|
|
protected $conditionBuilders = [ |
74
|
|
|
'NOT' => 'buildNotCondition', |
75
|
|
|
'AND' => 'buildAndCondition', |
76
|
|
|
'OR' => 'buildAndCondition', |
77
|
|
|
'BETWEEN' => 'buildBetweenCondition', |
78
|
|
|
'NOT BETWEEN' => 'buildBetweenCondition', |
79
|
|
|
'IN' => 'buildInCondition', |
80
|
|
|
'NOT IN' => 'buildInCondition', |
81
|
|
|
'LIKE' => 'buildLikeCondition', |
82
|
|
|
'ILIKE' => 'buildLikeCondition', |
83
|
|
|
'NOT LIKE' => 'buildLikeCondition', |
84
|
|
|
'NOT ILIKE' => 'buildLikeCondition', |
85
|
|
|
'OR LIKE' => 'buildLikeCondition', |
86
|
|
|
'OR ILIKE' => 'buildLikeCondition', |
87
|
|
|
'OR NOT LIKE' => 'buildLikeCondition', |
88
|
|
|
'OR NOT ILIKE' => 'buildLikeCondition', |
89
|
|
|
'EXISTS' => 'buildExistsCondition', |
90
|
|
|
'NOT EXISTS' => 'buildExistsCondition', |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Builds a SQL statement for creating a new index. |
96
|
|
|
* @param string $name the name of the index. The name will be properly quoted by the method. |
97
|
|
|
* @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
98
|
|
|
* @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
99
|
|
|
* separate them with commas or use an array to represent them. Each column name will be properly quoted |
100
|
|
|
* by the method, unless a parenthesis is found in the name. |
101
|
|
|
* @param boolean|string $unique whether to make this a UNIQUE index constraint. You can pass `true` or [[INDEX_UNIQUE]] to create |
102
|
|
|
* a unique index, `false` to make a non-unique index using the default index type, or one of the following constants to specify |
103
|
|
|
* the index method to use: [[INDEX_B_TREE]], [[INDEX_HASH]], [[INDEX_GIST]], [[INDEX_GIN]]. |
104
|
|
|
* @return string the SQL statement for creating a new index. |
105
|
|
|
* @see http://www.postgresql.org/docs/8.2/static/sql-createindex.html |
106
|
|
|
*/ |
107
|
|
|
public function createIndex($name, $table, $columns, $unique = false) |
108
|
|
|
{ |
109
|
|
|
if ($unique === self::INDEX_UNIQUE || $unique === true) { |
110
|
|
|
$index = false; |
111
|
|
|
$unique = true; |
112
|
|
|
} else { |
113
|
|
|
$index = $unique; |
114
|
|
|
$unique = false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') . |
118
|
|
|
$this->db->quoteTableName($name) . ' ON ' . |
119
|
|
|
$this->db->quoteTableName($table) . |
120
|
|
|
($index !== false ? " USING $index" : '') . |
121
|
|
|
' (' . $this->buildColumns($columns) . ')'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Builds a SQL statement for dropping an index. |
126
|
|
|
* @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
127
|
|
|
* @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
128
|
|
|
* @return string the SQL statement for dropping an index. |
129
|
|
|
*/ |
130
|
|
|
public function dropIndex($name, $table) |
131
|
|
|
{ |
132
|
|
|
return 'DROP INDEX ' . $this->db->quoteTableName($name); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Builds a SQL statement for renaming a DB table. |
137
|
|
|
* @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
138
|
|
|
* @param string $newName the new table name. The name will be properly quoted by the method. |
139
|
|
|
* @return string the SQL statement for renaming a DB table. |
140
|
|
|
*/ |
141
|
1 |
|
public function renameTable($oldName, $newName) |
142
|
|
|
{ |
143
|
1 |
|
return 'ALTER TABLE ' . $this->db->quoteTableName($oldName) . ' RENAME TO ' . $this->db->quoteTableName($newName); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Creates a SQL statement for resetting the sequence value of a table's primary key. |
148
|
|
|
* The sequence will be reset such that the primary key of the next new row inserted |
149
|
|
|
* will have the specified value or 1. |
150
|
|
|
* @param string $tableName the name of the table whose primary key sequence will be reset |
151
|
|
|
* @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
152
|
|
|
* the next new row's primary key will have a value 1. |
153
|
|
|
* @return string the SQL statement for resetting sequence |
154
|
|
|
* @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
155
|
|
|
*/ |
156
|
|
|
public function resetSequence($tableName, $value = null) |
157
|
|
|
{ |
158
|
|
|
$table = $this->db->getTableSchema($tableName); |
159
|
|
|
if ($table !== null && $table->sequenceName !== null) { |
160
|
|
|
// c.f. http://www.postgresql.org/docs/8.1/static/functions-sequence.html |
161
|
|
|
$sequence = $this->db->quoteTableName($table->sequenceName); |
162
|
|
|
$tableName = $this->db->quoteTableName($tableName); |
163
|
|
|
if ($value === null) { |
164
|
|
|
$key = reset($table->primaryKey); |
165
|
|
|
$value = "(SELECT COALESCE(MAX(\"{$key}\"),0) FROM {$tableName})+1"; |
166
|
|
|
} else { |
167
|
|
|
$value = (int) $value; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return "SELECT SETVAL('$sequence',$value,false)"; |
171
|
|
|
} elseif ($table === null) { |
172
|
|
|
throw new InvalidParamException("Table not found: $tableName"); |
173
|
|
|
} else { |
174
|
|
|
throw new InvalidParamException("There is not sequence associated with table '$tableName'."); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Builds a SQL statement for enabling or disabling integrity check. |
180
|
|
|
* @param boolean $check whether to turn on or off the integrity check. |
181
|
|
|
* @param string $schema the schema of the tables. |
182
|
|
|
* @param string $table the table name. |
183
|
|
|
* @return string the SQL statement for checking integrity |
184
|
|
|
*/ |
185
|
|
|
public function checkIntegrity($check = true, $schema = '', $table = '') |
186
|
|
|
{ |
187
|
|
|
$enable = $check ? 'ENABLE' : 'DISABLE'; |
188
|
|
|
$schema = $schema ? $schema : $this->db->getSchema()->defaultSchema; |
189
|
|
|
$tableNames = $table ? [$table] : $this->db->getSchema()->getTableNames($schema); |
190
|
|
|
$command = ''; |
191
|
|
|
|
192
|
|
|
foreach ($tableNames as $tableName) { |
193
|
|
|
$tableName = '"' . $schema . '"."' . $tableName . '"'; |
194
|
|
|
$command .= "ALTER TABLE $tableName $enable TRIGGER ALL; "; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// enable to have ability to alter several tables |
198
|
|
|
$this->db->getMasterPdo()->setAttribute(\PDO::ATTR_EMULATE_PREPARES, true); |
199
|
|
|
|
200
|
|
|
return $command; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Builds a SQL statement for changing the definition of a column. |
205
|
|
|
* @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
206
|
|
|
* @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
207
|
|
|
* @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
208
|
|
|
* column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
209
|
|
|
* in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
210
|
|
|
* will become 'varchar(255) not null'. You can also use PostgreSQL-specific syntax such as `SET NOT NULL`. |
211
|
|
|
* @return string the SQL statement for changing the definition of a column. |
212
|
|
|
*/ |
213
|
2 |
|
public function alterColumn($table, $column, $type) |
214
|
|
|
{ |
215
|
|
|
// https://github.com/yiisoft/yii2/issues/4492 |
216
|
|
|
// http://www.postgresql.org/docs/9.1/static/sql-altertable.html |
217
|
2 |
|
if (!preg_match('/^(DROP|SET|RESET)\s+/i', $type)) { |
218
|
2 |
|
$type = 'TYPE ' . $this->getColumnType($type); |
219
|
2 |
|
} |
220
|
2 |
|
return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' ALTER COLUMN ' |
221
|
2 |
|
. $this->db->quoteColumnName($column) . ' ' . $type; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @inheritdoc |
226
|
|
|
*/ |
227
|
5 |
|
public function batchInsert($table, $columns, $rows) |
228
|
|
|
{ |
229
|
5 |
|
$schema = $this->db->getSchema(); |
230
|
5 |
|
if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
231
|
4 |
|
$columnSchemas = $tableSchema->columns; |
232
|
4 |
|
} else { |
233
|
1 |
|
$columnSchemas = []; |
234
|
|
|
} |
235
|
|
|
|
236
|
5 |
|
$values = []; |
237
|
5 |
|
foreach ($rows as $row) { |
238
|
5 |
|
$vs = []; |
239
|
5 |
|
foreach ($row as $i => $value) { |
240
|
5 |
|
if (isset($columns[$i], $columnSchemas[$columns[$i]]) && !is_array($value)) { |
241
|
4 |
|
$value = $columnSchemas[$columns[$i]]->dbTypecast($value); |
242
|
4 |
|
} |
243
|
5 |
|
if (is_string($value)) { |
244
|
2 |
|
$value = $schema->quoteValue($value); |
245
|
5 |
|
} elseif ($value === true) { |
246
|
3 |
|
$value = 'TRUE'; |
247
|
5 |
|
} elseif ($value === false) { |
248
|
3 |
|
$value = 'FALSE'; |
249
|
5 |
|
} elseif ($value === null) { |
250
|
1 |
|
$value = 'NULL'; |
251
|
1 |
|
} |
252
|
5 |
|
$vs[] = $value; |
253
|
5 |
|
} |
254
|
5 |
|
$values[] = '(' . implode(', ', $vs) . ')'; |
255
|
5 |
|
} |
256
|
|
|
|
257
|
5 |
|
foreach ($columns as $i => $name) { |
258
|
5 |
|
$columns[$i] = $schema->quoteColumnName($name); |
259
|
5 |
|
} |
260
|
|
|
|
261
|
5 |
|
return 'INSERT INTO ' . $schema->quoteTableName($table) |
262
|
5 |
|
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|