From 67c8006d931ceeff1560f26e43c8f00b505a7358 Mon Sep 17 00:00:00 2001 From: turrsis Date: Sat, 16 Nov 2013 10:00:34 +0200 Subject: [PATCH] added sortable set list http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-update.html --- library/Zend/Db/Sql/Update.php | 63 +++++++++++++--------------- tests/ZendTest/Db/Sql/UpdateTest.php | 23 +++++++++- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/library/Zend/Db/Sql/Update.php b/library/Zend/Db/Sql/Update.php index 1a0e288d1d2..d9968ae1b83 100644 --- a/library/Zend/Db/Sql/Update.php +++ b/library/Zend/Db/Sql/Update.php @@ -14,6 +14,7 @@ use Zend\Db\Adapter\Platform\PlatformInterface; use Zend\Db\Adapter\Platform\Sql92; use Zend\Db\Adapter\StatementContainerInterface; +use Zend\Stdlib\PriorityList; /** * @@ -47,9 +48,9 @@ class Update extends AbstractSql implements SqlInterface, PreparableSqlInterface protected $emptyWhereProtection = true; /** - * @var array + * @var PriorityList */ - protected $set = array(); + protected $set; /** * @var string|Where @@ -67,6 +68,8 @@ public function __construct($table = null) $this->table($table); } $this->where = new Where(); + $this->set = new PriorityList(); + $this->set->isLIFO(false); } /** @@ -96,16 +99,15 @@ public function set(array $values, $flag = self::VALUES_SET) } if ($flag == self::VALUES_SET) { - $this->set = array(); + $this->set->clear(); } - + $priority = is_numeric($flag) ? $flag : 0; foreach ($values as $k => $v) { if (!is_string($k)) { throw new Exception\InvalidArgumentException('set() expects a string for the value key'); } - $this->set[$k] = $v; + $this->set->insert($k, $v, $priority); } - return $this; } @@ -132,7 +134,7 @@ public function getRawState($key = null) $rawState = array( 'emptyWhereProtection' => $this->emptyWhereProtection, 'table' => $this->table, - 'set' => $this->set, + 'set' => $this->set->toArray(), 'where' => $this->where ); return (isset($key) && array_key_exists($key, $rawState)) ? $rawState[$key] : $rawState; @@ -170,21 +172,18 @@ public function prepareStatement(AdapterInterface $adapter, StatementContainerIn $table = $platform->quoteIdentifier($schema) . $platform->getIdentifierSeparator() . $table; } - $set = $this->set; - if (is_array($set)) { - $setSql = array(); - foreach ($set as $column => $value) { - if ($value instanceof Expression) { - $exprData = $this->processExpression($value, $platform, $driver); - $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $exprData->getSql(); - $parameterContainer->merge($exprData->getParameterContainer()); - } else { - $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $driver->formatParameterName($column); - $parameterContainer->offsetSet($column, $value); - } + $setSql = array(); + foreach ($this->set as $column => $value) { + if ($value instanceof Expression) { + $exprData = $this->processExpression($value, $platform, $driver); + $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $exprData->getSql(); + $parameterContainer->merge($exprData->getParameterContainer()); + } else { + $setSql[] = $platform->quoteIdentifier($column) . ' = ' . $driver->formatParameterName($column); + $parameterContainer->offsetSet($column, $value); } - $set = implode(', ', $setSql); } + $set = implode(', ', $setSql); $sql = sprintf($this->specifications[static::SPECIFICATION_UPDATE], $table, $set); @@ -220,21 +219,18 @@ public function getSqlString(PlatformInterface $adapterPlatform = null) $table = $adapterPlatform->quoteIdentifier($schema) . $adapterPlatform->getIdentifierSeparator() . $table; } - $set = $this->set; - if (is_array($set)) { - $setSql = array(); - foreach ($set as $column => $value) { - if ($value instanceof Expression) { - $exprData = $this->processExpression($value, $adapterPlatform); - $setSql[] = $adapterPlatform->quoteIdentifier($column) . ' = ' . $exprData->getSql(); - } elseif ($value === null) { - $setSql[] = $adapterPlatform->quoteIdentifier($column) . ' = NULL'; - } else { - $setSql[] = $adapterPlatform->quoteIdentifier($column) . ' = ' . $adapterPlatform->quoteValue($value); - } + $setSql = array(); + foreach ($this->set as $column => $value) { + if ($value instanceof ExpressionInterface) { + $exprData = $this->processExpression($value, $adapterPlatform); + $setSql[] = $adapterPlatform->quoteIdentifier($column) . ' = ' . $exprData->getSql(); + } elseif ($value === null) { + $setSql[] = $adapterPlatform->quoteIdentifier($column) . ' = NULL'; + } else { + $setSql[] = $adapterPlatform->quoteIdentifier($column) . ' = ' . $adapterPlatform->quoteValue($value); } - $set = implode(', ', $setSql); } + $set = implode(', ', $setSql); $sql = sprintf($this->specifications[static::SPECIFICATION_UPDATE], $table, $set); if ($this->where->count() > 0) { @@ -270,5 +266,6 @@ public function __get($name) public function __clone() { $this->where = clone $this->where; + $this->set = clone $this->set; } } diff --git a/tests/ZendTest/Db/Sql/UpdateTest.php b/tests/ZendTest/Db/Sql/UpdateTest.php index cb7818f8979..4a854a98101 100644 --- a/tests/ZendTest/Db/Sql/UpdateTest.php +++ b/tests/ZendTest/Db/Sql/UpdateTest.php @@ -68,7 +68,28 @@ public function testConstruct() public function testSet() { $this->update->set(array('foo' => 'bar')); - $this->assertEquals(array('foo' => 'bar'), $this->readAttribute($this->update, 'set')); + $this->assertEquals(array('foo' => 'bar'), $this->update->getRawState('set')); + } + + /** + * @covers Zend\Db\Sql\Update::set + */ + public function testSortableSet() + { + $this->update->set(array( + 'two' => 'с_two', + 'three' => 'с_three', + )); + $this->update->set(array('one' => 'с_one'), 10); + + $this->assertEquals( + array( + 'one' => 'с_one', + 'two' => 'с_two', + 'three' => 'с_three', + ), + $this->update->getRawState('set') + ); } /**