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); }
/** * @return void */ private function makeConstraints(IDialect $dialect) { foreach ($this->table->getConstraints() as $constraint) { $queryParts = array(StringUtils::DELIM_STANDART, "\t"); $queryParts[] = $constraint->toDialectString($dialect); $this->commaSeparatedQueryParts[] = join('', $queryParts); } }
/** * @param array $fields fields of referencing table that reference primary another table * @param DBTable $referencedTable object that represents referenced table * @param AssociationBreakAction $associationBreakAction action which is performed on reference break */ function __construct(array $fields, DBTable $referencedTable, AssociationBreakAction $associationBreakAction) { foreach ($referencedTable->getConstraints() as $constraint) { if ($constraint instanceof DBPrimaryKeyConstraint) { $pkFields = $constraint->getFields(); Assert::isTrue(sizeof($pkFields) == sizeof($fields), 'foreign key (%s) should have the same number of columns as %s`s table primary key (%s)', join(', ', $fields), $referencedTable->getName(), join(', ', $pkFields)); $this->fields = new SqlFieldArray($fields); $this->pkFields = new SqlFieldArray($pkFields); $this->referencedTable = $referencedTable; $this->associationBreakAction = $associationBreakAction; return; } } Assert::isUnreachable('referenced table `%s` MUST contain DBPrimaryKeyConstraint', $referencedTable->getName()); }
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; }