Permalink
Browse files

Correcting quoted-alias replacement in DboSource, adding (and mocking…

…) MS SQL test, fixes #5942

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8003 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
1 parent 76fba20 commit d75b063dca918e256102672771a24b725e3d3c6a @nateabele nateabele committed Jan 16, 2009
@@ -1390,7 +1390,8 @@ function update(&$model, $fields = array(), $values = null, $conditions = null)
* @access protected
*/
function _prepareUpdateFields(&$model, $fields, $quoteValues = true, $alias = false) {
- $quotedAlias = $this->startQuote . $model->alias . $this->startQuote;
+ $quotedAlias = $this->startQuote . $model->alias . $this->endQuote;
+
foreach ($fields as $field => $value) {
if ($alias && strpos($field, '.') === false) {
$quoted = $model->escapeField($field);
@@ -1404,19 +1405,20 @@ function _prepareUpdateFields(&$model, $fields, $quoteValues = true, $alias = fa
if ($value === null) {
$updates[] = $quoted . ' = NULL';
+ continue;
+ }
+ $update = $quoted . ' = ';
+
+ if ($quoteValues) {
+ $update .= $this->value($value, $model->getColumnType($field), false);
+ } elseif (!$alias) {
+ $update .= str_replace($quotedAlias . '.', '', str_replace(
+ $model->alias . '.', '', $value
+ ));
} else {
- $update = $quoted . ' = ';
- if ($quoteValues) {
- $update .= $this->value($value, $model->getColumnType($field), false);
- } elseif (!$alias) {
- $update .= str_replace($quotedAlias . '.', '', str_replace(
- $model->alias . '.', '', $value
- ));
- } else {
- $update .= $value;
- }
- $updates[] = $update;
+ $update .= $value;
}
+ $updates[] = $update;
}
return $updates;
}
@@ -1674,7 +1676,11 @@ function fields(&$model, $alias = null, $fields = array(), $quote = true) {
$dot = strpos($fields[$i], '.');
if ($dot === false) {
- $fields[$i] = $this->name($alias . '.' . $fields[$i]);
+ $prefix = !(
+ strpos($fields[$i], ' ') !== false ||
+ strpos($fields[$i], '(') !== false
+ );
+ $fields[$i] = $this->name(($prefix ? '' : '') . $alias . '.' . $fields[$i]);
} else {
$value = array();
$comma = strpos($fields[$i], ',');
@@ -37,6 +37,14 @@
* @subpackage cake.tests.cases.libs.model.datasources.dbo
*/
class DboMssqlTestDb extends DboMssql {
+
+ function __construct() {}
+
+ function connect() {
+ $this->connected = true;
+ return true;
+ }
+ function lastError() {}
/**
* simulated property
*
@@ -69,6 +77,16 @@ function _execute($sql) {
* @access protected
* @return void
*/
+ function _matchRecords(&$model, $conditions = null) {
+ return $this->conditions(array('id' => array(1, 2)));
+ }
+/**
+ * fetchAll method
+ *
+ * @param mixed $sql
+ * @access protected
+ * @return void
+ */
function fetchAll($sql, $cache = true, $modelName = null) {
$result = parent::fetchAll($sql, $cache, $modelName);
if (!empty($this->fetchAllResultsStack)) {
@@ -297,6 +315,17 @@ function testDescribe() {
$this->assertEqual($result, $expected);
}
+ function testUpdateAllSyntax() {
+ $model = ClassRegistry::init('MssqlTestModel');
+ $fields = array('MssqlTestModel.client_id' => '[MssqlTestModel].[client_id] + 1');
+ $conditions = array('MssqlTestModel.updated <' => date('2009-01-01 00:00:00'));
+ $this->db->update($model, $fields, null, $conditions);
+
+ $result = $this->db->getLastQuery();
+ $this->assertNoPattern('/MssqlTestModel/', $result);
+ $this->assertPattern('/^UPDATE \[mssql_test_models\]/', $result);
+ $this->assertPattern('/SET \[client_id\] = \[client_id\] \+ 1/', $result);
+ }
/**
* tearDown method

0 comments on commit d75b063

Please sign in to comment.