Fixing redundant SQL condition generation in DboSource::conditionKeysToString(), fixing SQL ORDER parsing (Ticket #2266) and updating test cases

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@4644 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2007-03-22 00:26:42 +00:00
parent 77d789c0ac
commit 2a095d9ea6
4 changed files with 77 additions and 41 deletions

View file

@ -186,7 +186,6 @@ class DboSource extends DataSource {
return $this->fetchAll($args[0]);
} elseif (count($args) > 1 && (strpos(low($args[0]), 'findby') === 0 || strpos(low($args[0]), 'findallby') === 0)) {
$params = $args[1];
if (strpos(strtolower($args[0]), 'findby') === 0) {
@ -936,7 +935,7 @@ class DboSource extends DataSource {
case 'belongsTo':
if ($external) {
if ($type == 'hasOne') {
$conditions = $this->__mergeConditions($queryData['conditions'], array("{$alias}.{$assocData['foreignKey']}" => '{$__cakeForeignKey__$}'));
$conditions = $this->__mergeConditions($queryData['conditions'], array("{$alias}.{$assocData['foreignKey']}" => '{$__cakeID__$}'));
} elseif ($type == 'belongsTo') {
$conditions = $this->__mergeConditions($assocData['conditions'], array("{$alias}.{$linkModel->primaryKey}" => '{$__cakeForeignKey__$}'));
}
@ -1432,7 +1431,12 @@ class DboSource extends DataSource {
$data[strlen($data) - 2] = ')';
}
} else {
$out[] = '(' . join(') AND (', $this->conditionKeysToString($value, $quoteValues)) . ')';
$ret = $this->conditionKeysToString($value, $quoteValues);
if (count($ret) > 1) {
$out[] = '(' . join(') AND (', $ret) . ')';
} elseif (isset($ret[0])) {
$out[] = $ret[0];
}
}
} elseif (is_numeric($key)) {
$data = ' ' . $value;
@ -1468,6 +1472,7 @@ class DboSource extends DataSource {
if ($data != null) {
$out[] = $data;
$data = null;
}
}
$c++;
@ -1559,7 +1564,7 @@ class DboSource extends DataSource {
$dir = '';
}
$key = trim($this->name($key) . ' ' . $dir);
$key = trim($this->name(trim($key)) . ' ' . trim($dir));
}
$order[] = $this->order($key . $value);
}

View file

@ -43,30 +43,31 @@
class AclNodeTest extends UnitTestCase {
function setUp() {
$this->aro =& new Aro();
//$this->Aro =& new Aro();
}
function testNodeNesting() {
$this->aro->create(1, null, 'Food');
$this->aro->create(2, null, 'Fruit');
$this->aro->create(3, null, 'Red');
$this->aro->create(4, null, 'Cherry');
$this->aro->create(5, null, 'Yellow');
$this->aro->create(6, null, 'Banana');
$this->aro->create(7, null, 'Meat');
$this->aro->create(8, null, 'Beef');
$this->aro->create(9, null, 'Pork');
return;
$this->Aro->create(1, null, 'Food');
$this->Aro->create(2, null, 'Fruit');
$this->Aro->create(3, null, 'Red');
$this->Aro->create(4, null, 'Cherry');
$this->Aro->create(5, null, 'Yellow');
$this->Aro->create(6, null, 'Banana');
$this->Aro->create(7, null, 'Meat');
$this->Aro->create(8, null, 'Beef');
$this->Aro->create(9, null, 'Pork');
$this->aro->setParent('Food', 'Meat');
$this->aro->setParent('Food', 'Fruit');
$this->Aro->setParent('Food', 'Meat');
$this->Aro->setParent('Food', 'Fruit');
$this->aro->setParent('Fruit', 'Yellow');
$this->aro->setParent('Yellow', 'Banana');
$this->aro->setParent('Fruit', 'Red');
$this->aro->setParent('Red', 'Cherry');
$this->Aro->setParent('Fruit', 'Yellow');
$this->Aro->setParent('Yellow', 'Banana');
$this->Aro->setParent('Fruit', 'Red');
$this->Aro->setParent('Red', 'Cherry');
$this->aro->setParent('Meat', 'Pork');
$this->aro->setParent('Meat', 'Beef');
$this->Aro->setParent('Meat', 'Pork');
$this->Aro->setParent('Meat', 'Beef');
}
}

View file

@ -48,6 +48,10 @@ class TestModel extends Model {
return $conditions;
}
function findAll($conditions = null, $fields = null, $order = null, $recursive = null) {
return $conditions;
}
function loadInfo() {
return new Set(array(
array('name' => 'id', 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
@ -226,7 +230,6 @@ class TestModel7 extends Model {
array('name' => 'updated', 'type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
));
}
return $this->_tableInfo;
}
}
@ -263,6 +266,8 @@ class DboSourceTest extends UnitTestCase {
$this->db =& new DboTest($config->default);
$this->db->fullDebug = false;
$this->model = new TestModel();
$db =& ConnectionManager::getDataSource($this->model->useDbConfig);
$db->fullDebug = false;
}
function tearDown() {
@ -516,8 +521,8 @@ class DboSourceTest extends UnitTestCase {
$result = $this->db->generateAssociationQuery($this->model, $params['linkModel'], $params['type'], $params['assoc'], $params['assocData'], $queryData, $params['external'], $resultSet);
$this->assertPattern('/^SELECT\s+`TestModel7`\.`id`, `TestModel7`\.`name`, `TestModel7`\.`created`, `TestModel7`\.`updated`\s+/', $result);
$this->assertPattern('/\s+FROM\s+`test_model7` AS `TestModel7`\s+JOIN\s+`test_model4_test_model7`+/', $result);
$this->assertPattern('/\s+ON\s+(?:\()`test_model4_test_model7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}(?:\))?/', $result);
$this->assertPattern('/\s+AND\s+(?:\()`test_model4_test_model7`\.`test_model7_id`\s+=\s+`TestModel7`.`id`(?:\))?\s+WHERE\s+/', $result);
$this->assertPattern('/\s+ON\s+(?:\()?`test_model4_test_model7`\.`test_model4_id`\s+=\s+{\$__cakeID__\$}(?:\))?/', $result);
$this->assertPattern('/\s+AND\s+(?:\()?`test_model4_test_model7`\.`test_model7_id`\s+=\s+`TestModel7`.`id`(?:\))?\s+WHERE\s+/', $result);
$result = $this->db->generateAssociationQuery($this->model, $null, null, null, null, $queryData, false, $null);
$this->assertPattern('/^SELECT\s+`TestModel4`\.`id`, `TestModel4`\.`name`, `TestModel4`\.`created`, `TestModel4`\.`updated`\s+/', $result);
@ -687,15 +692,15 @@ class DboSourceTest extends UnitTestCase {
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array('or' => array('score' => 'BETWEEN 4 AND 5', array('score' => '> 20')) ));
$expected = " WHERE (`score` > 20) OR (`score` BETWEEN '4' AND '5')";
$expected = " WHERE (`score` BETWEEN '4' AND '5') OR (`score` > 20)";
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array('and' => array( 'score' => 'BETWEEN 4 AND 5', array('score' => '> 20')) ));
$expected = " WHERE (`score` > 20) AND (`score` BETWEEN '4' AND '5')";
$expected = " WHERE (`score` BETWEEN '4' AND '5') AND (`score` > 20)";
$this->assertEqual($result, $expected);
$result = $this->db->conditions(array('published' => 1, 'or' => array( 'score' => '< 2', array('score' => '> 20')) ));
$expected = " WHERE `published` = 1 AND (`score` > 20) OR (`score` < 2)";
$result = $this->db->conditions(array('published' => 1, 'or' => array('score' => '< 2', array('score' => '> 20')) ));
$expected = " WHERE `published` = 1 AND (`score` < 2) OR (`score` > 20)";
$this->assertEqual($result, $expected);
}
@ -995,11 +1000,15 @@ class DboSourceTest extends UnitTestCase {
$expected = array('TestModel.field_name' => '= value');
$this->assertEqual($result, $expected);
/*
$result = $this->db->query('findAllByFieldName', array('value'), $this->model);
$expected = array('TestModel.field_name' => '= value');
$this->assertEqual($result, $expected);
/*$this->db->fullDebug = true;
$result = $this->db->query('findAllById', array('a'), $this->model);
$expected = array('TestModel.id' => '= value');
$this->assertEqual($result, $expected);
*/
$this->db->fullDebug = false;*/
$result = $this->db->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->model);
$expected = array('TestModel.field_name' => array('value1', 'value2', 'value3'));
@ -1018,6 +1027,33 @@ class DboSourceTest extends UnitTestCase {
$result = $this->db->order("ADDTIME(Event.time_begin, '-06:00:00') ASC");
$expected = " ORDER BY ADDTIME(`Event`.`time_begin`, '-06:00:00') ASC";
$this->assertEqual($result, $expected);
$result = $this->db->order("title, id");
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
$result = $this->db->order("title desc, id desc");
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result);
$result = $this->db->order(array("title desc, id desc"));
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+desc,\s+`id`\s+desc\s*$/', $result);
$result = $this->db->order(array("title", "id"));
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
$result = $this->db->order(array(array('title'), array('id')));
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC,\s+`id`\s+ASC\s*$/', $result);
$result = $this->db->order(array("Post.title" => 'asc', "Post.id" => 'desc'));
$this->assertPattern('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result);
$result = $this->db->order(array(array("Post.title" => 'asc', "Post.id" => 'desc')));
$this->assertPattern('/^\s*ORDER BY\s+`Post`.`title`\s+asc,\s+`Post`.`id`\s+desc\s*$/', $result);
$result = $this->db->order(array("title"));
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result);
$result = $this->db->order(array(array("title")));
$this->assertPattern('/^\s*ORDER BY\s+`title`\s+ASC\s*$/', $result);
}
}

View file

@ -148,6 +148,9 @@ class ModelTest extends UnitTestCase {
if (!isset($this->db) || !$this->db->isConnected()) {
restore_error_handler();
@$db =& ConnectionManager::getDataSource('test');
if (is_object($db) && isset($db->fullDebug)) {
$db->fullDebug = false;
}
set_error_handler('simpleTestErrorHandler');
if (!$db->isConnected()) {
@ -157,9 +160,7 @@ class ModelTest extends UnitTestCase {
$config = $db->config;
$config['prefix'] .= 'test_suite_';
ConnectionManager::create('test_suite', $config);
$this->db =& ConnectionManager::getDataSource('test_suite');
$this->db =& ConnectionManager::create('test_suite', $config);
$this->db->fullDebug = false;
} else {
$config = $this->db->config;
@ -705,8 +706,6 @@ class ModelTest extends UnitTestCase {
);
$this->assertEqual($result, $expected);
$this->db->fullDebug = true;
$result = $this->model->findAll(array('Article.user_id' => 3), null, null, null, 1, 2);
$expected = array (
array (
@ -746,11 +745,6 @@ class ModelTest extends UnitTestCase {
)
)
);
/*pr($result);
pr($expected);
pr(var_dump($expected[0]['Comment'][0]['Attachment']));
pr(var_dump($result[0]['Comment'][0]['Attachment']));
pr(array_diff_recursive($result, $expected));*/
$this->assertEqual($expected, $result);
}