Skip to content

Commit

Permalink
Fix method signatures of database drivers.
Browse files Browse the repository at this point in the history
They were incompatible with Datasource.
Add tests for MySQL and SQLite as their implementation changed a bit.
Update docs for Postgres.

Fixes #2100
  • Loading branch information
markstory committed Oct 15, 2011
1 parent 6bf6d79 commit 6d9b709
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
10 changes: 6 additions & 4 deletions lib/Cake/Model/Datasource/Database/Mysql.php
Expand Up @@ -292,19 +292,21 @@ public function getCharsetName($name) {
/**
* Returns an array of the fields in given table name.
*
* @param Model $model Name of database table to inspect or model instance
* @param Model|string $model Name of database table to inspect or model instance
* @return array Fields in table. Keys are name and type
* @throws CakeException
*/
public function describe(Model $model) {
public function describe($model) {
$cache = parent::describe($model);
if ($cache != null) {
return $cache;
}
$table = $this->fullTableName($model);

$fields = false;
$cols = $this->_execute('SHOW FULL COLUMNS FROM ' . $this->fullTableName($model));
$cols = $this->_execute('SHOW FULL COLUMNS FROM ' . $table);
if (!$cols) {
throw new CakeException(__d('cake_dev', 'Could not describe table for %s', $model->name));
throw new CakeException(__d('cake_dev', 'Could not describe table for %s', $table));
}

foreach ($cols as $column) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Model/Datasource/Database/Postgres.php
Expand Up @@ -184,7 +184,7 @@ public function listSources($data = null) {
/**
* Returns an array of the fields in given table name.
*
* @param Model $model Name of database table to inspect
* @param Model|string $model Name of database table to inspect
* @return array Fields in table. Keys are name and type
*/
public function describe($model) {
Expand Down
9 changes: 5 additions & 4 deletions lib/Cake/Model/Datasource/Database/Sqlite.php
Expand Up @@ -156,16 +156,17 @@ public function listSources($data = null) {
/**
* Returns an array of the fields in given table name.
*
* @param Model $model
* @param Model|string $model Either the model or table name you want described.
* @return array Fields in table. Keys are name and type
*/
public function describe(Model $model) {
public function describe($model) {
$cache = parent::describe($model);
if ($cache != null) {
return $cache;
}
$table = $this->fullTableName($model);
$fields = array();
$result = $this->_execute('PRAGMA table_info(' . $model->tablePrefix . $model->table . ')');
$result = $this->_execute('PRAGMA table_info(' . $table . ')');

foreach ($result as $column) {
$column = (array) $column;
Expand All @@ -187,7 +188,7 @@ public function describe(Model $model) {
}

$result->closeCursor();
$this->_cacheDescription($model->tablePrefix . $model->table, $fields);
$this->_cacheDescription($table, $fields);
return $fields;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -195,11 +195,11 @@ public function listSources($data = null) {
/**
* Returns an array of the fields in given table name.
*
* @param Model $model Model object to describe
* @param Model|string $model Model object to describe, or a string table name.
* @return array Fields in table. Keys are name and type
* @throws CakeException
*/
public function describe(Model $model) {
public function describe($model) {
$cache = parent::describe($model);
if ($cache != null) {
return $cache;
Expand All @@ -219,7 +219,7 @@ public function describe(Model $model) {
WHERE TABLE_NAME = '" . $table . "'"
);
if (!$cols) {
throw new CakeException(__d('cake_dev', 'Could not describe table for %s', $model->name));
throw new CakeException(__d('cake_dev', 'Could not describe table for %s', $table));
}

foreach ($cols as $column) {
Expand Down
20 changes: 20 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/Database/MysqlTest.php
Expand Up @@ -761,6 +761,26 @@ public function testVirtualFieldSeparators() {
$this->assertEqual($expected, $result);
}
/**
* Test describe() on a fixture.
*
* @return void
*/
public function testDescribe() {
$this->loadFixtures('Apple');
$model = new Apple();
$result = $this->Dbo->describe($model);
$this->assertTrue(isset($result['id']));
$this->assertTrue(isset($result['color']));
$result = $this->Dbo->describe($model->useTable);
$this->assertTrue(isset($result['id']));
$this->assertTrue(isset($result['color']));
}
/**
* test that a describe() gets additional fieldParameters
*
Expand Down
5 changes: 4 additions & 1 deletion lib/Cake/Test/Case/Model/Datasource/Database/SqliteTest.php
Expand Up @@ -292,7 +292,10 @@ public function testDescribe() {
'length' => null,
)
);
$this->assertEqual($expected, $result);
$this->assertEquals($expected, $result);

$result = $this->Dbo->describe($Model->useTable);
$this->assertEquals($expected, $result);
}

/**
Expand Down

0 comments on commit 6d9b709

Please sign in to comment.