From c41167d49ff3f30ac62319857f41b8930cbaec3e Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 15 Jan 2008 05:40:08 +0000 Subject: [PATCH] bug fixed --- dibi/drivers/mssql.php | 1 - dibi/drivers/mysql.php | 1 - dibi/drivers/mysqli.php | 3 +-- dibi/drivers/odbc.php | 1 - dibi/drivers/oracle.php | 1 - dibi/drivers/pdo.php | 1 + dibi/drivers/postgre.php | 3 +-- dibi/drivers/sqlite.php | 2 +- dibi/libs/DibiTable.php | 49 +++++++++++++++++++++++++++++++------- dibi/libs/DibiVariable.php | 1 + dibi/libs/NException.php | 1 + examples/dibi.table.php | 13 +++++----- 12 files changed, 53 insertions(+), 24 deletions(-) diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index e9d3ebc1..b1f7a697 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -227,7 +227,6 @@ class DibiMsSqlDriver extends NObject implements IDibiDriver - /** * Returns the number of rows in a result set * diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 253be23a..4d0c9d67 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -277,7 +277,6 @@ class DibiMySqlDriver extends NObject implements IDibiDriver - /** * Returns the number of rows in a result set * diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 021f78a1..2061f61d 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -125,6 +125,7 @@ class DibiMySqliDriver extends NObject implements IDibiDriver } + /** * Disconnects from a database * @@ -256,8 +257,6 @@ class DibiMySqliDriver extends NObject implements IDibiDriver - - /** * Returns the number of rows in a result set * diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 406ff476..77a6d5e2 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -239,7 +239,6 @@ class DibiOdbcDriver extends NObject implements IDibiDriver - /** * Returns the number of rows in a result set * diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index df8be526..c1e70547 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -232,7 +232,6 @@ class DibiOracleDriver extends NObject implements IDibiDriver - /** * Returns the number of rows in a result set * diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index f9e9cbb3..3bfe6462 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -95,6 +95,7 @@ class DibiPdoDriver extends NObject implements IDibiDriver } + /** * Disconnects from a database * diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 41886bf6..e40b8f64 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -69,6 +69,7 @@ class DibiPostgreDriver extends NObject implements IDibiDriver } + /** * Connects to a database * @@ -262,8 +263,6 @@ class DibiPostgreDriver extends NObject implements IDibiDriver - - /** * Returns the number of rows in a result set * diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 74124279..440e4074 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -105,6 +105,7 @@ class DibiSqliteDriver extends NObject implements IDibiDriver } + /** * Disconnects from a database * @@ -235,7 +236,6 @@ class DibiSqliteDriver extends NObject implements IDibiDriver - /** * Returns the number of rows in a result set * diff --git a/dibi/libs/DibiTable.php b/dibi/libs/DibiTable.php index 999dac7f..18688203 100644 --- a/dibi/libs/DibiTable.php +++ b/dibi/libs/DibiTable.php @@ -49,6 +49,9 @@ abstract class DibiTable extends NObject /** @var string primary key type */ protected $primaryModifier = '%i'; + /** @var array */ + protected $blankRow = array(); + /** * Table constructor @@ -202,10 +205,10 @@ abstract class DibiTable extends NObject if (!is_array($what)) { $what = func_get_args(); } - return $this->connection->query( + return $this->complete($this->connection->query( 'SELECT * FROM %n', $this->name, 'WHERE %n', $this->primary, 'IN (' . $this->primaryModifier, $what, ')' - ); + )); } @@ -218,15 +221,15 @@ abstract class DibiTable extends NObject public function findAll($order = NULL) { if ($order === NULL) { - return $this->connection->query( + return $this->complete($this->connection->query( 'SELECT * FROM %n', $this->name - ); + )); } else { $order = func_get_args(); - return $this->connection->query( + return $this->complete($this->connection->query( 'SELECT * FROM %n', $this->name, 'ORDER BY %n', $order - ); + )); } } @@ -235,14 +238,42 @@ abstract class DibiTable extends NObject /** * Fetches single row * @param scalar primary key value - * @return array row + * @return array|object row */ public function fetch($what) { - $this->connection->query( + return $this->complete($this->connection->query( 'SELECT * FROM %n', $this->name, 'WHERE %n', $this->primary, '=' . $this->primaryModifier, $what - )->fetch(); + ))->fetch(); + } + + + + /** + * Returns a blank row (not fetched from database) + * @return array|object + */ + public function createBlank() + { + $row = $this->blankRow; + $row[$this->primary] = NULL; + if ($this->connection->getConfig('result:objects')) { + $row = (object) $row; + } + return $row; + } + + + + /** + * User DibiResult post-processing + * @param DibiResult + * @return DibiResult + */ + protected function complete($res) + { + return $res; } } diff --git a/dibi/libs/DibiVariable.php b/dibi/libs/DibiVariable.php index 468c8314..962a1980 100644 --- a/dibi/libs/DibiVariable.php +++ b/dibi/libs/DibiVariable.php @@ -39,6 +39,7 @@ class DibiVariable extends NObject implements IDibiVariable } + public function toSql(IDibiDriver $driver, $modifier) { return $driver->format($this->value, $this->type); diff --git a/dibi/libs/NException.php b/dibi/libs/NException.php index bf877701..6c846971 100644 --- a/dibi/libs/NException.php +++ b/dibi/libs/NException.php @@ -114,6 +114,7 @@ class NException extends Exception } + /** * Internal error handler */ diff --git a/examples/dibi.table.php b/examples/dibi.table.php index eeb38e78..e08d0a43 100644 --- a/examples/dibi.table.php +++ b/examples/dibi.table.php @@ -5,15 +5,17 @@ require_once '../dibi/dibi.php'; -copy('sample.sdb', 'sample_tmp.sdb'); - dibi::connect(array( 'driver' => 'sqlite', - 'database' => 'sample_tmp.sdb', + 'database' => 'sample.sdb', )); +dibi::begin(); +// autodetection: primary keys are customer_id, order_id, ... +DibiTable::$primaryMask = '%s_id'; + // table products class Products extends DibiTable @@ -24,8 +26,7 @@ class Products extends DibiTable } -// autodetection: primary keys are customer_id, order_id, ... -DibiTable::$primaryMask = '%s_id'; + @@ -38,7 +39,7 @@ echo "Primary key: $products->primary\n"; // Finds rows by primary key foreach ($products->find(1, 3) as $row) { - ... + print_r($row); }