Skip to content

Commit

Permalink
Move Mysql describe() and tests to Dialect system.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 25, 2013
1 parent 07ca29c commit dc1f766
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
19 changes: 0 additions & 19 deletions lib/Cake/Database/Connection.php
Expand Up @@ -471,25 +471,6 @@ public function lastInsertId($table) {
return $this->_driver->lastInsertId($table);
}

/**
* Get the schema information for a given table/collection
*
* @param string $table The table/collection you want schema information for.
* @return array The schema data for the requested table.
*/
public function describe($table) {
list($sql, $params) = $this->_driver->describeTableSql($table, $this->_config);
$statement = $this->execute($sql, $params);
$schema = [];

$fieldParams = $this->_driver->extraSchemaColumns();
$rows = $statement->fetchAll('assoc');
foreach ($rows as $row) {
$schema += $this->_driver->convertFieldDescription($row, $fieldParams);
}
return $schema;
}

/**
* Enables or disables query logging for this connection.
*
Expand Down
22 changes: 19 additions & 3 deletions lib/Cake/Database/Schema/Collection.php
Expand Up @@ -17,6 +17,7 @@
namespace Cake\Database\Schema;

use Cake\Database\Connection;
use Cake\Database\Schema\Table;
use Cake\Error;

/**
Expand Down Expand Up @@ -69,12 +70,27 @@ public function listTables() {
/**
* Get the column metadata for a table.
*
*
* @param string $name The name of the table to describe
* @return Cake\Schema\Table object with column metdata.
* @param string $name The name of the table to describe.
* @return Cake\Schema\Table|null Object with column metadata, or null.
* @see Collection::fullDescribe()
*/
public function describe($name) {
list($sql, $params) = $this->_dialect->describeTableSql(
$name,
$this->_connection->config()
);
$statement = $this->_connection->execute($sql, $params);
if (count($statement) == 0) {
return null;
}

$columns = [];
$fieldParams = $this->_dialect->extraSchemaColumns();
$rows = $statement->fetchAll('assoc');
foreach ($rows as $row) {
$columns += $this->_dialect->convertFieldDescription($row, $fieldParams);
}
return new Table($name, $columns);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/Cake/Database/Schema/Table.php
Expand Up @@ -100,9 +100,13 @@ class Table {
* Constructor.
*
* @param string $table The table name.
* @param array $columns The list of columns for the schema.
*/
public function __construct($table) {
public function __construct($table, $columns = array()) {
$this->_table = $table;
foreach ($columns as $field => $definition) {
$this->addColumn($field, $definition);
}
}

/**
Expand Down
35 changes: 31 additions & 4 deletions lib/Cake/Test/TestCase/Database/Schema/Dialect/MysqlTest.php
Expand Up @@ -203,18 +203,22 @@ public function testListTables() {
* @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');
$schema = new SchemaCollection($connection);
$result = $schema->describe('articles');
$this->assertInstanceOf('Cake\Database\Schema\Table', $result);
$expected = [
'id' => [
'type' => 'biginteger',
'null' => false,
'default' => null,
'length' => 20,
'key' => 'primary',
'fixed' => null,
'comment' => null,
'collate' => null,
'charset' => null,
],
'title' => [
'type' => 'string',
Expand All @@ -223,40 +227,63 @@ public function testDescribeTable() {
'length' => 20,
'collate' => 'utf8_general_ci',
'comment' => 'A title',
'fixed' => null,
'charset' => null,
],
'body' => [
'type' => 'text',
'null' => true,
'default' => null,
'length' => null,
'collate' => 'utf8_general_ci',
'fixed' => null,
'comment' => null,
'charset' => null,
],
'author_id' => [
'type' => 'integer',
'null' => false,
'default' => null,
'length' => 11,
'fixed' => null,
'comment' => null,
'collate' => null,
'charset' => null,
],
'published' => [
'type' => 'boolean',
'null' => true,
'default' => 0,
'length' => null,
'fixed' => null,
'comment' => null,
'collate' => null,
'charset' => null,
],
'allow_comments' => [
'type' => 'boolean',
'null' => true,
'default' => 0,
'length' => null,
'fixed' => null,
'comment' => null,
'collate' => null,
'charset' => null,
],
'created' => [
'type' => 'datetime',
'null' => true,
'default' => null,
'length' => null,
'fixed' => null,
'comment' => null,
'collate' => null,
'charset' => null,
],
];
$this->assertEquals($expected, $result);
foreach ($expected as $field => $definition) {
$this->assertEquals($definition, $result->column($field));
}
}

}
20 changes: 20 additions & 0 deletions lib/Cake/Test/TestCase/Database/Schema/TableTest.php
Expand Up @@ -24,6 +24,26 @@
*/
class TableTest extends TestCase {

/**
* Test construction with columns
*
* @return void
*/
public function testConstructWithColumns() {
$columns = [
'id' => [
'type' => 'integer',
'length' => 11,
],
'title' => [
'type' => 'string',
'length' => 255
]
];
$table = new Table('articles', $columns);
$this->assertEquals(['id', 'title'], $table->columns());
}

/**
* Test adding columns.
*
Expand Down

0 comments on commit dc1f766

Please sign in to comment.