Changeset:
        23a136da33a7
        
https://sourceforge.net/p/mrbs/hg-code/ci/23a136da33a76b20f52d90e444fe469abe637be6
Author:
        John Beranek <[email protected]>
Date:
        Sat Sep 24 20:42:51 2016 +0100
Log message:

Worked version using classes, with sql_*() wrappers in dbsys.inc

diffstat:

 web/auth/auth_db_ext.inc  |  115 ++----
 web/dbsys.inc             |  727 ++-------------------------------------------
 web/dbtest.php            |    9 +-
 web/lib/MRBS/DB.php       |   40 ++-
 web/lib/MRBS/DB_mysql.php |    2 +-
 web/lib/MRBS/DB_pgsql.php |    2 +-
 web/mysqli.inc            |  635 ----------------------------------------
 web/pgsql.inc             |  662 -----------------------------------------
 web/upgrade.inc           |    4 +-
 9 files changed, 126 insertions(+), 2070 deletions(-)

diffs (truncated from 2501 to 300 lines):

diff -r ccd01b3edd4f -r 23a136da33a7 web/auth/auth_db_ext.inc
--- a/web/auth/auth_db_ext.inc  Sat Sep 24 12:11:27 2016 +0100
+++ b/web/auth/auth_db_ext.inc  Sat Sep 24 20:42:51 2016 +0100
@@ -16,15 +16,23 @@
 *
 ******************************************************************************/
 
-// $Id$
 
-// We no longer have a 'mysql' abstraction, but to support people who upgrade 
MRBS and used to
-// use the 'mysql' abstraction, we change the abstraction to the supported 
'mysqli'.
-if ($auth['db_ext']['db_system'] == 'mysql')
+if (empty($auth['db_ext']['db_system']))
 {
-  $auth['db_ext']['db_system'] = 'mysqli';
+  $auth['db_ext']['db_system'] = 'mysql';
 }
 
+$persist = 0;
+$port = isset($auth['db_ext']['db_port']) ? (int)$auth['db_ext']['db_port'] : 
NULL;
+  
+static $db_ext_conn = DBFactory::create($auth['db_ext']['db_system'],
+                                        $auth['db_ext']['db_host'],
+                                        $auth['db_ext']['db_username'],
+                                        $auth['db_ext']['db_password'],
+                                        $auth['db_ext']['db_name'],
+                                        $persist,
+                                        $port);
+
 
 /* authValidateUser($user, $pass)
  * 
@@ -40,48 +48,33 @@
 
 function authValidateUser($user, $pass)
 {
-  global $auth;
+  global $auth, $db_ext_conn;
 
   $retval = 0;
 
-  if (empty($auth['db_ext']['db_system']))
-  {
-    $auth['db_ext']['db_system'] = 'mysqli';
-  }
-
-  $persist = 0;
-  $port = isset($auth['db_ext']['db_port']) ? (int)$auth['db_ext']['db_port'] 
: NULL;
-  
-  $conn = sql_connect($auth['db_ext']['db_system'],
-                      $auth['db_ext']['db_host'],
-                      $auth['db_ext']['db_username'],
-                      $auth['db_ext']['db_password'],
-                      $auth['db_ext']['db_name'],
-                      $persist,
-                      $port);
 
   // sql_syntax_casesensitive_equals() modifies our SQL params array for us.   
We need an exact match -
   // MySQL allows trailing spaces when using an '=' comparison, eg 'john' = 
'john '
 
   $sql_params = array();
 
-  $query = "SELECT " . sql_quote($auth['db_ext']['column_name_password']) .
-            " FROM " . sql_quote($auth['db_ext']['db_table']) .
-           " WHERE " . 
sql_syntax_casesensitive_equals($auth['db_ext']['column_name_username'],
-                                                       utf8_strtolower($user),
-                                                       $sql_params);
+  $query = "SELECT " . 
$db_ext_conn->quote($auth['db_ext']['column_name_password']) .
+           "FROM " . $db_ext_conn->quote($auth['db_ext']['db_table']) .
+           "WHERE " . 
$db_ext_conn->syntax_casesensitive_equals($auth['db_ext']['column_name_username'],
+                                                                
utf8_strtolower($user),
+                                                                $sql_params);
 
-  $r = sql_query($query, $sql_params, $conn);
+  $r = $db_ext_conn->query($query, $sql_params);
   
   if ($r === FALSE)
   {
-    trigger_error(sql_error($conn), E_USER_WARNING);
+    trigger_error($db_ext_conn->error(), E_USER_WARNING);
     fatal_error(FALSE, get_vocab("fatal_db_error"));
   }
 
-  if (sql_count($r, $conn) == 1) // force a unique match
+  if ($db_ext_conn->count($r) == 1) // force a unique match
   {
-    $row = sql_row($r, 0, $conn);
+    $row = $db_ext_conn->row($r, 0);
 
     switch ($auth['db_ext']['password_format'])
     {
@@ -152,7 +145,8 @@
  */
 function authGetUserLevel($user)
 {
-  global $auth;
+  global $auth, $db_ext_conn;
+
   $admins = $auth['admin'];
   // User not logged in, user level '0'
   if(!isset($user))
@@ -175,39 +169,31 @@
      $persist = 0;
      $port = isset($auth['db_ext']['db_port']) ? 
(int)$auth['db_ext']['db_port'] : NULL;
      
-     $conn = sql_connect($auth['db_ext']['db_system'],
-                         $auth['db_ext']['db_host'],
-                         $auth['db_ext']['db_username'],
-                         $auth['db_ext']['db_password'],
-                         $auth['db_ext']['db_name'],
-                         $persist,
-                         $port);
-
      // sql_syntax_casesensitive_equals() modifies our SQL params array for 
us.   We need an exact match -
      // MySQL allows trailing spaces when using an '=' comparison, eg 'john' = 
'john '
 
      $sql_params = array();
-     $query = "SELECT " . sql_quote($auth['db_ext']['column_name_level']) . "
-                 FROM " . sql_quote($auth['db_ext']['db_table']) . "
-                WHERE " . 
sql_syntax_casesensitive_equals($auth['db_ext']['column_name_username'],
-                                                          
utf8_strtolower($user),
-                                                          $sql_params) . "
+     $query = "SELECT " . 
$db_ext_conn->quote($auth['db_ext']['column_name_level']) . "
+                 FROM " . $db_ext_conn->quote($auth['db_ext']['db_table']) . "
+                WHERE " . 
$db_ext_conn->syntax_casesensitive_equals($auth['db_ext']['column_name_username'],
+                                                                    
utf8_strtolower($user),
+                                                                    
$sql_params) . "
                 LIMIT 1";
 
-     $r = sql_query($query, $sql_params, $conn);
+     $r = $db_ext_conn->query($query, $sql_params);
      
      if ($r === FALSE)
      {
-       trigger_error(sql_error($conn), E_USER_WARNING);
+       trigger_error($db_ext_conn->error(), E_USER_WARNING);
        fatal_error(FALSE, get_vocab("fatal_db_error"));
      }
      
-     if (sql_count($r) == 0)
+     if ($db_ext_conn->count($r) == 0)
      {
        return 0;
      }
      
-     $row = sql_row($r, 0, $conn);
+     $row = $db_ext_conn->row($r, 0);
      
      return $row[0];
   }
@@ -221,7 +207,7 @@
 // string if one can't be found
 function authGetUserEmail($user)
 {
-  global $auth;
+  global $auth, $db_ext_conn;
   
   if (!isset($user) || $user === '')
   {
@@ -233,48 +219,35 @@
     return get_default_email($user);
   }
   
-  if (empty($auth['db_ext']['db_system']))
-  {
-    $auth['db_ext']['db_system'] = 'mysqli';
-  }
 
   $persist = 0;
   $port = isset($auth['db_ext']['db_port']) ? (int)$auth['db_ext']['db_port'] 
: NULL;
   
-  $conn = sql_connect($auth['db_ext']['db_system'],
-                      $auth['db_ext']['db_host'],
-                      $auth['db_ext']['db_username'],
-                      $auth['db_ext']['db_password'],
-                      $auth['db_ext']['db_name'],
-                      $persist,
-                      $port);
-
   // sql_syntax_casesensitive_equals() modifies our SQL params array for us.   
We need an exact match -
   // MySQL allows trailing spaces when using an '=' comparison, eg 'john' = 
'john '
 
   $sql_params = array();
-  $query = "SELECT " . sql_quote($auth['db_ext']['column_name_email']) . "
-              FROM " . sql_quote($auth['db_ext']['db_table']) . "
-             WHERE " . 
sql_syntax_casesensitive_equals($auth['db_ext']['column_name_username'],
-                                                       utf8_strtolower($user),
-                                                       $sql_params) . "
+  $query = "SELECT " . 
$db_ext_conn->quote($auth['db_ext']['column_name_email']) . "
+              FROM " . $db_ext_conn->quote($auth['db_ext']['db_table']) . "
+             WHERE " . 
$db_ext_conn->syntax_casesensitive_equals($auth['db_ext']['column_name_username'],
+                                                                 
utf8_strtolower($user),
+                                                                 $sql_params) 
. "
              LIMIT 1";
 
-  $r = sql_query($query, $sql_params, $conn);
+  $r = $db_ext_conn->query($query, $sql_params);
   
   if ($r === FALSE)
   {
-    trigger_error(sql_error($conn), E_USER_WARNING);
+    trigger_error($db_ext_conn->error(), E_USER_WARNING);
     fatal_error(FALSE, get_vocab("fatal_db_error"));
   }
   
-  if (sql_count($r) == 0)
+  if ($db_ext_conn->count($r) == 0)
   {
     return 0;
   }
   
-  $row = sql_row($r, 0, $conn);
+  $row = $db_ext_conn->row($r, 0);
   
   return $row[0];
 }
-
diff -r ccd01b3edd4f -r 23a136da33a7 web/dbsys.inc
--- a/web/dbsys.inc     Sat Sep 24 12:11:27 2016 +0100
+++ b/web/dbsys.inc     Sat Sep 24 20:42:51 2016 +0100
@@ -27,763 +27,116 @@
 $db_schema_version = 49;
 $local_db_schema_version = 1;
 
-// Include the abstraction configured to be used for the default MRBS
-// database
-require_once "${dbsys}.inc";
 
+// Convenience wrapper functions
 
-// All the sql_* functions below apart from sql_connect(), 
sql_default_connect()
-// and sql_close() and take an optional final argument which is
-// the MRBS database handle to use. If this is not passed, the default
-// MRBS database connection is used.
-
-// The namespace needs to be prepended to the function names assigned to $f 
because
-// they are generated dynamically.
-
-
-// Free a result object
 function sql_free($r)
 {
-  global $dbsys;
-  
-  if (!is_resource($r))
-  {
-    // No need to do anything - resource has probably been
-    // freed automatically by reading to its end
-    return;
-  }
-  
-  if (func_num_args() > 1)
-  {
-    $handle = func_get_arg(1);
-    $db_sys = $handle['system'];
-    $db_conn = $handle['connection'];
-  }
-  else
-  {
-    global $dbsys;
-
-    $db_sys = $dbsys;
-    $db_conn = null;
-  }
-
-  $f = __NAMESPACE__ . "\\sql_${db_sys}_free";
-  $f($r, $db_conn);
+  return DB::default_db()->free($r);
 }
-
-
-// Quote a table or column name
-// NOTE:  In PostgreSQL the identifier is also converted to lower case.  See
-// the comments in pgsql.inc for an explanation.
 function sql_quote($identifier)
 {
-  global $dbsys;
-  
-  $db_sys = $dbsys;
-  $f = __NAMESPACE__ . "\\sql_${db_sys}_quote";
-  return $f($identifier);
+  return DB::default_db()->quote($identifier);
 }
-
-
-// Run an SQL query that doesn't produce results
-function sql_command($sql, $params = array())
+function sql_command($sql, $params = null)
 {
-  if (func_num_args() > 2)
-  {
-    $handle = func_get_arg(2);
-    $db_sys = $handle['system'];
-    $db_conn = $handle['connection'];
-  }
-  else
-  {
-    global $dbsys;
-
-    $db_sys = $dbsys;

------------------------------------------------------------------------------
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits

Reply via email to