forked from edgardmessias/yii2-firebird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schema.php
494 lines (457 loc) · 18 KB
/
Schema.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace edgardmessias\db\firebird;
use yii\db\ColumnSchema;
use yii\db\Exception;
use yii\db\TableSchema;
/**
* Schema represents the Firebird schema information.
*
* @property string[] $indexNames All index names in the Firebird. This property is read-only.
* @property IndexSchema[] $indexSchemas The metadata for all indexes in the Firebird. Each array element is an
* instance of [[IndexSchema]] or its child class. This property is read-only.
* @property array $indexTypes All index types in the Firebird in format: index name => index type. This
* property is read-only.
* @property QueryBuilder $queryBuilder The query builder for this connection. This property is read-only.
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @since 2.0
*/
class Schema extends \yii\db\Schema
{
private $_lastInsertID = null;
/**
* @var array map of DB errors and corresponding exceptions
* If left part is found in DB error message exception class from the right part is used.
*/
public $exceptionMap = [
'SQLSTATE[23' => 'yii\db\IntegrityException',
'SQLSTATE[HY000]: General error: -803 violation of PRIMARY' => 'yii\db\IntegrityException',
];
public $reservedWords = [
'ORDER',
'TIME',
'POSITION',
];
/**
* @var array mapping from physical column types (keys) to abstract column types (values)
*/
public $typeMap = [
'bigint' => self::TYPE_BIGINT,
'char' => self::TYPE_STRING,
'varchar' => self::TYPE_STRING,
'timestamp' => self::TYPE_TIMESTAMP,
'decimal' => self::TYPE_DECIMAL,
'float' => self::TYPE_FLOAT,
'blob' => self::TYPE_BINARY,
'integer' => self::TYPE_INTEGER,
'blob sub_type text' => self::TYPE_TEXT,
'numeric' => self::TYPE_DECIMAL,
'double precision' => self::TYPE_DOUBLE,
'smallint' => self::TYPE_SMALLINT,
];
/**
* Creates a query builder for the database.
* This method may be overridden by child classes to create a DBMS-specific query builder.
* @return QueryBuilder query builder instance
*/
public function createQueryBuilder()
{
return new QueryBuilder($this->db);
}
public function quoteSimpleTableName($name)
{
if (in_array(strtoupper($name), $this->reservedWords)) {
return strpos($name, '"') !== false ? $name : '"' . $name . '"';
}
return $name;
}
public function quoteSimpleColumnName($name)
{
if (in_array(strtoupper($name), $this->reservedWords)) {
return parent::quoteSimpleColumnName($name);
}
return $name;
}
protected function loadTableSchema($name)
{
$table = new TableSchema;
$this->resolveTableNames($table, $name);
if ($this->findColumns($table)) {
$this->findConstraints($table);
return $table;
}
return null;
}
public function getPdoType($data)
{
static $typeMap = [
// php type => PDO type
'boolean' => \PDO::PARAM_INT,
'integer' => \PDO::PARAM_INT,
'string' => \PDO::PARAM_STR,
'resource' => \PDO::PARAM_LOB,
'NULL' => \PDO::PARAM_NULL,
];
$type = gettype($data);
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR;
}
/**
*
* @param TableSchema $table
* @param string $name
*/
protected function resolveTableNames($table, $name)
{
$parts = explode('.', str_replace('"', '', $name));
if (isset($parts[1])) {
$table->schemaName = $parts[0];
$table->name = strtolower($parts[1]);
$table->fullName = $this->quoteTableName($table->schemaName) . '.' . $this->quoteTableName($table->name);
} else {
$table->name = strtolower($parts[0]);
$table->fullName = $this->quoteTableName($table->name);
}
}
/**
* Collects the table column metadata.
*
* @param TableSchema $table the table metadata
* @return boolean whether the table exists in the database
*/
protected function findColumns($table)
{
// Zoggo - Converted sql to use join syntax
// robregonm - Added isAutoInc
$sql = 'SELECT
rel.rdb$field_name AS fname,
rel.rdb$default_source AS fdefault,
fld.rdb$field_type AS fcodtype,
fld.rdb$field_sub_type AS fcodsubtype,
fld.rdb$field_length AS flength,
fld.rdb$character_length AS fcharlength,
fld.rdb$field_scale AS fscale,
fld.rdb$field_precision AS fprecision,
rel.rdb$null_flag AS fnull,
fld.rdb$default_value AS fdefault_value,
(SELECT RDB$TRIGGER_SOURCE FROM RDB$TRIGGERS
WHERE RDB$SYSTEM_FLAG = 0
AND UPPER(RDB$RELATION_NAME)=UPPER(\'' . $table->name . '\')
AND RDB$TRIGGER_TYPE = 1
AND RDB$TRIGGER_INACTIVE = 0
AND (UPPER(REPLACE(RDB$TRIGGER_SOURCE,\' \',\'\')) LIKE \'%NEW.\'||TRIM(rel.rdb$field_name)||\'=GEN_ID%\'
OR UPPER(REPLACE(RDB$TRIGGER_SOURCE,\' \',\'\')) LIKE \'%NEW.\'||TRIM(rel.rdb$field_name)||\'=NEXTVALUEFOR%\'))
AS fautoinc
FROM
rdb$relation_fields rel
JOIN rdb$fields fld ON rel.rdb$field_source=fld.rdb$field_name
WHERE
UPPER(rel.rdb$relation_name)=UPPER(\'' . $table->name . '\')
ORDER BY
rel.rdb$field_position;';
try {
$columns = $this->db->createCommand($sql)->queryAll();
if (!$columns) {
return false;
}
} catch (Exception $e) {
return false;
}
$sql = 'SELECT
idx.rdb$field_name AS fname
FROM
rdb$relation_constraints rc
JOIN rdb$index_segments idx ON idx.rdb$index_name=rc.rdb$index_name
WHERE rc.rdb$constraint_type=\'PRIMARY KEY\'
AND UPPER(rc.rdb$relation_name)=UPPER(\'' . $table->name . '\')';
try {
$pkeys = $this->db->createCommand($sql)->queryColumn();
} catch (Exception $e) {
return false;
}
$pkeys = array_map("rtrim", $pkeys);
$pkeys = array_map("strtolower", $pkeys);
foreach ($columns as $key => $column) {
$column = array_map("strtolower", $column);
$columns[$key]['fprimary'] = in_array(rtrim($column['fname']), $pkeys);
}
foreach ($columns as $column) {
$c = $this->loadColumnSchema($column);
if ($table->sequenceName === null && $c->autoIncrement) {
$matches = [];
if (preg_match("/NEW.{$c->name}\s*=\s*GEN_ID\((\w+)/i", $column['fautoinc'], $matches)) {
$table->sequenceName = $matches[1];
} elseif (preg_match("/NEW.{$c->name}\s*=\s*NEXT\s+VALUE\s+FOR\s+(\w+)/i", $column['fautoinc'], $matches)) {
$table->sequenceName = $matches[1];
}
}
$table->columns[$c->name] = $c;
if ($c->isPrimaryKey) {
$table->primaryKey[] = $c->name;
}
}
return (count($table->columns) > 0);
}
/**
* @return \yii\db\ColumnSchema
* @throws \yii\base\InvalidConfigException
*/
protected function createColumnSchema()
{
return \Yii::createObject('\edgardmessias\db\firebird\ColumnSchema');
}
/**
* Creates a table column.
*
* @param array $column column metadata
* @return ColumnSchema normalized column metadata
*/
protected function loadColumnSchema($column)
{
$c = $this->createColumnSchema();
$c->name = strtolower(rtrim($column['fname']));
$c->allowNull = $column['fnull'] !== '1';
$c->isPrimaryKey = $column['fprimary'];
$c->autoIncrement = (boolean)$column['fautoinc'];
$c->type = self::TYPE_STRING;
$defaultValue = null;
if (!empty($column['fdefault'])) {
// remove whitespace, 'DEFAULT ' prefix and surrounding single quotes; all optional
if (preg_match("/\s*(DEFAULT\s+){0,1}('(.*)'|(.*))\s*/i", $column['fdefault'], $parts)) {
$defaultValue = array_pop($parts);
}
// handle escaped single quotes like in "funny''quoted''string"
$defaultValue = str_replace('\'\'', '\'', $defaultValue);
}
if ($defaultValue === null) {
$defaultValue = $column['fdefault_value'];
}
$dbType = "";
$baseTypes = array(
7 => 'SMALLINT',
8 => 'INTEGER',
16 => 'INT64',
9 => 'QUAD',
10 => 'FLOAT',
11 => 'D_FLOAT',
17 => 'BOOLEAN',
27 => 'DOUBLE PRECISION',
12 => 'DATE',
13 => 'TIME',
35 => 'TIMESTAMP',
261 => 'BLOB',
40 => 'CSTRING',
45 => 'BLOB_ID',
);
$baseCharTypes = array(
37 => 'VARCHAR',
14 => 'CHAR',
);
if (array_key_exists((int) $column['fcodtype'], $baseTypes)) {
$dbType = $baseTypes[(int) $column['fcodtype']];
} elseif (array_key_exists((int) $column['fcodtype'], $baseCharTypes)) {
$c->size = (int) $column['fcharlength'];
$c->precision = $c->size;
$dbType = $baseCharTypes[(int) $column['fcodtype']] . "($c->size)";
}
switch ((int) $column['fcodtype']) {
case 7:
case 8:
switch ((int) $column['fcodsubtype']) {
case 1:
$c->precision = (int) $column['fprecision'];
$c->size = $c->precision;
$c->scale = abs((int) $column['fscale']);
$dbType = "NUMERIC({$c->precision},{$c->scale})";
break;
case 2:
$c->precision = (int) $column['fprecision'];
$c->size = $c->precision;
$c->scale = abs((int) $column['fscale']);
$dbType = "DECIMAL({$c->precision},{$c->scale})";
break;
}
break;
case 16:
switch ((int) $column['fcodsubtype']) {
case 1:
$c->precision = (int) $column['fprecision'];
$c->size = $c->precision;
$c->scale = abs((int) $column['fscale']);
$dbType = "NUMERIC({$c->precision},{$c->scale})";
break;
case 2:
$c->precision = (int) $column['fprecision'];
$c->size = $c->precision;
$c->scale = abs((int) $column['fscale']);
$dbType = "DECIMAL({$c->precision},{$c->scale})";
break;
default :
$dbType = 'BIGINT';
break;
}
break;
case 261:
switch ((int) $column['fcodsubtype']) {
case 1:
$dbType = 'BLOB SUB_TYPE TEXT';
$c->size = null;
break;
}
break;
}
$c->dbType = strtolower($dbType);
$c->type = self::TYPE_STRING;
if (preg_match('/^([\w\ ]+)(?:\(([^\)]+)\))?/', $c->dbType, $matches)) {
$type = strtolower($matches[1]);
if (isset($this->typeMap[$type])) {
$c->type = $this->typeMap[$type];
}
}
$c->phpType = $this->getColumnPhpType($c);
$c->defaultValue = null;
if ($defaultValue !== null) {
if (in_array($c->type, [self::TYPE_DATE, self::TYPE_DATETIME, self::TYPE_TIME, self::TYPE_TIMESTAMP])
&& preg_match('/(CURRENT_|NOW|NULL|TODAY|TOMORROW|YESTERDAY)/i', $defaultValue)) {
$c->defaultValue = new \yii\db\Expression(trim($defaultValue));
} else {
$c->defaultValue = $c->phpTypecast($defaultValue);
}
}
return $c;
}
/**
* Collects the foreign key column details for the given table.
*
* @param TableSchema $table the table metadata
*/
protected function findConstraints($table)
{
// Zoggo - Converted sql to use join syntax
$sql = 'SELECT
a.rdb$constraint_name as fconstraint,
c.rdb$relation_name AS ftable,
d.rdb$field_name AS pfield,
e.rdb$field_name AS ffield
FROM
rdb$ref_constraints b
JOIN rdb$relation_constraints a ON a.rdb$constraint_name=b.rdb$constraint_name
JOIN rdb$relation_constraints c ON b.rdb$const_name_uq=c.rdb$constraint_name
JOIN rdb$index_segments d ON c.rdb$index_name=d.rdb$index_name
JOIN rdb$index_segments e ON a.rdb$index_name=e.rdb$index_name AND e.rdb$field_position = d.rdb$field_position
WHERE
a.rdb$constraint_type=\'FOREIGN KEY\' AND
UPPER(a.rdb$relation_name)=UPPER(\'' . $table->name . '\') ';
try {
$fkeys = $this->db->createCommand($sql)->queryAll();
} catch (Exception $e) {
return false;
}
$constraints = [];
foreach ($fkeys as $fkey) {
// Zoggo - Added strtolower here to guarantee that values are
// returned lower case. Otherwise gii generates wrong code.
$fkey = array_map("rtrim", $fkey);
$fkey = array_map("strtolower", $fkey);
if (!isset($constraints[$fkey['fconstraint']])) {
$constraints[$fkey['fconstraint']] = [
$fkey['ftable']
];
}
$constraints[$fkey['fconstraint']][$fkey['ffield']] = $fkey['pfield'];
}
$table->foreignKeys = array_values($constraints);
}
protected function findTableNames($schema = '')
{
$sql = 'SELECT
rdb$relation_name
FROM
rdb$relations
WHERE
(rdb$view_blr is null) AND
(rdb$system_flag is null OR rdb$system_flag=0)';
try {
$tables = $this->db->createCommand($sql)->queryColumn();
} catch (Exception $e) {
return false;
}
foreach ($tables as $key => $table) {
$tables[$key] = strtolower(rtrim($table));
}
return $tables;
}
/**
* Sets the isolation level of the current transaction.
* @param string $level The transaction isolation level to use for this transaction.
* This can be one of [[Transaction::READ_UNCOMMITTED]], [[Transaction::READ_COMMITTED]], [[Transaction::REPEATABLE_READ]]
* and [[Transaction::SERIALIZABLE]] but also a string containing DBMS specific syntax to be used
* after `SET TRANSACTION ISOLATION LEVEL`.
* @see http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Isolation_levels
*/
public function setTransactionIsolationLevel($level)
{
if ($level == \yii\db\Transaction::READ_UNCOMMITTED) {
parent::setTransactionIsolationLevel('READ COMMITTED RECORD_VERSION');
} elseif ($level == \yii\db\Transaction::REPEATABLE_READ) {
parent::setTransactionIsolationLevel('SNAPSHOT');
} elseif ($level == \yii\db\Transaction::SERIALIZABLE) {
parent::setTransactionIsolationLevel('SNAPSHOT TABLE STABILITY');
} else {
parent::setTransactionIsolationLevel($level);
}
}
/**
* @inheritdoc
*/
public function insert($table, $columns)
{
$this->_lastInsertID = false;
$params = [];
$sql = $this->db->getQueryBuilder()->insert($table, $columns, $params);
$returnColumns = $this->getTableSchema($table)->primaryKey;
if (!empty($returnColumns)) {
$returning = [];
foreach ((array)$returnColumns as $name) {
$returning[] = $this->quoteColumnName($name);
}
$sql .= ' RETURNING ' . implode(', ', $returning);
}
$command = $this->db->createCommand($sql, $params);
$command->prepare(false);
$result = $command->queryOne();
if (!$command->pdoStatement->rowCount()) {
return false;
} else {
if (!empty($returnColumns)) {
foreach ((array)$returnColumns as $name) {
if ($this->getTableSchema($table)->getColumn($name)->autoIncrement) {
$this->_lastInsertID = $result[$name];
break;
}
}
}
return $result;
}
}
/**
* @inheritdoc
*/
public function getLastInsertID($sequenceName = '')
{
if (!$this->db->isActive) {
throw new InvalidCallException('DB Connection is not active.');
}
if ($sequenceName !== '') {
return $this->db->createCommand('SELECT GEN_ID('. $this->db->quoteTableName($sequenceName) . ', 0 ) FROM RDB$DATABASE;')->queryScalar();
}
if ($this->_lastInsertID !== false) {
return $this->_lastInsertID;
}
return null;
}
}