Skip to content

Commit

Permalink
Added methods to fetchAssoc(), fetchColumn($position), fetchObject(),…
Browse files Browse the repository at this point in the history
… fetchAllObjects()
  • Loading branch information
Eugene Ritter committed Mar 31, 2018
1 parent 6565302 commit 051c6e3
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 7 deletions.
17 changes: 16 additions & 1 deletion src/Database/Statement/BufferedStatement.php
Expand Up @@ -92,7 +92,6 @@ public function fetch($type = 'num')
}

$record = parent::fetch($type);

if ($record === false) {
$this->_allFetched = true;
$this->_counter = $this->_count + 1;
Expand All @@ -106,6 +105,22 @@ public function fetch($type = 'num')
return $this->_records[] = $record;
}

/**
* {@inheritdoc}
*/
public function fetchAssoc()
{
return $this->fetch(parent::FETCH_TYPE_ASSOC);
}

/**
* {@inheritdoc}
*/
public function fetchobject()
{
return $this->fetch(parent::FETCH_TYPE_OBJ);
}

/**
* {@inheritDoc}
*
Expand Down
12 changes: 6 additions & 6 deletions src/Database/Statement/PDOStatement.php
Expand Up @@ -90,13 +90,13 @@ public function bindValue($column, $value, $type = 'string')
*/
public function fetch($type = 'num')
{
if ($type === 'num') {
if ($type === static::FETCH_TYPE_NUM) {
return $this->_statement->fetch(PDO::FETCH_NUM);
}
if ($type === 'assoc') {
if ($type === static::FETCH_TYPE_ASSOC) {
return $this->_statement->fetch(PDO::FETCH_ASSOC);
}
if ($type === 'obj') {
if ($type === static::FETCH_TYPE_OBJ) {
return $this->_statement->fetch(PDO::FETCH_OBJ);
}

Expand All @@ -119,13 +119,13 @@ public function fetch($type = 'num')
*/
public function fetchAll($type = 'num')
{
if ($type === 'num') {
if ($type === static::FETCH_TYPE_NUM) {
return $this->_statement->fetchAll(PDO::FETCH_NUM);
}
if ($type === 'assoc') {
if ($type === static::FETCH_TYPE_ASSOC) {
return $this->_statement->fetchAll(PDO::FETCH_ASSOC);
}
if ($type === 'obj') {
if ($type === static::FETCH_TYPE_OBJ) {
return $this->_statement->fetchAll(PDO::FETCH_OBJ);
}

Expand Down
63 changes: 63 additions & 0 deletions src/Database/Statement/StatementDecorator.php
Expand Up @@ -35,6 +35,25 @@ class StatementDecorator implements StatementInterface, Countable, IteratorAggre

use TypeConverterTrait;

/**
* Used to designate that numeric indexes be returned in a result when calling fetch methods
*
* @var string
*/
const FETCH_TYPE_NUM = 'num';
/**
* Used to designate that associated array be returned in a result when calling fetch methods
*
* @var string
*/
const FETCH_TYPE_ASSOC = 'assoc';
/**
* Used to designate that objects of \StdClass be returned in a result when calling fetch methods
*
* @var string
*/
const FETCH_TYPE_OBJ = 'obj';

/**
* Statement instance implementation, such as PDOStatement
* or any other custom implementation.
Expand Down Expand Up @@ -196,6 +215,40 @@ public function fetch($type = 'num')
return $this->_statement->fetch($type);
}

/**
* Returns the next row as a StdClass object
*
* @return array|false Result array containing columns and values in an anonymous object or false if no results
*/
public function fetchObject()
{
return $this->fetch(static::FETCH_TYPE_OBJ);
}

/**
* @return array|false Result array containing columns and values an an associative array or false if no results
*/
public function fetchAssoc()
{
return $this->fetch(static::FETCH_TYPE_ASSOC);
}

/**
* Returns the value of the result at position.
*
* @param int $position The numeric position of the column to retrieve in the result
* @return mixed|false Returns the specific value of the column designated at $position
*/
public function fetchColumn($position)
{
$result = $this->_statement->fetch(static::FETCH_TYPE_NUM);
if (isset($result[$position])) {
return $result[$position];
};

return false;
}

/**
* Returns an array with all rows resulting from executing this statement.
*
Expand All @@ -215,6 +268,16 @@ public function fetchAll($type = 'num')
return $this->_statement->fetchAll($type);
}

/**
* Returns an array of \StdClass objects or false
*
* @return \StdClass[]|false
*/
public function fetchAllObjects()
{
return $this->fetchAll(static::FETCH_TYPE_OBJ);
}

/**
* Returns the number of rows affected by this SQL statement.
*
Expand Down
118 changes: 118 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -4697,4 +4697,122 @@ public function assertQuotedQuery($pattern, $query, $optional = false)
$pattern = str_replace('>', '[`"\]]' . $optional, $pattern);
$this->assertRegExp('#' . $pattern . '#', $query);
}

/**
* Test that calling fetchAssoc return an associated array.
* @return void
*/
public function testFetchAssoc()
{
$this->loadFixtures('Profiles');
$query = new Query($this->connection);
$results = $query
->select([
'id',
'user_id',
'is_active'
])
->from('profiles')
->limit(1)
->execute()
->fetchAssoc();
$this->assertSame(['id' => '1', 'user_id' => '1', 'is_active' => '0'], $results);
}

/**
* Test that calling fetchObject returns a \StdClass object
* @return void
*/
public function testFetchObject()
{
$this->loadFixtures('Profiles');
$query = new Query($this->connection);
$results = $query
->select([
'id',
'user_id',
'is_active'
])
->from('profiles')
->limit(1)
->execute()
->fetchObject();
$this->assertInstanceOf(\StdClass::class, $results);
$this->assertObjectHasAttribute('id', $results);
$this->assertObjectHasAttribute('user_id', $results);
$this->assertObjectHasAttribute('is_active', $results);
$this->assertEquals($results->id, '1');
$this->assertEquals($results->user_id, '1');
$this->assertEquals($results->is_active, '0');
}

/**
* Test that calling fetchColumn returns the correct column value.
* @return void
*/
public function testFetchColumn()
{
$this->loadFixtures('Profiles');
$query = new Query($this->connection);
$query
->select([
'id',
'user_id',
'is_active'
])
->from('profiles')
->where(['id' => 2])
->limit(1);
$statement = $query->execute();
$results = $statement->fetchColumn(0);
$this->assertEquals('2', $results[0]);
}

/**
* Test that calling fetchAssoc, fetchColum and fetchObject in sequence
* alters the fetched data to the correct types and values.
* @return void
*/
public function testFetchAllAssocColumnAndObj()
{
$this->loadFixtures('Profiles');
$query = new Query($this->connection);
$query
->select([
'id',
'user_id',
'is_active'
])
->from('profiles');
$statement = $query->execute();
$results = $statement->fetchAssoc();
$this->assertEquals('1', $results['id']);
$results = $statement->fetchAssoc();
$this->assertEquals('2', $results['id']);
$results = $statement->fetchColumn(0);
$this->assertEquals('3', $results[0]);
$results = $statement->fetchObject();
$this->assertEquals('4', $results->id);
}

/**
* Test that an array of objects is returned
* @return void
*/
public function testFetchAllObjects()
{
$this->loadFixtures('Profiles');
$query = new Query($this->connection);
$query
->select([
'id',
'user_id',
'is_active'
])
->from('profiles');
$statement = $query->execute();
$results = $statement->fetchAllObjects();
$this->assertInstanceOf(\StdClass::class, $results[0]);
$this->assertEquals('2', $results[1]->id);
}
}

0 comments on commit 051c6e3

Please sign in to comment.