Example #1
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;
 }
Example #2
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;
 }
Example #3
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;
 }
Example #4
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;
 }
Example #5
0
 public function __construct(Table $before, Table $after)
 {
     $this->before = $before;
     $this->after = $after;
     $this->bindDatabase($before->database());
     $this->alterExpression = new SimpleExpression('ALTER TABLE ?' . PHP_EOL, new Symbol($this->after->schemaName));
     $this->alterLines = new SimpleExpression();
     $this->alterLines->setOpComma(',' . PHP_EOL);
     $this->add($this->alterExpression);
     $this->alterExpression->appendExpr($this->alterLines);
     $this->processColumns();
     $this->processIndexes();
     $this->addFkExpression = new SimpleExpression();
     $this->processForeignKeys();
     $this->alterExpression->appendExpr($this->addFkExpression);
     if ($this->alterLines->isEmpty()) {
         $this->alterExpression->disable();
     }
 }
Example #6
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();
     }
 }
Example #7
0
 public function getTypeString()
 {
     return $this->table->database()->getUtility()->getColumnTypeString($this);
 }