123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- <?php
- namespace yii\db\cubrid;
- use yii\base\NotSupportedException;
- use yii\db\Constraint;
- use yii\db\ConstraintFinderInterface;
- use yii\db\ConstraintFinderTrait;
- use yii\db\Expression;
- use yii\db\ForeignKeyConstraint;
- use yii\db\IndexConstraint;
- use yii\db\TableSchema;
- use yii\db\Transaction;
- use yii\helpers\ArrayHelper;
- class Schema extends \yii\db\Schema implements ConstraintFinderInterface
- {
- use ConstraintFinderTrait;
-
- public $typeMap = [
-
- 'short' => self::TYPE_SMALLINT,
- 'smallint' => self::TYPE_SMALLINT,
- 'int' => self::TYPE_INTEGER,
- 'integer' => self::TYPE_INTEGER,
- 'bigint' => self::TYPE_BIGINT,
- 'numeric' => self::TYPE_DECIMAL,
- 'decimal' => self::TYPE_DECIMAL,
- 'float' => self::TYPE_FLOAT,
- 'real' => self::TYPE_FLOAT,
- 'double' => self::TYPE_DOUBLE,
- 'double precision' => self::TYPE_DOUBLE,
- 'monetary' => self::TYPE_MONEY,
-
- 'date' => self::TYPE_DATE,
- 'time' => self::TYPE_TIME,
- 'timestamp' => self::TYPE_TIMESTAMP,
- 'datetime' => self::TYPE_DATETIME,
-
- 'char' => self::TYPE_CHAR,
- 'varchar' => self::TYPE_STRING,
- 'char varying' => self::TYPE_STRING,
- 'nchar' => self::TYPE_CHAR,
- 'nchar varying' => self::TYPE_STRING,
- 'string' => self::TYPE_STRING,
-
- 'blob' => self::TYPE_BINARY,
- 'clob' => self::TYPE_BINARY,
-
- 'bit' => self::TYPE_INTEGER,
- 'bit varying' => self::TYPE_INTEGER,
-
- 'set' => self::TYPE_STRING,
- 'multiset' => self::TYPE_STRING,
- 'list' => self::TYPE_STRING,
- 'sequence' => self::TYPE_STRING,
- 'enum' => self::TYPE_STRING,
- ];
-
- public $exceptionMap = [
- 'Operation would have caused one or more unique constraint violations' => 'yii\db\IntegrityException',
- ];
-
- protected $tableQuoteCharacter = '"';
-
- protected function findTableNames($schema = '')
- {
- $pdo = $this->db->getSlavePdo();
- $tables = $pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE);
- $tableNames = [];
- foreach ($tables as $table) {
-
- if ($table['TYPE'] != 0) {
- $tableNames[] = $table['NAME'];
- }
- }
- return $tableNames;
- }
-
- protected function loadTableSchema($name)
- {
- $pdo = $this->db->getSlavePdo();
- $tableInfo = $pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name);
- if (!isset($tableInfo[0]['NAME'])) {
- return null;
- }
- $table = new TableSchema();
- $table->fullName = $table->name = $tableInfo[0]['NAME'];
- $sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name);
- $columns = $this->db->createCommand($sql)->queryAll();
- foreach ($columns as $info) {
- $column = $this->loadColumnSchema($info);
- $table->columns[$column->name] = $column;
- }
- $primaryKeys = $pdo->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $table->name);
- foreach ($primaryKeys as $key) {
- $column = $table->columns[$key['ATTR_NAME']];
- $column->isPrimaryKey = true;
- $table->primaryKey[] = $column->name;
- if ($column->autoIncrement) {
- $table->sequenceName = '';
- }
- }
- $foreignKeys = $pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name);
- foreach ($foreignKeys as $key) {
- if (isset($table->foreignKeys[$key['FK_NAME']])) {
- $table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME'];
- } else {
- $table->foreignKeys[$key['FK_NAME']] = [
- $key['PKTABLE_NAME'],
- $key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME'],
- ];
- }
- }
- return $table;
- }
-
- protected function loadTablePrimaryKey($tableName)
- {
- $primaryKey = $this->db->getSlavePdo()->cubrid_schema(\PDO::CUBRID_SCH_PRIMARY_KEY, $tableName);
- if (empty($primaryKey)) {
- return null;
- }
- ArrayHelper::multisort($primaryKey, 'KEY_SEQ', SORT_ASC, SORT_NUMERIC);
- return new Constraint([
- 'name' => $primaryKey[0]['KEY_NAME'],
- 'columnNames' => ArrayHelper::getColumn($primaryKey, 'ATTR_NAME'),
- ]);
- }
-
- protected function loadTableForeignKeys($tableName)
- {
- static $actionTypes = [
- 0 => 'CASCADE',
- 1 => 'RESTRICT',
- 2 => 'NO ACTION',
- 3 => 'SET NULL',
- ];
- $foreignKeys = $this->db->getSlavePdo()->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $tableName);
- $foreignKeys = ArrayHelper::index($foreignKeys, null, 'FK_NAME');
- ArrayHelper::multisort($foreignKeys, 'KEY_SEQ', SORT_ASC, SORT_NUMERIC);
- $result = [];
- foreach ($foreignKeys as $name => $foreignKey) {
- $result[] = new ForeignKeyConstraint([
- 'name' => $name,
- 'columnNames' => ArrayHelper::getColumn($foreignKey, 'FKCOLUMN_NAME'),
- 'foreignTableName' => $foreignKey[0]['PKTABLE_NAME'],
- 'foreignColumnNames' => ArrayHelper::getColumn($foreignKey, 'PKCOLUMN_NAME'),
- 'onDelete' => isset($actionTypes[$foreignKey[0]['DELETE_RULE']]) ? $actionTypes[$foreignKey[0]['DELETE_RULE']] : null,
- 'onUpdate' => isset($actionTypes[$foreignKey[0]['UPDATE_RULE']]) ? $actionTypes[$foreignKey[0]['UPDATE_RULE']] : null,
- ]);
- }
- return $result;
- }
-
- protected function loadTableIndexes($tableName)
- {
- return $this->loadTableConstraints($tableName, 'indexes');
- }
-
- protected function loadTableUniques($tableName)
- {
- return $this->loadTableConstraints($tableName, 'uniques');
- }
-
- protected function loadTableChecks($tableName)
- {
- throw new NotSupportedException('CUBRID does not support check constraints.');
- }
-
- protected function loadTableDefaultValues($tableName)
- {
- throw new NotSupportedException('CUBRID does not support default value constraints.');
- }
-
- public function releaseSavepoint($name)
- {
-
- }
-
- public function createQueryBuilder()
- {
- return new QueryBuilder($this->db);
- }
-
- protected function loadColumnSchema($info)
- {
- $column = $this->createColumnSchema();
- $column->name = $info['Field'];
- $column->allowNull = $info['Null'] === 'YES';
- $column->isPrimaryKey = false;
- $column->autoIncrement = stripos($info['Extra'], 'auto_increment') !== false;
- $column->dbType = $info['Type'];
- $column->unsigned = strpos($column->dbType, 'unsigned') !== false;
- $column->type = self::TYPE_STRING;
- if (preg_match('/^([\w ]+)(?:\(([^\)]+)\))?$/', $column->dbType, $matches)) {
- $type = strtolower($matches[1]);
- $column->dbType = $type . (isset($matches[2]) ? "({$matches[2]})" : '');
- if (isset($this->typeMap[$type])) {
- $column->type = $this->typeMap[$type];
- }
- if (!empty($matches[2])) {
- if ($type === 'enum') {
- $values = preg_split('/\s*,\s*/', $matches[2]);
- foreach ($values as $i => $value) {
- $values[$i] = trim($value, "'");
- }
- $column->enumValues = $values;
- } else {
- $values = explode(',', $matches[2]);
- $column->size = $column->precision = (int) $values[0];
- if (isset($values[1])) {
- $column->scale = (int) $values[1];
- }
- if ($column->size === 1 && $type === 'bit') {
- $column->type = 'boolean';
- } elseif ($type === 'bit') {
- if ($column->size > 32) {
- $column->type = 'bigint';
- } elseif ($column->size === 32) {
- $column->type = 'integer';
- }
- }
- }
- }
- }
- $column->phpType = $this->getColumnPhpType($column);
- if ($column->isPrimaryKey) {
- return $column;
- }
- if ($column->type === 'timestamp' && $info['Default'] === 'SYS_TIMESTAMP' ||
- $column->type === 'datetime' && $info['Default'] === 'SYS_DATETIME' ||
- $column->type === 'date' && $info['Default'] === 'SYS_DATE' ||
- $column->type === 'time' && $info['Default'] === 'SYS_TIME'
- ) {
- $column->defaultValue = new Expression($info['Default']);
- } elseif (isset($type) && $type === 'bit') {
- $column->defaultValue = hexdec(trim($info['Default'], 'X\''));
- } else {
- $column->defaultValue = $column->phpTypecast($info['Default']);
- }
- return $column;
- }
-
- public function getPdoType($data)
- {
- static $typeMap = [
-
- '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;
- }
-
- public function setTransactionIsolationLevel($level)
- {
-
- switch ($level) {
- case Transaction::SERIALIZABLE:
- $level = '6';
- break;
- case Transaction::REPEATABLE_READ:
- $level = '5';
- break;
- case Transaction::READ_COMMITTED:
- $level = '4';
- break;
- case Transaction::READ_UNCOMMITTED:
- $level = '3';
- break;
- }
- parent::setTransactionIsolationLevel($level);
- }
-
- public function createColumnSchemaBuilder($type, $length = null)
- {
- return new ColumnSchemaBuilder($type, $length, $this->db);
- }
-
- private function loadTableConstraints($tableName, $returnType)
- {
- $constraints = $this->db->getSlavePdo()->cubrid_schema(\PDO::CUBRID_SCH_CONSTRAINT, $tableName);
- $constraints = ArrayHelper::index($constraints, null, ['TYPE', 'NAME']);
- ArrayHelper::multisort($constraints, 'KEY_ORDER', SORT_ASC, SORT_NUMERIC);
- $result = [
- 'indexes' => [],
- 'uniques' => [],
- ];
- foreach ($constraints as $type => $names) {
- foreach ($names as $name => $constraint) {
- $isUnique = in_array((int) $type, [0, 2], true);
- $result['indexes'][] = new IndexConstraint([
- 'isPrimary' => (bool) $constraint[0]['PRIMARY_KEY'],
- 'isUnique' => $isUnique,
- 'name' => $name,
- 'columnNames' => ArrayHelper::getColumn($constraint, 'ATTR_NAME'),
- ]);
- if ($isUnique) {
- $result['uniques'][] = new Constraint([
- 'name' => $name,
- 'columnNames' => ArrayHelper::getColumn($constraint, 'ATTR_NAME'),
- ]);
- }
- }
- }
- foreach ($result as $type => $data) {
- $this->setTableMetadata($tableName, $type, $data);
- }
- return $result[$returnType];
- }
- }
|