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

Commit

Permalink
Merge branch 'hotfix/76'
Browse files Browse the repository at this point in the history
Close #76
  • Loading branch information
weierophinney committed Apr 12, 2016
2 parents e396ce2 + a627361 commit a23161b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ All notable changes to this project will be documented in this file, in reverse
- [#71](https://github.com/zendframework/zend-db/pull/71) updates the `Pgsql`
adapter to allow passing the connection charset; this can be done with the
`charset` option when creating your adapter.
- [#76](https://github.com/zendframework/zend-db/pull/76) fixes the behavior of
`Zend\Db\Sql\Insert` when an array of names is used for columns to ensure the
string names are used, and not the array indices.

## 2.7.0 - 2016-02-22

Expand Down
25 changes: 21 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,38 @@ 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;
$this->columns = $this->isAssocativeArray($values)
? $values
: array_combine(array_keys($this->columns), array_values($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 array $array
* @return bool
*/
private function isAssocativeArray(array $array)
{
return array_keys($array) !== range(0, count($array) - 1);
}

/**
* Create INTO SELECT clause
*
Expand Down Expand Up @@ -160,7 +176,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 a23161b

Please sign in to comment.