From 6f4d2c545dc213b9b0d41f61a608e30b0bb8e173 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 28 Aug 2007 22:13:53 +0000 Subject: [PATCH] * removed variables $insertId & $affectedRows --- dibi/drivers/mssql.php | 23 ++++++--------- dibi/drivers/mysql.php | 30 +++++++------------ dibi/drivers/mysqli.php | 30 +++++++------------ dibi/drivers/odbc.php | 18 ++++++------ dibi/drivers/pdo.php | 16 +++++------ dibi/drivers/postgre.php | 8 ++++-- dibi/drivers/sqlite.php | 31 +++++++------------- examples/connect.php | 62 +++++++++++++++++++++++++++++++++------- 8 files changed, 115 insertions(+), 103 deletions(-) diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index f86c6448..73078d50 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -22,9 +22,6 @@ if (!class_exists('dibi', FALSE)) die(); */ class DibiMSSqlDriver extends DibiDriver { - private - $affectedRows = FALSE; - public $formats = array( 'TRUE' => "1", @@ -78,27 +75,25 @@ class DibiMSSqlDriver extends DibiDriver public function nativeQuery($sql) { - $this->affectedRows = FALSE; - $connection = $this->getConnection(); - $res = @mssql_query($sql, $connection); + $res = @mssql_query($sql, $this->getConnection()); - if ($res === FALSE) return FALSE; + if ($res === FALSE) { + return FALSE; - $this->affectedRows = mssql_rows_affected($connection); - if ($this->affectedRows < 0) $this->affectedRows = FALSE; - - if (is_resource($res)) { + } elseif (is_resource($res)) { return new DibiMSSqlResult($res); - } - return TRUE; + } else { + return TRUE; + } } public function affectedRows() { - return $this->affectedRows; + $rows = mssql_rows_affected($this->getConnection()); + return $rows < 0 ? FALSE : $rows; } diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index ae5a2107..8acf5330 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -22,10 +22,6 @@ if (!class_exists('dibi', FALSE)) die(); */ class DibiMySqlDriver extends DibiDriver { - private - $insertId = FALSE, - $affectedRows = FALSE; - public $formats = array( 'TRUE' => "1", @@ -115,37 +111,33 @@ class DibiMySqlDriver extends DibiDriver public function nativeQuery($sql) { - $this->insertId = $this->affectedRows = FALSE; - $connection = $this->getConnection(); - $res = @mysql_query($sql, $connection); + $res = @mysql_query($sql, $this->getConnection()); - if ($res === FALSE) return FALSE; + if ($res === FALSE) { + return FALSE; - $this->affectedRows = mysql_affected_rows($connection); - if ($this->affectedRows < 0) $this->affectedRows = FALSE; - - $this->insertId = mysql_insert_id($connection); - if ($this->insertId < 1) $this->insertId = FALSE; - - if (is_resource($res)) { + } elseif (is_resource($res)) { return new DibiMySqlResult($res); - } - return TRUE; + } else { + return TRUE; + } } public function affectedRows() { - return $this->affectedRows; + $rows = mysql_affected_rows($this->getConnection()); + return $rows < 0 ? FALSE : $rows; } public function insertId() { - return $this->insertId; + $id = mysql_insert_id($this->getConnection()); + return $id < 1 ? FALSE : $id; } diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index ee836f28..25bc74c7 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -22,10 +22,6 @@ if (!class_exists('dibi', FALSE)) die(); */ class DibiMySqliDriver extends DibiDriver { - private - $insertId = FALSE, - $affectedRows = FALSE; - public $formats = array( 'TRUE' => "1", @@ -85,37 +81,33 @@ class DibiMySqliDriver extends DibiDriver public function nativeQuery($sql) { - $this->insertId = $this->affectedRows = FALSE; - $connection = $this->getConnection(); - $res = @mysqli_query($connection, $sql); + $res = @mysqli_query($this->getConnection(), $sql); - if ($res === FALSE) return FALSE; + if ($res === FALSE) { + return FALSE; - $this->affectedRows = mysqli_affected_rows($connection); - if ($this->affectedRows < 0) $this->affectedRows = FALSE; - - $this->insertId = mysqli_insert_id($connection); - if ($this->insertId < 1) $this->insertId = FALSE; - - if (is_object($res)) { + } elseif (is_object($res)) { return new DibiMySqliResult($res); - } - return TRUE; + } else { + return TRUE; + } } public function affectedRows() { - return $this->affectedRows; + $rows = mysqli_affected_rows($this->getConnection()); + return $rows < 0 ? FALSE : $rows; } public function insertId() { - return $this->insertId; + $id = mysqli_insert_id($this->getConnection()); + return $id < 1 ? FALSE : $id; } diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 51793fb6..539e711f 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -91,20 +91,20 @@ class DibiOdbcDriver extends DibiDriver public function nativeQuery($sql) { $this->affectedRows = FALSE; + $res = @odbc_exec($this->getConnection(), $sql); - $connection = $this->getConnection(); - $res = @odbc_exec($connection, $sql); + if ($res === FALSE) { + return FALSE; - if ($res === FALSE) return FALSE; + } elseif (is_resource($res)) { + $this->affectedRows = odbc_num_rows($res); + if ($this->affectedRows < 0) $this->affectedRows = FALSE; - $this->affectedRows = odbc_num_rows($res); - if ($this->affectedRows < 0) $this->affectedRows = FALSE; - - if (is_resource($res)) { return new DibiOdbcResult($res); - } - return TRUE; + } else { + return TRUE; + } } diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 937e7ee8..dda4123d 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -22,8 +22,6 @@ if (!class_exists('dibi', FALSE)) die(); */ class DibiPdoDriver extends DibiDriver { - private $affectedRows = FALSE; - public $formats = array( 'TRUE' => "1", @@ -65,25 +63,25 @@ class DibiPdoDriver extends DibiDriver public function nativeQuery($sql) { - $this->affectedRows = FALSE; - // TODO: or exec() ? $res = $this->getConnection()->query($sql); - if ($res === FALSE) return FALSE; + if ($res === FALSE) { + return FALSE; - if ($res instanceof PDOStatement) { + } elseif ($res instanceof PDOStatement) { return new DibiPdoResult($res); - } - return TRUE; + } else { + return TRUE; + } } public function affectedRows() { - return $this->affectedRows; + // not implemented } diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index ef9a3640..df3d752e 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -23,6 +23,7 @@ if (!class_exists('dibi', FALSE)) die(); class DibiPostgreDriver extends DibiDriver { private + $insertId = FALSE, $affectedRows = FALSE; public @@ -83,7 +84,7 @@ class DibiPostgreDriver extends DibiDriver public function nativeQuery($sql) { - $this->affectedRows = FALSE; + $this->insertId = $this->affectedRows = FALSE; $res = @pg_query($this->getConnection(), $sql); @@ -91,6 +92,9 @@ class DibiPostgreDriver extends DibiDriver return FALSE; } elseif (is_resource($res)) { + $this->insertId = pg_last_oid($res); + if ($this->insertId < 0) $this->insertId = FALSE; + $this->affectedRows = pg_affected_rows($res); if ($this->affectedRows < 0) $this->affectedRows = FALSE; @@ -112,7 +116,7 @@ class DibiPostgreDriver extends DibiDriver public function insertId() { - return FALSE; + return $this->insertId; } diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 6005ec2e..35d6cbbc 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -22,10 +22,6 @@ if (!class_exists('dibi', FALSE)) die(); */ class DibiSqliteDriver extends DibiDriver { - private - $insertId = FALSE, - $affectedRows = FALSE; - public $formats = array( 'TRUE' => "1", @@ -81,38 +77,33 @@ class DibiSqliteDriver extends DibiDriver public function nativeQuery($sql) { - $this->insertId = $this->affectedRows = FALSE; + $res = @sqlite_query($this->getConnection(), $sql, SQLITE_ASSOC); - $connection = $this->getConnection(); - $res = @sqlite_query($connection, $sql, SQLITE_ASSOC); + if ($res === FALSE) { + return FALSE; - if ($res === FALSE) return FALSE; - - $this->affectedRows = sqlite_changes($connection); - if ($this->affectedRows < 0) $this->affectedRows = FALSE; - - $this->insertId = sqlite_last_insert_rowid($connection); - if ($this->insertId < 1) $this->insertId = FALSE; - - if (is_resource($res)) { + } elseif (is_resource($res)) { return new DibiSqliteResult($res); - } - return TRUE; + } else { + return TRUE; + } } public function affectedRows() { - return $this->affectedRows; + $rows = sqlite_changes($this->getConnection()); + return $rows < 0 ? FALSE : $rows; } public function insertId() { - return $this->insertId; + $id = sqlite_last_insert_rowid($this->getConnection()); + return $id < 1 ? FALSE : $id; } diff --git a/examples/connect.php b/examples/connect.php index 60f5df6a..06f57205 100644 --- a/examples/connect.php +++ b/examples/connect.php @@ -1,21 +1,35 @@ +
  'sqlite',
         'database' => 'sample.sdb',
     ));
 
-    // connects to MySQL using DSN
+} catch (DibiException $e) {
+    echo 'DibiException: ', $e;
+}
+
+
+
+// connects to MySQL using DSN
+try {
     dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=utf8');
 
+} catch (DibiException $e) {
+    echo 'DibiException: ', $e;
+}
 
-    // connects to MySQL / MySQLi
+
+
+
+// connects to MySQL / MySQLi
+try {
     dibi::connect(array(
         'driver'   => 'mysql',  // or 'mysqli'
         'host'     => 'localhost',
@@ -25,8 +39,15 @@ try {
         'charset'  => 'utf8',
     ));
 
+} catch (DibiException $e) {
+    echo 'DibiException: ', $e;
+}
 
-    // connects to ODBC
+
+
+
+// connects to ODBC
+try {
     dibi::connect(array(
         'driver'   => 'odbc',
         'username' => 'root',
@@ -34,22 +55,43 @@ try {
         'database' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Database.mdb',
     ));
 
+} catch (DibiException $e) {
+    echo 'DibiException: ', $e;
+}
 
-    // connects to PostgreSql
+
+
+
+// connects to PostgreSql
+try {
     dibi::connect(array(
         'driver'     => 'postgre',
         'string'     => 'host=localhost port=5432 dbname=mary',
         'persistent' => TRUE,
     ));
 
+} catch (DibiException $e) {
+    echo 'DibiException: ', $e;
+}
 
-    // connects to PDO
+
+
+
+// connects to PDO
+try {
     dibi::connect(array(
         'driver'  => 'pdo',
         'dsn'     => 'sqlite2::memory:',
     ));
 
-    // connects to MS SQL
+} catch (DibiException $e) {
+    echo 'DibiException: ', $e;
+}
+
+
+
+// connects to MS SQL
+try {
     dibi::connect(array(
         'driver'   => 'mssql',
         'host'     => 'localhost',
@@ -58,7 +100,5 @@ try {
     ));
 
 } catch (DibiException $e) {
-
-    echo "DibiException: 
", $e;
-
+    echo 'DibiException: ', $e;
 }