public function createTable(Table $table) { $createDefinition = array(); $fields = $table->getFields(); if (empty($fields)) { throw new SQLException('A table must have at least one field'); } foreach ($fields as $field) { $createDefinition[] = $this->buildColumnDefinition($field); } $primary = $table->getPrimaryKey(); if ($primary != null) { $createDefinition[] = $this->buildPrimaryKeyDefinition($primary); } $indices = $table->getIndices(); foreach ($indices as $index) { $createDefinition[] = $this->buildIndexDefinition($index); } $foreignKeys = $table->getForeignKeys(); foreach ($foreignKeys as $foreignKey) { $createDefinition[] = $this->buildForeignKeyDefinition($foreignKey); } $query = 'CREATE TABLE ' . $this->addPrefix($table->getName()) . "(\n"; $query .= implode(",\n", $createDefinition) . "\n)"; $query .= " ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"; $comment = $table->getComment(); if (strlen($comment) > 0) { $query .= ' COMMENT=' . $this->pdoDriver->escapeString($comment); } $query .= ';'; $this->pdoDriver->executeQuery($query, null, false); }
/** * Tests getting the last insert ID. * * @depends testCreateTable * @covers empire\framework\db\DB::lastInsertID * * @param DB[] $dbs the database objects to work on */ public function testLastInsertID($dbs) { foreach ($dbs as $db) { /* @var $db DB */ $table = new Table('tlastinsert'); $table->addField(new Field('flastinsert', Field::TYPE_INT, '', false, true)); $table->addField(new Field('ftest', new Type(Field::TYPE_VARCHAR, 10))); $table->setPrimaryKey(new PrimaryKey('flastinsert', array('flastinsert'))); if ($db->tableExists($table->getName())) { $db->dropTable($table->getName()); } $db->createTable($table); $bindings = array(':::table' => 'tlastinsert', ':value' => 'hello'); for ($i = 1; $i <= 100; $i++) { $db->execute('INSERT INTO :::table (ftest) VALUES(:value)', $bindings); $this->assertSame($i, $db->lastInsertID()); } $db->dropTable($table->getName()); } }