Skip to content

Commit

Permalink
Starting code for schema instrospection in postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Apr 1, 2013
1 parent 4a67078 commit ba22518
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
Expand Up @@ -148,4 +148,55 @@ protected function _transformFunctionExpression(FunctionExpression $expression)
}
}

/**
* Get the SQL to list the tables
*
* @param array $config The connection configuration to use for
* getting tables from.
* @return array An array of (sql, params) to execute.
*/
public function listTablesSql() {
$sql = "SELECT table_name as name FROM INFORMATION_SCHEMA.tables WHERE table_schema = ? ORDER BY name";
$schema = empty($config['schema']) ? 'public' : $config['schema'];
return [$sql, [$schema]];
}

/**
* Get the SQL to describe a table in Sqlite.
*
* @param string $table The table name to describe
* @return array An array of (sql, params) to execute.
*/
public function describeTableSql($table) {
}


/**
* Convert a column definition to the abstract types.
*
* The returned type will be a type that
* Cake\Model\Datasource\Database\Type can handle.
*
* @param string $column The column type + length
* @return array List of (type, length)
*/
public function convertColumn($column) {
}

/**
* Additional metadata columns in table descriptions.
*
* @return array
*/
public function extraSchemaColumns() {
}

/**
* 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 = []) {
}

}
Expand Up @@ -17,6 +17,9 @@
*/
namespace Cake\Test\TestCase\Model\Datasource\Database\Driver;

use Cake\Core\Configure;
use Cake\Model\Datasource\Database\Connection;
use Cake\Model\Datasource\Database\Driver\Postgres;
use \PDO;

/**
Expand Down Expand Up @@ -126,4 +129,65 @@ public function testConnectionConfigCustom() {
$driver->connect($config);
}

/**
* Helper method for skipping tests that need a real connection.
*
* @return void
*/
protected function _needsConnection() {
$config = Configure::read('Datasource.test');
$this->skipIf(strpos($config['datasource'], 'Postgres') === false, 'Not using Postgres for test config');
}

/**
* Helper method for testing methods.
*
* @return void
*/
protected function _createTables($connection) {
$this->_needsConnection();
$connection->execute('DROP TABLE IF EXISTS articles');
$connection->execute('DROP TABLE IF EXISTS authors');

$table = <<<SQL
CREATE TABLE authors(
id SERIAL,
name VARCHAR(50),
bio DATE,
created TIMESTAMP
)
SQL;
$connection->execute($table);

$table = <<<SQL
CREATE TABLE articles(
id BIGINT PRIMARY KEY,
title VARCHAR(20),
body TEXT,
author_id INTEGER NOT NULL,
published BOOLEAN DEFAULT false,
views SMALLINT DEFAULT 0,
created TIMESTAMP
)
SQL;
$connection->execute($table);
$connection->execute('COMMENT ON COLUMN "articles"."title" IS \'a title\'');
}

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

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

}

0 comments on commit ba22518

Please sign in to comment.