Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Zend\Db: Insert and Update prepare & getSqlString now allow expressio…
Browse files Browse the repository at this point in the history
…ns as values
  • Loading branch information
Ralph Schindler committed Jun 22, 2012
1 parent 27cfa5a commit 0670bdc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
12 changes: 10 additions & 2 deletions library/Zend/Db/Sql/Insert.php
Expand Up @@ -167,7 +167,6 @@ public function prepareStatement(Adapter $adapter, StatementInterface $statement
$values[$cIndex] = $driver->formatParameterName($column);
$parameterContainer->offsetSet($column, $this->values[$cIndex]);
}

}

$sql = sprintf(
Expand All @@ -194,7 +193,16 @@ public function getSqlString(PlatformInterface $adapterPlatform = null)
$columns = array_map(array($adapterPlatform, 'quoteIdentifier'), $this->columns);
$columns = implode(', ', $columns);

$values = array_map(array($adapterPlatform, 'quoteValue'), $this->values);
$values = array();
foreach ($this->values as $value) {
if ($value instanceof Expression) {
$exprData = $this->processExpression($value, $adapterPlatform);
$values[] = $exprData['sql'];
} else {
$values[] = $adapterPlatform->quoteValue($value);
}
}

$values = implode(', ', $values);

return sprintf($this->specifications[self::SPECIFICATION_INSERT], $table, $columns, $values);
Expand Down
21 changes: 17 additions & 4 deletions library/Zend/Db/Sql/Update.php
Expand Up @@ -182,8 +182,16 @@ public function prepareStatement(Adapter $adapter, StatementInterface $statement
if (is_array($set)) {
$setSql = array();
foreach ($set as $column => $value) {
$parameterContainer->offsetSet($column, $value);
$setSql[] = $platform->quoteIdentifier($column) . ' = ' . $driver->formatParameterName($column);
if ($value instanceof Expression) {
$exprData = $this->processExpression($value, $platform, $driver);
$setSql[] = $platform->quoteIdentifier($column) . ' = ' . $exprData['sql'];
if (count($exprData['parameters']) > 0) {
$parameterContainer->merge($exprData['parameters']);
}
} else {
$setSql[] = $platform->quoteIdentifier($column) . ' = ' . $driver->formatParameterName($column);
$parameterContainer->offsetSet($column, $value);
}
}
$set = implode(', ', $setSql);
}
Expand Down Expand Up @@ -215,8 +223,13 @@ public function getSqlString(PlatformInterface $adapterPlatform = null)
$set = $this->set;
if (is_array($set)) {
$setSql = array();
foreach ($set as $setName => $setValue) {
$setSql[] = $adapterPlatform->quoteIdentifier($setName) . ' = ' . $adapterPlatform->quoteValue($setValue);
foreach ($set as $column => $value) {
if ($value instanceof Expression) {
$exprData = $this->processExpression($value, $adapterPlatform);
$setSql[] = $adapterPlatform->quoteIdentifier($column) . ' = ' . $exprData['sql'];
} else {
$setSql[] = $adapterPlatform->quoteIdentifier($column) . ' = ' . $adapterPlatform->quoteValue($value);
}
}
$set = implode(', ', $setSql);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Zend/Db/Sql/InsertTest.php
Expand Up @@ -82,9 +82,9 @@ public function testPrepareStatement()
public function testGetSqlString()
{
$this->insert->into('foo')
->values(array('bar' => 'baz'));
->values(array('bar' => 'baz', 'boo' => new Expression('NOW()')));

$this->assertEquals('INSERT INTO "foo" ("bar") VALUES (\'baz\')', $this->insert->getSqlString());
$this->assertEquals('INSERT INTO "foo" ("bar", "boo") VALUES (\'baz\', NOW())', $this->insert->getSqlString());
}

/**
Expand Down
14 changes: 9 additions & 5 deletions tests/Zend/Db/Sql/UpdateTest.php
@@ -1,5 +1,9 @@
<?php
namespace Zend\Db\Sql;
namespace ZendTest\Db\Sql;

use Zend\Db\Sql\Update;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;

/**
* Generated by PHPUnit_SkeletonGenerator on 2012-03-01 at 23:40:25.
Expand Down Expand Up @@ -96,10 +100,10 @@ public function testPrepareStatement()

$mockStatement->expects($this->at(1))
->method('setSql')
->with($this->equalTo('UPDATE "foo" SET "bar" = ? WHERE x = y'));
->with($this->equalTo('UPDATE "foo" SET "bar" = ?, "boo" = NOW() WHERE x = y'));

$this->update->table('foo')
->set(array('bar' => 'baz'))
->set(array('bar' => 'baz', 'boo' => new Expression('NOW()')))
->where('x = y');

$this->update->prepareStatement($mockAdapter, $mockStatement);
Expand All @@ -112,10 +116,10 @@ public function testPrepareStatement()
public function testGetSqlString()
{
$this->update->table('foo')
->set(array('bar' => 'baz'))
->set(array('bar' => 'baz', 'boo' => new Expression('NOW()')))
->where('x = y');

$this->assertEquals('UPDATE "foo" SET "bar" = \'baz\' WHERE x = y', $this->update->getSqlString());
$this->assertEquals('UPDATE "foo" SET "bar" = \'baz\', "boo" = NOW() WHERE x = y', $this->update->getSqlString());
}

/**
Expand Down

0 comments on commit 0670bdc

Please sign in to comment.