Supporting Model.* syntax on postgres.

This commit is contained in:
renan.saddam 2010-03-25 17:33:34 -03:00
parent 12e608c671
commit dd4ad2f83d
2 changed files with 108 additions and 17 deletions

View file

@ -440,9 +440,23 @@ class DboPostgres extends DboSource {
} }
$count = count($fields); $count = count($fields);
if ($count >= 1 && $fields[0] != '*' && strpos($fields[0], 'COUNT(*)') === false) { if ($count >= 1 && strpos($fields[0], 'COUNT(*)') === false) {
$result = array();
for ($i = 0; $i < $count; $i++) { for ($i = 0; $i < $count; $i++) {
if (!preg_match('/^.+\\(.*\\)/', $fields[$i]) && !preg_match('/\s+AS\s+/', $fields[$i])) { if (!preg_match('/^.+\\(.*\\)/', $fields[$i]) && !preg_match('/\s+AS\s+/', $fields[$i])) {
if (substr($fields[$i], -1) == '*') {
if (strpos($fields[$i], '.') !== false && $fields[$i] != $alias . '.*') {
$build = explode('.', $fields[$i]);
$AssociatedModel = $model->{$build[0]};
} else {
$AssociatedModel = $model;
}
$_fields = $this->fields($AssociatedModel, $AssociatedModel->alias, array_keys($AssociatedModel->schema()));
$result = array_merge($result, $_fields);
continue;
}
$prepend = ''; $prepend = '';
if (strpos($fields[$i], 'DISTINCT') !== false) { if (strpos($fields[$i], 'DISTINCT') !== false) {
$prepend = 'DISTINCT '; $prepend = 'DISTINCT ';
@ -456,7 +470,9 @@ class DboPostgres extends DboSource {
$fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '__' . $build[1]); $fields[$i] = $prepend . $this->name($build[0]) . '.' . $this->name($build[1]) . ' AS ' . $this->name($build[0] . '__' . $build[1]);
} }
} }
$result[] = $fields[$i];
} }
return $result;
} }
return $fields; return $fields;
} }

View file

@ -84,6 +84,18 @@ class PostgresTestModel extends Model {
*/ */
var $useTable = false; var $useTable = false;
/**
* belongsTo property
*
* @var array
* @access public
*/
var $belongsTo = array(
'PostgresClientTestModel' => array(
'foreignKey' => 'client_id'
)
);
/** /**
* find method * find method
* *
@ -142,6 +154,47 @@ class PostgresTestModel extends Model {
} }
} }
/**
* PostgresClientTestModel class
*
* @package cake
* @subpackage cake.tests.cases.libs.model.datasources
*/
class PostgresClientTestModel extends Model {
/**
* name property
*
* @var string 'PostgresClientTestModel'
* @access public
*/
var $name = 'PostgresClientTestModel';
/**
* useTable property
*
* @var bool false
* @access public
*/
var $useTable = false;
/**
* schema method
*
* @access public
* @return void
*/
function schema() {
return array(
'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8', 'key' => 'primary'),
'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
'email' => array('type' => 'string', 'null' => '1', 'default' => '', 'length' => '155'),
'created' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => ''),
'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
);
}
}
/** /**
* DboPostgresTest class * DboPostgresTest class
* *
@ -227,13 +280,12 @@ class DboPostgresTest extends CakeTestCase {
} }
/** /**
* Test field and value quoting method * Test field quoting method
* *
* @access public * @access public
*/ */
function testQuoting() { function testFieldQuoting() {
$result = $this->db2->fields($this->model); $fields = array(
$expected = array(
'"PostgresTestModel"."id" AS "PostgresTestModel__id"', '"PostgresTestModel"."id" AS "PostgresTestModel__id"',
'"PostgresTestModel"."client_id" AS "PostgresTestModel__client_id"', '"PostgresTestModel"."client_id" AS "PostgresTestModel__client_id"',
'"PostgresTestModel"."name" AS "PostgresTestModel__name"', '"PostgresTestModel"."name" AS "PostgresTestModel__name"',
@ -253,15 +305,29 @@ class DboPostgresTest extends CakeTestCase {
'"PostgresTestModel"."created" AS "PostgresTestModel__created"', '"PostgresTestModel"."created" AS "PostgresTestModel__created"',
'"PostgresTestModel"."updated" AS "PostgresTestModel__updated"' '"PostgresTestModel"."updated" AS "PostgresTestModel__updated"'
); );
$result = $this->db->fields($this->model);
$expected = $fields;
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$expected = "'1.2'"; $result = $this->db->fields($this->model, null, 'PostgresTestModel.*');
$result = $this->db2->value(1.2, 'float'); $expected = $fields;
$this->assertIdentical($expected, $result); $this->assertEqual($result, $expected);
$expected = "'1,2'"; $result = $this->db->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name'));
$result = $this->db2->value('1,2', 'float'); $expected = array_merge($fields, array(
$this->assertIdentical($expected, $result); '"AnotherModel"."id" AS "AnotherModel__id"',
'"AnotherModel"."name" AS "AnotherModel__name"'));
$this->assertEqual($result, $expected);
$result = $this->db->fields($this->model, null, array('*', 'PostgresClientTestModel.*'));
$expected = array_merge($fields, array(
'"PostgresClientTestModel"."id" AS "PostgresClientTestModel__id"',
'"PostgresClientTestModel"."name" AS "PostgresClientTestModel__name"',
'"PostgresClientTestModel"."email" AS "PostgresClientTestModel__email"',
'"PostgresClientTestModel"."created" AS "PostgresClientTestModel__created"',
'"PostgresClientTestModel"."updated" AS "PostgresClientTestModel__updated"'));
$this->assertEqual($result, $expected);
} }
/** /**
@ -286,6 +352,9 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testValueQuoting() { function testValueQuoting() {
$this->assertIdentical($this->db2->value(1.2, 'float'), "'1.2'");
$this->assertEqual($this->db2->value('1,2', 'float'), "'1,2'");
$this->assertEqual($this->db2->value('0', 'integer'), "'0'"); $this->assertEqual($this->db2->value('0', 'integer'), "'0'");
$this->assertEqual($this->db2->value('', 'integer'), 'NULL'); $this->assertEqual($this->db2->value('', 'integer'), 'NULL');
$this->assertEqual($this->db2->value('', 'float'), 'NULL'); $this->assertEqual($this->db2->value('', 'float'), 'NULL');
@ -682,8 +751,11 @@ class DboPostgresTest extends CakeTestCase {
$this->db->query($this->db->dropSchema($schema1)); $this->db->query($this->db->dropSchema($schema1));
} }
/** /**
* Test it is possible to use virtual field with postgresql * Test it is possible to use virtual field with postgresql
*
* @access public
* @return void
*/ */
function testVirtualFields() { function testVirtualFields() {
$this->loadFixtures('Article', 'Comment'); $this->loadFixtures('Article', 'Comment');
@ -701,8 +773,11 @@ class DboPostgresTest extends CakeTestCase {
$this->assertEqual($result['Article']['subquery'], 6); $this->assertEqual($result['Article']['subquery'], 6);
} }
/** /**
* Tests additional order options for postgres * Tests additional order options for postgres
*
* @access public
* @return void
*/ */
function testOrderAdditionalParams() { function testOrderAdditionalParams() {
$result = $this->db->order(array('title' => 'DESC NULLS FIRST', 'body' => 'DESC')); $result = $this->db->order(array('title' => 'DESC NULLS FIRST', 'body' => 'DESC'));