Framework: Database
This commit is contained in:
parent
8f58cb6c8e
commit
e9fca64d16
5
.gitignore
vendored
5
.gitignore
vendored
@ -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
|
||||
|
@ -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/
|
||||
|
10
htdocs/composer.json
Normal file
10
htdocs/composer.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "chu-tdap/uelearning",
|
||||
"description": "無所不在學習導引系統之研製",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Yuan Chiu",
|
||||
"email": "chyuaner@gmail.com"
|
||||
}
|
||||
}
|
||||
}
|
105
htdocs/config.sample.php
Normal file
105
htdocs/config.sample.php
Normal file
@ -0,0 +1,105 @@
|
||||
<?php
|
||||
// 資料庫 =====================================================================
|
||||
/**
|
||||
* 資料庫類型
|
||||
*/
|
||||
define('DB_TYPE', "mysql"); // or mysql
|
||||
|
||||
/**
|
||||
* 資料庫位址
|
||||
*/
|
||||
define('DB_HOST', "localhost");
|
||||
|
||||
/**
|
||||
* 資料庫連結埠
|
||||
*
|
||||
* usually 5432 for PostgreSQL, 3306 for MySQL
|
||||
*/
|
||||
define('DB_PORT', '');
|
||||
|
||||
/**
|
||||
* 資料庫連結帳號
|
||||
*/
|
||||
define('DB_USER', "user");
|
||||
|
||||
/**
|
||||
* 資料庫連結密碼
|
||||
*/
|
||||
define('DB_PASS', "passwd123");
|
||||
|
||||
/**
|
||||
* 資料庫名稱
|
||||
*/
|
||||
define('DB_NAME', "chu_elearning");
|
||||
|
||||
/**
|
||||
* 資料庫內資料表前綴字串
|
||||
*
|
||||
* 每一張表格名稱的起始字串,為了避開一些網頁空間只允許建立一個資料庫的限制。
|
||||
*/
|
||||
define('DB_PREFIX', "chu_");
|
||||
|
||||
// 網站設定 ===================================================================
|
||||
/**
|
||||
* 網站標題
|
||||
*/
|
||||
define('SITE_NAME', '無所不在學習導引系統');
|
||||
|
||||
/**
|
||||
* 網站副標題
|
||||
*/
|
||||
define('SITE_SUBNAME', '');
|
||||
|
||||
/**
|
||||
* 網站標題簡稱
|
||||
*/
|
||||
define('SITE_NAME_REFERRED', 'E-learning');
|
||||
|
||||
/**
|
||||
* 網站首頁網址
|
||||
*
|
||||
* Warning: 網址後面務必加上"/"
|
||||
*/
|
||||
define('SITE_URL', 'http://localhost/');
|
||||
|
||||
/**
|
||||
* 本系統根網址
|
||||
*
|
||||
* 給絕對路徑用的。
|
||||
* Warning: 網址後面務必加上"/"
|
||||
*/
|
||||
define('SITE_URL_ROOT', 'http://localhost/');
|
||||
|
||||
/**
|
||||
* 要用哪種加密方式
|
||||
*
|
||||
* 目前提供選項:
|
||||
* <ul>
|
||||
* <li>MD5</li>
|
||||
* <li>SHA1</li>
|
||||
* <li>CRYPT</li>
|
||||
* </ul>
|
||||
*/
|
||||
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__);
|
||||
|
11
htdocs/lib/Config/Exceptions.php
Normal file
11
htdocs/lib/Config/Exceptions.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php namespace UElearning\Config;
|
||||
/**
|
||||
* @file
|
||||
* 設定檔的例外
|
||||
*
|
||||
* @package UElearning
|
||||
* @subpackage Config
|
||||
*/
|
||||
|
||||
class ConfigNoFoundException extends \UnexpectedValueException {}
|
||||
class ConfigErrException extends \UnexpectedValueException {}
|
24
htdocs/lib/Database/DBAdmin.php
Normal file
24
htdocs/lib/Database/DBAdmin.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php namespace UElearning\Database;
|
||||
/**
|
||||
* @file
|
||||
* 整體資料庫操作
|
||||
*
|
||||
* 此檔案針對整體資料庫的功能,像是建立此資料庫、建立表格、清空...等等
|
||||
*
|
||||
* @package UElearning
|
||||
* @subpackage Database
|
||||
*/
|
||||
|
||||
/**
|
||||
* 資料庫整體管理
|
||||
*
|
||||
* 對資料庫的管理操作。
|
||||
*
|
||||
* @author Yuan Chiu <chyuaner@gmail.com>
|
||||
* @version 3.0
|
||||
* @package UElearning
|
||||
* @subpackage Database
|
||||
*/
|
||||
class DBAdmin extends Database {
|
||||
|
||||
}
|
19
htdocs/lib/Database/DBUser.php
Normal file
19
htdocs/lib/Database/DBUser.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php namespace UElearning\Database;
|
||||
/**
|
||||
* @file
|
||||
* 使用者資料表
|
||||
*
|
||||
* 此檔案針對使用者資料表的功能。
|
||||
*
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
class DBUser {
|
||||
/**
|
||||
* 資料表名稱
|
||||
* @type string
|
||||
*/
|
||||
private $form_name = 'user';
|
||||
|
||||
|
||||
}
|
@ -4,23 +4,163 @@
|
||||
* 整體資料庫操作
|
||||
*
|
||||
* 此檔案針對整體資料庫的功能,像是建立此資料庫、建立表格、清空...等等
|
||||
*
|
||||
* @package UElearning
|
||||
* @subpackage Database
|
||||
*/
|
||||
require_once UELEARNING_LIB_ROOT.'Database/MySQLDB.php';
|
||||
require_once UELEARNING_LIB_ROOT.'Database/Exceptions.php';
|
||||
|
||||
/**
|
||||
* 資料庫整體管理
|
||||
*
|
||||
* 所有對於資料庫的操作(包含查詢、新增、修改、刪除),一律使用這個物件來操作。
|
||||
*
|
||||
* 例如:
|
||||
*
|
||||
* use UElearning\Database;
|
||||
* $db = new Database(array(
|
||||
* 'type' => 'mysql',
|
||||
* 'host' => 'localhost',
|
||||
* 'port' => '3306',
|
||||
* 'user' => 'user',
|
||||
* 'password' => '123456',
|
||||
* 'dbname' => 'chu-elearning',
|
||||
* 'prefix' => 'chu_'
|
||||
* ));
|
||||
*
|
||||
* @author Yuan Chiu <chyuaner@gmail.com>
|
||||
* @link https://github.com/CHU-TDAP/
|
||||
* @since 3.0
|
||||
* 一個資料表為一個類別,如果要對某資料表進行操作,請參閱
|
||||
*
|
||||
* @author Yuan Chiu <chyuaner@gmail.com>
|
||||
* @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 <chyuaner@gmail.com>
|
||||
* @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 <chyuaner@gmail.com>
|
||||
* @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
|
||||
|
||||
}
|
||||
}
|
34
htdocs/lib/Database/Exceptions.php
Normal file
34
htdocs/lib/Database/Exceptions.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php namespace UElearning\Database;
|
||||
/**
|
||||
* @file
|
||||
* 設定檔的例外
|
||||
*
|
||||
* @package UElearning
|
||||
* @subpackage Database
|
||||
*/
|
||||
|
||||
/**
|
||||
* 沒有支援的資料庫系統例外
|
||||
*/
|
||||
class DatabaseNoSupportException extends \UnexpectedValueException {
|
||||
private $type;
|
||||
|
||||
|
||||
/**
|
||||
* 沒有支援的資料庫系統
|
||||
* @param array $type 資料庫系統名稱
|
||||
*/
|
||||
public function __construct($type) {
|
||||
$this->type = $type;
|
||||
parent::__construct('No support: '.$this->type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得輸入的資料庫系統名稱
|
||||
* @return string 錯誤訊息內容
|
||||
*/
|
||||
public function getType() {
|
||||
return $this->type;
|
||||
}
|
||||
}
|
||||
|
@ -10,26 +10,31 @@
|
||||
use \PDO;
|
||||
|
||||
/**
|
||||
* 資料庫連接專用類別
|
||||
* 資料庫連接專用類別,採用自PDO
|
||||
*
|
||||
* @extends PDO
|
||||
* @author Yuan Chiu <chyuaner@gmail.com>
|
||||
* @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 <me@yuaner.tw>
|
||||
* @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 <me@yuaner.tw>
|
||||
* @version 3
|
||||
*/
|
||||
public function table($inputName){
|
||||
return DB_PREFIX.$inputName;
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
/**
|
||||
* 錯誤訊息的陣列
|
Loading…
x
Reference in New Issue
Block a user