Changeset:
ccd01b3edd4f
https://sourceforge.net/p/mrbs/hg-code/ci/ccd01b3edd4fdb79da55e7d3b1981f1f5cc3018b
Author:
John Beranek <[email protected]>
Date:
Sat Sep 24 12:11:27 2016 +0100
Log message:
New DB classes, to replace mysqli.inc, pgsql.inc and most of dbsys.inc
diffstat:
web/dbtest.php | 28 ++++
web/lib/MRBS/DB.php | 257 ++++++++++++++++++++++++++++++++++++++
web/lib/MRBS/DBFactory.php | 26 +++
web/lib/MRBS/DB_mysql.php | 292 +++++++++++++++++++++++++++++++++++++++++++
web/lib/MRBS/DB_pgsql.php | 303 +++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 906 insertions(+), 0 deletions(-)
diffs (truncated from 926 to 300 lines):
diff -r fd741544b502 -r ccd01b3edd4f web/dbtest.php
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/web/dbtest.php Sat Sep 24 12:11:27 2016 +0100
@@ -0,0 +1,28 @@
+<?php
+namespace MRBS;
+
+// $Id$
+
+// Index is just a stub to redirect to the appropriate view
+// as defined in config.inc.php using the variable $default_view
+// If $default_room is defined in config.inc.php then this will
+// be used to redirect to a particular room.
+
+require "defaultincludes.inc";
+require_once "mrbs_sql.inc";
+
+print "<h2>pgsql</h2>";
+
+$db_obj = DBFactory::create('pgsql', 'localhost', 'mrbs', 'mrbs-password',
'mrbs');
+print $db_obj->query1("SELECT VERSION()");
+print "<p>".$db_obj->version()."</p>";
+
+print "<pre>".print_r($db_obj->field_info("mrbs_entry"), 1)."</pre>";
+
+print "<h2>default db</h2>";
+
+print "<p>".DB::default_db()->version()."</p>";
+
+print "<p>".DB::default_db()->query1("SELECT 1+4")."</p>";
+
+print "<pre>".print_r(DB::default_db()->field_info("mrbs_entry"), 1)."</pre>";
diff -r fd741544b502 -r ccd01b3edd4f web/lib/MRBS/DB.php
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/MRBS/DB.php Sat Sep 24 12:11:27 2016 +0100
@@ -0,0 +1,257 @@
+<?php
+
+namespace MRBS;
+
+use PDO;
+use PDOException;
+
+
+//
+class DB
+{
+ const DB_DEFAULT_PORT = null;
+ const DB_DBO_DRIVER = null;
+ private $dbh = null;
+ private $mutex_lock_name;
+ static private $default_db_obj;
+
+ //
+ public function __construct($db_host, $db_username, $db_password,
+ $db_name, $persist = 0, $db_port = null)
+ {
+
+ // Early error handling, could be in constructor instead?
+ if (is_null(static::DB_DBO_DRIVER) ||
+ is_null(static::DB_DEFAULT_PORT))
+ {
+ print "Encountered a fatal bug in DB abstraction code!\n";
+ return null;
+ }
+
+ // If no port has been provided, set a SQL variant dependent default
+ if (empty($db_port))
+ {
+ $db_port = static::DB_DEFAULT_PORT;
+ print "Setting default port to $db_port\n";
+ }
+
+ // Establish a database connection.
+
+ // On connection error, the message will be output without a proper HTML
+ // header. There is no way I can see around this; if track_errors isn't on
+ // there seems to be no way to supress the automatic error message output
and
+ // still be able to access the error text.
+
+ try
+ {
+ $this->dbh = new
PDO(static::DB_DBO_DRIVER.":host=$db_host;port=$db_port;dbname=$db_name",
+ $db_username,
+ $db_password,
+ array(PDO::ATTR_PERSISTENT => ($persist ? true :
false)));
+ }
+ catch (PDOException $e)
+ {
+ trigger_error($e->getMessage(), E_USER_WARNING);
+ if ($e->getCode() == 2054)
+ {
+ $message = "It looks like you have an old style MySQL password stored,
which cannot be " .
+ "used with PDO (though it is possible that mysqli may have
accepted it). Try " .
+ "deleting the MySQL user and recreating it with the same
password.";
+ trigger_error($message, E_USER_WARNING);
+ }
+ echo "\n<p>\n" . get_vocab("failed_connect_db") . "\n</p>\n";
+ exit;
+ }
+ }
+
+
+ // Static function to return a DB object for the default MRBS database
connection,
+ // will make the connection if it hasn't been made yet.
+ static public function default_db()
+ {
+ if (is_null(self::$default_db_obj))
+ {
+ global $db_persist, $db_host, $db_login, $db_password,
+ $db_database, $db_port, $dbsys;
+
+ self::$default_db_obj = DBFactory::create($dbsys, $db_host, $db_login,
$db_password,
+ $db_database, $db_persist,
$db_port);
+ }
+ return self::$default_db_obj;
+ }
+
+
+ //
+ public function error()
+ {
+ $error = "No database connection!";
+
+ if ($this->dbh)
+ {
+ $error_info = $this->dbh->errorInfo();
+ $error = $error_info[2];
+ }
+ return $error;
+ }
+
+ // 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.
+ public function command($sql, $params = array())
+ {
+ $ret = -1;
+
+ try
+ {
+ $sth = $this->dbh->prepare($sql);
+ $sth->execute($params);
+ }
+ catch (PDOException $e)
+ {
+ return $ret;
+ }
+
+ if ($sth)
+ {
+ $ret = $sth->rowCount();
+ }
+
+ return $ret;
+ }
+
+
+ // Execute an SQL query which should return a single non-negative number
value.
+ // This is a lightweight alternative to sql_query, good for use with count(*)
+ // and similar queries. It returns -1 on error or if the query did not return
+ // 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 query1($sql, $params = array())
+ {
+ $sth = $this->dbh->prepare($sql);
+ if (!$sth)
+ {
+ trigger_error($this->error(), E_USER_WARNING);
+ return -1;
+ }
+ $sth->execute($params);
+
+ if (($sth->rowCount() != 1) || ($sth->columnCount() != 1) ||
+ (($row = $sth->fetch(PDO::FETCH_NUM)) == NULL))
+ {
+ $result = -1;
+ }
+ else
+ {
+ $result = $row[0];
+ }
+ $sth->closeCursor();
+ return $result;
+ }
+
+
+ // Execute an SQL query. Returns a result handle, which should be passed
+ // back to row() or row_keyed() to get the results.
+ // Returns FALSE on error; use error() to get the error message.
+ public function query ($sql, $params = array())
+ {
+ $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 sql_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);
+ }
+
+ // 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);
+ }
+
+
+ // Commit (end) a transaction. See begin().
+ function commit()
+ {
+ $result = $this->command("COMMIT");
+
+ if ($result < 0)
+ {
+ trigger_error ($this->error(), E_USER_WARNING);
+ }
+ }
+
+
+ // Commit (end) a transaction. See begin().
+ function rollback()
+ {
+ $result = $this->command("ROLLBACK", array());
+
+ if ($result < 0)
+ {
+ trigger_error ($this->error(), E_USER_WARNING);
+ }
+ }
+
+
+ // Return the number of rows returned by a result handle from query().
+ public function count($sth)
+ {
+ return $sth->rowCount();
+ }
+
+
+ // Returns the number of fields in a result.
+ public function num_fields($result)
+ {
+ return $result->columnCount();
+ }
+
+
+ // Return a string identifying the database version
+ function version()
+ {
+ return $this->query1("SELECT VERSION()");
+ }
+
+
+ //
+ public function special($foo)
+ {
+ return "nope";
+ }
+}
diff -r fd741544b502 -r ccd01b3edd4f web/lib/MRBS/DBFactory.php
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/MRBS/DBFactory.php Sat Sep 24 12:11:27 2016 +0100
@@ -0,0 +1,26 @@
+<?php
+
+namespace MRBS;
------------------------------------------------------------------------------
_______________________________________________
Mrbs-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mrbs-commits