コード例 #1
0
ファイル: SchemaReader.php プロジェクト: php-yaoi/php-yaoi
 private function readIndexes(Table $def)
 {
     $res = $this->database->select()->select('t.relname as table_name, i.relname as index_name, a.attname as column_name')->select('ix.indisunique::int as is_unique,ix.indisprimary::int as is_primary')->from('pg_class t, pg_class i, pg_index ix, pg_attribute a')->where('t.oid = ix.indrelid')->where('i.oid = ix.indexrelid')->where('a.attrelid = t.oid')->where('a.attnum = ANY(ix.indkey)')->where('t.relkind = \'r\'')->where('t.relname = ?', $def->schemaName)->order('t.relname, i.relname, ix.indnatts')->query()->fetchAll();
     $indexData = array();
     foreach ($res as $row) {
         if ($row['is_primary']) {
             $row['index_name'] = 'PRIMARY';
         }
         $indexData[$row['index_name']]['columns'][$row['column_name']] = $row['column_name'];
         $indexData[$row['index_name']]['is_unique'] = $row['is_unique'];
         $indexData[$row['index_name']]['is_primary'] = $row['is_unique'];
     }
     $columns = $def->getColumns();
     if (isset($indexData['PRIMARY'])) {
         $primaryKey = array();
         foreach ($indexData['PRIMARY']['columns'] as $columnName) {
             $primaryKey[] = $columns->{$columnName};
         }
         $def->setPrimaryKey($primaryKey);
         unset($indexData['PRIMARY']);
     }
     foreach ($indexData as $indexName => $indexInfo) {
         $indexColumns = array();
         foreach ($indexInfo['columns'] as $columnName) {
             $indexColumns[] = $columns->{$columnName};
         }
         $index = new Index($indexColumns);
         $index->setType($indexInfo['is_unique'] ? Index::TYPE_UNIQUE : Index::TYPE_KEY);
         $index->setName($indexName);
         $def->addIndex($index);
     }
 }
コード例 #2
0
ファイル: SchemaReader.php プロジェクト: php-yaoi/php-yaoi
 private function readIndexes(Table $table)
 {
     $res = $this->database->query("PRAGMA INDEX_LIST (?)", $table->schemaName)->fetchAll();
     $res = array_reverse($res);
     foreach ($res as $indexRow) {
         $cols = $this->database->query("PRAGMA INDEX_INFO (?)", $indexRow['name']);
         $columns = array();
         foreach ($cols as $colRow) {
             $columns[] = $table->getColumn($colRow['name']);
         }
         $index = new Index($columns);
         $index->setType($indexRow['unique'] ? Index::TYPE_UNIQUE : Index::TYPE_KEY);
         $table->addIndex($index);
     }
 }
コード例 #3
0
ファイル: SchemaReader.php プロジェクト: php-yaoi/php-yaoi
 public function getTableDefinition($tableName)
 {
     $tableSymbol = new Symbol($tableName);
     $res = $this->database->query("DESC ?", $tableSymbol);
     $columns = new \stdClass();
     while ($row = $res->fetchRow()) {
         $type = $row['Type'];
         $field = $row['Field'];
         $phpType = $this->getTypeByString($type);
         if ('auto_increment' === $row['Extra']) {
             $phpType += Column::AUTO_ID;
         }
         $column = new Column($phpType);
         $columns->{$field} = $column;
         $column->schemaName = $field;
         $notNull = $row['Null'] === 'NO';
         if ($row['Default'] !== null || !$notNull) {
             $column->setDefault($row['Default']);
         }
         $column->setFlag(Column::NOT_NULL, $notNull);
     }
     $definition = new Table($columns, $this->database, $tableName);
     $res = $this->database->query("SHOW INDEX FROM ?", $tableSymbol);
     $indexes = array();
     $uniqueIndex = array();
     foreach ($res as $row) {
         $indexes[$row['Key_name']][$row['Seq_in_index']] = $columns->{$row['Column_name']};
         $uniqueIndex[$row['Key_name']] = !$row['Non_unique'];
     }
     foreach ($indexes as $indexName => $indexData) {
         ksort($indexData);
         $index = new Index(array_values($indexData));
         $index->setName($indexName);
         $index->setType($uniqueIndex[$indexName] ? Index::TYPE_UNIQUE : Index::TYPE_KEY);
         if ($indexName === self::_PRIMARY) {
             $definition->setPrimaryKey($index->columns);
         } else {
             $definition->addIndex($index);
         }
     }
     return $definition;
 }
コード例 #4
0
ファイル: Columns.php プロジェクト: php-yaoi/php-yaoi
 public function __set($name, $column)
 {
     if (is_int($column)) {
         $column = new Column($column);
         //$this->_arrayOfColumnData[$name] = $column;
     }
     // another column reference
     if (!empty($column->table) && $column->table->schemaName != $this->table->schemaName) {
         $refColumn = $column;
         $column = clone $column;
         $column->propertyName = $name;
         $column->schemaName = Utils::fromCamelCase($name);
         $column->table = $this->table;
         //$this->_arrayOfColumnData[$name] = $column;
         $foreignKey = new ForeignKey(array($column), array($refColumn));
         $column->foreignKey = $foreignKey;
         //$this->table->addForeignKey($foreignKey);
         $column->setFlag(Column::AUTO_ID, false);
     } else {
         $column->propertyName = $name;
         $column->schemaName = Utils::fromCamelCase($name);
         $column->table = $this->table;
     }
     if ($column->flags & Column::AUTO_ID) {
         $this->table->autoIdColumn = $column;
         if (!$this->table->primaryKey) {
             $this->table->setPrimaryKey($column);
         }
     }
     if ($column->isUnique) {
         $index = new Index($column);
         $index->setType(Index::TYPE_UNIQUE);
         $this->table->addIndex($index);
     } elseif ($column->isIndexed) {
         $index = new Index($column);
         $index->setType(Index::TYPE_KEY);
         $this->table->addIndex($index);
     }
     $this->table->database()->getUtility()->checkColumn($column);
     $this->_arrayOfColumnData[$name] = $column;
 }
コード例 #5
0
 private function buildIndexes()
 {
     foreach ($this->indexes as $indexData) {
         $type = $indexData[0];
         $name = $indexData[1];
         $columns = array();
         foreach ($indexData[2] as $columnName) {
             $columns[] = $this->columns->{$columnName};
         }
         $index = new Index($columns);
         $index->setName($name);
         $index->setType($type);
         $this->table->addIndex($index);
     }
 }
コード例 #6
0
ファイル: Table.php プロジェクト: php-yaoi/php-yaoi
 public function dropIndex($index)
 {
     if (!$index instanceof Index) {
         $args = func_get_args();
         $type = array_shift($args);
         $columns = $args;
         $index = Index::create($columns)->setType($type);
     }
     if ($index->type === Index::TYPE_PRIMARY) {
         throw new Exception('Can not drop primary key', Exception::NOT_IMPLEMENTED);
     }
     if (isset($this->indexes[$index->getName()])) {
         unset($this->indexes[$index->getName()]);
     }
     return $this;
 }