mirror of
https://github.com/thinkjs/thinkjs.git
synced 2026-01-25 14:42:47 +00:00
update model
This commit is contained in:
parent
258ac21450
commit
ff5fb1b0a0
@ -236,9 +236,11 @@ var caches = {};
|
||||
global.S = function(name, value, options){
|
||||
if (options === undefined) {
|
||||
options = {};
|
||||
}else if (options === true) {
|
||||
options = {type: true};
|
||||
};
|
||||
var type = options.type || C('data_cache_type');
|
||||
if (type) {
|
||||
if (type && type !== true) {
|
||||
type = ucfirst(type.toLowerCase()) + "Cache";
|
||||
}else{
|
||||
type = "Cache";
|
||||
|
||||
@ -191,7 +191,7 @@ var db = module.exports = Class(function(){
|
||||
}
|
||||
whereStr += ")" + operate;
|
||||
}
|
||||
whereStr = whereStr.substr(0, -operate.length);
|
||||
whereStr = whereStr.substr(0, whereStr.length - operate.length);
|
||||
}
|
||||
return whereStr ? (" WHERE " + whereStr) : "";
|
||||
},
|
||||
@ -217,25 +217,54 @@ var db = module.exports = Class(function(){
|
||||
}else{
|
||||
whereStr += key + " " + this.comparison[val[0].toLowerCase()] + " " + this.parseValue(val[1]);
|
||||
}
|
||||
};
|
||||
}else if(val[0].toLowerCase() == 'exp'){ // 使用表达式
|
||||
whereStr += "(" + key + " " + val[1] + ")";
|
||||
}else if(/IN/i.test(val[0])){ // IN 运算
|
||||
if (val[2] == 'exp') {
|
||||
whereStr += key + " " + val[0].toUpperCase() + " " + val[1];
|
||||
}else if(val[0].toLowerCase() == 'exp'){ // 使用表达式
|
||||
whereStr += "(" + key + " " + val[1] + ")";
|
||||
}else if(/IN/i.test(val[0])){ // IN 运算
|
||||
if (val[2] == 'exp') {
|
||||
whereStr += key + " " + val[0].toUpperCase() + " " + val[1];
|
||||
}else{
|
||||
if (is_string(val[1])) {
|
||||
val[1] = val[1].split(",");
|
||||
};
|
||||
var zone = this.parseValue(val[1]).join(",");
|
||||
whereStr += key + " " + val[0].toUpperCase() + " (" + zone + ")";
|
||||
}
|
||||
}else if(/BETWEEN/i.test(val[0])){ // BETWEEN运算
|
||||
var data = is_string(val[1]) ? val[1].split(",") : val[1];
|
||||
whereStr += " (" + key + " " + val[0].toUpperCase() + " " + this.parseValue(data[0]) + " AND " + this.parseValue(data[1]) + ")";
|
||||
}else{
|
||||
if (is_string(val[1])) {
|
||||
val[1] = val[1].split(",");
|
||||
};
|
||||
var zone = this.parseValue(val[1]).join(",");
|
||||
whereStr += key + " " + val[0].toUpperCase() + " (" + zone + ")";
|
||||
throw_error("_EXPRESS_ERROR_" + key + val);
|
||||
}
|
||||
}else if(/BETWEEN/i.test(val[0])){ // BETWEEN运算
|
||||
var data = is_string(val[1]) ? val[1].split(",") : val[1];
|
||||
|
||||
}else{
|
||||
var length = val.length;
|
||||
var rule = val[val.length - 1] || "";
|
||||
var ruleList = ['AND','OR','XOR'];
|
||||
if (rule && ruleList.indexOf(rule) > -1) {
|
||||
length = length - 1;
|
||||
}else{
|
||||
rule = "AND";
|
||||
}
|
||||
for(var i=0; i<length; i++){
|
||||
var data = is_array(val[i]) ? val[i][1] : val[i];
|
||||
var exp = ((is_array(val[i]) && val[i][0]) + "").toLowerCase();
|
||||
if (exp == 'exp') {
|
||||
whereStr += "(" + key + " " + data + ") " + rule + " ";
|
||||
}else{
|
||||
var op = is_array(val[i]) ? this.comparison[val[i][0].toLowerCase()] : "=";
|
||||
whereStr += "(" + key + " " + op + " " + this.parseValue(data) + ") " + rule + " ";
|
||||
}
|
||||
}
|
||||
whereStr = whereStr.substr(0, whereStr.length - 4);
|
||||
}
|
||||
};
|
||||
return "";
|
||||
}else{
|
||||
//对字符串类型字段采用模糊匹配
|
||||
if (C('db_like_fields') && (new RegExp(C('db_like_fields'), "i")).test(key)) {
|
||||
whereStr += key + " LIKE " + this.parseValue("%" + val + "%");
|
||||
}else{
|
||||
whereStr = key + " = " + this.parseValue(val);
|
||||
}
|
||||
}
|
||||
return whereStr;
|
||||
},
|
||||
parseThinkWhere: function(key, val){
|
||||
var whereStr = "";
|
||||
@ -340,6 +369,185 @@ var db = module.exports = Class(function(){
|
||||
sql.push(sql);
|
||||
}
|
||||
return sql.join(" ");
|
||||
},
|
||||
parseLock: function(lock){
|
||||
if (!lock) {
|
||||
return "";
|
||||
};
|
||||
return " FOR UPDATE ";
|
||||
},
|
||||
buildSelectSql: function(options){
|
||||
options = options || {};
|
||||
if ("page" in options) {
|
||||
// 根据页数计算limit
|
||||
var page = options.page || "";
|
||||
var listRows = 0;
|
||||
if (page.indexOf(",") > -1) {
|
||||
page = page.split(",");
|
||||
page = page[0];
|
||||
listRows = page[1];
|
||||
}
|
||||
page = parseInt(page, 10) || 1;
|
||||
listRows = listRows || options.limit || 20;
|
||||
var offset = listRows * (page - 1);
|
||||
options.limit = offset + "," + listRows;
|
||||
};
|
||||
// SQL创建缓存
|
||||
var key = "";
|
||||
if (C('db_sql_build_cache')) {
|
||||
key = md5(JSON.stringify(options));
|
||||
//使用默认对象的缓存方式,这样是直接获取
|
||||
value = S(key, undefined, true);
|
||||
if (value !== false) {
|
||||
return value;
|
||||
};
|
||||
};
|
||||
var sql = this.parseSql(this.selectSql, options);
|
||||
sql += this.parseLock(options.lock);
|
||||
if (key) {
|
||||
S(key, sql, {
|
||||
type: true,
|
||||
expire: 0
|
||||
}
|
||||
};
|
||||
return sql;
|
||||
},
|
||||
parseSql: function(sql, options){
|
||||
options = options || {};
|
||||
var self = this;
|
||||
return sql.replace(/\%([A-Z]+)\%/g, function(a, type){
|
||||
return self[ucfirst(type)](options[type.toLowerCase()] || "");
|
||||
})
|
||||
},
|
||||
insert: function(data, options, replace){
|
||||
data = data || {};
|
||||
options = options || {};
|
||||
var values = [];
|
||||
var fields = [];
|
||||
this.model = options.model;
|
||||
for(var key in data){
|
||||
var val = data[key];
|
||||
val = this.parseValue(val);
|
||||
if (is_scalar(val)) {
|
||||
values.push(val);
|
||||
fields.push(this.parseKey(key));
|
||||
};
|
||||
}
|
||||
var sql = (replace ? "REPLACE" : "INSERT") + " INTO ";
|
||||
sql += this.parseTable(options.table) + " (" + fields.join(",") + ") ";
|
||||
sql += "VALUES(" + values.join(",") + ")";
|
||||
sql += this.parseLock(options.lock) + this.parseComment(options.comment);
|
||||
return this.execute(sql);
|
||||
},
|
||||
selectInsert: function(fields, table, options){
|
||||
options = options || {};
|
||||
this.model = options.model;
|
||||
if (is_string(fields)) {
|
||||
fields = fields.split(",");
|
||||
};
|
||||
var self = this;
|
||||
fields = fields.map(function(item){
|
||||
return self.parseKey(item);
|
||||
});
|
||||
var sql = "INSERT INTO " + this.parseTable(options.table) + " (" + fields.join(",") + ")";
|
||||
sql += this.buildSelectSql(options);
|
||||
return this.execute(sql);
|
||||
},
|
||||
delete: function(options){
|
||||
options = options || {};
|
||||
this.model = options.model;
|
||||
var sql = [
|
||||
"DELETE FROM ",
|
||||
this.parseTable(options.table),
|
||||
this.parseWhere(options.where),
|
||||
this.parseOrder(options.order),
|
||||
this.parseLimit(options.limit),
|
||||
this.parseLock(options.lock),
|
||||
this.parseComment(options.comment)
|
||||
].join("");
|
||||
return this.execute(sql);
|
||||
},
|
||||
/**
|
||||
* 更新数据
|
||||
* @param {[type]} data [description]
|
||||
* @param {[type]} options [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
update: function(data, options){
|
||||
options = options || {}
|
||||
this.model = options.model;
|
||||
var sql = [
|
||||
"UPDATE ",
|
||||
this.parseTable(options.table),
|
||||
this.parseSet(data),
|
||||
this.parseWhere(options.where),
|
||||
this.parseOrder(options.order),
|
||||
this.parseLimit(options.limit),
|
||||
this.parseLock(options.lock),
|
||||
this.parseComment(options.comment)
|
||||
].join("");
|
||||
return this.execute(sql);
|
||||
},
|
||||
/**
|
||||
* 数据查询
|
||||
* @todo 返回是个promise,缓存调用需要修改
|
||||
* @param {[type]} options [description]
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
select: function(options){
|
||||
options = options || {};
|
||||
this.model = options.model;
|
||||
var sql = this.buildSelectSql(options);
|
||||
var cache = options.cache;
|
||||
if (cache) {
|
||||
var key = cache.key || md5(sql);
|
||||
var value = S(key, undefined, cache);
|
||||
if (value !== false) {
|
||||
return value;
|
||||
};
|
||||
};
|
||||
var result = this.query(sql);
|
||||
if (cache && result !== false) {
|
||||
S(key, result, cache);
|
||||
};
|
||||
return result;
|
||||
},
|
||||
escapeString: function(str){
|
||||
if (!str) {
|
||||
return "";
|
||||
};
|
||||
return str.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function(s) {
|
||||
switch(s) {
|
||||
case "\0": return "\\0";
|
||||
case "\n": return "\\n";
|
||||
case "\r": return "\\r";
|
||||
case "\b": return "\\b";
|
||||
case "\t": return "\\t";
|
||||
case "\x1a": return "\\Z";
|
||||
default: return "\\"+s;
|
||||
}
|
||||
});
|
||||
},
|
||||
setModel: function(model){
|
||||
this.model = model;
|
||||
},
|
||||
getLastSql: function(model){
|
||||
return model ? this.modelSql[model] : this.queryStr;
|
||||
},
|
||||
getLastInsID: function(){
|
||||
return this.lastInsID;
|
||||
},
|
||||
getError: function(){
|
||||
return this.error;
|
||||
},
|
||||
query: function(){
|
||||
|
||||
},
|
||||
execute: function(){
|
||||
|
||||
},
|
||||
close: function(){
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -23,10 +23,6 @@ module.exports = {
|
||||
http.res.statusCode = err.code || 403;
|
||||
http.res.write(err.msg, C('encoding'));
|
||||
break;
|
||||
case "redirect":
|
||||
http.res.statusCode = err.code || 302;
|
||||
http.res.setHeader("Location", err.msg);
|
||||
break;
|
||||
default:
|
||||
http.res.statusCode = err.code || 500;
|
||||
http.res.write(err.msg, C('encoding'));
|
||||
|
||||
@ -2,8 +2,23 @@
|
||||
* mysql数据库
|
||||
* @return {[type]} [description]
|
||||
*/
|
||||
var mysqlSocket = think_require("MysqlSocket");
|
||||
var db = module.exports = Db(function(){
|
||||
return {
|
||||
init: function(config){
|
||||
this.super("init");
|
||||
if (config) {
|
||||
this.config = config;
|
||||
};
|
||||
},
|
||||
connect: function(){
|
||||
|
||||
},
|
||||
query: function(){
|
||||
|
||||
},
|
||||
execute: function(){
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user