All pastes #732058 Raw Edit

Mine

public php v1 · immutable
·published 2007-10-10 16:51 UTC
rendered paste body
class Db {    /**     * PDO resource     *      * @var resource     */    private $hResource     = FALSE;        /**     * PDO object handle     *      * @var object     */    private $hMySql        = NULL;        /**     * Current type of the database     *      * @var int     */    public  $dbType        = DB_MYSQL;        /**     * Count of the fields returned by query     *      * @var int     */    private $count         = 0;        /**     * Status of the transaction     *      * @var boolean     */    private $isTransaction = FALSE;        /**     * The count of queries executed by this instance of the database     *      * @var int     */    public  $totalQueries  = 0;        /**     * Total duration of the queries in miliseconds     *      * @var int     */    public  $queriesTakes  = 0;        /**     * Fields selected from the database     *      * @var array     */    public $fields = array();        /**     * The fetchmode     *      * @var int     */    public $fetchMode = PDO::FETCH_ASSOC;        /**     * Make a connection with SQL and choose a database     * @throws Exception     * @return void     */    function __construct() {        $tmp = '';        if (DB_CONNECT_TYPE == 'unix_socket') {            $tmp = ':';        }                if (DB_PERSISTENT) {            $this->hMySql = mysql_pconnect($tmp . DB_SERVER, DB_USER, DB_PASSWORD);        } else {            $this->hMySql = mysql_connect($tmp . DB_SERVER, DB_USER, DB_PASSWORD);        }                if (!$this->hMySql) {            throw new Exception('mysql_connect() failed: ' . mysql_error());        }                        if (!mysql_select_db(DB_NAME, $this->hMySql)) {            throw new Exception('mysql_select_db() failed: FALSE returned.');        }        $this->execute('SELECT NOW()');        $this->execute("SET NAMES " . DB_CHARSET);    }    /**     * Build SQL query     *      * @param   string  query with specifiers     * @param   mixed   arguments     * @return  string  builded query (on error is returned boolean FALSE)     * @throws  Exception     *      * @example $builded = buildQuery("SELECT * FROM users WHERE name=%s AND id=%u", "John Brown", 10);     * The possible specifiers are:     * %d double     * %f float     * %s string     * %u integer     * %x hexadecimal     *      * @author Sysel2001     */    public function buildQuery($query, $args) {        $chunks     = array();        $start      = strpos($query, "%");        $length     = strlen($query);        $otherStart = 0;        while (FALSE !== $start && ($start < $length - 1)) {            $chunks[]   = substr($query, $otherStart, $start - $otherStart);            $otherStart = $start + 2;            $type       = $query[1 + $start];            if (strstr("dfsuxn", $type)) {                $next = each($args);                if (!$next) {                    throw new Exception("We have more specifiers, than arguments.");                }                         $arg = $next[1];                switch ($type) {                    case "d":                        $value = intval($arg, 10);                        break;                                              case "f":                        $value = floatval($arg);                        break;                                               case "s":                        if (strcasecmp('NULL', $arg) != 0) {                            $value = $this->hPdo->quote($arg, PDO::PARAM_STR);                        } else {                            $value = $arg;                        }                                                break;                                        case "u":                        $value = abs(intval($arg, 10));                        break;                                               case "x":                        $value = "0x". dechex($arg);                        break;                }                                  $chunks[] = $value;            } else {                $chunks[] = ($type != "%" ? "%$type" : "%");            }                      $start = strpos($query, "%", $start + 2);        }              if (each($args)) {            throw new Exception('We have more arguments, than specifiers.');        }              $chunks[] = substr($query, $otherStart);        return implode('', $chunks);    }        /**     * Execute SQL query     *     * @param   string   SQL query     * @param   mixed    this parameter isn't nescessary, it can contain args     * @return  void     * @throws  Exception     */    public function execute() {        $this->totalQueries++;        $queryStarted    = get_microtime();        $args            = func_get_args();        $query           = array_shift($args);        $this->count     = 0;        $this->fields    = array();        if (FALSE !== stripos($query, "SELECT")) {            $this->hResource = FALSE;            $this->hResource = mysql_query($this->buildQuery($query, $args), $this->hMySql);            if (FALSE === $this->hResource) {                if ($this->isTransaction) {                    mysql_query('ROLLBACK', $this->hMySql);                }                                throw new Exception('Database error: ' . mysql_errno($this->hMySql) . "\n\n" . mysql_error());            }                        $fetchType = $this->fetchMode == PDO::FETCH_ASSOC ? MYSQL_ASSOC : MYSQL_NUM;            $this->fields    = mysql_fetch_array($this->hResource, $fetchType);            $this->count     = sizeof($this->fields);        } else {            if (!mysql_query($this->buildQuery($query, $args), $this->hMySql)) {                if ($this->isTransaction) {                    mysql_query('ROLLBACK', $this->hMySql);                }                                throw new Exception('Database error: ' . mysql_errno($this->hMySql) . "\n\n" . mysql_error());            }        }                $this->queriesTakes += get_microtime() - $queryStarted;    }        /**     * Get the number of selected records from database     *      * @return  int  the number of selected records     */    public function recordCount() {        return $this->count;    }        /**     * Begin transaction     *      * @return void     * @throws Exception     */    public function beginTransaction() {        if ($this->isTransaction) {            mysql_query('ROLLBACK', $this->hMySql);            throw new Exception('Uncommited transaction, rollbacking...');        }                mysql_query('BEGIN TRANSACTION', $this->hMySql);        $this->isTransaction = TRUE;            }        /**     * Finish transaction     *     * @return void     * @throws Exception     */    public function commit() {        if (!$this->isTransaction) {            throw new Exception('commit() called without a reason.');        }                mysql_query('COMMIT', $this->hMySql);        $this->isTransaction = FALSE;    }        /**     * Destruct this object     * @return void     * @throws Exception     */    function __destruct() {        if ($this->isTransaction) {            mysql_query('ROLLBACK', $this->hMySql);            throw new Exception('Uncommited transaction, rollbacking...');        }                mysql_close($this->hMySql);        $this->hMySql = NULL;    }        /**     * MySql connection cannot be cloned, while with PDO it can be...     */    function __clone() {        clone($this);        die('here');    }}?>