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

Commit

Permalink
Merge pull request zendframework/zendframework#2258 from weierophinne…
Browse files Browse the repository at this point in the history
…y/hotfix/zf2-506

[ZF2-506] Fix mixing of positional, named parameters
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 33 deletions.
66 changes: 34 additions & 32 deletions src/Db/AbstractDb.php
Expand Up @@ -284,45 +284,47 @@ public function setSelect(DbSelect $select)
*/
public function getSelect()
{
if (null === $this->select) {
$adapter = $this->getAdapter();
$driver = $adapter->getDriver();
$platform = $adapter->getPlatform();

/**
* Build select object
*/
$select = new DbSelect();
$tableIdentifier = new TableIdentifier($this->table, $this->schema);
$select->from($tableIdentifier)->columns(
array($this->field)
if ($this->select instanceof DbSelect) {
return $this->select;
}

$adapter = $this->getAdapter();
$driver = $adapter->getDriver();
$platform = $adapter->getPlatform();

/*
* Build select object
*/
$select = new DbSelect();
$tableIdentifier = new TableIdentifier($this->table, $this->schema);
$select->from($tableIdentifier)->columns(
array($this->field)
);

// Support both named and positional parameters
if (DbDriverInterface::PARAMETERIZATION_NAMED == $driver->getPrepareType()) {
$select->where(
$platform->quoteIdentifier($this->field, true) . ' = :value'
);
} else {
$select->where(
$platform->quoteIdentifier($this->field, true) . ' = ?'
);
}

// Support both named and positional parameters
if (DbDriverInterface::PARAMETERIZATION_NAMED == $driver->getPrepareType()) {
$select->where(
$platform->quoteIdentifier($this->field, true) . ' = :value'
if ($this->exclude !== null) {
if (is_array($this->exclude)) {
$select->where->notEqualTo(
$this->exclude['field'],
$this->exclude['value']
);
} else {
$select->where(
$platform->quoteIdentifier($this->field, true) . ' = ?'
);
$select->where($this->exclude);
}

if ($this->exclude !== null) {
if (is_array($this->exclude)) {
$select->where(
$platform->quoteIdentifier($this->exclude['field'], true) .
' != ?', $this->exclude['value']
);
} else {
$select->where($this->exclude);
}
}

$this->select = $select;
}

$this->select = $select;

return $this->select;
}

Expand Down
38 changes: 37 additions & 1 deletion test/Db/RecordExistsTest.php
Expand Up @@ -10,8 +10,9 @@

namespace ZendTest\Validator\Db;

use Zend\Validator\Db\RecordExists;
use ArrayObject;
use Zend\Db\Adapter\Adapter;
use Zend\Validator\Db\RecordExists;

/**
* @category Zend
Expand Down Expand Up @@ -259,4 +260,39 @@ public function testEqualsMessageTemplates()
$this->assertAttributeEquals($validator->getOption('messageTemplates'),
'messageTemplates', $validator);
}

/**
* Test that we don't get a mix of positional and named parameters
* @group ZF2-502
*/
public function testSelectDoesNotMixPositionalAndNamedParameters()
{
if (!extension_loaded('sqlite3')) {
$this->markTestSkipped('Relies on SQLite extension');
}
$adapter = new Adapter(array(
'driver' => 'Pdo_Sqlite',
'database' => 'sqlite::memory:',
));
$validator = new RecordExists(
array(
'table' => 'users',
'schema' => 'my'
),
'field1',
array(
'field' => 'foo',
'value' => 'bar'
),
$adapter
);
$select = $validator->getSelect();
$this->assertInstanceOf('Zend\Db\Sql\Select', $select);
$string = $select->getSqlString();
if (preg_match('/:[a-zA-Z]+/', $string)) {
$this->assertNotContains(' != ?', $string);
} else {
$this->assertContains(' != ?', $string);
}
}
}

0 comments on commit 7f833d2

Please sign in to comment.