Permalink
Browse files

Merge pull request #2258 from weierophinney/hotfix/zf2-506

[ZF2-506] Fix mixing of positional, named parameters
  • Loading branch information...
EvanDotPro committed Aug 29, 2012
2 parents 475c89b + ddff7a8 commit 20a47a9b6b7c4b129864964f52a18dd8c42b85e1
Showing with 71 additions and 33 deletions.
  1. +34 −32 library/Zend/Validator/Db/AbstractDb.php
  2. +37 −1 tests/ZendTest/Validator/Db/RecordExistsTest.php
@@ -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;
}
@@ -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
@@ -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 20a47a9

Please sign in to comment.