Changeset:
c362f89e8be9
https://sourceforge.net/p/mrbs/hg-code/ci/c362f89e8be9ceff55f7785be0ab2fc87d1fc4b7
Author:
John Beranek <[email protected]>
Date:
Sun Sep 25 08:27:55 2016 +0100
Log message:
Took DB abstraction further, by building a DBStatement class
diffstat:
convert_db_to_utf8.php | 26 ++++++------
web/auth/auth_db_ext.inc | 24 +++++-----
web/dbsys.inc | 24 +++++-----
web/lib/MRBS/DB.php | 85 +---------------------------------------
web/lib/MRBS/DBStatement.php | 90 ++++++++++++++++++++++++++++++++++++++++++++
web/lib/MRBS/DB_mysql.php | 16 +++---
web/lib/MRBS/DB_pgsql.php | 6 +-
7 files changed, 142 insertions(+), 129 deletions(-)
diffs (truncated from 495 to 300 lines):
diff -r 717100a34911 -r c362f89e8be9 convert_db_to_utf8.php
--- a/convert_db_to_utf8.php Sun Sep 25 00:24:11 2016 +0100
+++ b/convert_db_to_utf8.php Sun Sep 25 08:27:55 2016 +0100
@@ -164,9 +164,9 @@
Updating '$table' table...
";
$sql = "SELECT id,".implode(',',$columns)." FROM $table";
- $res = $db_handle->query($sql);
+ $stmt = $db_handle->query($sql);
- for ($i = 0; ($row = $db_handle->row_keyed($res, $i)); $i++)
+ for ($i = 0; ($row = $stmt->row_keyed($i)); $i++)
{
$sql_params = array();
$updates = array();
@@ -228,14 +228,14 @@
global $db_handle;
$sq='SHOW CREATE DATABASE `'.$db.'`;';
- $res = $db_handle->query($sq);
- if(!$res)
+ $stmt = $db_handle->query($sq);
+ if(!$stmt)
{
echo "\n\n".$sq."\n".$db_handle->error()."\n\n";
}
else
{
- for ($i = 0; ($row = $db_handle->row_keyed($res, $i)); $i++)
+ for ($i = 0; ($row = $stmt->row_keyed($i)); $i++)
{
$tokenized = explode(' ', $row[1]);
@@ -283,24 +283,24 @@
}
$db_handle->command("USE $db");
- $rs = $db_handle->query("SHOW TABLES");
- if(!$rs)
+ $stmt = $db_handle->query("SHOW TABLES");
+ if(!$stmt)
{
echo "\n\n".$db_handle->error()."\n\n";
}
else
{
- for ($i = 0; ($data = $db_handle->row($rs, $i)); $i++)
+ for ($i = 0; ($data = $stmt->row($i)); $i++)
{
echo "Converting '$data[0]' table...\n";
- $rs1 = $db_handle->query("show FULL columns from $data[0]");
- if(!$rs1)
+ $stmt1 = $db_handle->query("show FULL columns from $data[0]");
+ if(!$statement1)
{
echo "\n\n".$db_handle->error()."\n\n";
}
else
{
- for ($j = 0; ($data1 = $db_handle->row_keyed($rs1, $j)); $j++)
+ for ($j = 0; ($data1 = $stmt1->row_keyed($j)); $j++)
{
if (in_array(array_shift(split("\\(",
$data1['Type'],2)),
@@ -372,7 +372,7 @@
} // end of if (substr)
} // end of if (in_array)
} // end of inner for
- } // end of if ($rs1)
+ } // end of if ($stmt1)
if ($altertablecharset)
{
@@ -394,7 +394,7 @@
} // end of if ($altertablecharset)
print "done.<br>\n";
} // end of outer for
- } // end of if (!$rs)
+ } // end of if (!$stmt)
if ($alterdatabasecharset)
{
$sq='ALTER DATABASE `'.$db."` ".
diff -r 717100a34911 -r c362f89e8be9 web/auth/auth_db_ext.inc
--- a/web/auth/auth_db_ext.inc Sun Sep 25 00:24:11 2016 +0100
+++ b/web/auth/auth_db_ext.inc Sun Sep 25 08:27:55 2016 +0100
@@ -65,17 +65,17 @@
utf8_strtolower($user),
$sql_params);
- $r = $db_ext_conn->query($query, $sql_params);
+ $stmt = $db_ext_conn->query($query, $sql_params);
- if ($r === FALSE)
+ if ($stmt === FALSE)
{
trigger_error($db_ext_conn->error(), E_USER_WARNING);
fatal_error(FALSE, get_vocab("fatal_db_error"));
}
- if ($db_ext_conn->count($r) == 1) // force a unique match
+ if ($stmt->count() == 1) // force a unique match
{
- $row = $db_ext_conn->row($r, 0);
+ $row = $stmt->row(0);
switch ($auth['db_ext']['password_format'])
{
@@ -181,20 +181,20 @@
$sql_params) . "
LIMIT 1";
- $r = $db_ext_conn->query($query, $sql_params);
+ $stmt = $db_ext_conn->query($query, $sql_params);
- if ($r === FALSE)
+ if ($stmt === FALSE)
{
trigger_error($db_ext_conn->error(), E_USER_WARNING);
fatal_error(FALSE, get_vocab("fatal_db_error"));
}
- if ($db_ext_conn->count($r) == 0)
+ if ($stmt->count() == 0)
{
return 0;
}
- $row = $db_ext_conn->row($r, 0);
+ $row = $stmt->row(0);
return $row[0];
}
@@ -235,20 +235,20 @@
$sql_params)
. "
LIMIT 1";
- $r = $db_ext_conn->query($query, $sql_params);
+ $stmt = $db_ext_conn->query($query, $sql_params);
- if ($r === FALSE)
+ if ($stmt === FALSE)
{
trigger_error($db_ext_conn->error(), E_USER_WARNING);
fatal_error(FALSE, get_vocab("fatal_db_error"));
}
- if ($db_ext_conn->count($r) == 0)
+ if ($stmt->count() == 0)
{
return 0;
}
- $row = $db_ext_conn->row($r, 0);
+ $row = $stmt->row(0);
return $row[0];
}
diff -r 717100a34911 -r c362f89e8be9 web/dbsys.inc
--- a/web/dbsys.inc Sun Sep 25 00:24:11 2016 +0100
+++ b/web/dbsys.inc Sun Sep 25 08:27:55 2016 +0100
@@ -30,9 +30,9 @@
// Convenience wrapper functions
-function sql_free($r)
+function sql_free($stmt)
{
- return DB::default_db()->free($r);
+ return $stmt->free();
}
function sql_quote($identifier)
{
@@ -54,21 +54,21 @@
{
return DB::default_db()->query($sql, $params);
}
-function sql_row($r, $i)
+function sql_row($stmt, $i)
{
- return DB::default_db()->row($r, $i);
+ return $stmt->row($i);
}
-function sql_row_keyed($r, $i)
+function sql_row_keyed($stmt, $i)
{
- return DB::default_db()->row_keyed($r, $i);
+ return $stmt->row_keyed($i);
}
-function sql_all_rows_keyed($r)
+function sql_all_rows_keyed($stmt)
{
- return DB::default_db()->all_rows_keyed($r);
+ return $stmt->all_rows_keyed();
}
-function sql_count($r)
+function sql_count($stmt)
{
- return DB::default_db()->count($r);
+ return $stmt->count();
}
function sql_insert_id($table, $field)
{
@@ -126,9 +126,9 @@
{
return DB::default_db()->syntax_bitwise_xor();
}
-function sql_num_fields($result)
+function sql_num_fields($stmt)
{
- return DB::default_db()->num_fields($result);
+ return $stmt->num_fields();
}
function sql_table_exists($table)
{
diff -r 717100a34911 -r c362f89e8be9 web/lib/MRBS/DB.php
--- a/web/lib/MRBS/DB.php Sun Sep 25 00:24:11 2016 +0100
+++ b/web/lib/MRBS/DB.php Sun Sep 25 08:27:55 2016 +0100
@@ -155,16 +155,16 @@
// no results, or FALSE if there's an error
public function query_array($sql, $params = null)
{
- $res = $this->query($sql, $params);
+ $stmt = $this->query($sql, $params);
- if ($res === FALSE)
+ if ($stmt === FALSE)
{
return FALSE;
}
else
{
$result = array();
- for ($i = 0; ($row = $this->row($res, $i)); $i++)
+ for ($i = 0; ($row = $stmt->row($i)); $i++)
{
$result[] = $row[0];
}
@@ -181,64 +181,7 @@
$sth = $this->dbh->prepare($sql);
$sth->execute($params);
- return $sth;
- }
-
-
- // Return a row from a result. The first row is 0.
- // The row is returned as an array with index 0=first column, etc.
- // When called with i >= number of rows in the result, cleans up from
- // the query and returns 0.
- // Typical usage: $i = 0; while ((a = $db_obj->row($r, $i++))) { ... }
- public function row ($sth, $i)
- {
- if ($i >= $sth->rowCount())
- {
- $sth->closeCursor();
- return 0;
- }
- return $sth->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, $i);
- }
-
-
- // Return a row from a result as an associative array keyed by field name.
- // The first row is 0.
- // This is actually upward compatible with row() since the underlying
- // 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.
- public function row_keyed ($sth, $i)
- {
- if ($i >= $sth->rowCount())
- {
- $sth->closeCursor();
- return 0;
- }
- return $sth->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $i);
- }
-
- // Return all the rows from a result object, as an array of arrays
- // keyed on the column name
- public function all_rows_keyed($r)
- {
- $result = array();
-
- for ($i=0; $row = $this->row_keyed($r, $i); $i++)
- {
- $result[] = $row;
- }
-
- return $result;
- }
-
-
- // Free a results handle. You need not call this if you call row() or
- // row_keyed() until the row returns 0, since those methods free the results
- // handle when you finish reading the rows.
- function free (&$sth)
- {
- $sth->closeCursor();
- unset($sth);
+ return new DBStatement($this, $sth);
------------------------------------------------------------------------------
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits