From a0febd3d504024f0a28ca058bf598c7439b596aa Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 24 Nov 2007 08:23:26 +0000 Subject: [PATCH] * added support for affectedRows in DibiPdoDriver --- dibi/drivers/mysql.php | 2 +- dibi/drivers/mysqli.php | 2 +- dibi/drivers/odbc.php | 2 +- dibi/drivers/oracle.php | 2 +- dibi/drivers/pdo.php | 38 +++++++++++++++++++++++------ dibi/libs/DibiConnection.php | 2 +- examples/sql-condition.php | 46 ------------------------------------ 7 files changed, 36 insertions(+), 58 deletions(-) delete mode 100644 examples/sql-condition.php diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index b1aedcde..6f38a095 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -387,7 +387,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface * * @throws DibiDriverException */ - protected function throwException($sql=NULL) + protected function throwException($sql = NULL) { throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection), $sql); } diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index b138abc5..65bfcd0c 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -362,7 +362,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface * * @throws DibiDriverException */ - protected function throwException($sql=NULL) + protected function throwException($sql = NULL) { throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection), $sql); } diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 90b3290e..d147725c 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -339,7 +339,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface * * @throws DibiDriverException */ - protected function throwException($sql=NULL) + protected function throwException($sql = NULL) { throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql); } diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index 10c3d066..b6c85d85 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -296,7 +296,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface * * @throws DibiDriverException */ - protected function throwException($sql=NULL) + protected function throwException($sql = NULL) { $err = oci_error($this->connection); throw new DibiDriverException($err['message'], $err['code'], $sql); diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index b756aed5..dc02b770 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -50,6 +50,13 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface private $resultset; + /** + * Affected rows + * @var int + */ + private $affectedRows = FALSE; + + /** * Connects to a database @@ -101,13 +108,30 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface */ public function query($sql) { - $this->resultset = $this->connection->query($sql); + // must detect if SQL returns resultset or num of affected rows + $cmd = strtoupper(substr(ltrim($sql), 0, 6)); + $list = array('UPDATE'=>1, 'DELETE'=>1, 'INSERT'=>1, 'REPLAC'=>1); - if ($this->resultset === FALSE) { - $this->throwException($sql); + if (isset($list[$cmd])) { + $this->resultset = NULL; + $this->affectedRows = $this->connection->exec($sql); + + if ($this->affectedRows === FALSE) { + $this->throwException($sql); + } + + return FALSE; + + } else { + $this->resultset = $this->connection->query($sql); + $this->affectedRows = FALSE; + + if ($this->resultset === FALSE) { + $this->throwException($sql); + } + + return TRUE; } - - return $this->resultset instanceof PDOStatement; } @@ -119,7 +143,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface */ public function affectedRows() { - throw new BadMethodCallException(__METHOD__ . ' is not implemented'); + return $this->affectedRows; } @@ -286,7 +310,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface * * @throws DibiDriverException */ - protected function throwException($sql=NULL) + protected function throwException($sql = NULL) { $err = $this->connection->errorInfo(); throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql); diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 7e920188..3dfdbb36 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -291,7 +291,7 @@ class DibiConnection extends NObject { $id = $this->driver->insertId($sequence); if ($id < 1) throw new DibiException('Cannot retrieve last generated ID'); - return $id; + return (int) $id; } diff --git a/examples/sql-condition.php b/examples/sql-condition.php deleted file mode 100644 index 93a25826..00000000 --- a/examples/sql-condition.php +++ /dev/null @@ -1,46 +0,0 @@ - -

dibi conditional SQL example

-
- 'sqlite',
-    'database' => 'sample.sdb',
-));
-
-
-$cond1 = rand(0,2) < 1;
-$cond2 = rand(0,2) < 1;
-
-
-$name = $cond1 ? 'K%' : NULL;
-
-// if & end
-dibi::test('
-SELECT *
-FROM [customers]
-%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
-);
-
-
-// if & else & end (last end is optional)
-dibi::test('
-SELECT *
-FROM %if', $cond1, '[customers] %else [products]'
-);
-
-
-// nested condition
-dibi::test('
-SELECT *
-FROM [customers]
-WHERE
-    %if', isset($name), '[name] LIKE %s', $name, '
-        %if', $cond2, 'AND [admin]=1 %end
-    %else 1 LIMIT 10 %end'
-);