Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Portability wrapper _defaultFetchMode #348

Merged
merged 3 commits into from
Aug 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Connection implements DriverConnection
/**
* @var integer
*/
private $_defaultFetchMode = PDO::FETCH_ASSOC;
protected $defaultFetchMode = PDO::FETCH_ASSOC;

/**
* Initializes a new instance of the Connection class.
Expand Down Expand Up @@ -373,7 +373,7 @@ public function connect()
*/
public function setFetchMode($fetchMode)
{
$this->_defaultFetchMode = $fetchMode;
$this->defaultFetchMode = $fetchMode;
}

/**
Expand Down Expand Up @@ -648,7 +648,7 @@ public function prepare($statement)
throw DBALException::driverExceptionDuringQuery($ex, $statement);
}

$stmt->setFetchMode($this->_defaultFetchMode);
$stmt->setFetchMode($this->defaultFetchMode);

return $stmt;
}
Expand Down Expand Up @@ -701,7 +701,7 @@ public function executeQuery($query, array $params = array(), $types = array(),
throw DBALException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types));
}

$stmt->setFetchMode($this->_defaultFetchMode);
$stmt->setFetchMode($this->defaultFetchMode);

if ($logger) {
$logger->stopQuery();
Expand Down Expand Up @@ -745,7 +745,7 @@ public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qc
$stmt = new ResultCacheStatement($this->executeQuery($query, $params, $types), $resultCache, $cacheKey, $realKey, $qcp->getLifetime());
}

$stmt->setFetchMode($this->_defaultFetchMode);
$stmt->setFetchMode($this->defaultFetchMode);

return $stmt;
}
Expand Down Expand Up @@ -810,7 +810,7 @@ public function query()
throw DBALException::driverExceptionDuringQuery($ex, $args[0]);
}

$statement->setFetchMode($this->_defaultFetchMode);
$statement->setFetchMode($this->defaultFetchMode);

if ($logger) {
$logger->stopQuery();
Expand Down
21 changes: 18 additions & 3 deletions lib/Doctrine/DBAL/Portability/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Cache\QueryCacheProfile;

/**
* Portability wrapper for a Connection.
*
* @link www.doctrine-project.org
* @since 2.0
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class Connection extends \Doctrine\DBAL\Connection
{
const PORTABILITY_ALL = 255;
Expand Down Expand Up @@ -105,15 +112,21 @@ public function getFetchCase()
*/
public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
{
return new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
$stmt = new Statement(parent::executeQuery($query, $params, $types, $qcp), $this);
$stmt->setFetchMode($this->defaultFetchMode);

return $stmt;
}

/**
* {@inheritdoc}
*/
public function prepare($statement)
{
return new Statement(parent::prepare($statement), $this);
$stmt = new Statement(parent::prepare($statement), $this);
$stmt->setFetchMode($this->defaultFetchMode);

return $stmt;
}

/**
Expand All @@ -124,7 +137,9 @@ public function query()
$this->connect();

$stmt = call_user_func_array(array($this->_conn, 'query'), func_get_args());
$stmt = new Statement($stmt, $this);
$stmt->setFetchMode($this->defaultFetchMode);

return new Statement($stmt, $this);
return $stmt;
}
}
36 changes: 33 additions & 3 deletions tests/Doctrine/Tests/DBAL/Functional/PortabilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public function tearDown()
}
}

/**
* @param integer $portabilityMode
* @param integer $case
* @return Connection
*/
private function getPortableConnection($portabilityMode = \Doctrine\DBAL\Portability\Connection::PORTABILITY_ALL, $case = \PDO::CASE_LOWER)
{
if (!$this->portableConnection) {
Expand Down Expand Up @@ -67,18 +72,42 @@ public function testFullFetchMode()
}

$stmt = $this->getPortableConnection()->query('SELECT * FROM portability_table');
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
while (($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the extra braces you added are not needed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know but it highlights that it's intended to evaluate the result instead of being a typo ( = instead of ==). Most code editors also see the 'Assignment in condition' as a code smell. With the added braces the editor knows it's intended.

$this->assertFetchResultRow($row);
}

$stmt = $this->getPortableConnection()->prepare('SELECT * FROM portability_table');
$stmt->execute();

while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
while (($row = $stmt->fetch(\PDO::FETCH_ASSOC))) {
$this->assertFetchResultRow($row);
}
}

public function testConnFetchMode()
{
$conn = $this->getPortableConnection();
$conn->setFetchMode(\PDO::FETCH_ASSOC);

$rows = $conn->fetchAll('SELECT * FROM portability_table');
$this->assertFetchResultRows($rows);

$stmt = $conn->query('SELECT * FROM portability_table');
foreach ($stmt as $row) {
$this->assertFetchResultRow($row);
}

$stmt = $conn->query('SELECT * FROM portability_table');
while (($row = $stmt->fetch())) {
$this->assertFetchResultRow($row);
}

$stmt = $conn->prepare('SELECT * FROM portability_table');
$stmt->execute();
while (($row = $stmt->fetch())) {
$this->assertFetchResultRow($row);
}
}

public function assertFetchResultRows($rows)
{
$this->assertEquals(2, count($rows));
Expand All @@ -93,5 +122,6 @@ public function assertFetchResultRow($row)
$this->assertArrayHasKey('test_string', $row, "Case should be lowered.");
$this->assertEquals(3, strlen($row['test_string']), "test_string should be rtrimed to length of three for CHAR(32) column.");
$this->assertNull($row['test_null']);
$this->assertArrayNotHasKey(0, $row, "PDO::FETCH_ASSOC should not return numerical keys.");
}
}