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

Commit

Permalink
Merge pull request #76 from settermjd/feature/fix-generated-sql-when-…
Browse files Browse the repository at this point in the history
…columns-and-values-methods-are-used

Refactor Insert::values() method to properly merge values with columns
  • Loading branch information
weierophinney committed Apr 12, 2016
2 parents e396ce2 + 5a26129 commit ac7e74a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
28 changes: 24 additions & 4 deletions src/Sql/Insert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace Zend\Db\Sql;

use Zend\Db\Adapter\Driver\DriverInterface;
use Zend\Db\Adapter\ParameterContainer;
use Zend\Db\Adapter\Platform\PlatformInterface;
use Zend\Db\Adapter\Driver\DriverInterface;

class Insert extends AbstractPreparableSql
{
Expand Down Expand Up @@ -106,22 +106,41 @@ public function values($values, $flag = self::VALUES_SET)
'values() expects an array of values or Zend\Db\Sql\Select instance'
);
}

if ($this->select && $flag == self::VALUES_MERGE) {
throw new Exception\InvalidArgumentException(
'An array of values cannot be provided with the merge flag when a Zend\Db\Sql\Select instance already exists as the value source'
);
}

if ($flag == self::VALUES_SET) {
$this->columns = $values;
if (!$this->isAssocativeArray($values)) {
$this->columns = array_combine(
array_keys($this->columns), array_values($values)
);
} else {
$this->columns = $values;
}
} else {
foreach ($values as $column=>$value) {
foreach ($values as $column => $value) {
$this->columns[$column] = $value;
}
}
return $this;
}


/**
* Simple test for an associative array
* @link http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
* @param $arr
* @return bool
*/
private function isAssocativeArray($arr)
{
return array_keys($arr) !== range(0, count($arr) - 1);
}

/**
* Create INTO SELECT clause
*
Expand Down Expand Up @@ -160,7 +179,8 @@ protected function processInsert(PlatformInterface $platform, DriverInterface $d

$columns = [];
$values = [];
foreach ($this->columns as $column=>$value) {

foreach ($this->columns as $column => $value) {
$columns[] = $platform->quoteIdentifier($column);
if (is_scalar($value) && $parameterContainer) {
$values[] = $driver->formatParameterName($column);
Expand Down
22 changes: 19 additions & 3 deletions test/Sql/InsertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public function testInto()
*/
public function testColumns()
{
$this->insert->columns(['foo', 'bar']);
$this->assertEquals(['foo', 'bar'], $this->insert->getRawState('columns'));
$columns = ['foo', 'bar'];
$this->insert->columns($columns);
$this->assertEquals($columns, $this->insert->getRawState('columns'));
}

/**
Expand Down Expand Up @@ -218,8 +219,23 @@ public function testGetSqlString()

// with Select and columns
$this->insert->columns(['col1', 'col2']);
$this->assertEquals(
'INSERT INTO "foo" ("col1", "col2") SELECT "bar".* FROM "bar"',
$this->insert->getSqlString(new TrustingSql92Platform())
);
}

$this->assertEquals('INSERT INTO "foo" ("col1", "col2") SELECT "bar".* FROM "bar"', $this->insert->getSqlString(new TrustingSql92Platform()));
public function testGetSqlStringUsingColumnsAndValuesMethods()
{
// With columns() and values()
$this->insert
->into('foo')
->columns(['col1', 'col2', 'col3'])
->values(['val1', 'val2', 'val3']);
$this->assertEquals(
'INSERT INTO "foo" ("col1", "col2", "col3") VALUES (\'val1\', \'val2\', \'val3\')',
$this->insert->getSqlString(new TrustingSql92Platform())
);
}

/**
Expand Down

0 comments on commit ac7e74a

Please sign in to comment.