搜尋
首頁php教程PHP源码MST Library 3.1 数据库连接工厂DBC

MST Library 3.1 数据库连接工厂DBC

<?php

/**
 * DataBase Common Connector
 * 数据库共用连接器,连接驱动以如下的方式存放:
 * DBC/PdoOci.php
 * DBC/PdoMySQL.php
 * DBC/MySQLi.php
 * 连接驱动必须实现MST_IDBC接口
 *
 * 调用一个数据库的连接实例如下:
 * 
 * MST_DBC::connect($remote);
 * # $remote对应config中配置
 * 
 *
 * @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		= &#39;default&#39;,
		PDO_MYSQL	= &#39;mysql&#39;,
		PDO_OCI		= &#39;oracle&#39;,
		OCI8		= &#39;oci8&#39;,
		CLOB		= &#39;CLOB&#39;,
		BLOB		= &#39;BLOB&#39;,
		CREATE		= &#39;create&#39;,
		UPDATE		= &#39;update&#39;,
		DELETE		= &#39;delete&#39;,
		FETCH_ASSOC	= 2,
		FETCH_NUM	= 3,
		FETCH_BOTH	= 4;

	private static
		$_dbConfigKey = &#39;database&#39;,
		$_dbConfig = array(),
		$_adapters = array(
			self::PDO_MYSQL		=> &#39;PdoMySQL&#39;,
			self::PDO_OCI		=> &#39;PdoOci&#39;,
			self::OCI8			=> &#39;Oci8&#39;,
		),
		$_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[&#39;adapter&#39;])
			 || !isset(self::$_adapters[$config[&#39;adapter&#39;]]))
				MST_Core::error(302, $remote);
			$adapter = self::$_adapters[$config[&#39;adapter&#39;]];
			$adapterClass = __CLASS__ . &#39;_&#39; . $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();
	}

}

                       

           

2.配置文件举例

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

           

       

 以上就是MST Library 3.1 数据库连接工厂DBC的内容,更多相关内容请关注PHP中文网(www.php.cn)!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
1 個月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)