Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Copyright 2009 Mo McRoberts.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. The names of the author(s) of this software may not be used to endorse
- * or promote products derived from this software without specific prior
- * written permission.
- *
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
- * AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- class DBCore
- {
- protected $rsClass;
- public function vquery($query, $params)
- {
- if(!is_array($params)) $params = array();
- $sql = preg_replace('/\?/e', "\$this->quote(array_shift(\$params))", $query);
- return $this->execute($sql);
- }
- public function exec($query)
- {
- $params = func_get_args();
- array_shift($params);
- if($this->vquery($query, $params))
- {
- return true;
- }
- return false;
- }
- /* $rs = $inst->query('SELECT * FROM `sometable` WHERE `field` = ? AND `otherfield` = ?', $something, 27); */
- public function query($query)
- {
- $params = func_get_args();
- array_shift($params);
- if(($r = $this->vquery($query, $params)))
- {
- return new $this->rsClass($this, $r);
- }
- return null;
- }
- public function getRow($query)
- {
- $row = null;
- $params = func_get_args();
- array_shift($params);
- if(($r = $this->vquery($query, $params)))
- {
- $rs = $this->rsClass($this, $r);
- $row = $rs->next();
- $rs = null;
- }
- return $row;
- }
- protected function reportError($errmsg, $sqlString)
- {
- ini_set('display_errors', 'On');
- trigger_error($errmsg . ' while executing ' . $sqlString, E_USER_ERROR);
- }
- public function insert($table, $kv)
- {
- $keys = array_keys($kv);
- $values = array_values($kv);
- array_walk($keys, array($this, 'quoteObjectRef'));
- array_walk($values, array($this, 'quoteRef'));
- $sql = 'INSERT INTO ' . $this->quoteObject($table) . '(' . implode(',', $keys) . ') VALUES (' . implode(',', $values) . ')';
- return $this->execute($sql);
- }
- public function update($table, $kv, $clause)
- {
- $sql = 'UPDATE ' . $this->quoteObject($table) . ' SET ';
- foreach($kv as $key => $value)
- {
- $sql .= $this->quoteObject($key) . ' = ' . $this->quote($value) . ', ';
- }
- $sql = substr($sql, 0, -2);
- if(is_string($clause) && strlen($clause))
- {
- $sql .= ' WHERE ' . $clause;
- }
- else if(is_array($clause) && count($clause))
- {
- $sql .= ' WHERE ';
- foreach($clause as $key => $value)
- {
- $sql .= $this->quoteObject($key) . ' = ' . $this->quote($value) . ' AND ';
- }
- $sql = substr($sql, 0, -4);
- }
- return $this->execute($sql);
- }
- public function quoteObject($name)
- {
- return '"' . $name . '"';
- }
- public function quoteRef(&$value)
- {
- $value = $this->quote($value);
- }
- public function quoteObjectRef(&$name)
- {
- $name = $this->quoteObject($name);
- }
- }
- /* while(($row = $rs->next())) { ... } */
- class DBDataSet
- {
- public $fields = array();
- public $EOF = true;
- public $db;
- protected $resource;
- public function __construct($db, $resource)
- {
- $this->db = $db;
- $this->resource = $resource;
- $this->EOF = false;
- }
- public function next()
- {
- if($this->EOF) return false;
- if(!$this->getRow())
- {
- $this->EOF = true;
- return null;
- }
- return $this->fields;
- }
- }
- class MySQL extends DBCore
- {
- protected $rsClass = 'MySQLSet';
- protected $mysql;
- public function __construct($name = null, $host = null, $user = null, $pass = null)
- {
- if(!($this->mysql = mysql_connect($host, $user, $pass)))
- {
- $this->raiseError(null);
- }
- if(!mysql_select_db($name, $this->mysql))
- {
- $this->raiseError(null);
- }
- }
- public function quoteObject($name)
- {
- return '`' . $name . '`';
- }
- protected function execute($sql)
- {
- $r = mysql_query($sql, $this->mysql);
- if($r === false)
- {
- $this->raiseError($sql);
- }
- return $r;
- }
- protected function raiseError($query)
- {
- return $this->reportError(mysql_error($this->mysql), $query);
- }
- public function quote($string)
- {
- if(is_null($string))
- {
- return 'NULL';
- }
- if(is_bool($string))
- {
- return ($string ? "'Y'" : '"N"');
- }
- return "'" . mysql_real_escape_string($string, $this->mysql) . "'";
- }
- public function getRow($query)
- {
- $row = null;
- $params = func_get_args();
- array_shift($params);
- if(($r = $this->vquery($query . ' LIMIT 1', $params)))
- {
- $row = mysql_fetch_assoc($r);
- }
- return $row;
- }
- public function insertId()
- {
- return mysql_insert_id($this->mysql);
- }
- }
- class MySQLSet extends DBDataSet
- {
- protected function getRow()
- {
- return ($this->fields = mysql_fetch_assoc($this->resource));
- }
- }
- global $DB;
- $DB = new MySQL(DB_NAME, DB_HOST, DB_USER, DB_PASSWORD);
Add Comment
Please, Sign In to add comment