Exemple #1
0
 public static function entityDefinition()
 {
     /** @var static $columns */
     $columns = new \stdClass();
     $columns->diafilmId = Diafilm::entityDefinition()->id;
     $columns->imageId = Image::entityDefinition()->id;
     $columns->position = new Column(Column::INTEGER);
     $def = new Table($columns);
     $def->setPrimaryKey($columns->diafilmId, $columns->imageId);
     return $def;
 }
Exemple #2
0
 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);
     }
 }
 private function buildForeignKeys()
 {
     foreach ($this->foreignKeys as $data) {
         $name = $data[0];
         $localColumnNames = $data[1];
         $localColumns = array();
         foreach ($localColumnNames as &$columnName) {
             $localColumns[] = $this->columns->{$columnName};
         }
         $referenceTableName = $data[2];
         $referenceColumnNames = $data[3];
         $referenceColumns = array();
         foreach ($referenceColumnNames as &$columnName) {
             $column = new Column();
             $column->schemaName = $columnName;
             $column->table = new Table(null, null, $referenceTableName);
             $referenceColumns[] = $column;
         }
         $foreignKey = new Database\Definition\ForeignKey($localColumns, $referenceColumns);
         // ON UPDATE
         if ($data[4]) {
             $foreignKey->onUpdate = $data[4];
         }
         // ON DELETE
         if ($data[5]) {
             $foreignKey->onDelete = $data[5];
         }
         $foreignKey->setName($name);
         $this->table->addForeignKey($foreignKey);
     }
 }
Exemple #4
0
 protected function processForeignKeys()
 {
     /** @var ForeignKey[] $beforeForeignKeys */
     $beforeForeignKeys = array();
     if (!$this->before->disableForeignKeys) {
         foreach ($this->before->getForeignKeys() as $foreignKey) {
             $beforeForeignKeys[$foreignKey->getName()] = $foreignKey;
         }
     }
     $afterForeignKeys = $this->after->getForeignKeys();
     if ($this->after->disableForeignKeys) {
         $afterForeignKeys = array();
     }
     foreach ($afterForeignKeys as $foreignKey) {
         if (!isset($beforeForeignKeys[$foreignKey->getName()])) {
             $this->addFkExpression->commaExpr('ADD');
             $this->addFkExpression->appendExpr($this->database()->getUtility()->generateForeignKeyExpression($foreignKey));
         } else {
             unset($beforeForeignKeys[$foreignKey->getName()]);
         }
     }
     foreach ($beforeForeignKeys as $foreignKey) {
         $this->alterLines->commaExpr('DROP FOREIGN KEY ?', new Symbol($foreignKey->getName()));
     }
 }
 public function flush()
 {
     //echo "FLUSHING", PHP_EOL;
     if (!$this->count) {
         return $this;
     }
     $database = $this->table->database();
     $insert = $database->insert($this->table->schemaName);
     foreach ($this->items as $item) {
         $insert->valuesRow($item->toArray());
     }
     $insert->query();
     $this->items = array();
     $this->count = 0;
     return $this;
 }
Exemple #6
0
 /**
  * @return Table|static
  */
 public static function entityDefinition()
 {
     /** @var static $columns */
     $columns = new \stdClass();
     $columns->id = new Column(Column::AUTO_ID);
     $columns->title = new Column();
     $columns->status = new Column(Column::INTEGER);
     $columns->updated = new Column(Column::TIMESTAMP);
     $columns->coverImageId = Image::entityDefinition()->id;
     // TODO auto relation here
     $columns->source = new Column();
     $columns->sourceHref = new Column();
     $columns->frames = new Column(Column::INTEGER && Column::SIZE_1B);
     $columns->meta = new Column();
     $def = new Table($columns);
     $def->addIndex(Index::TYPE_UNIQUE, $columns->source, $columns->sourceHref);
     return $def;
 }
Exemple #7
0
 /**
  * @return bool
  * @throws Exception
  */
 public function rollback()
 {
     if (self::$enableStateCache && isset(self::$rolledBack[$this->table->entityClassName])) {
         if ($this->log) {
             $this->log->push(Expression::create('# Migration for table ? (?) already rolled back, skipping', $this->table->schemaName, $this->table->entityClassName));
         }
         return true;
     }
     $utility = $this->table->database()->getUtility();
     $tableExists = $utility->tableExists($this->table->schemaName);
     $requires = $tableExists;
     if ($this->log) {
         $this->log->push(Expression::create('# Rollback, table ? (?) ?', $this->table->schemaName, $this->table->entityClassName, $requires ? 'requires deletion' : 'is already non-existent'));
     }
     if (!$requires) {
         self::setRolledBack($this->table->entityClassName);
         return false;
     }
     /** @var Migration[] $dependentMigrations */
     $dependentMigrations = array();
     foreach ($this->table->dependentTables as $dependentTable) {
         $referenceMigration = $dependentTable->migration();
         $referenceMigration->dryRun = $this->dryRun;
         $referenceMigration->log = $this->log;
         $dependentMigrations[$referenceMigration->table->schemaName] = $referenceMigration;
     }
     if (!$this->dryRun) {
         try {
             if ($dependentMigrations) {
                 $dropFk = $utility->generateDropForeignKeys($this->table->schemaName);
                 $this->runStatement($dropFk);
                 self::setRolledBack($this->table->entityClassName);
                 if ($this->log) {
                     $this->log->push('# Dependent tables found: ' . implode(', ', array_keys($dependentMigrations)));
                 }
                 foreach ($dependentMigrations as $migration) {
                     $migration->rollback();
                 }
                 $this->runStatement($utility->generateDropTable($this->table->schemaName));
             } else {
                 $this->runStatement($utility->generateDropTable($this->table->schemaName));
                 self::setRolledBack($this->table->entityClassName);
             }
             if ($this->log) {
                 $this->log->push('# OK', Log::TYPE_SUCCESS);
             }
         } catch (Exception $exception) {
             if ($this->log) {
                 $this->log->push($exception->getMessage(), Log::TYPE_ERROR);
             }
             throw $exception;
         }
     }
     return true;
 }
Exemple #8
0
 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;
 }
Exemple #9
0
 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;
 }
Exemple #10
0
 public function flush()
 {
     //echo "FLUSHING", PHP_EOL;
     if (!$this->count) {
         return $this;
     }
     $database = $this->table->database();
     $insert = $database->insert($this->table->schemaName);
     foreach ($this->items as $item) {
         $insert->valuesRow($item->toArray(true));
     }
     try {
         $insert->query()->execute();
     } catch (Exception $exception) {
         echo PHP_EOL, $exception->query, PHP_EOL;
         throw $exception;
     }
     $this->items = array();
     $this->count = 0;
     return $this;
 }
Exemple #11
0
 public function __construct(Table $table)
 {
     $this->table = $table;
     $this->bindDatabase($table->database());
     $this->createLines = new SimpleExpression();
     $this->createLines->setOpComma(',' . PHP_EOL);
     $this->fkLines = new SimpleExpression();
     $this->fkLines->setOpComma(',' . PHP_EOL);
     $createExpression = new SimpleExpression('CREATE TABLE ? (' . PHP_EOL, $this->table);
     $this->add($createExpression);
     $createExpression->appendExpr($this->createLines);
     $createExpression->appendExpr(PHP_EOL . ')');
     $this->appendColumns();
     $this->appendIndexes();
     $this->createLines->commaExpr($this->fkLines);
     $this->appendForeignKeys();
     $this->appendPrimaryKey();
     if ($this->createLines->isEmpty()) {
         $createExpression->disable();
     }
 }
Exemple #12
0
 /**
  * Optional setup table indexes and other properties, can be left empty
  * @param Table $table
  * @param static|\stdClass $columns
  * @return void
  */
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setPrimaryKey($columns->seriesId, $columns->tagId);
 }
Exemple #13
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setSchemaName('Track');
 }
Exemple #14
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_UNIQUE, $columns->providerUserId, $columns->providerId);
 }
Exemple #15
0
 private function readForeignKeys(Table $def)
 {
     $res = $this->database->select()->select('tc.constraint_name, kcu.column_name')->select('ccu.table_name  AS foreign_table_name, ccu.column_name AS foreign_column_name')->from('information_schema.table_constraints AS tc')->innerJoin('information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name')->innerJoin('information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name')->where('constraint_type = \'FOREIGN KEY\'')->where('tc.table_name = ?', $def->schemaName)->query()->fetchAll();
     $fk = array();
     foreach ($res as $r) {
         $fk[$r['constraint_name']][$r['column_name']] = array($r['foreign_table_name'], $r['foreign_column_name']);
     }
     foreach ($fk as $constraintName => $constraintColumns) {
         $localColumns = array();
         $referenceColumns = array();
         foreach ($constraintColumns as $localName => $refData) {
             $localColumns[] = $def->columns->{$localName};
             $column = new Column();
             $column->table = new Table(null, null, $refData[0]);
             $column->schemaName = $refData[1];
             $referenceColumns[] = $column;
         }
         $foreignKey = new Database\Definition\ForeignKey($localColumns, $referenceColumns);
         $foreignKey->setName($constraintName);
         $def->addForeignKey($foreignKey);
     }
 }
Exemple #16
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_UNIQUE, $columns->host, $columns->port);
 }
Exemple #17
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setPrimaryKey($columns->userId, $columns->identityId);
 }
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setSchemaName('phperf_xhprof_symbol_stat');
     $table->setPrimaryKey($columns->symbolId, $columns->runId, $columns->isInclusive);
 }
Exemple #19
0
 /**
  * @param \Yaoi\Database\Definition\Table $table
  * @param \Yaoi\Database\Definition\Columns|static $columns
  */
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_UNIQUE, $columns->userId, $columns->createdAt);
 }
Exemple #20
0
 /**
  * Optional setup table indexes and other properties, can be left empty
  * @param Table $table
  * @param static|\stdClass $columns
  * @return void
  */
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setSchemaName('phperf_xhprof_tag');
 }
Exemple #21
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setSchemaName('TrackElement');
     Column::cast($columns->id)->schemaName = '_id';
     Column::cast($columns->trackId)->schemaName = 'trackId';
 }
Exemple #22
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_KEY, $columns->ut);
 }
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setSchemaName('phperf_xhprof_related_stat');
     $table->setPrimaryKey($columns->parentSymbolId, $columns->childSymbolId, $columns->runId);
 }
Exemple #24
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $columns->wakaUserId->flags -= Column::NOT_NULL;
     $table->addIndex(Index::TYPE_UNIQUE, $columns->dateUt, $columns->name, $columns->type);
     $table->setSchemaName('day_stat');
 }
Exemple #25
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $columns->wakaUserId->flags -= Column::NOT_NULL;
     $table->setSchemaName('duration')->addIndex(Index::TYPE_UNIQUE, $columns->time, $columns->entity);
 }
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_UNIQUE, $columns->serverId, $columns->parameterId, $columns->ut);
 }
Exemple #27
0
 public function getTypeString()
 {
     return $this->table->database()->getUtility()->getColumnTypeString($this);
 }