Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 30 additions & 33 deletions library/Zend/Db/Sql/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand Down Expand Up @@ -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
Expand All @@ -67,6 +68,8 @@ public function __construct($table = null)
$this->table($table);
}
$this->where = new Where();
$this->set = new PriorityList();
$this->set->isLIFO(false);
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -270,5 +266,6 @@ public function __get($name)
public function __clone()
{
$this->where = clone $this->where;
$this->set = clone $this->set;
}
}
23 changes: 22 additions & 1 deletion tests/ZendTest/Db/Sql/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

/**
Expand Down