資料庫操作類是由 跡憶部落格提供
Db類的介紹
1.採用單例設計模式 該類採用單例設計模式,保證了該類執行個體對象的唯一性 實現方式
public static $_instance; //靜態屬性,儲存執行個體對象
/** * 私人化建構函式 這是構造單例設計模式必須的一步 */private function __construct($config=''){ $this->config = $this->parseConfig($config);}
/** * 執行個體化對象 採用靜態公用方法 */public static function Instance(){ if(self::$_instance instanceof self){ return self::$_instance; } self::$_instance = new self; return self::$_instance;}
2.功能介紹
首先該類支援主從複製資料庫的串連,支援一主多從模式的資料庫
當線程正在串連的一台從伺服器宕機時,程式會自動重新串連其他正常的從伺服器
其次當新增資料到資料表中時,該類支援一次添加多條資料,並且這多條資料可以是不同的表
可以通過addMore函數的第二個參數來指明是多表插入,其實現核心代碼如下
/** * 一次性插入多條資料,支援不同表的插入 * 當使用多表插入功能時需要在第二個參數中指定 $options['multitable'] = true * 並且$data的格式為 * array( * '表名1'=>array(array(),array()), * '表名2'=>array(array(),array()) * ) * @param array $data * @param array $options * @return boolean */ public function addMore($data = array(),$options = array()){ if(isset($options['table'])) $this->table($options['table']); if(!is_array($data)) return false; /* * 開啟交易處理多條語句 */ $this->startTransaction(); foreach($data as $key=>$val){ //查看是否是多表插入 if(isset($options['multitable'])&&$options['multitable']){ /* * 多表插入,則$key為表名,$val為要插入的資料 * 使用遞迴的方式再次對多條資料進行插入 */ $res = $this->addMore($val,array('table'=>$key)); }else{ //單表插入 $res = $this->add($val); } if(!$res){ //如果有一條資料插入失敗,則復原事務,撤銷所有的操作 $this->rollback(); return false; } } //如果所有插入操作無誤,則提交事務 $this->commit(); return true;}
同時支援交易處理,當多條插入資料之中的一條資料插入失敗,可通過交易回復撤銷其它插入的資料
3.其它支援正常的增刪改查
下面介紹使用到的函數的用法
1)執行個體化該對象
使用該類需要先執行個體化該類的對象
$obj = Db::Instance();
2) 尋找資料
尋找資料用到的函數有 select()和find()兩個函數
select()函數尋找多條資料
使用執行個體
$res = $obj->field('id,name')->where('id > 10')->select();
傳回值:
尋找失敗 返回 false 尋找成功 返回多條資料
array( array('id'=>11,'name'=>'跡憶部落格1'), array('id'=>12,'name'=>'跡憶部落格2'),)
find()是返回一條資料
$res = $obj->field('id,name')->where('id=10')->find()
傳回值
尋找失敗 返回 false 尋找成功 返回一條資料
array('id'=>10,'name'=>'跡憶部落格')
3)添加資料
添加資料有兩個函數 add($data,$options) 和addMore($data,$options)
add($data.$options)
$data 要添加的資料
資料格式
array('id'=>13,'name'=>'onmpw')
$options 選擇性參數
可指定表名 格式為 array('table'=>'表名') 此處指定表名的優先順序最高
傳回值
插入失敗 返回 false 插入成功 返回插入的條數
addMore($data,$options)
可以通過$options選項指定是奪標插入還是單表插入
'multitable'=>true 多表插入 如果設定此項則預設是單表插入 多表插入$data的資料格式$data = array( 'tablename1'=>array( array('id'=>20,'name'=>'跡憶部落格1'), array('id'=>21,'name'=>'跡憶部落格2'), array('id'=>22,'name'=>'跡憶部落格3') ), 'tablename2'=>array( array('id'=>20,'name'=>'跡憶部落格1','url'=>'www.onmpw.com'), array('id'=>21,'name'=>'跡憶部落格2','url'=>'www.onmpw.com'), array('id'=>22,'name'=>'跡憶部落格3','url'=>'www.onmpw.com') ))
'multitable'=>false / 不設定此項 單表插入 $data的資料格式為 $data = array( array('id'=>31,'name'=>'跡憶部落格1','url'=>'www.onmpw.com'), array('id'=>32,'name'=>'跡憶部落格2','url'=>'www.onmpw.com'), array('id'=>33,'name'=>'跡憶部落格3','url'=>'www.onmpw.com'), )
'table'=>'表名' 指定插入資料的資料表名,此項在單表插入時有效,並且較之於其他指定表名的方式優先順序高
傳回值
插入失敗 返回 false 插入成功返回插入的條數
4)修改 update($data,$options)
修改資料函數
$data 要修改的資料,格式為
array('name'=>'onmpw','url'=>'http://onmpw.com');
$options 可以指定表名
'table'=>'表名'
傳回值
更新失敗 返回 false 更新成功返回更新的條數
5)刪除 delete($options)
$options 可以指定表名
'table'=>'表名' 此種指定表名的優先順序最高
執行個體
$res = $obj->table('repl')->where('id=10')->delete(); //刪除repl表下id=10的記錄$res = $obj->table('repl')->where('id=13')->delete(array('table'=>'test')); //刪除test表下id=13的記錄
等價於
$res = $obj->where('id=13')->delete(array('table'=>'test'))
傳回值
刪除失敗 返回false 刪除成功返回 刪除的記錄條數
6) table($str) 函數 指定表名
$obj->table('test') //指定當前操作的表為test表
傳回值為 當前對象 object
7) where($where) 指定where條件
$where 可以是字串也可以是數組
字串
$obj->table('test')->where("name='跡憶部落格',url='www.onmpw.com'");
數組
$obj->table('test')->where(array('name'=>'跡憶部落格','url'=>'www.onmpw.com'))
傳回值為 當前對象 object
8)field($field) 指定查詢的欄位名稱
$obj->table('test')->field('name,url')->select();
如果在查詢的時候不適用field()函數指定欄位,預設會查詢該表的所有欄位
傳回值為 當前對象 object
9) orderby($str) 指定按照那個欄位排序
$obj->table('test')->field('id,name,url')->where("name='跡憶部落格'")->orderby('id DESC')->select();
按照id 降序排列
$obj->table('test')->field('id,name,url')->where("name='跡憶部落格'")->orderby('id')->select();
也可以不指定是降序或者升序
傳回值為 當前對象 object
10) limit($limit)
$limt 可以為字串也可以為數組
數組
array(page,listrows)
page 指定當前的頁數 listrows指定每頁取出的條數
字串
10,12
10表示從第十條記錄開始取,12表示取出的條數
$res = $obj->table('test')->field('id,name,url')->where("name='跡憶部落格'")->orderby('id DESC')->limit('10,12')->select()
傳回值為 當前對象 object
11)sql($sql) 執行指定的sql語句
$sql = "select name,url from test where name='跡憶部落格'";$res = $obj->sql($sql);
返回執行的結果