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

Feature/db sql insert (and update) expression #1561

Merged
merged 2 commits into from
Jun 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions library/Zend/Db/Sql/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @package Zend_Db
* @subpackage Sql
*/
class Insert implements SqlInterface, PreparableSqlInterface
class Insert extends AbstractSql implements SqlInterface, PreparableSqlInterface
{
/**#@+
* Constants
Expand Down Expand Up @@ -125,7 +125,6 @@ public function values(array $values, $flag = self::VALUES_SET)
public function getRawState($key = null)
{
$rawState = array(
'columns' => $this->columns,
'table' => $this->table,
'columns' => $this->columns,
'values' => $this->values
Expand Down Expand Up @@ -158,8 +157,16 @@ public function prepareStatement(Adapter $adapter, StatementInterface $statement

foreach ($this->columns as $cIndex => $column) {
$columns[$cIndex] = $platform->quoteIdentifier($column);
$values[$cIndex] = $driver->formatParameterName($column);
$parameterContainer->offsetSet($column, $this->values[$cIndex]);
if ($this->values[$cIndex] instanceof Expression) {
$exprData = $this->processExpression($this->values[$cIndex], $platform, $driver);
$values[$cIndex] = $exprData['sql'];
if (count($exprData['parameters']) > 0) {
$parameterContainer->merge($exprData['parameters']);
}
} else {
$values[$cIndex] = $driver->formatParameterName($column);
$parameterContainer->offsetSet($column, $this->values[$cIndex]);
}
}

$sql = sprintf(
Expand All @@ -186,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
Original file line number Diff line number Diff line change
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
22 changes: 8 additions & 14 deletions tests/Zend/Db/Sql/InsertTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
namespace Zend\Db\Sql;
namespace ZendTest\Db\Sql;

use Zend\Db\Sql\Insert;
use Zend\Db\Sql\Expression;

/**
* Generated by PHPUnit_SkeletonGenerator on 2012-03-01 at 23:39:00.
Expand All @@ -20,14 +23,6 @@ protected function setUp()
$this->insert = new Insert;
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}

/**
* @covers Zend\Db\Sql\Insert::into
*/
Expand Down Expand Up @@ -72,10 +67,10 @@ public function testPrepareStatement()
$mockStatement->expects($this->any())->method('getParameterContainer')->will($this->returnValue($pContainer));
$mockStatement->expects($this->at(1))
->method('setSql')
->with($this->equalTo('INSERT INTO "foo" ("bar") VALUES (?)'));
->with($this->equalTo('INSERT INTO "foo" ("bar", "boo") VALUES (?, NOW())'));

$this->insert->into('foo')
->values(array('bar' => 'baz'));
->values(array('bar' => 'baz', 'boo' => new Expression('NOW()')));

$this->insert->prepareStatement($mockAdapter, $mockStatement);
}
Expand All @@ -87,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 Expand Up @@ -126,7 +121,6 @@ public function test__isset()

/**
* @covers Zend\Db\Sql\Insert::__get
* @todo Implement test__get().
*/
public function test__get()
{
Expand Down
14 changes: 9 additions & 5 deletions tests/Zend/Db/Sql/UpdateTest.php
Original file line number Diff line number Diff line change
@@ -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