1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 13:47:33 +02:00

- returns result-set rows as DibiRow objects!

- removed option 'resultObjects'
- SQlite driver removes quotes from result-set column names
- this revision may cause compatibility break
This commit is contained in:
David Grudl
2008-10-10 17:39:33 +00:00
parent ae77148773
commit 3e04378375
15 changed files with 70 additions and 137 deletions

View File

@@ -114,10 +114,8 @@ class dibi
/**
* Configuration options
*/
const
RESULT_WITH_TABLES = 'resultWithTables',
RESULT_OBJECTS = 'resultObjects',
RESULT_CLASS = 'resultClass';
const
RESULT_WITH_TABLES = 'resultWithTables'; // for MySQL
/**
* Connection registry storage for DibiConnection objects.
@@ -327,7 +325,7 @@ class dibi
* Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch().
*
* @param array|mixed one or more arguments
* @return array
* @return DibiRow
* @throws DibiException
*/
public static function fetch($args)
@@ -342,7 +340,7 @@ class dibi
* Executes SQL query and fetch results - Monostate for DibiConnection::query() & fetchAll().
*
* @param array|mixed one or more arguments
* @return array
* @return array of DibiRow
* @throws DibiException
*/
public static function fetchAll($args)

View File

@@ -297,9 +297,9 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($type)
public function fetch($assoc)
{
return mssql_fetch_array($this->resultSet, $type ? MSSQL_ASSOC : MSSQL_NUM);
return mssql_fetch_array($this->resultSet, $assoc ? MSSQL_ASSOC : MSSQL_NUM);
}

View File

@@ -359,9 +359,9 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($type)
public function fetch($assoc)
{
return mysql_fetch_array($this->resultSet, $type ? MYSQL_ASSOC : MYSQL_NUM);
return mysql_fetch_array($this->resultSet, $assoc ? MYSQL_ASSOC : MYSQL_NUM);
}

View File

@@ -342,9 +342,9 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($type)
public function fetch($assoc)
{
return mysqli_fetch_array($this->resultSet, $type ? MYSQLI_ASSOC : MYSQLI_NUM);
return mysqli_fetch_array($this->resultSet, $assoc ? MYSQLI_ASSOC : MYSQLI_NUM);
}

View File

@@ -309,9 +309,9 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($type)
public function fetch($assoc)
{
if ($type) {
if ($assoc) {
return odbc_fetch_array($this->resultSet, ++$this->row);
} else {
$set = $this->resultSet;

View File

@@ -305,9 +305,9 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($type)
public function fetch($assoc)
{
return oci_fetch_array($this->resultSet, ($type ? OCI_ASSOC : OCI_NUM) | OCI_RETURN_NULLS);
return oci_fetch_array($this->resultSet, ($assoc ? OCI_ASSOC : OCI_NUM) | OCI_RETURN_NULLS);
}

View File

@@ -351,9 +351,9 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($type)
public function fetch($assoc)
{
return $this->resultSet->fetch($type ? PDO::FETCH_ASSOC : PDO::FETCH_NUM);
return $this->resultSet->fetch($assoc ? PDO::FETCH_ASSOC : PDO::FETCH_NUM);
}

View File

@@ -353,9 +353,9 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($type)
public function fetch($assoc)
{
return pg_fetch_array($this->resultSet, NULL, $type ? PGSQL_ASSOC : PGSQL_NUM);
return pg_fetch_array($this->resultSet, NULL, $assoc ? PGSQL_ASSOC : PGSQL_NUM);
}

View File

@@ -309,9 +309,17 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record
*/
public function fetch($type)
public function fetch($assoc)
{
return sqlite_fetch_array($this->resultSet, $type ? SQLITE_ASSOC : SQLITE_NUM);
$row = sqlite_fetch_array($this->resultSet, $assoc ? SQLITE_ASSOC : SQLITE_NUM);
if ($assoc && $row) {
$tmp = array();
foreach ($row as $k => $v) {
$tmp[str_replace(array('[', ']'), '', $k)] = $v;
}
return $tmp;
}
return $row;
}
@@ -355,7 +363,7 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
$count = sqlite_num_fields($this->resultSet);
$meta = array();
for ($i = 0; $i < $count; $i++) {
$pair = explode('.', sqlite_field_name($this->resultSet, $i));
$pair = explode('.', str_replace(array('[', ']'), '', sqlite_field_name($this->resultSet, $i)));
if (!isset($pair[1])) {
array_unshift($pair, NULL);
}

View File

@@ -53,12 +53,6 @@ class DibiConnection extends DibiObject
*/
private $inTxn = FALSE;
/**
* Result set encapsulation.
* @var string
*/
private $resultClass = 'DibiResult';
/**
@@ -99,28 +93,6 @@ class DibiConnection extends DibiObject
}
}
if (isset($config['result:withtables'])) {
$config[dibi::RESULT_WITH_TABLES] = $config['result:withtables'];
unset($config['result:withtables']);
}
if (isset($config['result:objects'])) {
$config[dibi::RESULT_OBJECTS] = $config['result:objects'];
unset($config['result:objects']);
}
if (isset($config[dibi::RESULT_OBJECTS])) { // normalize
$val = $config[dibi::RESULT_OBJECTS];
$config[dibi::RESULT_OBJECTS] = is_string($val) && !is_numeric($val) ? $val : (bool) $val;
}
if (isset($config[dibi::RESULT_CLASS])) {
if (strcasecmp($config[dibi::RESULT_CLASS], 'DibiResult') && !is_subclass_of($config[dibi::RESULT_CLASS], 'DibiResult')) {
throw new InvalidArgumentException("Class '$config[resultClass]' is not DibiResult descendant.");
}
$this->resultClass = $config[dibi::RESULT_CLASS];
}
$config['name'] = $name;
$this->config = $config;
$this->driver = new $class;
@@ -306,7 +278,7 @@ class DibiConnection extends DibiObject
dibi::notify($this, 'beforeQuery', $sql);
if ($res = $this->driver->query($sql)) { // intentionally =
$res = new $this->resultClass($res, $this->config);
$res = new DibiResult($res, $this->config);
}
$time += microtime(TRUE);

View File

@@ -228,7 +228,7 @@ class DibiFluent extends DibiObject
/**
* Generates, executes SQL query and fetches the single row.
* @return array|FALSE array on success, FALSE if no next record
* @return DibiRow|FALSE array on success, FALSE if no next record
* @throws DibiException
*/
public function fetch()

View File

@@ -27,7 +27,6 @@
* $result = dibi::query('SELECT * FROM [table]');
*
* $row = $result->fetch();
* $obj = $result->fetch(TRUE);
* $value = $result->fetchSingle();
* $table = $result->fetchAll();
* $pairs = $result->fetchPairs();
@@ -73,12 +72,6 @@ class DibiResult extends DibiObject implements IDataSource
*/
private $withTables = FALSE;
/**
* Fetch as objects or arrays?
* @var mixed TRUE | FALSE | class name
*/
private $objects = FALSE;
/**
@@ -92,10 +85,6 @@ class DibiResult extends DibiObject implements IDataSource
if (!empty($config[dibi::RESULT_WITH_TABLES])) {
$this->setWithTables(TRUE);
}
if (isset($config[dibi::RESULT_OBJECTS])) {
$this->setObjects($config[dibi::RESULT_OBJECTS]);
}
}
@@ -211,39 +200,13 @@ class DibiResult extends DibiObject implements IDataSource
/**
* Returns rows as arrays or objects?
*
* @param mixed TRUE | FALSE | class name
* @return void
*/
public function setObjects($type)
{
$this->objects = $type;
}
/**
* Returns rows as arrays or objects?
*
* @return mixed TRUE | FALSE | class name
*/
public function getObjects()
{
return $this->objects;
}
/**
* Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position
*
* @param mixed fetch as object? Overrides $this->setObjects()
* @return array|FALSE array on success, FALSE if no next record
* @return DibiRow|FALSE array on success, FALSE if no next record
*/
final public function fetch($objects = NULL)
final public function fetch()
{
if ($this->withTables === FALSE) {
$row = $this->getDriver()->fetch(TRUE);
@@ -266,19 +229,7 @@ class DibiResult extends DibiObject implements IDataSource
}
}
if ($objects === NULL) {
$objects = $this->objects;
}
if ($objects) {
if ($objects === TRUE) {
$row = (object) $row;
} else {
$row = new $objects($row);
}
}
return $row;
return new DibiRow($row, 2);
}
@@ -312,7 +263,7 @@ class DibiResult extends DibiObject implements IDataSource
*
* @param int offset
* @param int limit
* @return array
* @return array of DibiRow
*/
final public function fetchAll($offset = NULL, $limit = NULL)
{
@@ -339,13 +290,13 @@ class DibiResult extends DibiObject implements IDataSource
* builds a tree: $data[assoc1][index][assoc2]['assoc3']->value = {record}
*
* @param string associative descriptor
* @return array
* @return DibiRow
* @throws InvalidArgumentException
*/
final public function fetchAssoc($assoc)
{
$this->seek(0);
$row = $this->fetch(FALSE);
$row = $this->fetch();
if (!$row) return array(); // empty result set
$data = NULL;
@@ -359,7 +310,7 @@ class DibiResult extends DibiObject implements IDataSource
}
// strip leading = and @
$leaf = $this->objects ? $this->objects : '='; // gap
$leaf = '@'; // gap
$last = count($assoc) - 1;
while ($assoc[$last] === '=' || $assoc[$last] === '@') {
$leaf = $assoc[$last];
@@ -374,6 +325,7 @@ class DibiResult extends DibiObject implements IDataSource
// make associative tree
do {
$arr = (array) $row;
$x = & $data;
// iterative deepening
@@ -383,7 +335,7 @@ class DibiResult extends DibiObject implements IDataSource
} elseif ($as === '=') { // "record" node
if ($x === NULL) {
$x = $row;
$x = $arr;
$x = & $x[ $assoc[$i+1] ];
$x = NULL; // prepare child node
} else {
@@ -392,7 +344,7 @@ class DibiResult extends DibiObject implements IDataSource
} elseif ($as === '@') { // "object" node
if ($x === NULL) {
$x = (object) $row;
$x = clone $row;
$x = & $x->{$assoc[$i+1]};
$x = NULL; // prepare child node
} else {
@@ -401,21 +353,19 @@ class DibiResult extends DibiObject implements IDataSource
} else { // associative-array node
$x = & $x[ $row[ $as ] ];
$x = & $x[ $arr[ $as ] ];
}
}
if ($x === NULL) { // build leaf
if ($leaf === '=') {
$x = $row;
} elseif ($leaf === TRUE || $leaf === '@') {
$x = (object) $row;
$x = $arr;
} else {
$x = new $leaf($row);
$x = $row;
}
}
} while ($row = $this->fetch(FALSE));
} while ($row = $this->fetch());
unset($x);
return $data;
@@ -434,7 +384,7 @@ class DibiResult extends DibiObject implements IDataSource
final public function fetchPairs($key = NULL, $value = NULL)
{
$this->seek(0);
$row = $this->fetch(FALSE);
$row = $this->fetch();
if (!$row) return array(); // empty result set
$data = array();
@@ -445,12 +395,12 @@ class DibiResult extends DibiObject implements IDataSource
}
// autodetect
$tmp = array_keys($row);
$tmp = array_keys((array) $row);
$key = $tmp[0];
if (count($row) < 2) { // indexed-array
do {
$data[] = $row[$key];
} while ($row = $this->fetch(FALSE));
} while ($row = $this->fetch());
return $data;
}
@@ -464,7 +414,7 @@ class DibiResult extends DibiObject implements IDataSource
if ($key === NULL) { // indexed-array
do {
$data[] = $row[$value];
} while ($row = $this->fetch(FALSE));
} while ($row = $this->fetch());
return $data;
}
@@ -475,7 +425,7 @@ class DibiResult extends DibiObject implements IDataSource
do {
$data[ $row[$key] ] = $row[$value];
} while ($row = $this->fetch(FALSE));
} while ($row = $this->fetch());
return $data;
}
@@ -604,7 +554,7 @@ class DibiResult extends DibiObject implements IDataSource
{
$i = 0;
$this->seek(0);
while ($row = $this->fetch(FALSE)) {
while ($row = $this->fetch()) {
if ($i === 0) {
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n";
@@ -674,3 +624,17 @@ class DibiResult extends DibiObject implements IDataSource
}
/**
* dibi result-set row
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi
*/
class DibiRow extends ArrayObject
{
}

View File

@@ -270,7 +270,7 @@ abstract class DibiTable extends DibiObject
/**
* Fetches single row.
* @param scalar|array primary key value
* @return array|object row
* @return DibiRow
*/
public function fetch($conditions)
{
@@ -290,21 +290,12 @@ abstract class DibiTable extends DibiObject
/**
* Returns a blank row (not fetched from database).
* @return array|object
* @return DibiRow
*/
public function createBlank()
{
$row = $this->blankRow;
$row = new DibiRow($this->blankRow, 2);
$row[$this->primary] = NULL;
if ($class = $this->connection->getConfig(dibi::RESULT_OBJECTS)) {
if ($class === TRUE) {
$row = (object) $row;
} else {
$row = new $class($row);
}
}
return $row;
}
@@ -319,6 +310,7 @@ abstract class DibiTable extends DibiObject
{
if (is_object($data)) {
return (array) $data;
} elseif (is_array($data)) {
return $data;
}

View File

@@ -195,7 +195,7 @@ final class DibiTranslator extends DibiObject
public function formatValue($value, $modifier)
{
// array processing (with or without modifier)
if (is_array($value)) {
if (is_array($value) || $value instanceof ArrayObject) {
$vx = $kx = array();
$operator = ', ';

View File

@@ -10,7 +10,6 @@ try {
dibi::connect(array(
'driver' => 'sqlite',
'database' => 'sample.sdb',
'resultObjects' => TRUE, // fetch rows as objects
));
echo 'OK';