[geeklog-cvs] geeklog: Add support for the PHP MySQLi extension (patch #000130...

geeklog-cvs at lists.geeklog.net geeklog-cvs at lists.geeklog.net
Tue Jun 14 06:13:19 EDT 2011


changeset 8313:4bf2b5890d9b
url:  http://project.geeklog.net/cgi-bin/hgwebdir.cgi/geeklog/rev/4bf2b5890d9b
user: Dirk Haun <dirk at haun-online.de>
date: Mon Jun 13 16:40:59 2011 +0200
description:
Add support for the PHP MySQLi extension (patch #0001303, provided by mystral-kk)

diffstat:

 system/databases/mysqli.class.php |  723 ++++++++++++++++++++++++++++++++++++++
 system/lib-database.php           |   11 +-
 2 files changed, 731 insertions(+), 3 deletions(-)

diffs (truncated from 760 to 300 lines):

diff -r 9bb10ab247a3 -r 4bf2b5890d9b system/databases/mysqli.class.php
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/system/databases/mysqli.class.php	Mon Jun 13 16:40:59 2011 +0200
@@ -0,0 +1,723 @@
+<?php
+
+/* Reminder: always indent with 4 spaces (no tabs). */
+// +---------------------------------------------------------------------------+
+// | Geeklog 1.8                                                               |
+// +---------------------------------------------------------------------------+
+// | mysqli.class.php                                                          |
+// |                                                                           |
+// | mysql database class with MySQLi extension                                |
+// +---------------------------------------------------------------------------+
+// | Copyright (C) 2000-2011 by the following authors:                         |
+// |                                                                           |
+// | Authors: Tony Bibbs, tony AT tonybibbs DOT com                            |
+// |          Kenji Ito, geeklog AT mystral-kk DOT net                         |
+// +---------------------------------------------------------------------------+
+// |                                                                           |
+// | This program is free software; you can redistribute it and/or             |
+// | modify it under the terms of the GNU General Public License               |
+// | as published by the Free Software Foundation; either version 2            |
+// | of the License, or (at your option) any later version.                    |
+// |                                                                           |
+// | This program is distributed in the hope that it will be useful,           |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of            |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             |
+// | GNU General Public License for more details.                              |
+// |                                                                           |
+// | You should have received a copy of the GNU General Public License         |
+// | along with this program; if not, write to the Free Software Foundation,   |
+// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
+// |                                                                           |
+// +---------------------------------------------------------------------------+
+
+/**
+* This file is the mysql implementation of the Geeklog abstraction layer.
+* Unfortunately the Geeklog abstraction layer isn't 100% abstract because a few
+* key functions use MySQL's REPLACE INTO syntax which is not a SQL standard.
+* This issue will need to be resolved some time ...
+*/
+class database
+{
+    private $_host = '';
+    private $_name = '';
+    private $_user = '';
+    private $_pass = '';
+    private $_db = '';
+    private $_verbose = FALSE;
+    private $_display_error = FALSE;
+    private $_errorlog_fn = '';
+    private $_charset = '';
+    private $_mysql_version = 0;
+
+    /**
+    * Logs messages
+    *
+    * Logs messages by calling the function held in $_errorlog_fn
+    *
+    * @param    string      $msg        Message to log
+    * @access   private
+    */
+    private function _errorlog($msg)
+    {
+        $function = $this->_errorlog_fn;
+
+        if (function_exists($function)) {
+            $function($msg);
+        }
+    }
+
+    /**
+    * Connects to the MySQL database server
+    *
+    * This function connects to the MySQL server and returns the connection object
+    *
+    * @return   object      Returns connection object
+    * @access   private
+    */
+    private function _connect()
+    {
+        if ($this->_verbose) {
+            $this->_errorlog("\n*** Inside database->_connect ***");
+        }
+
+        // Connect to MySQL server
+        $this->_db = new MySQLi($this->_host, $this->_user, $this->_pass) OR die('Cannot connect to DB server');
+        $this->_mysql_version = $this->_db->server_version;
+
+        // Set the database
+        $this->_db->select_db($this->_name) OR die('error selecting database');
+
+        if (!($this->_db)) {
+            if ($this->_verbose) {
+                $this->_errorlog("\n*** Error in database->_connect ***");
+            }
+
+            // damn, got an error.
+            $this->dbError();
+        }
+
+        if ($this->_mysql_version >= 40100) {
+            if ($this->_charset === 'utf-8') {
+                $result = FALSE;
+
+                if (method_exists($this->_db, 'set_charset')) {
+                    $result = $this->_db->set_charset('utf8');
+                }
+
+                if (!$result) {
+                    @$this->_db->query("SET NAMES 'utf8'");
+                }
+            }
+        }
+
+        if ($this->_verbose) {
+            $this->_errorlog("\n***leaving database->_connect***");
+        }
+    }
+
+    /**
+    * constructor for database class
+    *
+    * This initializes an instance of the database object
+    *
+    * @param        string      $dbhost     Database host
+    * @param        string      $dbname     Name of database
+    * @param        sring       $dbuser     User to make connection as
+    * @param        string      $pass       Password for dbuser
+    * @param        string      $errorlogfn Name of the errorlog function
+    * @param        string      $charset    character set to use
+    */
+    public function __construct($dbhost, $dbname, $dbuser, $dbpass, $errorlogfn = '',
+        $charset = '')
+    {
+        $this->_host = $dbhost;
+        $this->_name = $dbname;
+        $this->_user = $dbuser;
+        $this->_pass = $dbpass;
+        $this->_verbose = FALSE;
+        $this->_errorlog_fn = $errorlogfn;
+        $this->_charset = strtolower($charset);
+        $this->_mysql_version = 0;
+
+        $this->_connect();
+    }
+
+    public function __destruct()
+    {
+        @$this->_db->close();
+        $this->_db = NULL;
+    }
+
+    /**
+    * Turns debug mode on
+    *
+    * Set this to TRUE to see debug messages
+    *
+    * @param    boolean     $flag
+    */
+    public function setVerbose($flag)
+    {
+        $this->_verbose = (bool) $flag;
+    }
+
+    /**
+    * Turns detailed error reporting on
+    *
+    * If set to TRUE, this will display detailed error messages on the site.
+    * Otherwise, it will only that state an error occurred without going into
+    * details. The complete error message (including the offending SQL request)
+    * is always available from error.log.
+    *
+    * @param    boolean     $flag
+    */
+    public function setDisplayError($flag)
+    {
+        $this->_display_error = (bool) $flag;
+    }
+
+    /**
+    * Checks to see if debug mode is on
+    *
+    * Returns value of $_verbose
+    *
+    * @return   boolean     TRUE if in verbose mode otherwise FALSE
+    */
+    public function isVerbose()
+    {
+        if ($this->_verbose
+         AND (empty($this->_errorlog_fn) OR !function_exists($this->_errorlog_fn))) {
+            echo "\n<br" . XHTML . "><b>Can't run mysqli.class.php verbosely because the errorlog "
+                . "function wasn't set or doesn't exist</b><br" . XHTML . ">\n";
+            return FALSE;
+        }
+
+        return $this->_verbose;
+    }
+
+    /**
+    * Sets the function this class should call to log debug messages
+    *
+    * @param        string      $functionname   Function name
+    */
+    public function setErrorFunction($functionname)
+    {
+        $this->_errorlog_fn = $functionname;
+    }
+
+    /**
+    * Executes a query on the MySQL server
+    *
+    * This executes the passed SQL and returns the recordset or errors out
+    *
+    * @param    string      $sql            SQL to be executed
+    * @param    boolean     $ignore_error   If 1 this function supresses any
+    *                                       error messages
+    * @return   object      Returns results of query
+    */
+    public function dbQuery($sql, $ignore_errors=0)
+    {
+        if ($this->_verbose) {
+            $this->_errorlog("\n***inside database->dbQuery***");
+            $this->_errorlog("\n*** sql to execute is $sql ***");
+        }
+
+        // Run query
+        if ($ignore_errors == 1) {
+            $result = @$this->_db->query($sql);
+        } else {
+            $result = @$this->_db->query($sql) OR trigger_error($this->dbError($sql), E_USER_ERROR);
+        }
+
+        // If OK, return otherwise echo error
+        if ($this->_db->errno == 0 AND ($result !== FALSE)) {
+            if ($this->_verbose) {
+                $this->_errorlog("\n***sql ran just fine***");
+                $this->_errorlog("\n*** Leaving database->dbQuery ***");
+            }
+
+            return $result;
+        } else {
+            // callee may want to supress printing of errors
+            if ($ignore_errors == 1) {
+                return FALSE;
+            }
+
+            if ($this->_verbose) {
+                $this->_errorlog("\n***sql caused an error***");
+                $this->_errorlog("\n*** Leaving database->dbQuery ***");
+            }
+        }
+    }
+
+    /**
+    * Saves information to the database
+    *
+    * This will use a REPLACE INTO to save a record into the
+    * database
+    *
+    * @param    string      $table      The table to save to
+    * @param    string      $fields     string  Comma demlimited list of fields to save
+    * @param    string      $values     Values to save to the database table
+    */
+    public function dbSave($table, $fields, $values)
+    {
+        if ($this->_verbose) {
+            $this->_errorlog("\n*** Inside database->dbSave ***");
+        }
+
+        $sql = "REPLACE INTO $table ($fields) VALUES ($values)";
+
+        $this->dbQuery($sql);
+
+        if ($this->_verbose) {
+            $this->_errorlog("\n*** Leaving database->dbSave ***");
+        }
+    }
+
+    /**
+    * Builds a pair of a column name and a value that can be used as a part of an SQL
+    *
+    * @param   scalar/array  $id
+    * @param   scalar/array  $value
+    * @return  mixed         string = column_name-value pair(s), FALSE = error
+    */
+    private function _buildIdValuePair($id, $value)
+    {
+        $retval = '';
+
+        if (is_array($id) OR is_array($value)) {
+            $num_ids = count($id);
+
+            if (is_array($id) AND is_array($value) AND ($num_ids === count($value))) {
+                // they are arrays, traverse them and build sql
+                $retval .= ' WHERE ';
+
+                for ($i = 1; $i <= $num_ids; $i ++) {
+                    $retval .= current($id) . " = '"



More information about the geeklog-cvs mailing list