Permalink
Browse files

Merge branch 'hotfix/3680'

Close #3680
Close #3658
Fixes #3656
  • Loading branch information...
2 parents 7384965 + 66967fb commit 2199f5d3c226327f2212b39d631068dfbc8a7979 @weierophinney weierophinney committed Feb 5, 2013
@@ -12,7 +12,8 @@
use Traversable;
use Zend\Db\Adapter\Adapter as DbAdapter;
use Zend\Db\Adapter\Driver\DriverInterface as DbDriverInterface;
-use Zend\Db\Sql\Select as DbSelect;
+use Zend\Db\Sql\Sql;
+use Zend\Db\Sql\Select;
use Zend\Db\Sql\TableIdentifier;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\AbstractValidator;
@@ -40,7 +41,7 @@
/**
* Select object to use. can be set, or will be auto-generated
*
- * @var DbSelect
+ * @var Select
*/
protected $select;
@@ -85,14 +86,14 @@
* 'exclude' => An optional where clause or field/value pair to exclude from the query
* 'adapter' => An optional database adapter to use
*
- * @param array|Traversable|DbSelect $options Options to use for this validator
+ * @param array|Traversable|Select $options Options to use for this validator
* @throws \Zend\Validator\Exception\InvalidArgumentException
*/
public function __construct($options = null)
{
parent::__construct($options);
- if ($options instanceof DbSelect) {
+ if ($options instanceof Select) {
$this->setSelect($options);
return;
}
@@ -261,10 +262,10 @@ public function setSchema($schema)
/**
* Sets the select object to be used by the validator
*
- * @param DbSelect $select
+ * @param Select $select
* @return self Provides a fluent interface
*/
- public function setSelect(DbSelect $select)
+ public function setSelect(Select $select)
{
$this->select = $select;
return $this;
@@ -276,37 +277,19 @@ public function setSelect(DbSelect $select)
* then it will auto-generate one from the given table,
* schema, field, and adapter options.
*
- * @return DbSelect The Select object which will be used
+ * @return Select The Select object which will be used
*/
public function getSelect()
{
- if ($this->select instanceof DbSelect) {
+ if ($this->select instanceof Select) {
return $this->select;
}
- $adapter = $this->getAdapter();
- $driver = $adapter->getDriver();
- $platform = $adapter->getPlatform();
-
- /*
- * Build select object
- */
- $select = new DbSelect();
+ // Build select object
+ $select = new Select();
$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) . ' = ?'
- );
- }
+ $select->from($tableIdentifier)->columns(array($this->field));
+ $select->where->equalTo($this->field, null);
if ($this->exclude !== null) {
if (is_array($this->exclude)) {
@@ -332,10 +315,13 @@ public function getSelect()
*/
protected function query($value)
{
- $adapter = $this->getAdapter();
- $statement = $adapter->createStatement();
- $this->getSelect()->prepareStatement($adapter, $statement);
-
- return $statement->execute(array('value' => $value))->current();
+ $sql = new Sql($this->getAdapter());
+ $select = $this->getSelect();
+ $statement = $sql->prepareStatementForSqlObject($select);
+ $parameters = $statement->getParameterContainer();
+ $parameters['where1'] = $value;
+ $result = $statement->execute();
+
+ return $result->current();
}
}
@@ -11,6 +11,7 @@
namespace ZendTest\Validator\Db;
use Zend\Validator\Db\NoRecordExists;
+use Zend\Db\Adapter\ParameterContainer;
use ArrayObject;
/**
@@ -37,21 +38,25 @@ protected function getMockHasResult()
$mockHasResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface');
$mockHasResult->expects($this->any())
- ->method('current')
- ->will($this->returnValue($mockHasResultRow));
+ ->method('current')
+ ->will($this->returnValue($mockHasResultRow));
$mockHasResultStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$mockHasResultStatement->expects($this->any())
- ->method('execute')
- ->will($this->returnValue($mockHasResult));
+ ->method('execute')
+ ->will($this->returnValue($mockHasResult));
+
+ $mockHasResultStatement->expects($this->any())
+ ->method('getParameterContainer')
+ ->will($this->returnValue(new ParameterContainer()));
$mockHasResultDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockHasResultDriver->expects($this->any())
- ->method('createStatement')
- ->will($this->returnValue($mockHasResultStatement));
+ ->method('createStatement')
+ ->will($this->returnValue($mockHasResultStatement));
$mockHasResultDriver->expects($this->any())
- ->method('getConnection')
- ->will($this->returnValue($mockConnection));
+ ->method('getConnection')
+ ->will($this->returnValue($mockConnection));
return $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockHasResultDriver));
}
@@ -68,21 +73,25 @@ protected function getMockNoResult()
$mockNoResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface');
$mockNoResult->expects($this->any())
- ->method('current')
- ->will($this->returnValue(null));
+ ->method('current')
+ ->will($this->returnValue(null));
$mockNoResultStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$mockNoResultStatement->expects($this->any())
- ->method('execute')
- ->will($this->returnValue($mockNoResult));
+ ->method('execute')
+ ->will($this->returnValue($mockNoResult));
+
+ $mockNoResultStatement->expects($this->any())
+ ->method('getParameterContainer')
+ ->will($this->returnValue(new ParameterContainer()));
$mockNoResultDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockNoResultDriver->expects($this->any())
- ->method('createStatement')
- ->will($this->returnValue($mockNoResultStatement));
+ ->method('createStatement')
+ ->will($this->returnValue($mockNoResultStatement));
$mockNoResultDriver->expects($this->any())
- ->method('getConnection')
- ->will($this->returnValue($mockConnection));
+ ->method('getConnection')
+ ->will($this->returnValue($mockConnection));
return $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockNoResultDriver));
}
@@ -12,6 +12,8 @@
use ArrayObject;
use Zend\Db\Adapter\Adapter;
+use Zend\Db\Adapter\ParameterContainer;
+use Zend\Db\Sql\Sql;
use Zend\Validator\Db\RecordExists;
/**
@@ -38,21 +40,25 @@ protected function getMockHasResult()
$mockHasResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface');
$mockHasResult->expects($this->any())
- ->method('current')
- ->will($this->returnValue($mockHasResultRow));
+ ->method('current')
+ ->will($this->returnValue($mockHasResultRow));
$mockHasResultStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$mockHasResultStatement->expects($this->any())
- ->method('execute')
- ->will($this->returnValue($mockHasResult));
+ ->method('execute')
+ ->will($this->returnValue($mockHasResult));
+
+ $mockHasResultStatement->expects($this->any())
+ ->method('getParameterContainer')
+ ->will($this->returnValue(new ParameterContainer()));
$mockHasResultDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockHasResultDriver->expects($this->any())
- ->method('createStatement')
- ->will($this->returnValue($mockHasResultStatement));
+ ->method('createStatement')
+ ->will($this->returnValue($mockHasResultStatement));
$mockHasResultDriver->expects($this->any())
- ->method('getConnection')
- ->will($this->returnValue($mockConnection));
+ ->method('getConnection')
+ ->will($this->returnValue($mockConnection));
return $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockHasResultDriver));
}
@@ -69,21 +75,25 @@ protected function getMockNoResult()
$mockNoResult = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface');
$mockNoResult->expects($this->any())
- ->method('current')
- ->will($this->returnValue(null));
+ ->method('current')
+ ->will($this->returnValue(null));
$mockNoResultStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$mockNoResultStatement->expects($this->any())
- ->method('execute')
- ->will($this->returnValue($mockNoResult));
+ ->method('execute')
+ ->will($this->returnValue($mockNoResult));
+
+ $mockNoResultStatement->expects($this->any())
+ ->method('getParameterContainer')
+ ->will($this->returnValue(new ParameterContainer()));
$mockNoResultDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockNoResultDriver->expects($this->any())
- ->method('createStatement')
- ->will($this->returnValue($mockNoResultStatement));
+ ->method('createStatement')
+ ->will($this->returnValue($mockNoResultStatement));
$mockNoResultDriver->expects($this->any())
- ->method('getConnection')
- ->will($this->returnValue($mockConnection));
+ ->method('getConnection')
+ ->will($this->returnValue($mockConnection));
return $this->getMock('Zend\Db\Adapter\Adapter', null, array($mockNoResultDriver));
}
@@ -232,28 +242,6 @@ public function testSelectAcknowledgesTableAndSchema()
$this->assertEquals(array('users','my'), $table->getTableAndSchema());
}
- /**
- * @group ZF-10642
- */
- public function testCreatesQueryBasedOnNamedOrPositionalAvailability()
- {
- $this->markTestIncomplete('This test (and code) need to be refactored to the new Zend\Db');
-
- $adapterHasResult = $this->getMockHasResult();
-
- //$adapterHasResult->setSupportsParametersValues(array('named' => false, 'positional' => true));
- $validator = new RecordExists('users', 'field1', null, $adapterHasResult);
- $validator->isValid('foo');
- $wherePart = $validator->getSelect()->getPart('where');
- $this->assertEquals('("field1" = ?)', $wherePart[0]);
-
- //$adapterHasResult->setSupportsParametersValues(array('named' => true, 'positional' => true));
- $validator = new RecordExists('users', 'field1', null, $adapterHasResult);
- $validator->isValid('foo');
- $wherePart = $validator->getSelect()->getPart('where');
- $this->assertEquals('("field1" = :value)', $wherePart[0]);
- }
-
public function testEqualsMessageTemplates()
{
$validator = new RecordExists('users', 'field1');
@@ -262,18 +250,10 @@ public function testEqualsMessageTemplates()
}
/**
- * Test that we don't get a mix of positional and named parameters
- * @group ZF2-502
+ * @testdox Zend\Validator\Db\RecordExists::getSelect
*/
- public function testSelectDoesNotMixPositionalAndNamedParameters()
+ public function testGetSelect()
{
- 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',
@@ -284,15 +264,16 @@ public function testSelectDoesNotMixPositionalAndNamedParameters()
'field' => 'foo',
'value' => 'bar'
),
- $adapter
+ $this->getMockHasResult()
);
$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);
- }
+ $this->assertEquals('SELECT "my"."users"."field1" AS "field1" FROM "my"."users" WHERE "field1" = \'\' AND "foo" != \'bar\'', $select->getSqlString());
+
+ $sql = new Sql($this->getMockHasResult());
+ $statement = $sql->prepareStatementForSqlObject($select);
+ $parameters = $statement->getParameterContainer();
+ $this->assertNull($parameters['where1']);
+ $this->assertEquals($parameters['where2'], 'bar');
}
}

0 comments on commit 2199f5d

Please sign in to comment.