比來在新浪的云平臺(SAE,http://sae.sina.com.cn)中做應用,底本應用SAE供給的mysql操縱類SaeMysql(http://apidoc.sinaapp.com/sae/SaeMysql.html),然則有些不便利:
1、SaeMysql沒有供給完全的增刪查改辦法,插入數據、刪除數據、更新數據都只能本身寫完全的sql然后在應用runSql 辦法履行;
2、要先初始化¥mysql = new SaeMysql();,今后要在其它函數內進行mysql操縱的話,都不克不及忘了把¥mysql 列入全局變量;
3、我不贊成在具體的營業代碼中直接應用SAE供給的類,今后如果把應用遷徙出SAE,會斗勁麻煩。
所以我本身把SaeMysql從頭封裝了一下,本身寫了個mysql操縱類。
代碼如下:
//by kuiGG www.kuigg.com
class kuigg_Db {
function tbname(¥tb) {
return “kuigg_{¥tb}”;
}
function getdata (¥arr , ¥separator = ‘,’) {
¥str = ¥s = ”;
foreach (¥arr as ¥k => ¥v) {
¥str .= ¥s.”`{¥k}`=’{¥v}’”;
¥s = ¥separator;
}
return ¥str;
}
function count (¥tb , ¥fields = ‘*’ , ¥terms = ”){
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥where = empty(¥terms) ? ’1′ : ¥terms;
¥query = “ count({¥fields}) `{¥tb}` where {¥where}”;
return ¥o->getVar(¥query);
}
function fetchdata (¥tb , ¥fields = ‘*’ , ¥terms = ”){
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = array();
¥query = “ {¥fields} `{¥tb}` {¥terms}”;
return ¥o->getData(¥query);
}
function fetchrow (¥tb , ¥fields = ‘*’ , ¥terms = ”){
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = array();
¥query = “ {¥fields} `{¥tb}` {¥terms}”;
return ¥o->getLine(¥query);
}
function fetchitem (¥tb , ¥field , ¥terms = ”){
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = array();
¥query = “ {¥field} `{¥tb}` {¥terms}”;
return ¥o->getVar(¥query);
}
function (¥tb, ¥arr, ¥getid = false, ¥replace = false) {
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = self::getdata(¥arr);
¥cmd = ¥replace ? ‘REPLACE INTO’ : ‘INSERT INTO’;
¥silence = ¥silence ? ‘SILENT’ : ”;
¥query = “{¥cmd} `{¥tb}` SET {¥data}”;
¥return = ¥o->runSql(¥query);
return ¥getid ? ¥o->lastId() : ¥return;
}
function _id() {
¥o = & self::in();
return ¥o->lastId();
}
function (¥tb, ¥arr, ¥terms = NULL , ¥getarows = false , ¥low_priority = false) {
¥o = & self::in();
¥tb= self::tbname(¥tb);
¥data = self::getdata(¥arr);
¥cmd = “UPDATE “.(¥low_priority ? ‘LOW_PRIORITY’ : ”);
¥where = empty(¥terms) ? ’1′ : ¥terms;
¥query = “{¥cmd} `{¥tb}` SET {¥data} WHERE {¥where}”;
¥return = ¥o->runSql(¥query);
return ¥getarows ? ¥o->affectedRows() : ¥return;
}
function (¥tb, ¥terms = NULL,¥getarows = false, ¥limit = 0) {
¥o = & self::in();
¥tb = self::tbname(¥tb);
¥where = empty(¥terms) ? ’1′ : ¥terms;
¥query = “DELETE FROM `{¥tb}` WHERE {¥where} “.(¥limit ? “LIMIT {¥limit}” : ”);
¥return = ¥o->runSql(¥query);
return ¥getarows ? ¥o->affectedRows() : ¥return;
}
function affected_rows() {
¥o = & self::in();
return ¥o->affectedRows();
}
function query(¥query) {
¥o = & self::in();
return ¥o->runSql(¥query);
}
function &in() {
static ¥object;
if(empty(¥object)) {
¥object = new SaeMysql();
}
return ¥object;
}
}
下載地址:http://www.kuigg.com/attachments/2011/10/kuigg_Db.rar
這個類不須要實例化,調用里面的辦法的時辰只須要以kuigg_Db::function的情勢就可以。
下面申明一下這里面的幾個辦法:
tbname:這是處理懲罰表名的辦法,給注解增長前綴用。
重點是供給了完全 增刪查改辦法:
增:(tb,¥arr,¥getid = false,¥replace = false)
須要供給的幾個參數分別是¥tb:須要插入的表名;¥arr:把插入的數據遵守key=字段名,value=值的情勢機關的數組;¥getid:是否獲取新插入的id,默認為false不獲取,若是設為true就會返回id值;¥replace:是否以調換體式格式插入,默認為false,一般也用不到。
示例:
¥arr = array(‘user’ => ‘kuigg’ , ‘email’ => ‘i@kuigg.com’ , ‘website’ => ‘http://www.kuigg.com’);
¥uid = kuigg_Db::(‘user’, ¥arr, true);
刪:(tb,¥terms = NULL,getarows = false,¥limit = 0)
須要供給的幾個參數分別是¥tb:須要刪除數據的表名;¥terms:刪除的前提;¥getarows:是否獲取被刪除的行數,默認為false不獲取,若是設為true就會此次操縱刪除的行數;¥limit:刪除的行數,默認0即刪除所有合適前提的行。
示例:
¥rownum = kuigg_Db::(‘user’, “`uid` = ’1′”, true);
查:供給了4個辦法:
fetchdata (¥tb , ¥fields = ‘*’ , ¥terms = ”)
fetchrow (¥tb , ¥fields = ‘*’ , ¥terms = ”)
fetchitem (¥tb , ¥field , ¥terms = ”)
count (¥tb , ¥fields = ‘*’ , ¥terms = ”)
分別可以取得多維數組、單維數組、單項值、統計數量。
改:(tb,¥arr,¥terms = NULL ,¥getarows = false ,¥low_priority = false)
須要供給的幾個參數分別是¥tb:須要更新數據的表名;¥terms:須要更新的前提;¥getarows:是否獲取更新的行數,默認為false不獲取,若是設為true就會此次操縱更新的行數。
¥arr = array(‘user’ => ‘kuigg’ , ‘email’ => ‘i@kuigg.com’ , ‘website’ => ‘http://www.kuigg.com’);
¥rownum = kuigg_Db::(‘user’, ¥arr, “where uid = ’1′ “, true);
用了這個類,將來若是把應用遷出,只須要把這個類批改一下就可以了。
Copyright@ 2011-2016 版權所有:大連千億科技有限公司 遼ICP備11013762-3號 google網站地圖 百度網站地圖 網站地圖
公司地址:大連市沙河口區中山路692號辰熙星海國際2317 客服電話:0411-39943997 QQ:2088827823 37482752
法律聲明:未經許可,任何模仿本站模板、轉載本站內容等行為者,本站保留追究其法律責任的權利! 隱私權政策聲明