From e9fca64d166bdc418e5f5fe06fc847f5a9a41964 Mon Sep 17 00:00:00 2001 From: Yuan Chiu Date: Thu, 24 Jul 2014 14:25:03 -0700 Subject: [PATCH] Framework: Database --- .gitignore | 5 +- README.md | 8 + htdocs/composer.json | 10 ++ htdocs/config.sample.php | 105 ++++++++++++ htdocs/lib/Config/Exceptions.php | 11 ++ htdocs/lib/Database/DBAdmin.php | 24 +++ htdocs/lib/Database/DBUser.php | 19 +++ htdocs/lib/Database/Database.php | 152 +++++++++++++++++- htdocs/lib/Database/Exceptions.php | 34 ++++ .../lib/Database/{PDODB.php => MySQLDB.php} | 40 ++--- htdocs/lib/Database/UserDB.php | 0 11 files changed, 376 insertions(+), 32 deletions(-) create mode 100644 htdocs/composer.json create mode 100644 htdocs/config.sample.php create mode 100644 htdocs/lib/Config/Exceptions.php create mode 100644 htdocs/lib/Database/DBAdmin.php create mode 100644 htdocs/lib/Database/DBUser.php create mode 100644 htdocs/lib/Database/Exceptions.php rename htdocs/lib/Database/{PDODB.php => MySQLDB.php} (62%) delete mode 100644 htdocs/lib/Database/UserDB.php diff --git a/.gitignore b/.gitignore index a37b901..30576f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ # 設定檔 +htdocs/config.php # 建立測試用的檔案 test.php +htdocs/tests/ # KDE Dolphin自己產生的垃圾 *.directory @@ -13,7 +15,8 @@ test.php # GitEye產生的 .project -# phpdoc產生的垃圾檔案 +# phpdoc產生的檔案 +docs/ docs/phpdoc-cache-* # PHP Composer diff --git a/README.md b/README.md index 5d84dcc..d2c96cb 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,11 @@ * pdo_mysql * zip * MariaDB 5.5.31 (可用MySQL) + + +## 開發文件 +已將整份專案使用[PHPDocumentor](http://www.phpdoc.org/)產生出[開發文件網站](docs/index.html) + +產生指令: + + phpdoc -d ./htdocs/lib -t ./docs/ diff --git a/htdocs/composer.json b/htdocs/composer.json new file mode 100644 index 0000000..0c5c5d1 --- /dev/null +++ b/htdocs/composer.json @@ -0,0 +1,10 @@ +{ + "name": "chu-tdap/uelearning", + "description": "無所不在學習導引系統之研製", + "authors": [ + { + "name": "Yuan Chiu", + "email": "chyuaner@gmail.com" + } + } +} diff --git a/htdocs/config.sample.php b/htdocs/config.sample.php new file mode 100644 index 0000000..a6e33d2 --- /dev/null +++ b/htdocs/config.sample.php @@ -0,0 +1,105 @@ + + *
  • MD5
  • + *
  • SHA1
  • + *
  • CRYPT
  • + * + */ + define('ENCRYPT_MODE', 'SHA1'); + + /** + * 你的地區 + */ + date_default_timezone_set('Asia/Taipei'); //設定時區 + +// 路徑設定 =================================================================== + /** + * 網站根目錄 + */ + define('UELEARNING_ROOT', __DIR__.'/'); + + /** + * 內部函式庫根目錄 + */ + define('UELEARNING_LIB_ROOT', UELEARNING_ROOT.'lib/'); + + /** + * 這份設定檔的路徑 + */ + define('UELEARNING_CONFIG_PATH', __FILE__); + diff --git a/htdocs/lib/Config/Exceptions.php b/htdocs/lib/Config/Exceptions.php new file mode 100644 index 0000000..14aa2e0 --- /dev/null +++ b/htdocs/lib/Config/Exceptions.php @@ -0,0 +1,11 @@ + + * @version 3.0 + * @package UElearning + * @subpackage Database + */ +class DBAdmin extends Database { + +} \ No newline at end of file diff --git a/htdocs/lib/Database/DBUser.php b/htdocs/lib/Database/DBUser.php new file mode 100644 index 0000000..9b7e632 --- /dev/null +++ b/htdocs/lib/Database/DBUser.php @@ -0,0 +1,19 @@ + 'mysql', + * 'host' => 'localhost', + * 'port' => '3306', + * 'user' => 'user', + * 'password' => '123456', + * 'dbname' => 'chu-elearning', + * 'prefix' => 'chu_' + * )); * - * @author Yuan Chiu - * @link https://github.com/CHU-TDAP/ - * @since 3.0 + * 一個資料表為一個類別,如果要對某資料表進行操作,請參閱 + * + * @author Yuan Chiu + * @version 3.0 + * @package UElearning + * @subpackage Database */ 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`的常數 + * + * @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 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; + } + /** * 測試資料庫有無連接成功 * - * @todo + * @since 3.0.0 */ public function connectTest() { + // TODO: Fill code in } @@ -30,18 +170,18 @@ class Database { * 在資料庫系統內建立一個資料庫。 * (注意!需有建立資料庫的權限) * - * @todo */ public function createDB() { + // TODO: Fill code in } /** * 建立所有所需的資料表 * - * @TODO rdkoprk */ public function createAllTable() { + // TODO: Fill code in } } \ No newline at end of file diff --git a/htdocs/lib/Database/Exceptions.php b/htdocs/lib/Database/Exceptions.php new file mode 100644 index 0000000..de2b675 --- /dev/null +++ b/htdocs/lib/Database/Exceptions.php @@ -0,0 +1,34 @@ +type = $type; + parent::__construct('No support: '.$this->type); + } + + /** + * 取得輸入的資料庫系統名稱 + * @return string 錯誤訊息內容 + */ + public function getType() { + return $this->type; + } +} + diff --git a/htdocs/lib/Database/PDODB.php b/htdocs/lib/Database/MySQLDB.php similarity index 62% rename from htdocs/lib/Database/PDODB.php rename to htdocs/lib/Database/MySQLDB.php index e22e317..daa8fa7 100644 --- a/htdocs/lib/Database/PDODB.php +++ b/htdocs/lib/Database/MySQLDB.php @@ -10,26 +10,31 @@ use \PDO; /** - * 資料庫連接專用類別 + * 資料庫連接專用類別,採用自PDO * * @extends PDO * @author Yuan Chiu - * @link https://github.com/CHU-TDAP/ - * @version Version 2.0 + * @version 3.0 * @see https://github.com/shuliu/myPDO + * @package UElearning + * @subpackage Database */ -class PDODB extends PDO { +class MySQLDB extends PDO { + /** * 連結資料庫 * - * @access public + * @param string $dbname 資料庫名稱 + * @param string $host 資料庫伺服器位址 + * @param int $port 資料庫伺服器連接埠 + * @param string $user 資料庫伺服器帳號 + * @param string $passwd 資料庫伺服器密碼 * @author Yuan Chiu - * @version 2 + * @since 3.0.0 */ - public function __construct(){ - parent::__construct("mysql:dbname=".DB_NAME.";host:".DB_HOST."; - charset=utf8", - DB_USER, DB_PASS); + public function __construct($dbname, $host, $port, $user, $passwd){ + parent::__construct("mysql:dbname=".$dbname.";host:".$host."port=".$port."; + charset=utf8", DB_USER, DB_PASS); //配合PHP< 5.3.6 PDO沒有charset用的 //參考: http://gdfan1114.wordpress.com/2013/06/24/php-5-3-6-%E7%89%88-pdo-%E9%85%8D%E5%90%88%E5%AD%98%E5%8F%96%E8%B3%87%E6%96%99%E5%BA%AB%E6%99%82%E7%9A%84%E4%B8%AD%E6%96%87%E5%95%8F%E9%A1%8C/ @@ -37,21 +42,6 @@ class PDODB extends PDO { } - // ======================================================================== - /** - * 取得帶有前綴字元的完整資料表名稱 - * - * @access public - * @param string $inputName 資料表名稱 - * @return string 完整的資料表名稱 - * - * @author Yuan Chiu - * @version 3 - */ - public function table($inputName){ - return DB_PREFIX.$inputName; - } - // ======================================================================== /** * 錯誤訊息的陣列 diff --git a/htdocs/lib/Database/UserDB.php b/htdocs/lib/Database/UserDB.php deleted file mode 100644 index e69de29..0000000