function getTableQuerySet(DBTable $table, $includeCreateTable = true) { $preQueries = array(); $postQueries = array(); foreach ($table->getColumns() as $column) { $type = $column->getType(); if ($type instanceof DBType && $type->isGenerated()) { $sqName = $this->getSequenceName($table->getName(), $column->getName()); $preQueries[] = new RawSqlQuery('CREATE SEQUENCE %s;', array(new SqlIdentifier($sqName))); $postQueries[] = new RawSqlQuery('ALTER SEQUENCE %s OWNED BY %s;', array(new SqlIdentifier($sqName), new SqlPath($table->getName(), $column->getName()))); $postQueries[] = new RawSqlQuery('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;', array(new SqlIdentifier($table->getName()), new SqlIdentifier($column->getName()), new SqlFunction('nextval', new SqlValue($sqName)))); } } foreach ($table->getConstraints() as $constraint) { $postQueries[] = new CreateConstraintQuery($table, $constraint); $columns = array(); // create indexes foreach ($constraint->getIndexableFields() as $field) { $columns[] = $this->quoteIdentifier($field); } if (!empty($columns)) { $postQueries[] = new RawSqlQuery('CREATE INDEX %s ON %s (' . join($columns) . ');', array(new SqlIdentifier($constraint->getName() . '_idx'), new SqlIdentifier($table->getName()))); } } if ($includeCreateTable) { $preQueries[] = new CreateTableQuery($table); } return array_merge($preQueries, $postQueries); }
function getExtraTableQueries(DBTable $table) { $queries = array(); foreach ($table->getColumns() as $column) { $type = $column->getType(); if ($type instanceof DBType && $type->isGenerated()) { $sqName = $this->getSequenceName($table->getName(), $column->getName()); $queries[] = new RawSqlQuery('CREATE SEQUENCE %s;', array(new SqlIdentifier($sqName))); $queries[] = new RawSqlQuery('ALTER SEQUENCE %s OWNED BY %s;', array(new SqlIdentifier($sqName), new SqlPath($table->getName(), $column->getName()))); $queries[] = new RawSqlQuery('ALTER TABLE %s ALTER COLUMN %s SET DEFAULT %s;', array(new SqlIdentifier($table->getName()), new SqlIdentifier($column->getName()), new SqlFunction('nextval', new SqlValue($sqName)))); } } foreach ($table->getConstraints() as $constraint) { if ($constraint instanceof DBOneToOneConstraint) { // create an explicit index for that $queries[] = new CreateIndexQuery(new DBIndex($constraint->getName() . '_idx', $constraint->getTable(), $constraint->getFields())); } } return $queries; }
/** * @return void */ private function makeColumns(IDialect $dialect) { foreach ($this->table->getColumns() as $column) { $this->makeColumn($column, $dialect); } }
public static function findDifferences(Dialect $dialect, DBTable $source, DBTable $target) { $out = array(); $head = 'ALTER TABLE ' . $dialect->quoteTable($target->getName()); $sourceColumns = $source->getColumns(); $targetColumns = $target->getColumns(); foreach ($sourceColumns as $name => $column) { if (isset($targetColumns[$name])) { if ($column->getType()->getId() != $targetColumns[$name]->getType()->getId()) { $targetColumn = $targetColumns[$name]; $out[] = $head . ' ALTER COLUMN ' . $dialect->quoteField($name) . ' TYPE ' . $targetColumn->getType()->toString() . ($targetColumn->getType()->hasSize() ? '(' . $targetColumn->getType()->getSize() . ($targetColumn->getType()->hasPrecision() ? ', ' . $targetColumn->getType()->getPrecision() : null) . ')' : null) . ';'; } if ($column->getType()->isNull() != $targetColumns[$name]->getType()->isNull()) { $out[] = $head . ' ALTER COLUMN ' . $dialect->quoteField($name) . ' ' . ($targetColumns[$name]->getType()->isNull() ? 'DROP' : 'SET') . ' NOT NULL;'; } } else { $out[] = $head . ' DROP COLUMN ' . $dialect->quoteField($name) . ';'; } } foreach ($targetColumns as $name => $column) { if (!isset($sourceColumns[$name])) { $out[] = $head . ' ADD COLUMN ' . $column->toDialectString($dialect) . ';'; if ($column->hasReference()) { $out[] = 'CREATE INDEX ' . $dialect->quoteField($name . '_idx') . ' ON ' . $dialect->quoteTable($target->getName()) . '(' . $dialect->quoteField($name) . ');'; } } } return $out; }