From 3e043783754ae260b38db7201a4b6b1723cdf8fc Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 10 Oct 2008 17:39:33 +0000 Subject: [PATCH] - 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 --- dibi/dibi.php | 10 ++-- dibi/drivers/mssql.php | 4 +- dibi/drivers/mysql.php | 4 +- dibi/drivers/mysqli.php | 4 +- dibi/drivers/odbc.php | 4 +- dibi/drivers/oracle.php | 4 +- dibi/drivers/pdo.php | 4 +- dibi/drivers/postgre.php | 4 +- dibi/drivers/sqlite.php | 14 ++++- dibi/libs/DibiConnection.php | 30 +--------- dibi/libs/DibiFluent.php | 2 +- dibi/libs/DibiResult.php | 104 ++++++++++++----------------------- dibi/libs/DibiTable.php | 16 ++---- dibi/libs/DibiTranslator.php | 2 +- examples/connect.php | 1 - 15 files changed, 70 insertions(+), 137 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index ea6f0e25..5579ceb3 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -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) diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index 5b1ed63f..6b528a23 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -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); } diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 67f5611c..1751af84 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -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); } diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index ddca60c2..4755fb89 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -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); } diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 770eb494..b4f7f08c 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -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; diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index e7e7ec26..6b1d6588 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -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); } diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 367d7145..d4bfeef7 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -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); } diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index ab7cf48d..2cdd570a 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -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); } diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 555eb8d5..29f9e72e 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -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); } diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 8fcfe91b..f99903e7 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -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); diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index bf5b514e..b508cc74 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -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() diff --git a/dibi/libs/DibiResult.php b/dibi/libs/DibiResult.php index 1814feb6..caa791d4 100644 --- a/dibi/libs/DibiResult.php +++ b/dibi/libs/DibiResult.php @@ -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\n\n\t\n\t\t\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 +{ +} diff --git a/dibi/libs/DibiTable.php b/dibi/libs/DibiTable.php index 1a72685a..b28e1929 100644 --- a/dibi/libs/DibiTable.php +++ b/dibi/libs/DibiTable.php @@ -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; } diff --git a/dibi/libs/DibiTranslator.php b/dibi/libs/DibiTranslator.php index 91d9fcc8..c47311d4 100644 --- a/dibi/libs/DibiTranslator.php +++ b/dibi/libs/DibiTranslator.php @@ -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 = ', '; diff --git a/examples/connect.php b/examples/connect.php index b4f7082e..a3577085 100644 --- a/examples/connect.php +++ b/examples/connect.php @@ -10,7 +10,6 @@ try { dibi::connect(array( 'driver' => 'sqlite', 'database' => 'sample.sdb', - 'resultObjects' => TRUE, // fetch rows as objects )); echo 'OK';
#row