Location: PHPKode > projects > Breeze CMS - A PHP Content Manage System > libs/Ice/Database/Driver.class.php
<?php
require_once ICE_DIR . "/Ice/Database/adodb_lite/adodb.inc.php";
// Driver
class Ice_Database_Driver
{
    /**
     * 数据库连接信息
     *
     * @var string
     */
    var $dsn = "mysql://hide@address.com/test?false#Pear";
    
    /**
     * 数据库连接句柄
     *
     * @var resource
     */
    var $dbo = null;
    
    /**
     * 数据表前缀
     *
     * @var string
     */
    var $dbPrefix = "";
    
    /**
     * SQL查询句柄
     *
     * @var resource
     */
    var $queryId = null;
    
    /**
     * SQL查询次数
     *
     * @var int
     */
    var $queryNum = 0;
    
    /**
     * 出错之后是否停止运行程序
     *
     * @var boolean
     */
    var $isHalt = true;
    
    /**
     * 错误信息
     *
     * @var string
     */
    var $error = null;
    
    /**
     * 构造函数
     *
     * @param string $dsn
     * @return void
     */
    function Ice_Database_Driver()
    {
    }
    
    function getInstance()
    {
        static $instance;
        
        if (!empty($instance))
        {
            $instance = new Ice_Database_Driver();
        }
        
        return $instance;
    }
    
    /**
     * 数据库连接
     *
     * @param string $dsn
     * @return boolean
     */
    function connect($dsn = null, $dbPrefix = "")
    {
        if ($this->dbo) return true;
        if (!$dsn) $dsn = $this->dsn;
        
        $this->dbPrefix = $dbPrefix;
        
        $this->dbo = ADONewConnection($dsn);

        if(!$this->dbo)
        {
        	$this->halt("Can't link to database!");	
        }
        
        $this->_execute("SET CHARACTER_SET_CLIENT = utf8, " .
        				"CHARACTER_SET_CONNECTION = utf8, " .
        				"CHARACTER_SET_DATABASE   = utf8, " .
        				"CHARACTER_SET_RESULTS    = utf8, " .
        				"CHARACTER_SET_SERVER     = utf8, " .
        				"COLLATION_CONNECTION     = utf8_general_ci, " .
        				"COLLATION_DATABASE       = utf8_general_ci, " .
        				"COLLATION_SERVER         = utf8_general_ci ");
        
        unset($user, $pass, $host, $port, $pconnect, $dbName);
        
        return true;
    }
    
    /**
     * SQL查询
     *
     * @param string $sql
     * @return resource
     */
    function query($sql)
    {
        $this->dbPrefix == 'bz_' or $sql = str_replace("bz_", $this->dbPrefix, $sql);
        
        return $this->_execute($sql);
    }

    /**
     * 执行一个查询,返回查询结果记录集
     *
     * @param string $sql
     * @return array
     */
    function getAll($sql)
    {
        $this->dbPrefix == 'bz_' or $sql = str_replace("bz_", $this->dbPrefix, $sql);
        
        return $this->dbo->GetAll($sql);
    }

    /**
     * 执行查询,返回第一条记录
     *
     * @param string $sql
     * @return mixed
     */
    function getOne($sql)
    {
        $this->dbPrefix == 'bz_' or $sql = str_replace("bz_", $this->dbPrefix, $sql);
        $list = $this->dbo->SelectLimit($sql, 1);

        return $list ? $list->GetAll() : false;
    }
    
    /**
     * execute sql 
     * @param $strQuery
     * @return mix
     */
    function _execute($sql) {

		if (null === $this->dbo->Execute($sql)) {
			$this->halt("Sorry, can't execute your SQL", $sql);
			return false;
		}
		return true;
	}
    
    /**
     * 显示错误信息
     * 
     * @return  boolean
     */
    function halt($msg, $sql = "")
    {
        $err = '<pre style="font: 12px vernada, arial;">';
        $err .= "<b>Error Infomation: </b>".$msg."\n";
        //$err .= "<b>MySQL Errno: </b> ".$this->errno()."<br />";
        //$err .= "<b>MySQL Error: </b> ".$this->error();

        if( $sql ) $err .= "\n<b>SQL:</b> ".$sql;

        $err .= '</pre>';

        if ($this->isHalt)
        {
            echo nl2br($err);die;
        }
        else
        {
            $this->error = $err;
        }
        
        return true;
    }
}
Return current item: Breeze CMS - A PHP Content Manage System