Skip to content

Commit

Permalink
Migrate listTables() to SchemaCollection
Browse files Browse the repository at this point in the history
Migrate the methods + tests for listTables() to the new schema
system.
  • Loading branch information
markstory committed Apr 25, 2013
1 parent 5de0541 commit 07ca29c
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 131 deletions.
24 changes: 9 additions & 15 deletions lib/Cake/Database/Connection.php
Expand Up @@ -113,6 +113,15 @@ public function __construct($config) {
}
}

/**
* Get the configuration data used to create the connection.
*
* @return array
*/
public function config() {
return $this->_config;
}

/**
* Sets the driver instance. If an string is passed it will be treated
* as a class name and will be instantiated.
Expand Down Expand Up @@ -462,21 +471,6 @@ public function lastInsertId($table) {
return $this->_driver->lastInsertId($table);
}

/**
* Get the list of tables available in the current connection.
*
* @return array The list of tables in the connected database/schema.
*/
public function listTables() {
list($sql, $params) = $this->_driver->listTablesSql($this->_config);
$result = [];
$statement = $this->execute($sql, $params);
while ($row = $statement->fetch()) {
$result[] = $row[0];
}
return $result;
}

/**
* Get the schema information for a given table/collection
*
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/Database/Driver.php
Expand Up @@ -89,7 +89,7 @@ public abstract function rollbackTransaction();
* Returns whether this driver supports save points for nested transactions
*
* @return boolean true if save points are supported, false otherwise
**/
*/
public function supportsSavePoints() {
return true;
}
Expand All @@ -98,7 +98,7 @@ public function supportsSavePoints() {
* Returns a value in a safe representation to be used in a query string
*
* @return string
**/
*/
public abstract function quote($value, $type);

/**
Expand Down
24 changes: 22 additions & 2 deletions lib/Cake/Database/Schema/Collection.php
Expand Up @@ -27,23 +27,43 @@
*/
class Collection {

/**
* Connection object
*
* @var Cake\Database\Connection
*/
protected $_connection;

/**
* Schema dialect instance.
*
* @var
*/
protected $_dialect;

/**
* Constructor.
*
* @param Cake\Database\Connection $connection
*/
public function __construct(Connection $connection) {
$this->_connection = $connection;
$this->_dialect = $connection->driver()->schemaDialect();
}

/**
* Get the list of tables in the connection's database.
* Get the list of tables available in the current connection.
*
* @return array A list of table names.
* @return array The list of tables in the connected database/schema.
*/
public function listTables() {
list($sql, $params) = $this->_dialect->listTablesSql($this->_connection->config());
$result = [];
$statement = $this->_connection->execute($sql, $params);
while ($row = $statement->fetch()) {
$result[] = $row[0];
}
return $result;
}

/**
Expand Down
9 changes: 7 additions & 2 deletions lib/Cake/Database/Schema/Dialect/Mysql.php
Expand Up @@ -18,6 +18,11 @@

class Mysql {

/**
* The driver instance being used.
*
* @var Cake\Database\Driver\Mysql
*/
protected $driver;

public function __construct($driver) {
Expand All @@ -32,7 +37,7 @@ public function __construct($driver) {
* @return array An array of (sql, params) to execute.
*/
public function listTablesSql(array $config) {
return ["SHOW TABLES FROM " . $this->quoteIdentifier($config['database']), []];
return ["SHOW TABLES FROM " . $this->_driver->quoteIdentifier($config['database']), []];
}

/**
Expand All @@ -42,7 +47,7 @@ public function listTablesSql(array $config) {
* @return array An array of (sql, params) to execute.
*/
public function describeTableSql($table) {
return ["SHOW FULL COLUMNS FROM " . $this->quoteIdentifier($table), []];
return ["SHOW FULL COLUMNS FROM " . $this->_driver->quoteIdentifier($table), []];
}

/**
Expand Down
110 changes: 0 additions & 110 deletions lib/Cake/Test/TestCase/Database/Driver/MysqlTest.php
Expand Up @@ -104,114 +104,4 @@ public function testConnectionConfigCustom() {
$driver->connect($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 INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
bio TEXT,
created DATETIME
)
SQL;
$connection->execute($table);

$table = <<<SQL
CREATE TABLE articles(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(20) COMMENT 'A title',
body TEXT,
author_id INT(11) NOT NULL,
published BOOLEAN DEFAULT 0,
allow_comments TINYINT(1) DEFAULT 0,
created DATETIME
) COLLATE=utf8_general_ci
SQL;
$connection->execute($table);
}

/**
* Test listing tables with Mysql
*
* @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]);
}

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

$result = $connection->describe('articles');
$expected = [
'id' => [
'type' => 'biginteger',
'null' => false,
'default' => null,
'length' => 20,
'key' => 'primary',
],
'title' => [
'type' => 'string',
'null' => true,
'default' => null,
'length' => 20,
'collate' => 'utf8_general_ci',
'comment' => 'A title',
],
'body' => [
'type' => 'text',
'null' => true,
'default' => null,
'length' => null,
'collate' => 'utf8_general_ci',
],
'author_id' => [
'type' => 'integer',
'null' => false,
'default' => null,
'length' => 11,
],
'published' => [
'type' => 'boolean',
'null' => true,
'default' => 0,
'length' => null,
],
'allow_comments' => [
'type' => 'boolean',
'null' => true,
'default' => 0,
'length' => null,
],
'created' => [
'type' => 'datetime',
'null' => true,
'default' => null,
'length' => null,
],
];
$this->assertEquals($expected, $result);
}
}
117 changes: 117 additions & 0 deletions lib/Cake/Test/TestCase/Database/Schema/Dialect/MysqlTest.php
Expand Up @@ -17,7 +17,10 @@
namespace Cake\Test\TestCase\Database\Schema\Dialect;

use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Schema\Collection as SchemaCollection;
use Cake\Database\Schema\Dialect\Mysql;
use Cake\Database\Schema\Driver\Mysql as MysqlDriver;
use Cake\TestSuite\TestCase;


Expand Down Expand Up @@ -142,4 +145,118 @@ public function testConvertIndex($input, $expected) {
$this->assertEquals($expected, $dialect->convertIndex($input));
}

/**
* 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 INT(11) PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
bio TEXT,
created DATETIME
)
SQL;
$connection->execute($table);

$table = <<<SQL
CREATE TABLE articles(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(20) COMMENT 'A title',
body TEXT,
author_id INT(11) NOT NULL,
published BOOLEAN DEFAULT 0,
allow_comments TINYINT(1) DEFAULT 0,
created DATETIME
) COLLATE=utf8_general_ci
SQL;
$connection->execute($table);
}

/**
* Integration test for SchemaCollection & MysqlDialect.
*
* @return void
*/
public function testListTables() {
$connection = new Connection(Configure::read('Datasource.test'));
$this->_createTables($connection);

$schema = new SchemaCollection($connection);
$result = $schema->listTables();

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

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

$result = $connection->describe('articles');
$expected = [
'id' => [
'type' => 'biginteger',
'null' => false,
'default' => null,
'length' => 20,
'key' => 'primary',
],
'title' => [
'type' => 'string',
'null' => true,
'default' => null,
'length' => 20,
'collate' => 'utf8_general_ci',
'comment' => 'A title',
],
'body' => [
'type' => 'text',
'null' => true,
'default' => null,
'length' => null,
'collate' => 'utf8_general_ci',
],
'author_id' => [
'type' => 'integer',
'null' => false,
'default' => null,
'length' => 11,
],
'published' => [
'type' => 'boolean',
'null' => true,
'default' => 0,
'length' => null,
],
'allow_comments' => [
'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 07ca29c

Please sign in to comment.