Changeset:
        d4316bea4fb8
        
https://sourceforge.net/p/mrbs/hg-code/ci/d4316bea4fb875e08ddb59dca9b10ce8e963f4d3
Author:
        John Beranek <jbera...@users.sourceforge.net>
Date:
        Sat Sep 17 16:03:54 2016 +0100
Log message:

pgsql abtraction updated to PDO

diffstat:

 web/pgsql.inc |  157 +++++++++++++++++++++++++++++++--------------------------
 1 files changed, 84 insertions(+), 73 deletions(-)

diffs (truncated from 393 to 300 lines):

diff -r aeb55a1aa62f -r d4316bea4fb8 web/pgsql.inc
--- a/web/pgsql.inc     Sat Sep 17 13:34:45 2016 +0100
+++ b/web/pgsql.inc     Sat Sep 17 16:03:54 2016 +0100
@@ -1,6 +1,8 @@
 <?php
 namespace MRBS;
 
+use PDO;
+
 // $Id$
 
 // pgsql.inc - Simple PHP database support for PostgreSQL.
@@ -57,11 +59,11 @@
 // Free a results handle. You need not call this if you call sql_row or
 // sql_row_keyed until the row returns 0, since sql_row frees the results
 // handle when you finish reading the rows.
-function sql_pgsql_free ($r, $db_conn = null)
+function sql_pgsql_free ($sth, $db_conn = null)
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  pg_free_result($r);
+  unset($sth);
 }
 
 
@@ -70,14 +72,7 @@
 {
   sql_pgsql_ensure_handle($db_conn);
   
-  if (function_exists('pg_escape_string'))
-  {
-    return pg_escape_string($db_conn, $str);
-  }
-  else
-  {
-    return addslashes($str);
-  }
+  return addslashes($str);
 }
 
 
@@ -105,19 +100,21 @@
 // Execute a non-SELECT SQL command (insert/update/delete).
 // Returns the number of tuples affected if OK (a number >= 0).
 // Returns -1 on error; use sql_error to get the error message.
-function sql_pgsql_command ($sql, $db_conn = null)
+function sql_pgsql_command ($sql, $params = array(), $db_conn = null)
 {
   sql_pgsql_ensure_handle($db_conn);
 
   $e = error_reporting(E_ALL & ~(E_WARNING|E_NOTICE));
-  $r = pg_query($db_conn, $sql);
+  
+  $sth = $db_conn->prepare($sql);
+  $sth->execute($params);
   error_reporting($e);
-  if (! $r)
+  if (! $sth)
   {
     return -1;
   }
-  $n = pg_affected_rows($r);
-  pg_free_result($r);
+  $n = $sth->rowCount();
+
   return $n;
 }
 
@@ -128,26 +125,34 @@
 // exactly one value, so error checking is somewhat limited.
 // It also returns -1 if the query returns a single NULL value, such as from
 // a MIN or MAX aggregate function applied over no rows.
-function sql_pgsql_query1 ($sql, $db_conn = null)
+function sql_pgsql_query1 ($sql, $params = array(), $db_conn = null)
 {
   sql_pgsql_ensure_handle($db_conn);
 
   $e = error_reporting(E_ALL & ~(E_WARNING|E_NOTICE));
-  $r = pg_query($db_conn, $sql);
+  
+  $sth = $db_conn->prepare($sql);
+  $sth->execute($params);
   error_reporting($e);
   
-  if (!$r)
+  if (!$sth)
   {
     trigger_error (sql_pgsql_error($db_conn), E_USER_WARNING);
     return -1;
   }
   
-  if (pg_num_rows($r) != 1 || pg_num_fields($r) != 1
-      || ($result = pg_fetch_result($r, 0, 0)) == "")
+  // Default to returning an error
+  $result = -1;
+
+  if (($sth->rowCount() == 1) &&
+      ($sth->columnCount() == 1))
   {
-    $result = -1;
+       $row = $sth->fetch(PDO::FETCH_NUM);
+       if ($row != NULL)
+       {
+      $result = $row[0];
+    }
   }
-  pg_free_result($r);
   return $result;
 }
 
@@ -155,14 +160,16 @@
 // Execute an SQL query. Returns a database-dependent result handle,
 // which should be passed back to sql_row or sql_row_keyed to get the results.
 // Returns FALSE on error; use sql_error to get the error message.
-function sql_pgsql_query ($sql, $db_conn = null)
+function sql_pgsql_query ($sql, $params = array(), $db_conn = null)
 {
   sql_pgsql_ensure_handle($db_conn);
 
   $e = error_reporting(E_ALL & ~(E_WARNING|E_NOTICE));
-  $r = pg_query($db_conn, $sql);
+  
+  $sth = $db_conn->prepare($sql);
+  $sth->execute($params);
   error_reporting($e);
-  return $r;
+  return $sth;
 }
 
 
@@ -171,16 +178,16 @@
 // When called with i >= number of rows in the result, cleans up from
 // the query and returns 0.
 // Typical usage: $i = 0; while ((a = sql_row($r, $i++))) { ... }
-function sql_pgsql_row ($r, $i, $db_conn = null)
+function sql_pgsql_row ($sth, $i, $db_conn = null)
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  if ($i >= pg_num_rows($r))
+  if ($i >= $sth->rowCount())
   {
-    pg_free_result($r);
+    unset($sth);
     return 0;
   }
-  return pg_fetch_row($r, $i);
+  return $sth->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, $i);
 }
 
 
@@ -190,27 +197,25 @@
 // routing also stores the data under number indexes.
 // When called with i >= number of rows in the result, cleans up from
 // the query and returns 0.
-function sql_pgsql_row_keyed ($r, $i, $db_conn = null)
+function sql_pgsql_row_keyed ($sth, $i, $db_conn = null)
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  if ($i >= pg_num_rows($r))
+  if ($i >= $sth->rowCount())
   {
-    pg_free_result($r);
+    unset($sth);
     return 0;
   }
-  // Use _array() rather _assoc() to ensure support
-  // for as many PHP versions as possible
-  return pg_fetch_array($r, $i, PGSQL_ASSOC);
+  return $sth->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $i);
 }
 
 
 // Return the number of rows returned by a result handle from sql_query.
-function sql_pgsql_count ($r, $db_conn = null)
+function sql_pgsql_count ($sth, $db_conn = null)
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  return pg_num_rows($r);
+  return $sth->rowCount();
 }
 
 
@@ -221,7 +226,7 @@
   sql_pgsql_ensure_handle($db_conn);
 
   $seq_name = $table . "_" . $field . "_seq";
-  return sql_pgsql_query1("select last_value from $seq_name", $db_conn);
+  return $db_conn->lastInsertId($seq_name);
 }
 
 
@@ -230,7 +235,9 @@
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  return pg_last_error($db_conn);
+  $error_info = $db_conn->errorInfo();
+
+  return $error_info[2];
 }
 
 
@@ -240,7 +247,7 @@
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  $result = sql_pgsql_command("BEGIN", $db_conn);
+  $result = sql_pgsql_command("BEGIN", array(), $db_conn);
   
   if ($result < 0)
   {
@@ -254,7 +261,7 @@
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  $result = sql_pgsql_command("COMMIT", $db_conn);
+  $result = sql_pgsql_command("COMMIT", array(), $db_conn);
   
   if ($result < 0)
   {
@@ -268,7 +275,7 @@
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  $result = sql_pgsql_command("ROLLBACK", $db_conn);
+  $result = sql_pgsql_command("ROLLBACK", array(), $db_conn);
   
   if ($result < 0)
   {
@@ -294,8 +301,8 @@
   sql_pgsql_ensure_handle($db_conn);
 
   global $sql_pgsql_mutex_unlock_name;
-  if ((sql_pgsql_command("BEGIN", $db_conn) < 0) ||
-      (sql_pgsql_command("LOCK TABLE $name IN EXCLUSIVE MODE", $db_conn) < 0))
+  if ((sql_pgsql_command("BEGIN", array(), $db_conn) < 0) ||
+      (sql_pgsql_command("LOCK TABLE $name IN EXCLUSIVE MODE", array(), 
$db_conn) < 0))
   {
     return 0;
   }
@@ -313,7 +320,7 @@
   sql_pgsql_ensure_handle($db_conn);
 
   global $sql_pgsql_mutex_unlock_name;
-  sql_pgsql_command("COMMIT", $db_conn);
+  sql_pgsql_command("COMMIT", array(), $db_conn);
   $sql_pgsql_mutex_unlock_name = NULL;
 }
 
@@ -323,10 +330,14 @@
 {
   global $sql_pgsql_mutex_unlock_name;
   
+  if (!$db_conn)
+  {
+       return;
+  }
   // Release any forgotten locks
   if (!empty($sql_pgsql_mutex_unlock_name))
   {
-    sql_pgsql_command("ABORT", $db_conn);
+    sql_pgsql_command("ABORT", array(), $db_conn);
     $sql_pgsql_mutex_unlock_name = NULL;
   }
   
@@ -340,7 +351,7 @@
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  $r = sql_pgsql_query("select version()", $db_conn);
+  $r = sql_pgsql_query("select version()", array(), $db_conn);
   $v = sql_pgsql_row($r, 0, $db_conn);
   sql_pgsql_free($r, $db_conn);
 
@@ -374,7 +385,7 @@
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  return " " . sql_pgsql_quote($fieldname) . "='" . sql_pgsql_escape($s) . "'";
+  return " " . sql_pgsql_quote($fieldname) . "=?";
 }
 
 
@@ -454,7 +465,7 @@
 {
   sql_pgsql_ensure_handle($db_conn);
 
-  return pg_num_fields($result);
+  return $result->columnCount();
 }
 
 
@@ -467,17 +478,20 @@
   // parts, the schema and table names
   $table_parts = sql_pgsql_resolve_table($table);
   

------------------------------------------------------------------------------
_______________________________________________
Mrbs-commits mailing list
Mrbs-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to