Example #1
0
 /**
  * {@inheritdoc}
  */
 public function create(Table $table)
 {
     $columns = $table->getColumns();
     $tableOptions = $table->getOptions();
     if (isset($tableOptions['id']) && $tableOptions['id'] === true) {
         $column = new Column();
         $column->setName('id')->setDataType('integer')->setAutoIncrement(true);
         array_unshift($columns, $column);
         $tableOptions['primaryKey'] = 'id';
     } elseif (isset($tableOptions['id']) && is_string($tableOptions['id'])) {
         $column = new Column();
         $column->setName($tableOptions['id'])->setDataType('integer')->setAutoIncrement(true);
         array_unshift($columns, $column);
         $tableOptions['primaryKey'] = $tableOptions['id'];
     }
     $sql = "CREATE TABLE {$this->quote($table->getName())} (";
     /** @var Column $column */
     foreach ($columns as $column) {
         $sql .= "{$this->quote($column->getName())} {$this->getColumnDefinition($column)},";
     }
     // Primary Key Assignment
     if (isset($tableOptions['primaryKey'])) {
         if (is_array($tableOptions['primaryKey'])) {
             $quotedCols = implode(",", $this->arrayQuote($tableOptions['primaryKey']));
             $sql .= " PRIMARY KEY ({$quotedCols})";
         } elseif (is_string($tableOptions['primaryKey'])) {
             $sql .= " PRIMARY KEY ({$this->quote($tableOptions['primaryKey'])})";
         }
     } else {
         $primaryKeysArr = $table->getPrimaryKeyNames();
         if (!empty($primaryKeysArr)) {
             $keys = implode(",", $this->arrayQuote($primaryKeysArr));
             $sql .= " PRIMARY KEY ({$keys})";
         }
     }
     // TODO: add handling for indexes
     // Foreign Key Assignment
     $foreignKeys = $table->getForeignKeys();
     if (!empty($foreignKeys)) {
         foreach ($foreignKeys as $foreignKey) {
             $sql .= ", {$this->getForeignKeyDefinition($foreignKey)}";
         }
     }
     $sql = rtrim($sql, ',');
     $sql .= ") {$table->getTableOptionsStr()};";
     $result = $this->getConnection()->exec($sql);
     if ($result !== false) {
         $result = true;
     }
     return $result;
 }