/** * {@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; }
/** * @param $columnName * @param $type * @param array $options * @return $this */ public function addColumn($columnName, $type = null, $options = []) { if (!$columnName instanceof Column) { $column = new Column(); $column->setName($columnName)->setDataType($type); if (!empty($options)) { $column->setOptions($options); if (isset($options['primaryKey'])) { $this->addPrimaryKey($column); } if (isset($options['foreignKey']) && isset($options['referencedTable']) && isset($options['referencedColumns'])) { $this->addForeignKey($column, $options['referencedTable'], $options['referencedColumns']); } } } else { $column = $columnName; } $this->setColumn($column); return $this; }
/** * @covers \Core\Database\Mapper\MySqlMapper::__construct * @covers \Core\Database\Mapper\MySqlMapper::addColumn * @covers \Core\Database\Column::__construct * @covers \Core\Database\Column::setName * @covers \Core\Database\Column::setDataType * @covers \Core\Database\Column::setDefault * @covers \Core\Database\Column::setAfter */ public function testAddColumn() { $this->mapper = new MySqlMapper($this->getConfig()); $newColumn = new Column(); $newColumn->setName('created_on')->setDataType('timestamp')->setDefault('CURRENT_TIMESTAMP')->setAfter('category'); $result = $this->mapper->addColumn('product', $newColumn); $this->assertTrue($result); }