Skip to content
Permalink
Browse files

Add basic table creation + test cases.

This rounds out basic table creation in SQLite. Foreign keys will be
added at a later date. Right now I want to get fixtures up and running
again.
  • Loading branch information...
markstory committed May 14, 2013
1 parent b2b9f29 commit ec426afe8056508343c485a7f8b15efe98562f49
@@ -183,6 +183,9 @@ public function columnSql(Table $table, $name) {
if (isset($data['null']) && $data['null'] === false) {
$out .= ' NOT NULL';
}
if (in_array($data['type'], ['integer']) && in_array($name, (array)$table->primaryKey())) {
$out .= ' PRIMARY KEY AUTOINCREMENT';
}
if (isset($data['null']) && $data['null'] === true) {
$out .= ' DEFAULT NULL';
unset($data['default']);
@@ -196,12 +199,23 @@ public function columnSql(Table $table, $name) {
/**
* Generate the SQL fragment for a single index in MySQL
*
* Note integer primary keys will return ''. This is intentional as Sqlite requires
* that integer primary keys be defined in the column definition.
*
* @param Cake\Database\Schema\Table $table The table object the column is in.
* @param string $name The name of the column.
* @return string SQL fragment.
*/
public function indexSql(Table $table, $name) {
$data = $table->index($name);
if (
count($data['columns']) === 1 &&
$table->column($data['columns'][0])['type'] === 'integer'
) {
return '';
}
$out = 'CONSTRAINT ';
$out .= $this->_driver->quoteIdentifier($name);
if ($data['type'] === Table::INDEX_PRIMARY) {
@@ -225,7 +239,7 @@ public function indexSql(Table $table, $name) {
* @return string A complete CREATE TABLE statement
*/
public function createTableSql($table, $lines) {
$content = implode(",\n", $lines);
$content = implode(",\n", array_filter($lines));
return sprintf("CREATE TABLE \"%s\" (\n%s\n);", $table, $content);
}
}
@@ -376,6 +376,31 @@ public function testColumnSql($name, $data, $expected) {
$this->assertEquals($expected, $schema->columnSql($table, $name));
}
/**
* Test generating a column that is a primary key.
*
* @return void
*/
public function testColumnSqlPrimaryKey() {
$driver = $this->_getMockedDriver();
$schema = new SqliteSchema($driver);
$table = new Table('articles');
$table->addColumn('id', [
'type' => 'integer',
'null' => false
])
->addIndex('primary', [
'type' => 'primary',
'columns' => ['id']
]);
$result = $schema->columnSql($table, 'id');
$this->assertEquals($result, '"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT');
$result = $schema->indexSql($table, 'primary');
$this->assertEquals('', $result, 'Integer primary keys are special in sqlite.');
}
/**
* Provide data for testing indexSql
*
@@ -415,6 +440,44 @@ public function testIndexSql($name, $data, $expected) {
$this->assertEquals($expected, $schema->indexSql($table, $name));
}
/**
* Integration test for converting a Schema\Table into MySQL table creates.
*
* @return void
*/
public function testCreateTableSql() {
$driver = $this->_getMockedDriver();
$connection = $this->getMock('Cake\Database\Connection', array(), array(), '', false);
$connection->expects($this->any())->method('driver')
->will($this->returnValue($driver));
$table = (new Table('articles'))->addColumn('id', [
'type' => 'integer',
'null' => false
])
->addColumn('title', [
'type' => 'string',
'null' => false,
])
->addColumn('body', ['type' => 'text'])
->addColumn('created', 'datetime')
->addIndex('primary', [
'type' => 'primary',
'columns' => ['id']
]);
$expected = <<<SQL
CREATE TABLE "articles" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" VARCHAR NOT NULL,
"body" TEXT,
"created" DATETIME
);
SQL;
$result = $table->createTableSql($connection);
$this->assertEquals($expected, $result);
}
/**
* Get a schema instance with a mocked driver/pdo instances
*

0 comments on commit ec426af

Please sign in to comment.
You can’t perform that action at this time.