'mysql', * 'host' => 'localhost', * 'port' => '3306', * 'user' => 'user', * 'password' => '123456', * 'dbname' => 'chu-elearning', * 'prefix' => 'chu_' * )); * * 實際範例可參考 `DBAdmin` 類別的說明文件 * * @author Yuan Chiu * @version 3.0 * @package UElearning * @subpackage Database */ abstract class Database { /** * 資料庫伺服器類型 * * 目前支援的: * * mysql * * @type string */ private $db_type; /** * 資料庫伺服器位址 * @type string */ private $db_host; /** * 資料庫伺服器連結埠 * @type string */ private $db_port; /** * 資料庫帳號 * @type string */ private $db_user; /** * 資料庫密碼 * @type string */ private $db_passwd; /** * 資料庫名稱 * @type string */ private $db_name; /** * 資料表前綴字元 * @type string */ private $db_prefix; // ------------------------------------------------------------------------ /** * 資料庫連結物件 * @type UElearning\Database\PDODB */ private $connDB; // ======================================================================== /** * 連接資料庫 * * @param array $conf (optional) 資料庫相關參數,格式為: * array( 'type' => 'mysql', * 'host' => 'localhost', * 'port' => '3306', * 'user' => 'user', * 'password' => '123456', * 'dbname' => 'chu-elearning', * 'prefix' => 'chu_' ) * 若不填寫將會直接使用設定在`config.php`的常數 * * @throws UElearning\Database\Exception\DatabaseNoSupportException * @author Yuan Chiu * @since 3.0.0 */ public function __construct($conf = null) { // 將資料庫設定資訊帶入 if(isset($conf)) { $this->db_type = $conf['type']; $this->db_host = $conf['host']; $this->db_port = $conf['port']; $this->db_user = $conf['user']; $this->db_passwd = $conf['password']; $this->db_name = $conf['dbname']; $this->db_prefix = $conf['prefix']; } else { $this->db_type = DB_TYPE; $this->db_host = DB_HOST; $this->db_port = DB_PORT; $this->db_user = DB_USER; $this->db_passwd = DB_PASS; $this->db_name = DB_NAME; $this->db_prefix = DB_PREFIX; } // 檢查是否有支援所設定的DBMS if($this->db_type == 'mysql') { $this->connDB = new MySQLDB($this->db_name, $this->db_host, $this->db_port, $this->db_user, $this->db_passwd); } else { throw new Exception\DatabaseNoSupportException($this->db_type); } } /** * 轉為完整的資料表名稱(包含前綴字元) * * @param string $tableName 資料表名稱 * @return string 完整的資料表名稱 * * @author Yuan Chiu * @since 3.0.0 */ public function table($tableName) { return $this->db_prefix.$tableName; } /** * 測試資料庫有無連接成功 * * @since 3.0.0 */ public function connectTest() { // TODO: Fill code in } }