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\cubrid; |
9
|
|
|
|
10
|
|
|
use yii\db\Expression; |
11
|
|
|
use yii\db\TableSchema; |
12
|
|
|
use yii\db\ColumnSchema; |
13
|
|
|
use yii\db\Transaction; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Schema is the class for retrieving metadata from a CUBRID database (version 9.3.x and higher). |
17
|
|
|
* |
18
|
|
|
* @author Carsten Brandt <[email protected]> |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
class Schema extends \yii\db\Schema |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array mapping from physical column types (keys) to abstract column types (values) |
25
|
|
|
* Please refer to [CUBRID manual](http://www.cubrid.org/manual/91/en/sql/datatype.html) for |
26
|
|
|
* details on data types. |
27
|
|
|
*/ |
28
|
|
|
public $typeMap = [ |
29
|
|
|
// Numeric data types |
30
|
|
|
'short' => self::TYPE_SMALLINT, |
31
|
|
|
'smallint' => self::TYPE_SMALLINT, |
32
|
|
|
'int' => self::TYPE_INTEGER, |
33
|
|
|
'integer' => self::TYPE_INTEGER, |
34
|
|
|
'bigint' => self::TYPE_BIGINT, |
35
|
|
|
'numeric' => self::TYPE_DECIMAL, |
36
|
|
|
'decimal' => self::TYPE_DECIMAL, |
37
|
|
|
'float' => self::TYPE_FLOAT, |
38
|
|
|
'real' => self::TYPE_FLOAT, |
39
|
|
|
'double' => self::TYPE_DOUBLE, |
40
|
|
|
'double precision' => self::TYPE_DOUBLE, |
41
|
|
|
'monetary' => self::TYPE_MONEY, |
42
|
|
|
// Date/Time data types |
43
|
|
|
'date' => self::TYPE_DATE, |
44
|
|
|
'time' => self::TYPE_TIME, |
45
|
|
|
'timestamp' => self::TYPE_TIMESTAMP, |
46
|
|
|
'datetime' => self::TYPE_DATETIME, |
47
|
|
|
// String data types |
48
|
|
|
'char' => self::TYPE_CHAR, |
49
|
|
|
'varchar' => self::TYPE_STRING, |
50
|
|
|
'char varying' => self::TYPE_STRING, |
51
|
|
|
'nchar' => self::TYPE_CHAR, |
52
|
|
|
'nchar varying' => self::TYPE_STRING, |
53
|
|
|
'string' => self::TYPE_STRING, |
54
|
|
|
// BLOB/CLOB data types |
55
|
|
|
'blob' => self::TYPE_BINARY, |
56
|
|
|
'clob' => self::TYPE_BINARY, |
57
|
|
|
// Bit string data types |
58
|
|
|
'bit' => self::TYPE_INTEGER, |
59
|
|
|
'bit varying' => self::TYPE_INTEGER, |
60
|
|
|
// Collection data types (considered strings for now) |
61
|
|
|
'set' => self::TYPE_STRING, |
62
|
|
|
'multiset' => self::TYPE_STRING, |
63
|
|
|
'list' => self::TYPE_STRING, |
64
|
|
|
'sequence' => self::TYPE_STRING, |
65
|
|
|
'enum' => self::TYPE_STRING, |
66
|
|
|
]; |
67
|
|
|
/** |
68
|
|
|
* @var array map of DB errors and corresponding exceptions |
69
|
|
|
* If left part is found in DB error message exception class from the right part is used. |
70
|
|
|
*/ |
71
|
|
|
public $exceptionMap = [ |
72
|
|
|
'Operation would have caused one or more unique constraint violations' => IntegrityException::class, |
73
|
|
|
]; |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function releaseSavepoint($name) |
80
|
|
|
{ |
81
|
|
|
// does nothing as cubrid does not support this |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Quotes a table name for use in a query. |
86
|
|
|
* A simple table name has no schema prefix. |
87
|
|
|
* @param string $name table name |
88
|
|
|
* @return string the properly quoted table name |
89
|
|
|
*/ |
90
|
|
|
public function quoteSimpleTableName($name) |
91
|
|
|
{ |
92
|
|
|
return strpos($name, '"') !== false ? $name : '"' . $name . '"'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Quotes a column name for use in a query. |
97
|
|
|
* A simple column name has no prefix. |
98
|
|
|
* @param string $name column name |
99
|
|
|
* @return string the properly quoted column name |
100
|
|
|
*/ |
101
|
|
|
public function quoteSimpleColumnName($name) |
102
|
|
|
{ |
103
|
|
|
return strpos($name, '"') !== false || $name === '*' ? $name : '"' . $name . '"'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Creates a query builder for the CUBRID database. |
108
|
|
|
* @return QueryBuilder query builder instance |
109
|
|
|
*/ |
110
|
|
|
public function createQueryBuilder() |
111
|
|
|
{ |
112
|
|
|
return new QueryBuilder($this->db); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Loads the metadata for the specified table. |
117
|
|
|
* @param string $name table name |
118
|
|
|
* @return TableSchema driver dependent table metadata. Null if the table does not exist. |
119
|
|
|
*/ |
120
|
|
|
protected function loadTableSchema($name) |
121
|
|
|
{ |
122
|
|
|
$pdo = $this->db->getSlavePdo(); |
123
|
|
|
|
124
|
|
|
$tableInfo = $pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name); |
125
|
|
|
|
126
|
|
|
if (!isset($tableInfo[0]['NAME'])) { |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$table = new TableSchema(); |
131
|
|
|
$table->fullName = $table->name = $tableInfo[0]['NAME']; |
132
|
|
|
|
133
|
|
|
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name); |
134
|
|
|
$columns = $this->db->createCommand($sql)->queryAll(); |
135
|
|
|
|
136
|
|
|
foreach ($columns as $info) { |
137
|
|
|
$column = $this->loadColumnSchema($info); |
138
|
|
|
$table->columns[$column->name] = $column; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$primaryKeys = $pdo->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $table->name); |
142
|
|
|
foreach ($primaryKeys as $key) { |
143
|
|
|
$column = $table->columns[$key['ATTR_NAME']]; |
144
|
|
|
$column->isPrimaryKey = true; |
145
|
|
|
$table->primaryKey[] = $column->name; |
146
|
|
|
if ($column->autoIncrement) { |
147
|
|
|
$table->sequenceName = ''; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$foreignKeys = $pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name); |
152
|
|
|
foreach ($foreignKeys as $key) { |
153
|
|
|
if (isset($table->foreignKeys[$key['FK_NAME']])) { |
154
|
|
|
$table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME']; |
155
|
|
|
} else { |
156
|
|
|
$table->foreignKeys[$key['FK_NAME']] = [ |
157
|
|
|
$key['PKTABLE_NAME'], |
158
|
|
|
$key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME'], |
159
|
|
|
]; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
$table->foreignKeys = array_values($table->foreignKeys); |
163
|
|
|
|
164
|
|
|
return $table; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Loads the column information into a [[ColumnSchema]] object. |
169
|
|
|
* @param array $info column information |
170
|
|
|
* @return ColumnSchema the column schema object |
171
|
|
|
*/ |
172
|
|
|
protected function loadColumnSchema($info) |
173
|
|
|
{ |
174
|
|
|
$column = $this->createColumnSchema(); |
175
|
|
|
|
176
|
|
|
$column->name = $info['Field']; |
177
|
|
|
$column->allowNull = $info['Null'] === 'YES'; |
178
|
|
|
$column->isPrimaryKey = false; // primary key will be set by loadTableSchema() later |
179
|
|
|
$column->autoIncrement = stripos($info['Extra'], 'auto_increment') !== false; |
180
|
|
|
|
181
|
|
|
$column->dbType = $info['Type']; |
182
|
|
|
$column->unsigned = strpos($column->dbType, 'unsigned') !== false; |
183
|
|
|
|
184
|
|
|
$column->type = self::TYPE_STRING; |
185
|
|
|
if (preg_match('/^([\w ]+)(?:\(([^\)]+)\))?$/', $column->dbType, $matches)) { |
186
|
|
|
$type = strtolower($matches[1]); |
187
|
|
|
$column->dbType = $type . (isset($matches[2]) ? "({$matches[2]})" : ''); |
188
|
|
|
if (isset($this->typeMap[$type])) { |
189
|
|
|
$column->type = $this->typeMap[$type]; |
190
|
|
|
} |
191
|
|
|
if (!empty($matches[2])) { |
192
|
|
|
if ($type === 'enum') { |
193
|
|
|
$values = preg_split('/\s*,\s*/', $matches[2]); |
194
|
|
|
foreach ($values as $i => $value) { |
195
|
|
|
$values[$i] = trim($value, "'"); |
196
|
|
|
} |
197
|
|
|
$column->enumValues = $values; |
198
|
|
|
} else { |
199
|
|
|
$values = explode(',', $matches[2]); |
200
|
|
|
$column->size = $column->precision = (int) $values[0]; |
201
|
|
|
if (isset($values[1])) { |
202
|
|
|
$column->scale = (int) $values[1]; |
203
|
|
|
} |
204
|
|
|
if ($column->size === 1 && $type === 'bit') { |
205
|
|
|
$column->type = 'boolean'; |
206
|
|
|
} elseif ($type === 'bit') { |
207
|
|
|
if ($column->size > 32) { |
208
|
|
|
$column->type = 'bigint'; |
209
|
|
|
} elseif ($column->size === 32) { |
210
|
|
|
$column->type = 'integer'; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$column->phpType = $this->getColumnPhpType($column); |
218
|
|
|
|
219
|
|
|
if ($column->isPrimaryKey) { |
220
|
|
|
return $column; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if ($column->type === 'timestamp' && $info['Default'] === 'SYS_TIMESTAMP' || |
224
|
|
|
$column->type === 'datetime' && $info['Default'] === 'SYS_DATETIME' || |
225
|
|
|
$column->type === 'date' && $info['Default'] === 'SYS_DATE' || |
226
|
|
|
$column->type === 'time' && $info['Default'] === 'SYS_TIME' |
227
|
|
|
) { |
228
|
|
|
$column->defaultValue = new Expression($info['Default']); |
229
|
|
|
} elseif (isset($type) && $type === 'bit') { |
230
|
|
|
$column->defaultValue = hexdec(trim($info['Default'], 'X\'')); |
231
|
|
|
} else { |
232
|
|
|
$column->defaultValue = $column->phpTypecast($info['Default']); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $column; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Returns all table names in the database. |
240
|
|
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
241
|
|
|
* @return array all table names in the database. The names have NO schema name prefix. |
242
|
|
|
*/ |
243
|
|
|
protected function findTableNames($schema = '') |
244
|
|
|
{ |
245
|
|
|
$pdo = $this->db->getSlavePdo(); |
246
|
|
|
$tables = $pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE); |
247
|
|
|
$tableNames = []; |
248
|
|
|
foreach ($tables as $table) { |
249
|
|
|
// do not list system tables |
250
|
|
|
if ($table['TYPE'] != 0) { |
251
|
|
|
$tableNames[] = $table['NAME']; |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $tableNames; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Determines the PDO type for the given PHP data value. |
260
|
|
|
* @param mixed $data the data whose PDO type is to be determined |
261
|
|
|
* @return integer the PDO type |
262
|
|
|
* @see http://www.php.net/manual/en/pdo.constants.php |
263
|
|
|
*/ |
264
|
|
|
public function getPdoType($data) |
265
|
|
|
{ |
266
|
|
|
static $typeMap = [ |
267
|
|
|
// php type => PDO type |
268
|
|
|
'boolean' => \PDO::PARAM_INT, // PARAM_BOOL is not supported by CUBRID PDO |
269
|
|
|
'integer' => \PDO::PARAM_INT, |
270
|
|
|
'string' => \PDO::PARAM_STR, |
271
|
|
|
'resource' => \PDO::PARAM_LOB, |
272
|
|
|
'NULL' => \PDO::PARAM_NULL, |
273
|
|
|
]; |
274
|
|
|
$type = gettype($data); |
275
|
|
|
|
276
|
|
|
return isset($typeMap[$type]) ? $typeMap[$type] : \PDO::PARAM_STR; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @inheritdoc |
281
|
|
|
* @see http://www.cubrid.org/manual/91/en/sql/transaction.html#database-concurrency |
282
|
|
|
*/ |
283
|
|
|
public function setTransactionIsolationLevel($level) |
284
|
|
|
{ |
285
|
|
|
// translate SQL92 levels to CUBRID levels: |
286
|
|
|
switch ($level) { |
287
|
|
|
case Transaction::SERIALIZABLE: |
288
|
|
|
$level = '6'; // SERIALIZABLE |
289
|
|
|
break; |
290
|
|
|
case Transaction::REPEATABLE_READ: |
291
|
|
|
$level = '5'; // REPEATABLE READ CLASS with REPEATABLE READ INSTANCES |
292
|
|
|
break; |
293
|
|
|
case Transaction::READ_COMMITTED: |
294
|
|
|
$level = '4'; // REPEATABLE READ CLASS with READ COMMITTED INSTANCES |
295
|
|
|
break; |
296
|
|
|
case Transaction::READ_UNCOMMITTED: |
297
|
|
|
$level = '3'; // REPEATABLE READ CLASS with READ UNCOMMITTED INSTANCES |
298
|
|
|
break; |
299
|
|
|
} |
300
|
|
|
parent::setTransactionIsolationLevel($level); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @inheritdoc |
305
|
|
|
*/ |
306
|
|
|
public function createColumnSchemaBuilder($type, $length = null) |
307
|
|
|
{ |
308
|
|
|
return new ColumnSchemaBuilder($type, $length, $this->db); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|