public function testAddIndex() { $table = new Table('table'); $field = new Field('field', 'type'); $index = new Index('index', array($field)); $table->addField($field); $table->addIndex($index); $indexes = $table->getIndexes(); $expected = array($index->getName() => $index); $this->assertEquals($expected, $indexes); }
/** * Creates a new table * @param zibo\library\database\definition\Table $table Table definition for the new table * @return null */ protected function createTable(Table $table) { $tableName = $this->connection->quoteIdentifier($table->getName()); $fields = $table->getFields(); $primaryKeys = array(); $indexes = array(); $uniques = array(); $sql = ''; foreach ($fields as $field) { $fieldName = $this->connection->quoteIdentifier($field->getName()); $sql .= $sql == '' ? '' : ', '; $sql .= $fieldName; $sql .= ' ' . $this->getFieldType($field); if ($field->isPrimaryKey()) { $primaryKeys[] = $fieldName; if ($field->isAutoNumbering()) { $sql .= ' AUTO_INCREMENT'; } continue; } $sql .= ' DEFAULT ' . $this->getDefaultValue($field); if ($field->isUnique()) { $uniques[] = $fieldName; } elseif ($field->isIndexed()) { $indexes[] = $fieldName; } } if ($primaryKeys) { $sql .= ', PRIMARY KEY (' . implode(', ', $primaryKeys) . ')'; } if ($uniques) { $sql .= ', UNIQUE (' . implode(', ', $uniques) . ')'; } $sql = 'CREATE TABLE ' . $tableName . ' (' . $sql . ') ENGINE=INNODB CHARACTER SET utf8'; $this->connection->execute($sql); foreach ($indexes as $fieldName) { $this->addIndexFromFieldName($tableName, $fieldName); } $indexes = $table->getIndexes(); foreach ($indexes as $index) { $this->addIndex($tableName, $index); } }