Ejemplo n.º 1
0
 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);
 }
 /**
  * @throws WrongArgumentException
  * @return DBSchema
  **/
 public function addTable(DBTable $table)
 {
     $name = $table->getName();
     Assert::isFalse(isset($this->tables[$name]), "table '{$name}' already exist");
     $this->tables[$table->getName()] = $table;
     $this->order[] = $name;
     return $this;
 }
Ejemplo n.º 3
0
 function toDialectString(IDialect $dialect)
 {
     $queryParts = array();
     $this->commaSeparatedQueryParts = array();
     $queryParts[] = 'CREATE TABLE ';
     $queryParts[] = $dialect->quoteIdentifier($this->table->getName());
     $queryParts[] = '(';
     $this->makeColumns($dialect);
     $queryParts[] = join(',', $this->commaSeparatedQueryParts);
     $queryParts[] = StringUtils::DELIM_STANDART;
     $queryParts[] = ');';
     return 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());
 }
Ejemplo n.º 5
0
 private function importConstraints(OrmProperty $property)
 {
     $name = $this->dbTable->getName() . '_' . $property->getName();
     $fields = $property->getFields();
     if ($property->isIdentifier()) {
         $this->dbTable->addConstraint(new DBPrimaryKeyConstraint($name . '_pk', $this->dbTable, $fields));
     } else {
         if ($property->isUnique()) {
             $this->dbTable->addConstraint(new DBUniqueConstraint($name . '_uq', $this->dbTable, $fields));
         }
     }
     $type = $property->getType();
     if ($type instanceof AssociationPropertyType) {
         $this->dbTable->addConstraint(new DBOneToOneConstraint($name . '_fk', $this->dbTable, $fields, $this->dbSchema->getTable($property->getType()->getContainer()->getTable()), $property->getType()->getAssociationBreakAction()));
     } else {
         if ($type instanceof CompositePropertyType) {
             foreach ($type->getProperties($property) as $_property) {
                 $this->importConstraints($_property);
             }
         }
     }
     if ($property->isQueryable()) {
         $this->dbTable->addIndex(new DBIndex($name . '_idx', $this->dbTable, $fields));
     }
 }
Ejemplo n.º 6
0
 /**
  * Adds the DBTable object to the schema
  *
  * @param DBTable $table table to add
  * @throws DuplicationException thrown when another DBTable with the same name already added
  * @return DBSchema itself
  */
 function addTable(DBTable $table)
 {
     $name = $table->getName();
     if (isset($this->tables[$name])) {
         throw new DuplicationException('table', $name);
     }
     $this->tables[$name] = $table;
     return $this;
 }
Ejemplo n.º 7
0
 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;
 }
Ejemplo n.º 8
0
 function toDialectString(IDialect $dialect)
 {
     return 'DROP TABLE ' . $dialect->quoteIdentifier($this->table->getName()) . ' CASCADE;';
 }
Ejemplo n.º 9
0
 function toDialectString(IDialect $dialect)
 {
     return 'INDEX ' . $dialect->quoteIdentifier($this->name) . ' ON ' . $dialect->quoteIdentifier($this->table->getName()) . ' (' . $this->getFieldsAsString($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;
 }
 function toDialectString(IDialect $dialect)
 {
     return 'ALTER TABLE ' . $dialect->quoteIdentifier($this->table->getName()) . ' ADD ' . $this->constraint->toDialectString($dialect) . ';';
 }