返回顶部

收藏

MST Library 3.1 数据库连接工厂DBC

更多

就是数据库连接工厂了,多的就不多说了

DBC类与接口

<?php

/**
 * DataBase Common Connector
 * 数据库共用连接器,连接驱动以如下的方式存放:
 * DBC/PdoOci.php
 * DBC/PdoMySQL.php
 * DBC/MySQLi.php
 * 连接驱动必须实现MST_IDBC接口
 *
 * 调用一个数据库的连接实例如下:
 * <code>
 * MST_DBC::connect($remote);
 * # $remote对应config中配置
 * </code>
 *
 * @author Janpoem
 */

interface MST_IDBC {

    public function connect(& $config);

    public function disconnect();

    public function getStatement();

    public function getConnector();

    public function getFecthMode($mode = MST_DBC::FETCH_ASSOC);

    public function lastSql(& $index = 0);

    public function lastInsertId($table, $column);

    public function & query($sql, $params = null);

    public function execute($sql, $params = null);

    public function select($conditions, $params = null);

    public function insert($table, array $data);

    public function update($table, array $target, array $data);

    public function updateAll($table, array $data);

    public function delete($table, array $target);

    public function deleteAll($table);

    public function truncate($table);

    public function showTables();

    public function getDtFormat();

    public function startTransaction();

    public function commit();

    public function rollBack();

    public function takeOverDisconnect();

    public function fetch($style = MST_DBC::FETCH_ASSOC);

    public function fetchAll($style = MST_DBC::FETCH_ASSOC);

    public function quote($val);

    public function isAutoCommit();
}

abstract class MST_DBC {

    const
        LOCAL       = 'default',
        PDO_MYSQL   = 'mysql',
        PDO_OCI     = 'oracle',
        OCI8        = 'oci8',
        CLOB        = 'CLOB',
        BLOB        = 'BLOB',
        CREATE      = 'create',
        UPDATE      = 'update',
        DELETE      = 'delete',
        FETCH_ASSOC = 2,
        FETCH_NUM   = 3,
        FETCH_BOTH  = 4;

    private static
        $_dbConfigKey = 'database',
        $_dbConfig = array(),
        $_adapters = array(
            self::PDO_MYSQL     => 'PdoMySQL',
            self::PDO_OCI       => 'PdoOci',
            self::OCI8          => 'Oci8',
        ),
        $_importAdapters = array(),
        $_register = array();

    protected static
        $_querySql = array(),
        $_lastHash = null;

    final static public function & connect($remote = null) {
        if ($remote == null) $remote = self::LOCAL;
        if (!isset(self::$_register[$remote])) {
            $config = MST_Core::getConfig(self::$_dbConfigKey, $remote);
            if (empty($config))
                MST_Core::error(301, $remote);
            if (empty($config['adapter'])
             || !isset(self::$_adapters[$config['adapter']]))
                MST_Core::error(302, $remote);
            $adapter = self::$_adapters[$config['adapter']];
            $adapterClass = __CLASS__ . '_' . $adapter;
            if (!isset(self::$_importAdapters[$adapter])) {
                if (!MST_Core::import("MST/DBC/{$adapter}", MST_Core::P_LIB)
                 || !class_exists($adapterClass))
                    MST_Core::error(302, "MST/DBC/$adapter");
                self::$_importAdapters[$adapter] = 1;
            }
            self::$_register[$remote] = new $adapterClass($config);
        }
        return self::$_register[$remote];
    }

    final static public function disconnect($remote = null) {
        if ($remote == null) {
            foreach (self::$_register as $conn) {
                if (!empty($conn)) $conn->disconnect();
            }
        }
        else {
            self::connect($remote)->disconnect();
        }
    }

    final static public function getConfig($key, $remote = self::LOCAL) {
        if (empty(self::$_dbConfig[$remote]))
            self::$_dbConfig[$remote] = MST_Core::getConfig(self::$_dbConfigKey, $remote);
        if (!empty(self::$_dbConfig[$remote][$key]))
            return self::$_dbConfig[$remote][$key];
        return null;
    }

    final static public function addAdapter($key, $val) {
        if (!isset(self::$_adapters[$key]))
            self::$_adapters[$key] = $val;
    }

    final static public function getLastSqlHash() {
        return self::$_lastHash;
    }

    /**
     * 启动数据库事务
     * @param string $remote 连接实例
     */
    final static public function startTransaction($remote = self::LOCAL) {
        return self::connect($remote)->startTransaction();
    }

    /**
     * 数据库事务提交
     * @param string $remote 连接实例
     */
    final static public function commit($remote = self::LOCAL) {
        return self::connect($remote)->commit();
    }

    /**
     * 数据库事务回滚
     * @param string $remote 连接实例
     */
    final static public function rollBack($remote = self::LOCAL) {
        return self::connect($remote)->rollBack();
    }

    /**
     * 接管某个连接实例的
     * @param unknown_type $remote
     */
    final static public function handleDisconnect($remote = self::LOCAL) {
        return self::connect($remote)->takeOverDisconnect();
    }

}

配置文件举例

<?php

return array(
    'common' => array(
    ),
    MST_Core::IN_DEV => array(
        'database' => array(
            MST_DBC::LOCAL => array(
                'adapter'   => MST_DBC::PDO_MYSQL,
                'host'      => '127.0.0.1', // 数据库连接ip
                'user'      => 'root',      // 数据库账号
                'password'  => '',  // 数据库密码
                'dbname'    => 'any_db',
                'prefix'    => 'pf',
            ),
            'iphone' => array(
                'adapter'   => MST_DBC::PDO_MYSQL,
                'host'      => '127.0.0.1', // 数据库连接ip
                'user'      => 'root',      // 数据库账号
                'password'  => '',  // 数据库密码
                'dbname'    => 'any_db_iphone',
                'prefix'    => false,
            ),
        ),
        MST_Mailer::FLAG => array(
            MST_Mailer::LOCAL => array(
                'host'      => 'localhost',
                'port'      => 25,
                'debug'     => 0,
                'charset'   => PROJECT_ENCODE,
                'language'  => PROJECT_LANG,
                MST_Mailer::IS_SMTP     => true,
                MST_Mailer::SMTP_AUTH   => false,
                MST_Mailer::FROM_MAIL   => 'any@host.com',
                MST_Mailer::FROM_NAME   => 'anyone',
            )
        ),
    ),
    MST_Core::IN_TEST => array(
        'database' => array(
            MST_DBC::LOCAL => array(
                'adapter'   => MST_DBC::PDO_MYSQL,
                'host'      => '127.0.0.1', // 数据库连接ip
                'user'      => 'root',      // 数据库账号
                'password'  => '',  // 数据库密码
                'dbname'    => 'any_db',
                'prefix'    => 'pf',
            ),
            'iphone' => array(
                'adapter'   => MST_DBC::PDO_MYSQL,
                'host'      => '127.0.0.1', // 数据库连接ip
                'user'      => 'root',      // 数据库账号
                'password'  => '',  // 数据库密码
                'dbname'    => 'any_db_iphone',
                'prefix'    => false,
            ),
        ),
        MST_Mailer::FLAG => array(
            MST_Mailer::LOCAL => array(
                'host'      => 'localhost',
                'port'      => 25,
                'debug'     => 0,
                'charset'   => PROJECT_ENCODE,
                'language'  => PROJECT_LANG,
                MST_Mailer::IS_SMTP     => true,
                MST_Mailer::SMTP_AUTH   => false,
                MST_Mailer::FROM_MAIL   => 'any@host.com',
                MST_Mailer::FROM_NAME   => 'anyone',
            )
        ),
    ),
    MST_Core::IN_PRO => array(
        'database' => array(
            MST_DBC::LOCAL => array(
                'adapter'   => MST_DBC::PDO_MYSQL,
                'host'      => '127.0.0.1', // 数据库连接ip
                'user'      => 'root',      // 数据库账号
                'password'  => '',  // 数据库密码
                'dbname'    => 'any_db',
                'prefix'    => 'pf',
            ),
            'iphone' => array(
                'adapter'   => MST_DBC::PDO_MYSQL,
                'host'      => '127.0.0.1', // 数据库连接ip
                'user'      => 'root',      // 数据库账号
                'password'  => '',  // 数据库密码
                'dbname'    => 'any_db_iphone',
                'prefix'    => false,
            ),
        ),
        MST_Mailer::FLAG => array(
            MST_Mailer::LOCAL => array(
                'host'      => 'localhost',
                'port'      => 25,
                'debug'     => 0,
                'charset'   => PROJECT_ENCODE,
                'language'  => PROJECT_LANG,
                MST_Mailer::IS_SMTP     => true,
                MST_Mailer::SMTP_AUTH   => false,
                MST_Mailer::FROM_MAIL   => 'any@host.com',
                MST_Mailer::FROM_NAME   => 'anyone',
            )
        ),
    ),
);

标签:PHP,MVC,DBC

收藏

0人收藏

支持

0

反对

0

发表评论