Exemplo n.º 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;
 }
Exemplo n.º 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);
     }
 }
Exemplo n.º 3
0
 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);
     }
 }
Exemplo n.º 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()));
     }
 }
Exemplo n.º 5
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());
     }
     $insert->query();
     $this->items = array();
     $this->count = 0;
     return $this;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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;
 }
Exemplo n.º 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();
     }
 }
Exemplo n.º 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);
 }
Exemplo n.º 13
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setSchemaName('Track');
 }
Exemplo n.º 14
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_UNIQUE, $columns->providerUserId, $columns->providerId);
 }
Exemplo n.º 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);
     }
 }
Exemplo n.º 16
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_UNIQUE, $columns->host, $columns->port);
 }
Exemplo n.º 17
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setPrimaryKey($columns->userId, $columns->identityId);
 }
Exemplo n.º 18
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setSchemaName('phperf_xhprof_symbol_stat');
     $table->setPrimaryKey($columns->symbolId, $columns->runId, $columns->isInclusive);
 }
Exemplo n.º 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);
 }
Exemplo n.º 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');
 }
Exemplo n.º 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';
 }
Exemplo n.º 22
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_KEY, $columns->ut);
 }
Exemplo n.º 23
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->setSchemaName('phperf_xhprof_related_stat');
     $table->setPrimaryKey($columns->parentSymbolId, $columns->childSymbolId, $columns->runId);
 }
Exemplo n.º 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');
 }
Exemplo n.º 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);
 }
Exemplo n.º 26
0
 static function setUpTable(\Yaoi\Database\Definition\Table $table, $columns)
 {
     $table->addIndex(Index::TYPE_UNIQUE, $columns->serverId, $columns->parameterId, $columns->ut);
 }
Exemplo n.º 27
0
 public function getTypeString()
 {
     return $this->table->database()->getUtility()->getColumnTypeString($this);
 }