1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 22:26:43 +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

@@ -115,9 +115,7 @@ class dibi
* Configuration options * Configuration options
*/ */
const const
RESULT_WITH_TABLES = 'resultWithTables', RESULT_WITH_TABLES = 'resultWithTables'; // for MySQL
RESULT_OBJECTS = 'resultObjects',
RESULT_CLASS = 'resultClass';
/** /**
* Connection registry storage for DibiConnection objects. * Connection registry storage for DibiConnection objects.
@@ -327,7 +325,7 @@ class dibi
* Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch(). * Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch().
* *
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return array * @return DibiRow
* @throws DibiException * @throws DibiException
*/ */
public static function fetch($args) public static function fetch($args)
@@ -342,7 +340,7 @@ class dibi
* Executes SQL query and fetch results - Monostate for DibiConnection::query() & fetchAll(). * Executes SQL query and fetch results - Monostate for DibiConnection::query() & fetchAll().
* *
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return array * @return array of DibiRow
* @throws DibiException * @throws DibiException
*/ */
public static function fetchAll($args) 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 * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @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 * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @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 * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @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 * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @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); return odbc_fetch_array($this->resultSet, ++$this->row);
} else { } else {
$set = $this->resultSet; $set = $this->resultSet;

View File

@@ -305,9 +305,9 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver
* @param bool TRUE for associative array, FALSE for numeric * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @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 * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @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 * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @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 * @param bool TRUE for associative array, FALSE for numeric
* @return array array on success, nonarray if no next record * @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); $count = sqlite_num_fields($this->resultSet);
$meta = array(); $meta = array();
for ($i = 0; $i < $count; $i++) { 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])) { if (!isset($pair[1])) {
array_unshift($pair, NULL); array_unshift($pair, NULL);
} }

View File

@@ -53,12 +53,6 @@ class DibiConnection extends DibiObject
*/ */
private $inTxn = FALSE; 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; $config['name'] = $name;
$this->config = $config; $this->config = $config;
$this->driver = new $class; $this->driver = new $class;
@@ -306,7 +278,7 @@ class DibiConnection extends DibiObject
dibi::notify($this, 'beforeQuery', $sql); dibi::notify($this, 'beforeQuery', $sql);
if ($res = $this->driver->query($sql)) { // intentionally = if ($res = $this->driver->query($sql)) { // intentionally =
$res = new $this->resultClass($res, $this->config); $res = new DibiResult($res, $this->config);
} }
$time += microtime(TRUE); $time += microtime(TRUE);

View File

@@ -228,7 +228,7 @@ class DibiFluent extends DibiObject
/** /**
* Generates, executes SQL query and fetches the single row. * 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 * @throws DibiException
*/ */
public function fetch() public function fetch()

View File

@@ -27,7 +27,6 @@
* $result = dibi::query('SELECT * FROM [table]'); * $result = dibi::query('SELECT * FROM [table]');
* *
* $row = $result->fetch(); * $row = $result->fetch();
* $obj = $result->fetch(TRUE);
* $value = $result->fetchSingle(); * $value = $result->fetchSingle();
* $table = $result->fetchAll(); * $table = $result->fetchAll();
* $pairs = $result->fetchPairs(); * $pairs = $result->fetchPairs();
@@ -73,12 +72,6 @@ class DibiResult extends DibiObject implements IDataSource
*/ */
private $withTables = FALSE; 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])) { if (!empty($config[dibi::RESULT_WITH_TABLES])) {
$this->setWithTables(TRUE); $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. * Fetches the row at current position, process optional type conversion.
* and moves the internal cursor to the next position * and moves the internal cursor to the next position
* *
* @param mixed fetch as object? Overrides $this->setObjects() * @return DibiRow|FALSE array on success, FALSE if no next record
* @return array|FALSE array on success, FALSE if no next record
*/ */
final public function fetch($objects = NULL) final public function fetch()
{ {
if ($this->withTables === FALSE) { if ($this->withTables === FALSE) {
$row = $this->getDriver()->fetch(TRUE); $row = $this->getDriver()->fetch(TRUE);
@@ -266,19 +229,7 @@ class DibiResult extends DibiObject implements IDataSource
} }
} }
if ($objects === NULL) { return new DibiRow($row, 2);
$objects = $this->objects;
}
if ($objects) {
if ($objects === TRUE) {
$row = (object) $row;
} else {
$row = new $objects($row);
}
}
return $row;
} }
@@ -312,7 +263,7 @@ class DibiResult extends DibiObject implements IDataSource
* *
* @param int offset * @param int offset
* @param int limit * @param int limit
* @return array * @return array of DibiRow
*/ */
final public function fetchAll($offset = NULL, $limit = NULL) 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} * builds a tree: $data[assoc1][index][assoc2]['assoc3']->value = {record}
* *
* @param string associative descriptor * @param string associative descriptor
* @return array * @return DibiRow
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
final public function fetchAssoc($assoc) final public function fetchAssoc($assoc)
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetch(FALSE); $row = $this->fetch();
if (!$row) return array(); // empty result set if (!$row) return array(); // empty result set
$data = NULL; $data = NULL;
@@ -359,7 +310,7 @@ class DibiResult extends DibiObject implements IDataSource
} }
// strip leading = and @ // strip leading = and @
$leaf = $this->objects ? $this->objects : '='; // gap $leaf = '@'; // gap
$last = count($assoc) - 1; $last = count($assoc) - 1;
while ($assoc[$last] === '=' || $assoc[$last] === '@') { while ($assoc[$last] === '=' || $assoc[$last] === '@') {
$leaf = $assoc[$last]; $leaf = $assoc[$last];
@@ -374,6 +325,7 @@ class DibiResult extends DibiObject implements IDataSource
// make associative tree // make associative tree
do { do {
$arr = (array) $row;
$x = & $data; $x = & $data;
// iterative deepening // iterative deepening
@@ -383,7 +335,7 @@ class DibiResult extends DibiObject implements IDataSource
} elseif ($as === '=') { // "record" node } elseif ($as === '=') { // "record" node
if ($x === NULL) { if ($x === NULL) {
$x = $row; $x = $arr;
$x = & $x[ $assoc[$i+1] ]; $x = & $x[ $assoc[$i+1] ];
$x = NULL; // prepare child node $x = NULL; // prepare child node
} else { } else {
@@ -392,7 +344,7 @@ class DibiResult extends DibiObject implements IDataSource
} elseif ($as === '@') { // "object" node } elseif ($as === '@') { // "object" node
if ($x === NULL) { if ($x === NULL) {
$x = (object) $row; $x = clone $row;
$x = & $x->{$assoc[$i+1]}; $x = & $x->{$assoc[$i+1]};
$x = NULL; // prepare child node $x = NULL; // prepare child node
} else { } else {
@@ -401,21 +353,19 @@ class DibiResult extends DibiObject implements IDataSource
} else { // associative-array node } else { // associative-array node
$x = & $x[ $row[ $as ] ]; $x = & $x[ $arr[ $as ] ];
} }
} }
if ($x === NULL) { // build leaf if ($x === NULL) { // build leaf
if ($leaf === '=') { if ($leaf === '=') {
$x = $row; $x = $arr;
} elseif ($leaf === TRUE || $leaf === '@') {
$x = (object) $row;
} else { } else {
$x = new $leaf($row); $x = $row;
} }
} }
} while ($row = $this->fetch(FALSE)); } while ($row = $this->fetch());
unset($x); unset($x);
return $data; return $data;
@@ -434,7 +384,7 @@ class DibiResult extends DibiObject implements IDataSource
final public function fetchPairs($key = NULL, $value = NULL) final public function fetchPairs($key = NULL, $value = NULL)
{ {
$this->seek(0); $this->seek(0);
$row = $this->fetch(FALSE); $row = $this->fetch();
if (!$row) return array(); // empty result set if (!$row) return array(); // empty result set
$data = array(); $data = array();
@@ -445,12 +395,12 @@ class DibiResult extends DibiObject implements IDataSource
} }
// autodetect // autodetect
$tmp = array_keys($row); $tmp = array_keys((array) $row);
$key = $tmp[0]; $key = $tmp[0];
if (count($row) < 2) { // indexed-array if (count($row) < 2) { // indexed-array
do { do {
$data[] = $row[$key]; $data[] = $row[$key];
} while ($row = $this->fetch(FALSE)); } while ($row = $this->fetch());
return $data; return $data;
} }
@@ -464,7 +414,7 @@ class DibiResult extends DibiObject implements IDataSource
if ($key === NULL) { // indexed-array if ($key === NULL) { // indexed-array
do { do {
$data[] = $row[$value]; $data[] = $row[$value];
} while ($row = $this->fetch(FALSE)); } while ($row = $this->fetch());
return $data; return $data;
} }
@@ -475,7 +425,7 @@ class DibiResult extends DibiObject implements IDataSource
do { do {
$data[ $row[$key] ] = $row[$value]; $data[ $row[$key] ] = $row[$value];
} while ($row = $this->fetch(FALSE)); } while ($row = $this->fetch());
return $data; return $data;
} }
@@ -604,7 +554,7 @@ class DibiResult extends DibiObject implements IDataSource
{ {
$i = 0; $i = 0;
$this->seek(0); $this->seek(0);
while ($row = $this->fetch(FALSE)) { while ($row = $this->fetch()) {
if ($i === 0) { if ($i === 0) {
echo "\n<table class=\"dump\">\n<thead>\n\t<tr>\n\t\t<th>#row</th>\n"; 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. * Fetches single row.
* @param scalar|array primary key value * @param scalar|array primary key value
* @return array|object row * @return DibiRow
*/ */
public function fetch($conditions) public function fetch($conditions)
{ {
@@ -290,21 +290,12 @@ abstract class DibiTable extends DibiObject
/** /**
* Returns a blank row (not fetched from database). * Returns a blank row (not fetched from database).
* @return array|object * @return DibiRow
*/ */
public function createBlank() public function createBlank()
{ {
$row = $this->blankRow; $row = new DibiRow($this->blankRow, 2);
$row[$this->primary] = NULL; $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; return $row;
} }
@@ -319,6 +310,7 @@ abstract class DibiTable extends DibiObject
{ {
if (is_object($data)) { if (is_object($data)) {
return (array) $data; return (array) $data;
} elseif (is_array($data)) { } elseif (is_array($data)) {
return $data; return $data;
} }

View File

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

View File

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