Skip to content

Commit

Permalink
Add listTables for SQlite.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 31, 2013
1 parent 7bc185d commit 692dd8c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/Cake/Model/Datasource/Database/Dialect/SqliteDialectTrait.php
Expand Up @@ -142,4 +142,15 @@ public function extraSchemaColumns() {
return [];
}

/**
* Get the SQL to list the tables in Sqlite
*
* @param array $config The connection configuration to use for
* getting tables from.
* @return array An array of (sql, params) to execute.
*/
public function listTablesSql() {
return ["SELECT name FROM sqlite_master WHERE type='table' ORDER BY name", []];
}

}
Expand Up @@ -18,6 +18,7 @@
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;

use Cake\Core\Configure;
use Cake\Model\Datasource\Database\Connection;
use Cake\Model\Datasource\Database\Driver\Sqlite;
use Cake\Testsuite\TestCase;
use \PDO;
Expand Down Expand Up @@ -182,4 +183,54 @@ public function testConvertColumnType($input, $expected) {
$this->assertEquals($driver->convertColumn($input), $expected);
}

/**
* Creates tables for testing listTables/describe()
*
* @param Connection $connection
* @return void
*/
protected function _createTables($connection) {
$connection->execute('DROP TABLE IF EXISTS articles');
$connection->execute('DROP TABLE IF EXISTS authors');

$table = <<<SQL
CREATE TABLE authors(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(50),
bio TEXT,
created DATETIME
)
SQL;
$connection->execute($table);

$table = <<<SQL
CREATE TABLE articles(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(20),
body TEXT,
author_id INT(11) NOT NULL,
published BOOLEAN,
created DATETIME
)
SQL;
$connection->execute($table);
}

/**
* Test listing tables with Sqlite
*
* @return void
*/
public function testListTables() {
$connection = new Connection(Configure::read('Datasource.test'));
$this->_createTables($connection);

$result = $connection->listTables();
$this->assertInternalType('array', $result);
$this->assertCount(3, $result);
$this->assertEquals('articles', $result[0]);
$this->assertEquals('authors', $result[1]);
$this->assertEquals('sqlite_sequence', $result[2]);
}

}

0 comments on commit 692dd8c

Please sign in to comment.