/** * {@inheritdoc} */ public function createTable(Table $table) { $this->startCommandTimer(); $options = $table->getOptions(); // Add the default primary key $columns = $table->getPendingColumns(); if (!isset($options['id']) || isset($options['id']) && $options['id'] === true) { $column = new Column(); $column->setName('id')->setType('integer')->setIdentity(true); array_unshift($columns, $column); $options['primary_key'] = 'id'; } elseif (isset($options['id']) && is_string($options['id'])) { // Handle id => "field_name" to support AUTO_INCREMENT $column = new Column(); $column->setName($options['id'])->setType('integer')->setIdentity(true); array_unshift($columns, $column); $options['primary_key'] = $options['id']; } $sql = 'CREATE TABLE '; $sql .= $this->quoteTableName($table->getName()) . ' ('; $sqlBuffer = array(); $columnsWithComments = array(); foreach ($columns as $column) { $sqlBuffer[] = $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column); // set column comments, if needed if ($column->getComment()) { $columnsWithComments[] = $column; } } // set the primary key(s) if (isset($options['primary_key'])) { $pkSql = sprintf('CONSTRAINT PK_%s PRIMARY KEY (', $table->getName()); if (is_string($options['primary_key'])) { // handle primary_key => 'id' $pkSql .= $this->quoteColumnName($options['primary_key']); } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') // PHP 5.4 will allow access of $this, so we can call quoteColumnName() directly in the anonymous function, // but for now just hard-code the adapter quotes $pkSql .= implode(',', array_map(function ($v) { return '[' . $v . ']'; }, $options['primary_key'])); } $pkSql .= ')'; $sqlBuffer[] = $pkSql; } // set the foreign keys $foreignKeys = $table->getForeignKeys(); if (!empty($foreignKeys)) { foreach ($foreignKeys as $foreignKey) { $sqlBuffer[] = $this->getForeignKeySqlDefinition($foreignKey, $table->getName()); } } $sql .= implode(', ', $sqlBuffer); $sql .= ');'; // process column comments if (!empty($columnsWithComments)) { foreach ($columnsWithComments as $column) { $sql .= $this->getColumnCommentSqlDefinition($column, $table->getName()); } } // set the indexes $indexes = $table->getIndexes(); if (!empty($indexes)) { foreach ($indexes as $index) { $sql .= $this->getIndexSqlDefinition($index, $table->getName()); } } // execute the sql $this->writeCommand('createTable', array($table->getName())); $this->execute($sql); $this->endCommandTimer(); }
/** * {@inheritdoc} */ public function createTable(Table $table) { $this->startCommandTimer(); // Add the default primary key $columns = $table->getPendingColumns(); $options = $table->getOptions(); if (!isset($options['id']) || isset($options['id']) && $options['id'] === true) { $column = new Column(); $column->setName('id')->setType('integer')->setIdentity(true); array_unshift($columns, $column); } elseif (isset($options['id']) && is_string($options['id'])) { // Handle id => "field_name" to support AUTO_INCREMENT $column = new Column(); $column->setName($options['id'])->setType('integer')->setIdentity(true); array_unshift($columns, $column); } $sql = 'CREATE TABLE '; $sql .= $this->quoteTableName($table->getName()) . ' ('; foreach ($columns as $column) { $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', '; } // set the primary key(s) if (isset($options['primary_key'])) { $sql = rtrim($sql); $sql .= ' PRIMARY KEY ('; if (is_string($options['primary_key'])) { // handle primary_key => 'id' $sql .= $this->quoteColumnName($options['primary_key']); } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') // PHP 5.4 will allow access of $this, so we can call quoteColumnName() directly in the anonymous function, // but for now just hard-code the adapter quotes $sql .= implode(',', array_map(function ($v) { return '`' . $v . '`'; }, $options['primary_key'])); } $sql .= ')'; } else { $sql = substr(rtrim($sql), 0, -1); // no primary keys } // set the foreign keys $foreignKeys = $table->getForeignKeys(); if (!empty($foreignKeys)) { foreach ($foreignKeys as $foreignKey) { $sql .= ', ' . $this->getForeignKeySqlDefinition($foreignKey); } } $sql = rtrim($sql) . ');'; // execute the sql $this->writeCommand('createTable', array($table->getName())); $this->execute($sql); $this->endCommandTimer(); foreach ($table->getIndexes() as $index) { $this->addIndex($table, $index); } }
/** * {@inheritdoc} */ public function createTable(Table $table) { $this->startCommandTimer(); // This method is based on the MySQL docs here: http://dev.mysql.com/doc/refman/5.1/en/create-index.html $defaultOptions = array('engine' => 'InnoDB', 'collation' => 'utf8_general_ci'); $options = array_merge($defaultOptions, $table->getOptions()); // Add the default primary key $columns = $table->getPendingColumns(); if (!isset($options['id']) || isset($options['id']) && $options['id'] === true) { $column = new Column(); $column->setName('id')->setType('integer')->setIdentity(true); array_unshift($columns, $column); $options['primary_key'] = 'id'; } elseif (isset($options['id']) && is_string($options['id'])) { // Handle id => "field_name" to support AUTO_INCREMENT $column = new Column(); $column->setName($options['id'])->setType('integer')->setIdentity(true); array_unshift($columns, $column); $options['primary_key'] = $options['id']; } // TODO - process table options like collation etc // process table engine (default to InnoDB) $optionsStr = 'ENGINE = InnoDB'; if (isset($options['engine'])) { $optionsStr = sprintf('ENGINE = %s', $options['engine']); } // process table collation if (isset($options['collation'])) { $charset = explode('_', $options['collation']); $optionsStr .= sprintf(' CHARACTER SET %s', $charset[0]); $optionsStr .= sprintf(' COLLATE %s', $options['collation']); } // set the table comment if (isset($options['comment'])) { $optionsStr .= sprintf(" COMMENT=%s ", $this->getConnection()->quote($options['comment'])); } $sql = 'CREATE TABLE '; $sql .= $this->quoteTableName($table->getName()) . ' ('; foreach ($columns as $column) { $sql .= $this->quoteColumnName($column->getName()) . ' ' . $this->getColumnSqlDefinition($column) . ', '; } // set the primary key(s) if (isset($options['primary_key'])) { $sql = rtrim($sql); $sql .= ' PRIMARY KEY ('; if (is_string($options['primary_key'])) { // handle primary_key => 'id' $sql .= $this->quoteColumnName($options['primary_key']); } elseif (is_array($options['primary_key'])) { // handle primary_key => array('tag_id', 'resource_id') // PHP 5.4 will allow access of $this, so we can call quoteColumnName() directly in the // anonymous function, but for now just hard-code the adapter quotes $sql .= implode(',', array_map(function ($v) { return '`' . $v . '`'; }, $options['primary_key'])); } $sql .= ')'; } else { $sql = substr(rtrim($sql), 0, -1); // no primary keys } // set the indexes $indexes = $table->getIndexes(); if (!empty($indexes)) { foreach ($indexes as $index) { $sql .= ', ' . $this->getIndexSqlDefinition($index); } } // set the foreign keys $foreignKeys = $table->getForeignKeys(); if (!empty($foreignKeys)) { foreach ($foreignKeys as $foreignKey) { $sql .= ', ' . $this->getForeignKeySqlDefinition($foreignKey); } } $sql .= ') ' . $optionsStr; $sql = rtrim($sql) . ';'; // execute the sql $this->writeCommand('createTable', array($table->getName())); $this->execute($sql); $this->endCommandTimer(); }
/** * Gets the table options. * * @return array */ public function getOptions() { return $this->table->getOptions(); }