Location: PHPKode > projects > Blackwolf CMF > projectBlackWolf/lib/Data/Storage/Mysql.class.php
<?php

/* Please see the README and LICENSE files. */

/**
 * Concrete database implementation for MYSQL databases
 */
class Data_Storage_Mysql extends Data_Storage_DatabaseController {

    /**
     * The default host for connection
     * @var String
     */
    private static $default_host = "";

    /**
     * Default database to pick
     * @var String
     */
    private static $default_db = "";

    /**
     * Username for read-only access
     * @var String
     */
    private static $default_read_user = "";

    /**
     * Password for read-only access
     * @var String
     */
    private static $default_read_pw = "";

    /**
     * Username for read/write access
     * @var String
     */
    private static $default_write_user = "";

    /**
     * Password for read/write access
     * @var String
     */
    private static $default_write_pw = "";

    const READ = 0;
    const WRITE = 1;

    protected $usingPrep = false;
    protected $connection;
    protected $stmt;
    protected $resultArgs;

    /**
     * @see Data_Storage_Database
     * @param Boolean $read_only True if read_only mode is on
     * @param Boolean $preparedStmt True if using prepared statements
     */
    public function connect($location=NULL, $user=NULL, $pw=NULL, $db=NULL, $read_only=true, $preparedStmt=false) {
        $this->usingPrep = $preparedStmt;
        $location = is_null($location) ? self::$default_host : $location;
        $db = is_null($db) ? self::$default_db : $db;
        $user = is_null($user) ?
                ($read_only == true ?
                        self::$default_read_user :
                        self::$default_write_user) :
                $location;
        $pass = is_null($pw) ?
                ($read_only == true ?
                        self::$default_read_pw :
                        self::$default_write_pw) :
                $pw;

        $connection = NULL;

        if ($preparedStmt) {
            $connection = new mysqli($location, $user, $pass, $db);
        } else {
            $connection = @mysql_connect($location, $user, $pass);
            if (!($connection === false)) {
                @mysql_select_db($db);
            }
        }
        $this->connection = $connection;
        return $this->connection;
    }

    /**
     * @see Data_Storage_Database
     */
    public function disconnect() {
        mysql_close($this->connection);
    }

    /**
     * @see Data_Storage_Database
     */
    public function get_error() {
        return mysql_error($this->connection);
    }

    /**
     * @see Data_Storage_Database
     * @return StdObject The result
     */
    public function get_result() {
        if(!isset($this->stmt) || $this->stmt == false || $this->stmt == NULL){
            return false;
        }
        
        $result = array();

        $metadata = $this->stmt->result_metadata();
        $fields = $metadata->fetch_fields();

        for (;;) {
            $pointers = array();
            $row = new stdClass();

            $pointers[] = $this->stmt;
            foreach ($fields as $field) {
                $fieldname = $field->name;
                $pointers[] = &$row->$fieldname;
            }

            call_user_func_array("mysqli_stmt_bind_result", $pointers);

            if (!$this->stmt->fetch()) {
                break;
            }
            $result[] = $row;
        }
        $metadata->free();
        return $result;
    }

    /**
     * @see Data_Storage_Database
     */
    public function query($sql, $params=NULL) {
        if ($this->usingPrep) {
            if (!$this->set_prepared_query($sql)) {
                return false;
            }
            $this->bind_prepared_args($params);
            $this->stmt->execute();
        } else {
            # TODO
        }
    }

    public function set_prepared_query($query) {
        $this->stmt = @$this->connection->prepare($query);
        if (!$this->stmt) {
            trigger_error("Invalid query. (" . mysqli_error($this->connection) . ")");
            return false;
        }
        return true;
    }

    public function bind_prepared_args($args) {
        mysqli_stmt_bind_param($this->stmt, array_shift($args), $args);
    }

}

?>
Return current item: Blackwolf CMF