Fixing quoting for complex conditions in DboSource, fixing broken DboSource tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7440 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-08-07 16:38:15 +00:00
parent 76f7d6dc43
commit b3395569cf
2 changed files with 40 additions and 10 deletions

View file

@ -1852,10 +1852,19 @@ class DboSource extends DataSource {
* @access private * @access private
*/ */
function __parseKey($model, $key, $value) { function __parseKey($model, $key, $value) {
$operatorMatch = '/^((' . join(')|(', $this->__sqlOps);
$operatorMatch .= '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is';
if (!strpos($key, ' ')) { if (!strpos($key, ' ')) {
$operator = '='; $operator = '=';
} else { } else {
$key = trim($key);
list($key, $operator) = explode(' ', $key, 2); list($key, $operator) = explode(' ', $key, 2);
if (!preg_match($operatorMatch, trim($operator))) {
$key = $key . ' ' . $operator;
list($key, $operator) = str_split($key, strrpos($key, ' '));
}
} }
$type = (is_object($model) ? $model->getColumnType($key) : null); $type = (is_object($model) ? $model->getColumnType($key) : null);
$null = ($value === null || (is_array($value) && empty($value))); $null = ($value === null || (is_array($value) && empty($value)));
@ -1864,7 +1873,7 @@ class DboSource extends DataSource {
$data = $this->conditionKeysToString(array($operator => array($key => $value)), true, $model); $data = $this->conditionKeysToString(array($operator => array($key => $value)), true, $model);
return $data[0]; return $data[0];
} }
if (!preg_match('/^((' . join(')|(', $this->__sqlOps) . '\\x20)|<[>=]?(?![^>]+>)\\x20?|[>=!]{1,3}(?!<)\\x20?)/is', trim($operator))) { if (!preg_match($operatorMatch, trim($operator))) {
$operator .= ' ='; $operator .= ' =';
} }

View file

@ -91,7 +91,7 @@ class TestModel extends CakeTestModel {
* @return void * @return void
*/ */
function find($conditions = null, $fields = null, $order = null, $recursive = null) { function find($conditions = null, $fields = null, $order = null, $recursive = null) {
return $conditions; return array($conditions, $fields);
} }
/** /**
* findAll method * findAll method
@ -2524,7 +2524,7 @@ class DboSourceTest extends CakeTestCase {
$result = $this->testDb->conditions(array( $result = $this->testDb->conditions(array(
'(Stats.clicks * 100) / Stats.views >' => 50 '(Stats.clicks * 100) / Stats.views >' => 50
)); ));
$expected = " WHERE (`Stats.clicks` * 100) / `Stats`.`views` > 50"; $expected = " WHERE (`Stats`.`clicks` * 100) / `Stats`.`views` > 50";
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
/** /**
@ -3057,27 +3057,47 @@ class DboSourceTest extends CakeTestCase {
*/ */
function testMagicMethodQuerying() { function testMagicMethodQuerying() {
$result = $this->testDb->query('findByFieldName', array('value'), $this->Model); $result = $this->testDb->query('findByFieldName', array('value'), $this->Model);
$expected = array('TestModel.field_name' => 'value'); $expected = array('first', array(
'conditions' => array('TestModel.field_name' => 'value'),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->query('findAllByFieldName', array('value'), $this->Model); $result = $this->testDb->query('findAllByFieldName', array('value'), $this->Model);
$expected = array('TestModel.field_name' => 'value'); $expected = array('all', array(
'conditions' => array('TestModel.field_name' => 'value'),
'fields' => null, 'order' => null, 'limit' => null,
'page' => null, 'recursive' => null
));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->query('findAllById', array('a'), $this->Model); $result = $this->testDb->query('findAllById', array('a'), $this->Model);
$expected = array('TestModel.id' => 'a'); $expected = array('all', array(
'conditions' => array('TestModel.id' => 'a'),
'fields' => null, 'order' => null, 'limit' => null,
'page' => null, 'recursive' => null
));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model); $result = $this->testDb->query('findByFieldName', array(array('value1', 'value2', 'value3')), $this->Model);
$expected = array('TestModel.field_name' => array('value1', 'value2', 'value3')); $expected = array('first', array(
'conditions' => array('TestModel.field_name' => array('value1', 'value2', 'value3')),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->query('findByFieldName', array(null), $this->Model); $result = $this->testDb->query('findByFieldName', array(null), $this->Model);
$expected = array('TestModel.field_name' => null); $expected = array('first', array(
'conditions' => array('TestModel.field_name' => null),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->query('findByFieldName', array('= a'), $this->Model); $result = $this->testDb->query('findByFieldName', array('= a'), $this->Model);
$expected = array('TestModel.field_name' => '= a'); $expected = array('first', array(
'conditions' => array('TestModel.field_name' => '= a'),
'fields' => null, 'order' => null, 'recursive' => null
));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->testDb->query('findByFieldName', array(), $this->Model); $result = $this->testDb->query('findByFieldName', array(), $this->Model);
@ -3475,8 +3495,9 @@ class DboSourceTest extends CakeTestCase {
* @return void * @return void
*/ */
function testReconnect() { function testReconnect() {
$this->testDb->reconnect(); $this->testDb->reconnect(array('prefix' => 'foo'));
$this->assertTrue($this->testDb->connected); $this->assertTrue($this->testDb->connected);
$this->assertEqual($this->testDb->config['prefix'], 'foo');
} }
/** /**
* testRealQueries method * testRealQueries method