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; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\Object; |
12
|
|
|
use yii\base\NotSupportedException; |
13
|
|
|
use yii\base\InvalidCallException; |
14
|
|
|
use yii\caching\Cache; |
15
|
|
|
use yii\caching\TagDependency; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Schema is the base class for concrete DBMS-specific schema classes. |
19
|
|
|
* |
20
|
|
|
* Schema represents the database schema information that is DBMS specific. |
21
|
|
|
* |
22
|
|
|
* @property string $lastInsertID The row ID of the last row inserted, or the last value retrieved from the |
23
|
|
|
* sequence object. This property is read-only. |
24
|
|
|
* @property QueryBuilder $queryBuilder The query builder for this connection. This property is read-only. |
25
|
|
|
* @property string[] $schemaNames All schema names in the database, except system schemas. This property is |
26
|
|
|
* read-only. |
27
|
|
|
* @property string[] $tableNames All table names in the database. This property is read-only. |
28
|
|
|
* @property TableSchema[] $tableSchemas The metadata for all tables in the database. Each array element is an |
29
|
|
|
* instance of [[TableSchema]] or its child class. This property is read-only. |
30
|
|
|
* @property string $transactionIsolationLevel The transaction isolation level to use for this transaction. |
31
|
|
|
* This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], |
32
|
|
|
* [[Transaction::REPEATABLE_READ]] and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific |
33
|
|
|
* syntax to be used after `SET TRANSACTION ISOLATION LEVEL`. This property is write-only. |
34
|
|
|
* |
35
|
|
|
* @author Qiang Xue <[email protected]> |
36
|
|
|
* @since 2.0 |
37
|
|
|
*/ |
38
|
|
|
abstract class Schema extends Object |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* The following are the supported abstract column data types. |
42
|
|
|
*/ |
43
|
|
|
const TYPE_PK = 'pk'; |
44
|
|
|
const TYPE_UPK = 'upk'; |
45
|
|
|
const TYPE_BIGPK = 'bigpk'; |
46
|
|
|
const TYPE_UBIGPK = 'ubigpk'; |
47
|
|
|
const TYPE_CHAR = 'char'; |
48
|
|
|
const TYPE_STRING = 'string'; |
49
|
|
|
const TYPE_TEXT = 'text'; |
50
|
|
|
const TYPE_SMALLINT = 'smallint'; |
51
|
|
|
const TYPE_INTEGER = 'integer'; |
52
|
|
|
const TYPE_BIGINT = 'bigint'; |
53
|
|
|
const TYPE_FLOAT = 'float'; |
54
|
|
|
const TYPE_DOUBLE = 'double'; |
55
|
|
|
const TYPE_DECIMAL = 'decimal'; |
56
|
|
|
const TYPE_DATETIME = 'datetime'; |
57
|
|
|
const TYPE_TIMESTAMP = 'timestamp'; |
58
|
|
|
const TYPE_TIME = 'time'; |
59
|
|
|
const TYPE_DATE = 'date'; |
60
|
|
|
const TYPE_BINARY = 'binary'; |
61
|
|
|
const TYPE_BOOLEAN = 'boolean'; |
62
|
|
|
const TYPE_MONEY = 'money'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var Connection the database connection |
66
|
|
|
*/ |
67
|
|
|
public $db; |
68
|
|
|
/** |
69
|
|
|
* @var string the default schema name used for the current session. |
70
|
|
|
*/ |
71
|
|
|
public $defaultSchema; |
72
|
|
|
/** |
73
|
|
|
* @var array map of DB errors and corresponding exceptions |
74
|
|
|
* If left part is found in DB error message exception class from the right part is used. |
75
|
|
|
*/ |
76
|
|
|
public $exceptionMap = [ |
77
|
|
|
'SQLSTATE[23' => 'yii\db\IntegrityException', |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var array list of ALL schema names in the database, except system schemas |
82
|
|
|
*/ |
83
|
|
|
private $_schemaNames; |
84
|
|
|
/** |
85
|
|
|
* @var array list of ALL table names in the database |
86
|
|
|
*/ |
87
|
|
|
private $_tableNames = []; |
88
|
|
|
/** |
89
|
|
|
* @var array list of loaded table metadata (table name => TableSchema) |
90
|
|
|
*/ |
91
|
|
|
private $_tables = []; |
92
|
|
|
/** |
93
|
|
|
* @var QueryBuilder the query builder for this database |
94
|
|
|
*/ |
95
|
|
|
private $_builder; |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \yii\db\ColumnSchema |
100
|
|
|
* @throws \yii\base\InvalidConfigException |
101
|
|
|
*/ |
102
|
349 |
|
protected function createColumnSchema() |
103
|
|
|
{ |
104
|
349 |
|
return Yii::createObject('yii\db\ColumnSchema'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Loads the metadata for the specified table. |
109
|
|
|
* @param string $name table name |
110
|
|
|
* @return null|TableSchema DBMS-dependent table metadata, null if the table does not exist. |
111
|
|
|
*/ |
112
|
|
|
abstract protected function loadTableSchema($name); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Obtains the metadata for the named table. |
116
|
|
|
* @param string $name table name. The table name may contain schema name if any. Do not quote the table name. |
117
|
|
|
* @param boolean $refresh whether to reload the table schema even if it is found in the cache. |
118
|
|
|
* @return null|TableSchema table metadata. Null if the named table does not exist. |
119
|
|
|
*/ |
120
|
423 |
|
public function getTableSchema($name, $refresh = false) |
121
|
|
|
{ |
122
|
423 |
|
if (array_key_exists($name, $this->_tables) && !$refresh) { |
123
|
358 |
|
return $this->_tables[$name]; |
124
|
|
|
} |
125
|
|
|
|
126
|
359 |
|
$db = $this->db; |
127
|
359 |
|
$realName = $this->getRawTableName($name); |
128
|
|
|
|
129
|
359 |
|
if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) { |
130
|
|
|
/* @var $cache Cache */ |
131
|
7 |
|
$cache = is_string($db->schemaCache) ? Yii::$app->get($db->schemaCache, false) : $db->schemaCache; |
132
|
7 |
|
if ($cache instanceof Cache) { |
133
|
7 |
|
$key = $this->getCacheKey($name); |
134
|
7 |
|
if ($refresh || ($table = $cache->get($key)) === false) { |
135
|
7 |
|
$this->_tables[$name] = $table = $this->loadTableSchema($realName); |
136
|
7 |
|
if ($table !== null) { |
137
|
7 |
|
$cache->set($key, $table, $db->schemaCacheDuration, new TagDependency([ |
138
|
7 |
|
'tags' => $this->getCacheTag(), |
139
|
7 |
|
])); |
140
|
7 |
|
} |
141
|
7 |
|
} else { |
142
|
|
|
$this->_tables[$name] = $table; |
143
|
|
|
} |
144
|
|
|
|
145
|
7 |
|
return $this->_tables[$name]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
352 |
|
return $this->_tables[$name] = $this->loadTableSchema($realName); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the cache key for the specified table name. |
154
|
|
|
* @param string $name the table name |
155
|
|
|
* @return mixed the cache key |
156
|
|
|
*/ |
157
|
7 |
|
protected function getCacheKey($name) |
158
|
|
|
{ |
159
|
|
|
return [ |
160
|
7 |
|
__CLASS__, |
161
|
7 |
|
$this->db->dsn, |
162
|
7 |
|
$this->db->username, |
163
|
7 |
|
$name, |
164
|
7 |
|
]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns the cache tag name. |
169
|
|
|
* This allows [[refresh()]] to invalidate all cached table schemas. |
170
|
|
|
* @return string the cache tag name |
171
|
|
|
*/ |
172
|
7 |
|
protected function getCacheTag() |
173
|
|
|
{ |
174
|
7 |
|
return md5(serialize([ |
175
|
7 |
|
__CLASS__, |
176
|
7 |
|
$this->db->dsn, |
177
|
7 |
|
$this->db->username, |
178
|
7 |
|
])); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns the metadata for all tables in the database. |
183
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
184
|
|
|
* @param boolean $refresh whether to fetch the latest available table schemas. If this is false, |
185
|
|
|
* cached data may be returned if available. |
186
|
|
|
* @return TableSchema[] the metadata for all tables in the database. |
187
|
|
|
* Each array element is an instance of [[TableSchema]] or its child class. |
188
|
|
|
*/ |
189
|
10 |
|
public function getTableSchemas($schema = '', $refresh = false) |
190
|
|
|
{ |
191
|
10 |
|
$tables = []; |
192
|
10 |
|
foreach ($this->getTableNames($schema, $refresh) as $name) { |
193
|
10 |
|
if ($schema !== '') { |
194
|
|
|
$name = $schema . '.' . $name; |
195
|
|
|
} |
196
|
10 |
|
if (($table = $this->getTableSchema($name, $refresh)) !== null) { |
197
|
10 |
|
$tables[] = $table; |
198
|
10 |
|
} |
199
|
10 |
|
} |
200
|
|
|
|
201
|
10 |
|
return $tables; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns all schema names in the database, except system schemas. |
206
|
|
|
* @param boolean $refresh whether to fetch the latest available schema names. If this is false, |
207
|
|
|
* schema names fetched previously (if available) will be returned. |
208
|
|
|
* @return string[] all schema names in the database, except system schemas. |
209
|
|
|
* @since 2.0.4 |
210
|
|
|
*/ |
211
|
1 |
|
public function getSchemaNames($refresh = false) |
212
|
|
|
{ |
213
|
1 |
|
if ($this->_schemaNames === null || $refresh) { |
214
|
1 |
|
$this->_schemaNames = $this->findSchemaNames(); |
215
|
1 |
|
} |
216
|
|
|
|
217
|
1 |
|
return $this->_schemaNames; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Returns all table names in the database. |
222
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema name. |
223
|
|
|
* If not empty, the returned table names will be prefixed with the schema name. |
224
|
|
|
* @param boolean $refresh whether to fetch the latest available table names. If this is false, |
225
|
|
|
* table names fetched previously (if available) will be returned. |
226
|
|
|
* @return string[] all table names in the database. |
227
|
|
|
*/ |
228
|
16 |
|
public function getTableNames($schema = '', $refresh = false) |
229
|
|
|
{ |
230
|
16 |
|
if (!isset($this->_tableNames[$schema]) || $refresh) { |
231
|
16 |
|
$this->_tableNames[$schema] = $this->findTableNames($schema); |
232
|
16 |
|
} |
233
|
|
|
|
234
|
16 |
|
return $this->_tableNames[$schema]; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @return QueryBuilder the query builder for this connection. |
239
|
|
|
*/ |
240
|
464 |
|
public function getQueryBuilder() |
241
|
|
|
{ |
242
|
464 |
|
if ($this->_builder === null) { |
243
|
371 |
|
$this->_builder = $this->createQueryBuilder(); |
244
|
371 |
|
} |
245
|
|
|
|
246
|
464 |
|
return $this->_builder; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Determines the PDO type for the given PHP data value. |
251
|
|
|
* @param mixed $data the data whose PDO type is to be determined |
252
|
|
|
* @return integer the PDO type |
253
|
|
|
* @see http://www.php.net/manual/en/pdo.constants.php |
254
|
|
|
*/ |
255
|
452 |
|
public function getPdoType($data) |
256
|
|
|
{ |
257
|
|
|
static $typeMap = [ |
258
|
|
|
// php type => PDO type |
259
|
|
|
'boolean' => \PDO::PARAM_BOOL, |
260
|
|
|
'integer' => \PDO::PARAM_INT, |
261
|
|
|
'string' => \PDO::PARAM_STR, |
262
|
|
|
'resource' => \PDO::PARAM_LOB, |
263
|
|
|
'NULL' => \PDO::PARAM_NULL, |
264
|
452 |
|
]; |
265
|
452 |
|
$type = gettype($data); |
266
|
|
|
|
267
|
452 |
|
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Refreshes the schema. |
272
|
|
|
* This method cleans up all cached table schemas so that they can be re-created later |
273
|
|
|
* to reflect the database schema change. |
274
|
|
|
*/ |
275
|
9 |
|
public function refresh() |
276
|
|
|
{ |
277
|
|
|
/* @var $cache Cache */ |
278
|
9 |
|
$cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache; |
279
|
9 |
|
if ($this->db->enableSchemaCache && $cache instanceof Cache) { |
280
|
1 |
|
TagDependency::invalidate($cache, $this->getCacheTag()); |
281
|
1 |
|
} |
282
|
9 |
|
$this->_tableNames = []; |
283
|
9 |
|
$this->_tables = []; |
284
|
9 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Refreshes the particular table schema. |
288
|
|
|
* This method cleans up cached table schema so that it can be re-created later |
289
|
|
|
* to reflect the database schema change. |
290
|
|
|
* @param string $name table name. |
291
|
|
|
* @since 2.0.6 |
292
|
|
|
*/ |
293
|
19 |
|
public function refreshTableSchema($name) |
294
|
|
|
{ |
295
|
19 |
|
unset($this->_tables[$name]); |
296
|
19 |
|
$this->_tableNames = []; |
297
|
|
|
/* @var $cache Cache */ |
298
|
19 |
|
$cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache; |
299
|
19 |
|
if ($this->db->enableSchemaCache && $cache instanceof Cache) { |
300
|
4 |
|
$cache->delete($this->getCacheKey($name)); |
301
|
4 |
|
} |
302
|
19 |
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Creates a query builder for the database. |
306
|
|
|
* This method may be overridden by child classes to create a DBMS-specific query builder. |
307
|
|
|
* @return QueryBuilder query builder instance |
308
|
|
|
*/ |
309
|
|
|
public function createQueryBuilder() |
310
|
|
|
{ |
311
|
|
|
return new QueryBuilder($this->db); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Create a column schema builder instance giving the type and value precision. |
316
|
|
|
* |
317
|
|
|
* This method may be overridden by child classes to create a DBMS-specific column schema builder. |
318
|
|
|
* |
319
|
|
|
* @param string $type type of the column. See [[ColumnSchemaBuilder::$type]]. |
320
|
|
|
* @param integer|string|array $length length or precision of the column. See [[ColumnSchemaBuilder::$length]]. |
321
|
|
|
* @return ColumnSchemaBuilder column schema builder instance |
322
|
|
|
* @since 2.0.6 |
323
|
|
|
*/ |
324
|
2 |
|
public function createColumnSchemaBuilder($type, $length = null) |
325
|
|
|
{ |
326
|
2 |
|
return new ColumnSchemaBuilder($type, $length); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Returns all schema names in the database, including the default one but not system schemas. |
331
|
|
|
* This method should be overridden by child classes in order to support this feature |
332
|
|
|
* because the default implementation simply throws an exception. |
333
|
|
|
* @return array all schema names in the database, except system schemas |
334
|
|
|
* @throws NotSupportedException if this method is called |
335
|
|
|
* @since 2.0.4 |
336
|
|
|
*/ |
337
|
|
|
protected function findSchemaNames() |
338
|
|
|
{ |
339
|
|
|
throw new NotSupportedException(get_class($this) . ' does not support fetching all schema names.'); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Returns all table names in the database. |
344
|
|
|
* This method should be overridden by child classes in order to support this feature |
345
|
|
|
* because the default implementation simply throws an exception. |
346
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
347
|
|
|
* @return array all table names in the database. The names have NO schema name prefix. |
348
|
|
|
* @throws NotSupportedException if this method is called |
349
|
|
|
*/ |
350
|
|
|
protected function findTableNames($schema = '') |
351
|
|
|
{ |
352
|
|
|
throw new NotSupportedException(get_class($this) . ' does not support fetching all table names.'); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Returns all unique indexes for the given table. |
357
|
|
|
* Each array element is of the following structure: |
358
|
|
|
* |
359
|
|
|
* ```php |
360
|
|
|
* [ |
361
|
|
|
* 'IndexName1' => ['col1' [, ...]], |
362
|
|
|
* 'IndexName2' => ['col2' [, ...]], |
363
|
|
|
* ] |
364
|
|
|
* ``` |
365
|
|
|
* |
366
|
|
|
* This method should be overridden by child classes in order to support this feature |
367
|
|
|
* because the default implementation simply throws an exception |
368
|
|
|
* @param TableSchema $table the table metadata |
369
|
|
|
* @return array all unique indexes for the given table. |
370
|
|
|
* @throws NotSupportedException if this method is called |
371
|
|
|
*/ |
372
|
|
|
public function findUniqueIndexes($table) |
373
|
|
|
{ |
374
|
|
|
throw new NotSupportedException(get_class($this) . ' does not support getting unique indexes information.'); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Returns the ID of the last inserted row or sequence value. |
379
|
|
|
* @param string $sequenceName name of the sequence object (required by some DBMS) |
380
|
|
|
* @return string the row ID of the last row inserted, or the last value retrieved from the sequence object |
381
|
|
|
* @throws InvalidCallException if the DB connection is not active |
382
|
|
|
* @see http://www.php.net/manual/en/function.PDO-lastInsertId.php |
383
|
|
|
*/ |
384
|
37 |
|
public function getLastInsertID($sequenceName = '') |
385
|
|
|
{ |
386
|
37 |
|
if ($this->db->isActive) { |
387
|
37 |
|
return $this->db->pdo->lastInsertId($sequenceName === '' ? null : $this->quoteTableName($sequenceName)); |
388
|
|
|
} else { |
389
|
|
|
throw new InvalidCallException('DB Connection is not active.'); |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @return boolean whether this DBMS supports [savepoint](http://en.wikipedia.org/wiki/Savepoint). |
395
|
|
|
*/ |
396
|
3 |
|
public function supportsSavepoint() |
397
|
|
|
{ |
398
|
3 |
|
return $this->db->enableSavepoint; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Creates a new savepoint. |
403
|
|
|
* @param string $name the savepoint name |
404
|
|
|
*/ |
405
|
3 |
|
public function createSavepoint($name) |
406
|
|
|
{ |
407
|
3 |
|
$this->db->createCommand("SAVEPOINT $name")->execute(); |
408
|
3 |
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Releases an existing savepoint. |
412
|
|
|
* @param string $name the savepoint name |
413
|
|
|
*/ |
414
|
|
|
public function releaseSavepoint($name) |
415
|
|
|
{ |
416
|
|
|
$this->db->createCommand("RELEASE SAVEPOINT $name")->execute(); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Rolls back to a previously created savepoint. |
421
|
|
|
* @param string $name the savepoint name |
422
|
|
|
*/ |
423
|
3 |
|
public function rollBackSavepoint($name) |
424
|
|
|
{ |
425
|
3 |
|
$this->db->createCommand("ROLLBACK TO SAVEPOINT $name")->execute(); |
426
|
3 |
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Sets the isolation level of the current transaction. |
430
|
|
|
* @param string $level The transaction isolation level to use for this transaction. |
431
|
|
|
* This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], [[Transaction::REPEATABLE_READ]] |
432
|
|
|
* and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific syntax to be used |
433
|
|
|
* after `SET TRANSACTION ISOLATION LEVEL`. |
434
|
|
|
* @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels |
435
|
|
|
*/ |
436
|
4 |
|
public function setTransactionIsolationLevel($level) |
437
|
|
|
{ |
438
|
4 |
|
$this->db->createCommand("SET TRANSACTION ISOLATION LEVEL $level;")->execute(); |
439
|
4 |
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Executes the INSERT command, returning primary key values. |
443
|
|
|
* @param string $table the table that new rows will be inserted into. |
444
|
|
|
* @param array $columns the column data (name => value) to be inserted into the table. |
445
|
|
|
* @return array primary key values or false if the command fails |
446
|
|
|
* @since 2.0.4 |
447
|
|
|
*/ |
448
|
40 |
|
public function insert($table, $columns) |
449
|
|
|
{ |
450
|
40 |
|
$command = $this->db->createCommand()->insert($table, $columns); |
451
|
40 |
|
if (!$command->execute()) { |
452
|
|
|
return false; |
453
|
|
|
} |
454
|
40 |
|
$tableSchema = $this->getTableSchema($table); |
455
|
40 |
|
$result = []; |
456
|
40 |
|
foreach ($tableSchema->primaryKey as $name) { |
457
|
38 |
|
if ($tableSchema->columns[$name]->autoIncrement) { |
458
|
34 |
|
$result[$name] = $this->getLastInsertID($tableSchema->sequenceName); |
459
|
34 |
|
break; |
460
|
|
|
} else { |
461
|
6 |
|
$result[$name] = isset($columns[$name]) ? $columns[$name] : $tableSchema->columns[$name]->defaultValue; |
462
|
|
|
} |
463
|
40 |
|
} |
464
|
40 |
|
return $result; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Quotes a string value for use in a query. |
469
|
|
|
* Note that if the parameter is not a string, it will be returned without change. |
470
|
|
|
* @param string $str string to be quoted |
471
|
|
|
* @return string the properly quoted string |
472
|
|
|
* @see http://www.php.net/manual/en/function.PDO-quote.php |
473
|
|
|
*/ |
474
|
389 |
|
public function quoteValue($str) |
475
|
|
|
{ |
476
|
389 |
|
if (!is_string($str)) { |
477
|
3 |
|
return $str; |
478
|
|
|
} |
479
|
|
|
|
480
|
389 |
|
if (($value = $this->db->getSlavePdo()->quote($str)) !== false) { |
481
|
389 |
|
return $value; |
482
|
|
|
} else { |
483
|
|
|
// the driver doesn't support quote (e.g. oci) |
484
|
|
|
return "'" . addcslashes(str_replace("'", "''", $str), "\000\n\r\\\032") . "'"; |
485
|
|
|
} |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Quotes a table name for use in a query. |
490
|
|
|
* If the table name contains schema prefix, the prefix will also be properly quoted. |
491
|
|
|
* If the table name is already quoted or contains '(' or '{{', |
492
|
|
|
* then this method will do nothing. |
493
|
|
|
* @param string $name table name |
494
|
|
|
* @return string the properly quoted table name |
495
|
|
|
* @see quoteSimpleTableName() |
496
|
|
|
*/ |
497
|
580 |
|
public function quoteTableName($name) |
498
|
|
|
{ |
499
|
580 |
|
if (strpos($name, '(') !== false || strpos($name, '{{') !== false) { |
500
|
154 |
|
return $name; |
501
|
|
|
} |
502
|
580 |
|
if (strpos($name, '.') === false) { |
503
|
580 |
|
return $this->quoteSimpleTableName($name); |
504
|
|
|
} |
505
|
4 |
|
$parts = explode('.', $name); |
506
|
4 |
|
foreach ($parts as $i => $part) { |
507
|
4 |
|
$parts[$i] = $this->quoteSimpleTableName($part); |
508
|
4 |
|
} |
509
|
|
|
|
510
|
4 |
|
return implode('.', $parts); |
511
|
|
|
|
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* Quotes a column name for use in a query. |
516
|
|
|
* If the column name contains prefix, the prefix will also be properly quoted. |
517
|
|
|
* If the column name is already quoted or contains '(', '[[' or '{{', |
518
|
|
|
* then this method will do nothing. |
519
|
|
|
* @param string $name column name |
520
|
|
|
* @return string the properly quoted column name |
521
|
|
|
* @see quoteSimpleColumnName() |
522
|
|
|
*/ |
523
|
651 |
|
public function quoteColumnName($name) |
524
|
|
|
{ |
525
|
651 |
|
if (strpos($name, '(') !== false || strpos($name, '[[') !== false) { |
526
|
29 |
|
return $name; |
527
|
|
|
} |
528
|
651 |
|
if (($pos = strrpos($name, '.')) !== false) { |
529
|
87 |
|
$prefix = $this->quoteTableName(substr($name, 0, $pos)) . '.'; |
530
|
87 |
|
$name = substr($name, $pos + 1); |
531
|
87 |
|
} else { |
532
|
651 |
|
$prefix = ''; |
533
|
|
|
} |
534
|
651 |
|
if (strpos($name, '{{') !== false) { |
535
|
3 |
|
return $name; |
536
|
|
|
} |
537
|
651 |
|
return $prefix . $this->quoteSimpleColumnName($name); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Quotes a simple table name for use in a query. |
542
|
|
|
* A simple table name should contain the table name only without any schema prefix. |
543
|
|
|
* If the table name is already quoted, this method will do nothing. |
544
|
|
|
* @param string $name table name |
545
|
|
|
* @return string the properly quoted table name |
546
|
|
|
*/ |
547
|
|
|
public function quoteSimpleTableName($name) |
548
|
|
|
{ |
549
|
|
|
return strpos($name, "'") !== false ? $name : "'" . $name . "'"; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Quotes a simple column name for use in a query. |
554
|
|
|
* A simple column name should contain the column name only without any prefix. |
555
|
|
|
* If the column name is already quoted or is the asterisk character '*', this method will do nothing. |
556
|
|
|
* @param string $name column name |
557
|
|
|
* @return string the properly quoted column name |
558
|
|
|
*/ |
559
|
185 |
|
public function quoteSimpleColumnName($name) |
560
|
|
|
{ |
561
|
185 |
|
return strpos($name, '"') !== false || $name === '*' ? $name : '"' . $name . '"'; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* Returns the actual name of a given table name. |
566
|
|
|
* This method will strip off curly brackets from the given table name |
567
|
|
|
* and replace the percentage character '%' with [[Connection::tablePrefix]]. |
568
|
|
|
* @param string $name the table name to be converted |
569
|
|
|
* @return string the real name of the given table name |
570
|
|
|
*/ |
571
|
359 |
|
public function getRawTableName($name) |
572
|
|
|
{ |
573
|
359 |
|
if (strpos($name, '{{') !== false) { |
574
|
60 |
|
$name = preg_replace('/\\{\\{(.*?)\\}\\}/', '\1', $name); |
575
|
|
|
|
576
|
60 |
|
return str_replace('%', $this->db->tablePrefix, $name); |
577
|
|
|
} else { |
578
|
300 |
|
return $name; |
579
|
|
|
} |
580
|
|
|
} |
581
|
|
|
|
582
|
|
|
/** |
583
|
|
|
* Extracts the PHP type from abstract DB type. |
584
|
|
|
* @param ColumnSchema $column the column schema information |
585
|
|
|
* @return string PHP type name |
586
|
|
|
*/ |
587
|
349 |
|
protected function getColumnPhpType($column) |
588
|
|
|
{ |
589
|
|
|
static $typeMap = [ |
590
|
|
|
// abstract type => php type |
591
|
|
|
'smallint' => 'integer', |
592
|
|
|
'integer' => 'integer', |
593
|
|
|
'bigint' => 'integer', |
594
|
|
|
'boolean' => 'boolean', |
595
|
|
|
'float' => 'double', |
596
|
|
|
'double' => 'double', |
597
|
|
|
'binary' => 'resource', |
598
|
349 |
|
]; |
599
|
349 |
|
if (isset($typeMap[$column->type])) { |
600
|
345 |
|
if ($column->type === 'bigint') { |
601
|
|
|
return PHP_INT_SIZE === 8 && !$column->unsigned ? 'integer' : 'string'; |
602
|
345 |
|
} elseif ($column->type === 'integer') { |
603
|
345 |
|
return PHP_INT_SIZE === 4 && $column->unsigned ? 'string' : 'integer'; |
604
|
|
|
} else { |
605
|
94 |
|
return $typeMap[$column->type]; |
606
|
|
|
} |
607
|
|
|
} else { |
608
|
334 |
|
return 'string'; |
609
|
|
|
} |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
/** |
613
|
|
|
* Converts a DB exception to a more concrete one if possible. |
614
|
|
|
* |
615
|
|
|
* @param \Exception $e |
616
|
|
|
* @param string $rawSql SQL that produced exception |
617
|
|
|
* @return Exception |
618
|
|
|
*/ |
619
|
19 |
|
public function convertException(\Exception $e, $rawSql) |
620
|
|
|
{ |
621
|
19 |
|
if ($e instanceof Exception) { |
622
|
|
|
return $e; |
623
|
|
|
} |
624
|
|
|
|
625
|
19 |
|
$exceptionClass = '\yii\db\Exception'; |
626
|
19 |
|
foreach ($this->exceptionMap as $error => $class) { |
627
|
19 |
|
if (strpos($e->getMessage(), $error) !== false) { |
628
|
6 |
|
$exceptionClass = $class; |
629
|
6 |
|
} |
630
|
19 |
|
} |
631
|
19 |
|
$message = $e->getMessage() . "\nThe SQL being executed was: $rawSql"; |
632
|
19 |
|
$errorInfo = $e instanceof \PDOException ? $e->errorInfo : null; |
633
|
19 |
|
return new $exceptionClass($message, $errorInfo, (int) $e->getCode(), $e); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* Returns a value indicating whether a SQL statement is for read purpose. |
638
|
|
|
* @param string $sql the SQL statement |
639
|
|
|
* @return boolean whether a SQL statement is for read purpose. |
640
|
|
|
*/ |
641
|
9 |
|
public function isReadQuery($sql) |
642
|
|
|
{ |
643
|
9 |
|
$pattern = '/^\s*(SELECT|SHOW|DESCRIBE)\b/i'; |
644
|
9 |
|
return preg_match($pattern, $sql) > 0; |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
|