diff --git a/lib/Lib/Core/Comparison.js b/lib/Lib/Core/Comparison.js new file mode 100644 index 00000000..8e7b76c1 --- /dev/null +++ b/lib/Lib/Core/Comparison.js @@ -0,0 +1,24 @@ +"use strict"; + +var COMPARISON = { + EQ: '=', + NEQ: '!=', + '<>': '!=', + GT: '>', + EGT: '>=', + LT: '<', + ELT: '<=', + NOTLIKE: 'NOT LIKE', + LIKE: 'LIKE', + NOTILIKE: 'NOT ILIKE', + ILIKE: 'ILIKE', + IN: 'IN', + NOTIN: 'NOT IN' +}; +var allowKeys = ['EXP', 'BETWEEN', 'NOT BETWEEN']; +var keys = Object.keys(COMPARISON); + +exports.COMPARISON = COMPARISON; +exports.COMPARISON_LIST = keys.concat(keys.map(function(item) { + return COMPARISON[item]; +})).concat(allowKeys); diff --git a/lib/Lib/Core/Controller.js b/lib/Lib/Core/Controller.js index 77c61bef..9c84520b 100644 --- a/lib/Lib/Core/Controller.js +++ b/lib/Lib/Core/Controller.js @@ -1,3 +1,4 @@ +"use strict"; /** * Controller 基类 * @return {[type]} [description] @@ -5,9 +6,30 @@ var fs = require('fs'); var path = require('path'); var url = require('url'); +var comparison = require('./Comparison.js'); +var utils = require('thinkjs-util'); + +var escapeComparison = function(value) { + if(Array.isArray(value) && typeof value[0] === 'string') { + if(comparison.COMPARISON_LIST.indexOf(value[0].toUpperCase()) > -1) { + value[0] += ' '; + } + } + if(utils.isObject(value)) { + var result = {}; + for(var key in value) { + if(comparison.COMPARISON_LIST.indexOf(key.toUpperCase()) > -1) { + result[key + ' '] = value[key]; + }else{ + result[key] = value[key]; + } + } + return result; + } + return value; +} module.exports = Class(function() { - 'use strict'; return { /** @@ -133,9 +155,13 @@ module.exports = Class(function() { */ get: function(name) { if (name === undefined) { - return this.http.get; + var result = {}; + for(var key in this.http.get) { + result[key] = escapeComparison(this.http.get[key] || ''); + } + return result; } - return this.http.get[name] || ''; + return escapeComparison(this.http.get[name] || ''); }, /** * 获取POST参数 @@ -143,8 +169,14 @@ module.exports = Class(function() { * @return {[type]} [description] */ post: function(name) { - var http = this.http; - return name ? (http.post[name] || '') : http.post; + if (name === undefined) { + var result = {}; + for(var key in this.http.post) { + result[key] = escapeComparison(this.http.post[key] || ''); + } + return result; + } + return escapeComparison(this.http.post[name] || ''); }, /** * 获取参数 diff --git a/lib/Lib/Core/Db.js b/lib/Lib/Core/Db.js index 7ffa2bbf..0d978073 100644 --- a/lib/Lib/Core/Db.js +++ b/lib/Lib/Core/Db.js @@ -1,10 +1,29 @@ +'use strict'; + var querystring = require('querystring'); +var comparison = require('./Comparison.js'); + +/** + * get comparison + * @param {String} comparison + */ +var getComparison = function(value) { + var comparisonUpper = value.toUpperCase(); + comparisonUpper = comparison.COMPARISON[comparisonUpper] || comparisonUpper; + if (comparison.COMPARISON_LIST.indexOf(comparisonUpper) > -1) { + return comparisonUpper; + } + throw new Error(value + 'is not valid'); +}; + + + /** * 数据库基类 * @return {[type]} [description] */ var Db = module.exports = Class(function(){ - 'use strict'; + //数据库连接 var dbConnections = {}; //获取数据配置的基本信息 @@ -36,19 +55,6 @@ var Db = module.exports = Class(function(){ //用于查询的sql语句,所有select语句根据该语句解析 selectSql: 'SELECT%DISTINCT% %FIELD% FROM %TABLE%%JOIN%%WHERE%%GROUP%%HAVING%%ORDER%%LIMIT% %UNION%%COMMENT%', //where条件里的表达式 - comparison: { - 'EQ': '=', - 'NEQ': '!=', - '<>': '!=', - 'GT': '>', - 'EGT': '>=', - 'LT': '<', - 'ELT': '<=', - 'NOTLIKE': 'NOT LIKE', - 'LIKE': 'LIKE', - 'IN': 'IN', - 'NOTIN': 'NOT IN' - }, /** * 初始化 * @return {[type]} [description] @@ -293,7 +299,7 @@ var Db = module.exports = Class(function(){ var result = []; for(var opr in val){ var nop = opr.toUpperCase(); - nop = this.comparison[nop] || nop; + nop = getComparison(nop); result.push(key + ' ' + nop + ' ' + this.parseValue(val[opr])); } return result.join(' ' + logic + ' '); @@ -309,7 +315,7 @@ var Db = module.exports = Class(function(){ var data; if (isString(val[0])) { var val0 = val[0].toUpperCase(); - val0 = this.comparison[val0] || val0; + val0 = getComparison(val0); if (/^(=|!=|>|>=|<|<=)$/.test(val0)) { // 比较运算 whereStr += key + ' ' + val0 + ' ' + this.parseValue(val[1]); }else if (/^(NOT\s+LIKE|LIKE)$/.test(val0)) { // 模糊查找 @@ -375,7 +381,7 @@ var Db = module.exports = Class(function(){ if (exp === 'EXP') { whereStr += '(' + key + ' ' + data + ') ' + rule + ' '; }else{ - var op = isArr ? (this.comparison[val[i][0].toUpperCase()] || val[i][0]) : '='; + var op = isArr ? getComparison(val[i][0]) : '='; whereStr += '(' + key + ' ' + op + ' ' + this.parseValue(data) + ') ' + rule + ' '; } } @@ -949,7 +955,6 @@ var Db = module.exports = Class(function(){ * @return {[type]} [description] */ Db.parseConfig = function(config){ - 'use strict'; config = config || {}; var conf = { type: config.db_type || C('db_type'), @@ -972,7 +977,6 @@ Db.parseConfig = function(config){ * @return {[type]} [description] */ Db.getInstance = function(config){ - 'use strict'; var conf = this.parseConfig(config); var instance = thinkRequire(ucfirst(conf.type) + 'Db')(conf); instance.dbType = conf.type.toUpperCase(); diff --git a/lib/Lib/Core/Think.js b/lib/Lib/Core/Think.js index 5cc685e4..7cf78ed3 100644 --- a/lib/Lib/Core/Think.js +++ b/lib/Lib/Core/Think.js @@ -303,7 +303,7 @@ module.exports = { for(var file in require.cache){ var flag = retainFiles.some(fn); if (!flag) { - require.cache[file] = null; + delete require.cache[file]; } } self.loadFiles(); @@ -321,7 +321,7 @@ module.exports = { if (C('log_process_pid') && cluster.isMaster) { mkdir(DATA_PATH); var pidFile = DATA_PATH + '/app.pid'; - fs.writeFileSync(pidFile, process.pid); + fs.writeFileSync(pidFile, process.pid + ''); chmod(pidFile); //进程退出时删除该文件 process.on('SIGTERM', function () { diff --git a/lib/Lib/Driver/Session/DbSession.js b/lib/Lib/Driver/Session/DbSession.js index d9296b89..bdb65d37 100644 --- a/lib/Lib/Driver/Session/DbSession.js +++ b/lib/Lib/Driver/Session/DbSession.js @@ -43,6 +43,7 @@ module.exports = Cache(function(){ model = D('Session'); } this.model = model; + this.isFresh = false; }, /** * 初始化数据 @@ -53,10 +54,7 @@ module.exports = Cache(function(){ var self = this; this.promise = model.where({cookie: this.cookie}).find().then(function(data){ if (isEmpty(data)) { - return model.add({ - cookie: self.cookie, - expire: Date.now() + self.options.timeout * 1000 - }); + self.isFresh = true; } if (Date.now() > data.expire) { return; @@ -119,7 +117,14 @@ module.exports = Cache(function(){ if (self.isChanged) { data.data = JSON.stringify(self.data); } - return model.where({cookie: self.cookie}).update(data); + if (self.isFresh) { + if (data.data) { + data.cookie = self.cookie; + return model.add(data); + } + } else { + return model.where({cookie: self.cookie}).update(data); + } }); }, /** diff --git a/lib/Lib/Driver/Socket/RedisSocket.js b/lib/Lib/Driver/Socket/RedisSocket.js index 737cadc5..8a16cc04 100644 --- a/lib/Lib/Driver/Socket/RedisSocket.js +++ b/lib/Lib/Driver/Socket/RedisSocket.js @@ -79,6 +79,9 @@ module.exports = Class(function(){ }, expire: function(name, timeout){ return this.wrap('expire', [name, timeout]); + }, + delete: function(name){ + return this.wrap('del', name); } } }); \ No newline at end of file diff --git a/lib/Lib/Util/Valid.js b/lib/Lib/Util/Valid.js index f7ec024c..0fd17b5b 100644 --- a/lib/Lib/Util/Valid.js +++ b/lib/Lib/Util/Valid.js @@ -132,11 +132,12 @@ var Valid = { */ int: function(value){ 'use strict'; + var val = parseInt(value, 10); if (isNaN(val)) { return false; } - return (val + '').length === value.length; + return (val + '').length === (value + '').length; }, /** * 浮点数 diff --git a/package.json b/package.json index b61b87c4..3833e4fb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "thinkjs", "description": "A Node.js MVC Web Framework Based On Promise", - "version": "1.2.10", + "version": "1.2.17", "author": { "name": "welefen", "email": "welefen@gmail.com" @@ -9,6 +9,9 @@ "scripts": { "test": "make test" }, + "publishConfig": { + "tag": "v1" + }, "contributors": [ { "name": "welefen", @@ -16,7 +19,7 @@ } ], "dependencies": { - "ejs": "2.2.4", + "ejs": "^2.5.7", "multiparty": "4.1.1", "es6-promise": "2.0.1", "mime": "1.2.11", diff --git a/test/Common/common.js b/test/Common/common.js index ffbb2631..8f5332f9 100644 --- a/test/Common/common.js +++ b/test/Common/common.js @@ -820,7 +820,7 @@ describe('getPromise', function(){ describe('getDefer', function(){ it('getDefer()', function(){ var deferred = getDefer(); - assert.equal(isObject(deferred.promise), true); + assert.equal(isObject(deferred.promise), false); assert.equal(isFunction(deferred.resolve), true); assert.equal(isFunction(deferred.reject), true) assert.equal(isFunction(deferred.promise.then), true); diff --git a/test/Lib/Core/Controller.js b/test/Lib/Core/Controller.js index 85782822..272b0dec 100644 --- a/test/Lib/Core/Controller.js +++ b/test/Lib/Core/Controller.js @@ -240,6 +240,29 @@ describe('Controller', function(){ done(); }) }) + + + it('instance.get all 2', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.get.key222 = ['EXP', 2]; + assert.deepEqual(instance.get().key222, ['EXP ', 2]); + delete instance.http.get.key222; + done(); + }) + }) + + it('instance.get all 3', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.get.key222 = {EXP: 2} + assert.deepEqual(instance.get().key222, {'EXP ': 2}); + delete instance.http.get.key222; + done(); + }) + }) + + it('instance.post()', function(done){ promise.then(function(instance){ //console.log(JSON.stringify(instance.get())); @@ -255,6 +278,88 @@ describe('Controller', function(){ }) }) + it('instance.post("xxx") 2', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.post.key222 = ['EXP ', 2]; + assert.deepEqual(instance.post('key222'), ['EXP ', 2]); + delete instance.http.post.key222; + done(); + }) + }) + + it('instance.post all 2', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.post.key222 = ['EXP ', 2]; + assert.deepEqual(instance.post().key222, ['EXP ', 2]); + delete instance.http.post.key222; + done(); + }) + }) + + + it('instance.post("xxx") 3', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.post.key333 = {EXP: 3} + assert.deepEqual(instance.post('key333'), {'EXP ': 3}); + delete instance.http.post.key333; + done(); + }) + }) + + it('instance.post all 3', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.post.key333 = {EXP: 3} + assert.deepEqual(instance.post().key333, {'EXP ': 3}); + delete instance.http.post.key333; + done(); + }) + }) + + it('instance.post("xxx") 4', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.post.key333 = {EXP3: 3} + assert.deepEqual(instance.post('key333'), {'EXP3': 3}); + delete instance.http.post.key333; + done(); + }) + }) + + it('instance.post all 4', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.post.key333 = {EXP3: 3} + assert.deepEqual(instance.post().key333, {'EXP3': 3}); + delete instance.http.post.key333; + done(); + }) + }) + + it('instance.post("xxx") 5', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.post.key222 = ['EXP2', 2]; + assert.deepEqual(instance.post('key222'), ['EXP2', 2]); + delete instance.http.post.key222; + done(); + }) + }) + + it('instance.post all 5', function(done){ + promise.then(function(instance){ + //console.log(JSON.stringify(instance.get())); + instance.http.post.key222 = ['EXP2', 2]; + assert.deepEqual(instance.post().key222, ['EXP2', 2]); + delete instance.http.post.key222; + done(); + }) + }) + + it('instance.param()', function(done){ promise.then(function(instance){ //console.log(instance.param()) diff --git a/test/Lib/Driver/Session/DbSession.js b/test/Lib/Driver/Session/DbSession.js index 4edbf7af..11e57181 100644 --- a/test/Lib/Driver/Session/DbSession.js +++ b/test/Lib/Driver/Session/DbSession.js @@ -28,7 +28,7 @@ describe('before', function(){ { Field: 'expire',Type: 'bigint(11)',Null: 'NO',Key: 'MUL',Default: null,Extra: '' } ]); }else if (sql === "SELECT * FROM `think_session` WHERE ( `cookie` = 'Nru4DV9uYy3jUP8_MDl4l-wbkVe-gQO_' ) LIMIT 1") { - return getPromise([ { id: 1,cookie: 'Nru4DV9uYy3jUP8_MDl4l-wbkVe-gQO_',data: '{"name":"suredy"}',expire: 1509896404697 } ]) + return getPromise([ { id: 1,cookie: 'Nru4DV9uYy3jUP8_MDl4l-wbkVe-gQO_',data: '{"name":"suredy"}',expire: 2509896404697 } ]) }else if (sql === "SELECT * FROM `think_session` WHERE ( `cookie` = 'expired' ) LIMIT 1") { return getPromise([ { id: 1,cookie: 'Nru4DV9uYy3jUP8_MDl4l-wbkVe-gQO_',data: '{"name":"suredy"}',expire: 1309896404697 } ]) }else if (sql === "SELECT * FROM `think_session` WHERE ( `cookie` = 'dataempty' ) LIMIT 1") { diff --git a/test/Lib/Util/valid.js b/test/Lib/Util/valid.js index 1b639512..68a5c0f9 100644 --- a/test/Lib/Util/valid.js +++ b/test/Lib/Util/valid.js @@ -333,6 +333,18 @@ describe('Valid', function(){ var ret = Valid(data); assert.equal(ret.welefen, undefined) }) + + it('Valid int', function(){ + var data = { + name: 'welefen', + value: 123123, + valid: 'int', + msg: "int is not valid" + } + var ret = Valid(data); + assert.equal(ret.welefen, undefined) + }) + it('Valid int', function(){ var data = { name: 'welefen',