diff --git a/lib/Common/functions.js b/lib/Common/functions.js index 29a02710..9c3d2a93 100644 --- a/lib/Common/functions.js +++ b/lib/Common/functions.js @@ -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"; diff --git a/lib/Lib/Core/Db.class.js b/lib/Lib/Core/Db.class.js index 5cb0e550..1341a4e6 100644 --- a/lib/Lib/Core/Db.class.js +++ b/lib/Lib/Core/Db.class.js @@ -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 -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(){ + } } }); diff --git a/lib/Lib/Core/Think.class.js b/lib/Lib/Core/Think.class.js index 8cb62def..a11dd974 100644 --- a/lib/Lib/Core/Think.class.js +++ b/lib/Lib/Core/Think.class.js @@ -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')); diff --git a/lib/Lib/Driver/Db/MysqlDb.class.js b/lib/Lib/Driver/Db/MysqlDb.class.js index 2e303f28..30dd0565 100644 --- a/lib/Lib/Driver/Db/MysqlDb.class.js +++ b/lib/Lib/Driver/Db/MysqlDb.class.js @@ -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(){ + + } } }); \ No newline at end of file