Skip to content

Commit

Permalink
First pass at implementing describe() for Sqlite.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Mar 31, 2013
1 parent b0eaecd commit 16bc093
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 10 deletions.
41 changes: 36 additions & 5 deletions lib/Cake/Model/Datasource/Database/Dialect/SqliteDialectTrait.php
Expand Up @@ -133,6 +133,17 @@ public function convertColumn($column) {
return ['text', null];
}

/**
* 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", []];
}

/**
* Additional metadata columns in table descriptions.
*
Expand All @@ -143,14 +154,34 @@ public function extraSchemaColumns() {
}

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

/**
* Convert field description results into abstract schema fields.
*
* @return array An array of with the key/values of schema data.
*/
public function convertFieldDescription($row, $fieldParams = []) {
list($type, $length) = $this->convertColumn($row['type']);
$schema = [];
$schema[$row['name']] = [
'type' => $type,
'null' => !$row['notnull'],
'default' => $row['dflt_value'] === null ? null : trim($row['dflt_value'], "'"),
'length' => $length,
];
if ($row['pk'] == true) {
$schema[$row['name']]['key'] = 'primary';
$schema[$row['name']]['null'] = false;
}
return $schema;
}

}
Expand Up @@ -29,12 +29,11 @@
class SqliteTest extends TestCase {

/**
* setup method
* Helper method for skipping tests that need a real connection.
*
* @return void
*/
public function setUp() {
parent::setUp();
protected function _needsConnection() {
$config = Configure::read('Datasource.test');
$this->skipIf(strpos($config['datasource'], 'Sqlite') === false, 'Not using Sqlite for test config');
}
Expand Down Expand Up @@ -190,6 +189,7 @@ public function testConvertColumnType($input, $expected) {
* @return void
*/
protected function _createTables($connection) {
$this->_needsConnection();
$connection->execute('DROP TABLE IF EXISTS articles');
$connection->execute('DROP TABLE IF EXISTS authors');

Expand All @@ -206,10 +206,10 @@ protected function _createTables($connection) {
$table = <<<SQL
CREATE TABLE articles(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(20),
title VARCHAR(20) DEFAULT 'testing',
body TEXT,
author_id INT(11) NOT NULL,
published BOOLEAN,
published BOOLEAN DEFAULT 0,
created DATETIME
)
SQL;
Expand All @@ -233,4 +233,56 @@ public function testListTables() {
$this->assertEquals('sqlite_sequence', $result[2]);
}

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

$result = $connection->describe('articles');
$expected = [
'id' => [
'type' => 'integer',
'null' => false,
'default' => null,
'length' => null,
'key' => 'primary',
],
'title' => [
'type' => 'string',
'null' => true,
'default' => 'testing',
'length' => 20,
],
'body' => [
'type' => 'text',
'null' => true,
'default' => null,
'length' => null,
],
'author_id' => [
'type' => 'integer',
'null' => false,
'default' => null,
'length' => 11,
],
'published' => [
'type' => 'boolean',
'null' => true,
'default' => 0,
'length' => null,
],
'created' => [
'type' => 'datetime',
'null' => true,
'default' => null,
'length' => null,
],
];
$this->assertEquals($expected, $result);
}

}

0 comments on commit 16bc093

Please sign in to comment.