Exemple #1
0
 /**
  * ->processQueueCallback(function (\Dja\Db\Model\Metadata $metadata, \Doctrine\DBAL\Schema\Table $table, array $sql, \Doctrine\DBAL\Connection $db) {})
  * @param callable|\Closure $callBack
  */
 public function processQueueCallback(\Closure $callBack)
 {
     $callbackQueue = [];
     while (count($this->generateQueue)) {
         $modelName = array_shift($this->generateQueue);
         try {
             /** @var Metadata $metadata */
             $metadata = $modelName::metadata();
             $tblName = $metadata->getDbTableName();
             if ($this->db->getSchemaManager()->tablesExist($tblName)) {
                 continue;
             }
             if (isset($this->generated[$tblName])) {
                 continue;
             }
             $table = $this->metadataToTable($metadata);
             $this->generated[$tblName] = 1;
             $sql = $this->dp->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES);
             array_unshift($callbackQueue, [$metadata, $table, $sql]);
             $fks = $table->getForeignKeys();
             if (count($fks)) {
                 $sql = [];
                 foreach ($fks as $fk) {
                     $sql[] = $this->dp->getCreateForeignKeySQL($fk, $table);
                 }
                 array_push($callbackQueue, [$metadata, $table, $sql]);
             }
         } catch (\Exception $e) {
             pr($e->__toString());
         }
     }
     foreach ($callbackQueue as $args) {
         $callBack($args[0], $args[1], $args[2], $this->db);
     }
 }
 /**
  * @param Table[] $tables
  */
 public function dumpTableStructure(array $tables)
 {
     $this->logger->info('Dumping table structure');
     foreach ($tables as $table) {
         $structure = $this->platform->getCreateTableSQL($table);
         $this->dumpOutput->writeln(implode(";\n", $structure) . ';');
     }
 }
 /**
  * @group DBAL-374
  */
 public function testQuotedColumnInForeignKeyPropagation()
 {
     $table = new Table('`quoted`');
     $table->addColumn('create', 'string');
     $table->addColumn('foo', 'string');
     $table->addColumn('`bar`', 'string');
     // Foreign table with reserved keyword as name (needs quotation).
     $foreignTable = new Table('foreign');
     $foreignTable->addColumn('create', 'string');
     // Foreign column with reserved keyword as name (needs quotation).
     $foreignTable->addColumn('bar', 'string');
     // Foreign column with non-reserved keyword as name (does not need quotation).
     $foreignTable->addColumn('`foo-bar`', 'string');
     // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
     $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_RESERVED_KEYWORD');
     // Foreign table with non-reserved keyword as name (does not need quotation).
     $foreignTable = new Table('foo');
     $foreignTable->addColumn('create', 'string');
     // Foreign column with reserved keyword as name (needs quotation).
     $foreignTable->addColumn('bar', 'string');
     // Foreign column with non-reserved keyword as name (does not need quotation).
     $foreignTable->addColumn('`foo-bar`', 'string');
     // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
     $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_NON_RESERVED_KEYWORD');
     // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
     $foreignTable = new Table('`foo-bar`');
     $foreignTable->addColumn('create', 'string');
     // Foreign column with reserved keyword as name (needs quotation).
     $foreignTable->addColumn('bar', 'string');
     // Foreign column with non-reserved keyword as name (does not need quotation).
     $foreignTable->addColumn('`foo-bar`', 'string');
     // Foreign table with special character in name (needs quotation on some platforms, e.g. Sqlite).
     $table->addForeignKeyConstraint($foreignTable, array('create', 'foo', '`bar`'), array('create', 'bar', '`foo-bar`'), array(), 'FK_WITH_INTENDED_QUOTATION');
     $sql = $this->_platform->getCreateTableSQL($table, AbstractPlatform::CREATE_FOREIGNKEYS);
     $this->assertEquals($this->getQuotedColumnInForeignKeySQL(), $sql);
 }
Exemple #4
0
 /**
  * @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
  * @param boolean                                   $saveMode
  *
  * @return array
  */
 protected function _toSql(AbstractPlatform $platform, $saveMode = false)
 {
     $sql = array();
     if ($platform->supportsSchemas()) {
         foreach ($this->newNamespaces as $newNamespace) {
             $sql[] = $platform->getCreateSchemaSQL($newNamespace);
         }
     }
     if ($platform->supportsForeignKeyConstraints() && $saveMode == false) {
         foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
             $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTableName());
         }
     }
     if ($platform->supportsSequences() == true) {
         foreach ($this->changedSequences as $sequence) {
             $sql[] = $platform->getAlterSequenceSQL($sequence);
         }
         if ($saveMode === false) {
             foreach ($this->removedSequences as $sequence) {
                 $sql[] = $platform->getDropSequenceSQL($sequence);
             }
         }
         foreach ($this->newSequences as $sequence) {
             $sql[] = $platform->getCreateSequenceSQL($sequence);
         }
     }
     $foreignKeySql = array();
     foreach ($this->newTables as $table) {
         $sql = array_merge($sql, $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES));
         if ($platform->supportsForeignKeyConstraints()) {
             foreach ($table->getForeignKeys() as $foreignKey) {
                 $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
             }
         }
     }
     $sql = array_merge($sql, $foreignKeySql);
     if ($saveMode === false) {
         foreach ($this->removedTables as $table) {
             $sql[] = $platform->getDropTableSQL($table);
         }
     }
     foreach ($this->changedTables as $tableDiff) {
         $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
     }
     return $sql;
 }
 /**
  * Create a new table.
  *
  * @param Table $table
  * @param int $createFlags
  */
 public function createTable(Table $table)
 {
     $createFlags = AbstractPlatform::CREATE_INDEXES | AbstractPlatform::CREATE_FOREIGNKEYS;
     $this->_execSql($this->_platform->getCreateTableSQL($table, $createFlags));
 }
 /**
  * Generate DDL Statements to create the accepted table with all its dependencies.
  *
  * @param Table $table
  */
 public function acceptTable(Table $table)
 {
     $namespace = $this->getNamespace($table);
     $this->_createTableQueries[$namespace] = array_merge($this->_createTableQueries[$namespace], $this->_platform->getCreateTableSQL($table));
 }
 /**
  * {@inheritDoc}
  */
 public function getCreateTableSQL(Table $table, $createFlags = null)
 {
     $createFlags = null === $createFlags ? self::CREATE_INDEXES | self::CREATE_FOREIGNKEYS : $createFlags;
     return parent::getCreateTableSQL($table, $createFlags);
 }
 /**
  * {@inheritdoc}
  */
 public function acceptTable(Table $table)
 {
     $this->createTableQueries = array_merge($this->createTableQueries, (array) $this->platform->getCreateTableSQL($table));
 }
Exemple #9
0
 /**
  * @override
  */
 public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDEXES)
 {
     $sql = parent::getCreateTableSQL($table, $createFlags);
     $primary = array();
     foreach ($table->getIndexes() as $index) {
         /* @var $index Index */
         if ($index->isPrimary()) {
             $primary = $index->getColumns();
         }
     }
     if (count($primary) === 1) {
         foreach ($table->getForeignKeys() as $definition) {
             $columns = $definition->getLocalColumns();
             if (count($columns) === 1 && in_array($columns[0], $primary)) {
                 $sql[0] = str_replace(' IDENTITY', '', $sql[0]);
             }
         }
     }
     return $sql;
 }