Query/src/Query/Drivers/Firebird/Result.php

270 lines
5.3 KiB
PHP
Raw Normal View History

2016-10-12 22:12:25 -04:00
<?php declare(strict_types=1);
2012-04-10 14:06:34 -04:00
/**
* Query
*
2016-09-07 13:17:17 -04:00
* SQL Query Builder / Database Abstraction Layer
2012-04-10 14:06:34 -04:00
*
2018-01-19 13:43:19 -05:00
* PHP version 7.1
2016-09-07 13:17:17 -04:00
*
* @package Query
* @author Timothy J. Warren <tim@timshomepage.net>
2018-01-19 13:43:19 -05:00
* @copyright 2012 - 2018 Timothy J. Warren
2016-09-07 13:17:17 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat4ion/Query
2012-04-10 14:06:34 -04:00
*/
namespace Query\Drivers\Firebird;
2014-04-02 17:08:50 -04:00
2016-09-07 17:39:19 -04:00
use PDOStatement;
use Query\Drivers\PDOStatementInterface;
2012-04-10 14:06:34 -04:00
/**
* Firebird result class to emulate PDOStatement Class - only implements
* data-fetching methods
*
2012-04-20 13:17:39 -04:00
* @package Query
* @subpackage Drivers
2012-04-10 14:06:34 -04:00
*/
2016-09-07 17:39:19 -04:00
class Result extends PDOStatement implements PDOStatementInterface {
2012-04-10 14:06:34 -04:00
/**
* Reference to fbird resource
*
* @var resource
*/
2012-04-10 14:06:34 -04:00
private $statement;
2014-02-25 13:47:35 -05:00
/**
* Current row in result array
*
2016-09-07 13:10:03 -04:00
* @var integer
*/
private $row;
2014-02-25 13:47:35 -05:00
/**
* Data pulled from query
*
2016-09-07 14:22:52 -04:00
* @var mixed
*/
2016-09-07 13:10:03 -04:00
private $result = [];
2012-04-10 14:06:34 -04:00
/**
* Reference to the db drive to de-duplicate error functions
*
2015-11-10 20:58:32 -05:00
* @var Driver
*/
private $db;
2012-04-10 14:06:34 -04:00
/**
* Create the object by passing the resource for
* the query
*
* @param resource $link
2015-07-17 16:01:41 -04:00
* @param Driver|null $db
2012-04-10 14:06:34 -04:00
*/
public function __construct($link, Driver $db = NULL)
2012-04-10 14:06:34 -04:00
{
2015-11-11 09:25:21 -05:00
if ( ! is_null($db))
{
$this->db = $db;
}
2012-04-10 14:06:34 -04:00
$this->statement = $link;
2014-04-02 17:08:50 -04:00
$this->setFetchMode(\PDO::FETCH_ASSOC);
$this->row = -1;
2016-09-07 13:10:03 -04:00
$this->result = [];
2014-02-25 13:47:35 -05:00
// Create the result array, so that we can get row counts
// Check the resource type, because prepared statements are "interbase query"
// but we only want "interbase result" types when attempting to fetch data
2014-04-02 17:08:50 -04:00
if (\is_resource($link) && \get_resource_type($link) === "interbase result")
{
2014-04-02 17:08:50 -04:00
while($row = \fbird_fetch_assoc($link, \IBASE_FETCH_BLOBS))
{
$this->result[] = $row;
}
2014-02-25 13:47:35 -05:00
// Free the result resource
2014-04-02 17:08:50 -04:00
\fbird_free_result($link);
}
2012-04-10 14:06:34 -04:00
}
2014-02-25 13:47:35 -05:00
/**
* Invalidate method for data consistency
*
* @param mixed $column
* @param mixed $param
* @param int $type
* @param mixed $maxlen
* @param array $driverdata
* @return NULL
*/
public function bindColumn($column, &$param, $type=NULL, $maxlen=NULL, $driverdata=NULL)
{
return NULL;
}
2014-02-25 13:47:35 -05:00
/**
* Invalidate method for data consistency
*
* @param mixed $parameter
* @param mixed $variable
2016-10-13 21:55:23 -04:00
* @param int $dataType
* @param mixed $maxlen
* @param array $driverdata
* @return NULL
*/
2016-10-13 21:55:23 -04:00
public function bindParam($parameter, &$variable, $dataType=NULL, $maxlen=NULL, $driverdata=NULL)
{
return NULL;
}
2014-02-25 13:47:35 -05:00
/**
* Invalidate method for data consistency
*
* @param mixed $parameter
* @param mixed $variable
2016-10-13 21:55:23 -04:00
* @param int $dataType
* @return NULL
*/
2016-10-13 21:55:23 -04:00
public function bindValue($parameter, $variable, $dataType=NULL)
{
return NULL;
}
2014-02-25 13:47:35 -05:00
/**
* Run a prepared statement query
*
2016-10-13 21:55:23 -04:00
* @param array $boundInputParams
* @return Result
*/
2016-10-13 21:55:23 -04:00
public function execute($boundInputParams = NULL)
{
//Add the prepared statement as the first parameter
2016-10-13 21:55:23 -04:00
\array_unshift($boundInputParams, $this->statement);
// Let php do all the hard stuff in converting
// the array of arguments into a list of arguments
// Then pass the resource to the constructor
2016-10-13 21:55:23 -04:00
$this->__construct(\call_user_func_array('fbird_execute', $boundInputParams));
return $this;
}
2012-04-10 14:06:34 -04:00
/**
* Emulate PDO fetch public function
*
2016-10-13 21:55:23 -04:00
* @param int $fetchStyle
* @param mixed $cursorOrientation
* @param mixed $cursorOffset
2012-04-10 14:06:34 -04:00
* @return mixed
*/
2016-10-13 21:55:23 -04:00
public function fetch($fetchStyle=\PDO::FETCH_ASSOC, $cursorOrientation = \PDO::FETCH_ORI_NEXT, $cursorOffset=NULL)
2014-02-25 13:47:35 -05:00
{
// If there is no result, continue
if (empty($this->result))
{
return NULL;
}
2014-02-25 13:47:35 -05:00
// Keep track of the current row being fetched
++$this->row;
2014-02-25 13:47:35 -05:00
// return NULL if the next row doesn't exist
if ( ! isset($this->result[$this->row]))
{
return NULL;
}
2014-02-25 13:47:35 -05:00
2016-10-13 21:55:23 -04:00
switch($fetchStyle)
2012-04-10 14:06:34 -04:00
{
2014-04-02 17:08:50 -04:00
case \PDO::FETCH_OBJ:
$row = (object) $this->result[$this->row];
2012-04-10 14:06:34 -04:00
break;
2014-04-02 17:08:50 -04:00
case \PDO::FETCH_NUM:
$row = \array_values($this->result[$this->row]);
2012-04-10 14:06:34 -04:00
break;
default:
$row = $this->result[$this->row];
2012-04-10 14:06:34 -04:00
break;
}
2014-02-25 13:47:35 -05:00
return $row;
2012-04-10 14:06:34 -04:00
}
/**
* Emulate PDO fetchAll public function
*
2016-10-13 21:55:23 -04:00
* @param int $fetchStyle
* @param mixed $statement
2016-10-13 21:55:23 -04:00
* @param mixed $ctorArgs
2012-04-10 14:06:34 -04:00
* @return mixed
*/
2016-10-13 21:55:23 -04:00
public function fetchAll($fetchStyle=\PDO::FETCH_ASSOC, $statement=NULL, $ctorArgs=NULL)
2012-04-10 14:06:34 -04:00
{
2016-09-07 13:10:03 -04:00
$all = [];
2012-04-10 14:06:34 -04:00
2016-10-13 21:55:23 -04:00
while($row = $this->fetch($fetchStyle, $statement))
2012-04-10 14:06:34 -04:00
{
$all[] = $row;
}
$this->result = $all;
return $all;
}
2014-02-25 13:47:35 -05:00
2012-04-12 13:44:31 -04:00
/**
* Emulate PDOStatement::fetchColumn
2014-02-25 13:47:35 -05:00
*
2016-10-13 21:55:23 -04:00
* @param int $columnNum
2014-02-25 13:47:35 -05:00
* @return mixed
2012-04-12 13:44:31 -04:00
*/
2016-10-13 21:55:23 -04:00
public function fetchColumn($columnNum=0)
2012-04-12 13:44:31 -04:00
{
2014-04-02 17:08:50 -04:00
$row = $this->fetch(\PDO::FETCH_NUM);
2016-10-13 21:55:23 -04:00
return $row[$columnNum];
2012-04-12 13:44:31 -04:00
}
2014-02-25 13:47:35 -05:00
/**
* Emulate PDOStatement::fetchObject, but only for the default use
2014-02-25 13:47:35 -05:00
*
2016-10-13 21:55:23 -04:00
* @param string $className
* @param array|null $ctorArgs
2016-09-07 17:39:19 -04:00
* @return object
*/
2016-10-13 21:55:23 -04:00
public function fetchObject($className='stdClass', $ctorArgs=NULL)
{
2014-04-02 17:08:50 -04:00
return $this->fetch(\PDO::FETCH_OBJ);
}
2012-04-10 14:06:34 -04:00
/**
* Return the number of rows affected by the previous query
*
* @return int
*/
public function rowCount()
{
return \fbird_affected_rows();
}
2014-02-25 13:47:35 -05:00
/**
* Method to emulate PDOStatement->errorCode
*
* @return string
*/
public function errorCode()
{
return $this->db->errorCode();
}
2012-04-10 14:06:34 -04:00
/**
* Method to emulate PDO->errorInfo / PDOStatement->errorInfo
*
* @return array
*/
public function errorInfo()
{
return $this->db->errorInfo();
2012-04-10 14:06:34 -04:00
}
2016-10-13 21:55:23 -04:00
}