diff --git a/lib/Extend/Model/AdvModel.class.js b/lib/Extend/Model/AdvModel.class.js index 91e44b6d..cda5f411 100644 --- a/lib/Extend/Model/AdvModel.class.js +++ b/lib/Extend/Model/AdvModel.class.js @@ -3,6 +3,28 @@ * @return {[type]} [description] */ module.exports = Model(function(){ + //解析page参数 + var parsePage = function(options){ + if ("page" in options) { + var page = options.page + ""; + var num = 0; + if (page.indexOf(",") > -1) { + page = page.split(","); + num = parseInt(page[1], 10); + page = page[0]; + } + num = num || C('db_nums_per_page') + page = parseInt(page, 10) || 1; + return { + page: page, + num: num + } + }; + return { + page: 1, + num: C('db_nums_per_page') + } + } return { /** * 关联定义 @@ -11,19 +33,39 @@ module.exports = Model(function(){ relation: {}, /** * 返回数据里含有count信息的查询 - * @return {[type]} [description] + * @param options 查询选项 + * @param pageFlag 当页面不合法时,处理方式,true为获取第一页,false为获取最后一页,undefined获取为空 + * @return promise */ - countSelect: function(options){ + countSelect: function(options, pageFlag){ + if (isBoolean(options)) { + pageFlag = options; + options = {}; + }; var self = this; - return this.parseOptions(options).then(function(options){ - var result = self.db.select(options); - return getPromise(self._afterSelect(result, options)).then(function(result){ - if (result === false) { - return getPromise("_afterSelect return false", true); - }; - return result; - }) - }); + //解析后的options + var parsedOptions = {}; + var result = {}; + return this.parseOptions().then(function(options){ + parsedOptions = options; + return self.options({ + where: options.where + }).count(self.getPk()); + }).then(function(count){ + var pageOptions = parsePage(parsedOptions); + var totalPage = Math.ceil(count / pageOptions.num); + if (isBoolean(pageFlag)) { + if (pageOptions.page > totalPage) { + pageOptions.page = pageFlag === true ? 1 : totalPage; + }; + parsedOptions.page = pageOptions.page + ',' + pageOptions.num; + }; + result = extend({count: count, totalPage: totalPage}, pageOptions); + return self.select(parsedOptions); + }).then(function(data){ + result.data = data; + return result; + }) } } }) \ No newline at end of file diff --git a/lib/Lib/Core/Model.class.js b/lib/Lib/Core/Model.class.js index 0d61c082..c21f2956 100644 --- a/lib/Lib/Core/Model.class.js +++ b/lib/Lib/Core/Model.class.js @@ -35,7 +35,7 @@ var Model = module.exports = Class(function(){ // 数据信息 this._data = {}; // 参数 - this.options = {}; + this._options = {}; // 是否自动检测数据表字段信息 this.autoCheckFields = true; // 初始化的promise @@ -239,7 +239,7 @@ var Model = module.exports = Class(function(){ expire = key; key = ""; }; - this.options.cache = { + this._options.cache = { key: key, expire: expire, type: type @@ -253,7 +253,7 @@ var Model = module.exports = Class(function(){ * @return {[type]} [description] */ limit: function(offset, length){ - this.options.limit = length === undefined ? offset : offset + "," + length; + this._options.limit = length === undefined ? offset : offset + "," + length; return this; }, /** @@ -261,7 +261,7 @@ var Model = module.exports = Class(function(){ * @return {[type]} [description] */ page: function(page, listRows){ - this.options.page = listRows === undefined ? page : page + "," + listRows; + this._options.page = listRows === undefined ? page : page + "," + listRows; return this; }, /** @@ -274,7 +274,7 @@ var Model = module.exports = Class(function(){ "_string": where } }; - this.options.where = extend(this.options.where || {}, where); + this._options.where = extend(this._options.where || {}, where); return this; }, /** @@ -289,8 +289,8 @@ var Model = module.exports = Class(function(){ }else if (!field) { field = "*"; }; - this.options.field = field; - this.options.fieldReverse = reverse; + this._options.field = field; + this._options.fieldReverse = reverse; return this; }, /** @@ -298,10 +298,10 @@ var Model = module.exports = Class(function(){ * @return {[type]} [description] */ union: function(union){ - if (!this.options.union) { - this.options.union = []; + if (!this._options.union) { + this._options.union = []; }; - this.options.union.push(union); + this._options.union.push(union); return this; }, /** @@ -311,12 +311,12 @@ var Model = module.exports = Class(function(){ */ join: function(join){ if (isArray(join)) { - this.options.join = join; + this._options.join = join; }else{ - if (!this.options.join) { - this.options.join = []; + if (!this._options.join) { + this._options.join = []; }; - this.options.join.push(join); + this._options.join.push(join); } return this; }, @@ -339,10 +339,10 @@ var Model = module.exports = Class(function(){ parseOptions: function(options, extraOptions){ var self = this; options = this.parseWhereOptions(options); - options = extend(this.options, options); + options = extend(this._options, options); options = extend(options, extraOptions); // 查询过后清空sql表达式组装 避免影响下次查询 - this.options = {}; + this._options = {}; var promise = null; //获取数据表下的字段信息 if (options.table) { @@ -436,11 +436,11 @@ var Model = module.exports = Class(function(){ } }; //安全过滤 - if (typeof this.options.filter == 'function') { + if (typeof this._options.filter == 'function') { for(var key in data){ - data[key] = this.options.filter.call(this, key, data[key]); + data[key] = this._options.filter.call(this, key, data[key]); } - delete this.options.filter; + delete this._options.filter; }; data = this._dataFilter(data); return data; @@ -495,10 +495,6 @@ var Model = module.exports = Class(function(){ data = self.parseData(data); return self._beforeAdd(data, options); }).then(function(data){ - //如果_beforeAdd返回false,则直接返回 - if (data === false) { - return getPromise("_beforeAdd return false", true); - }; return self.db.insert(data, options, replace); }).then(function(data){ //获取插入的ID @@ -508,9 +504,6 @@ var Model = module.exports = Class(function(){ }; return getPromise(self._afterAdd(data, options)); }).then(function(data){ - if (data === false) { - return getPromise("_afterAdd return false", true); - }; return data[pk]; }) }, @@ -548,12 +541,7 @@ var Model = module.exports = Class(function(){ return self.db.delete(options); }).then(function(data){ return self._afterDelete(data, options); - }).then(function(data){ - if (data === false) { - return getPromise("_afterDelete return false", true); - }; - return data; - }) + }); }, /** * 更新前置操作 @@ -599,7 +587,7 @@ var Model = module.exports = Class(function(){ if ( data === false) { return getPromise("_beforeUpdate return false", true); }; - if (isEmpty(self.options.where) && isEmpty(options.where)) { + if (isEmpty(self._options.where) && isEmpty(options.where)) { // 如果存在主键数据 则自动作为更新条件 if (!isEmpty(data[pk])) { var where = {}; @@ -702,6 +690,12 @@ var Model = module.exports = Class(function(){ return result; }) }, + /** + * 查询后置操作 + * @param {[type]} result [description] + * @param {[type]} options [description] + * @return {[type]} [description] + */ _afterSelect: function(result, options){ return result; }, @@ -715,12 +709,7 @@ var Model = module.exports = Class(function(){ return self.db.select(options); }).then(function(result){ return self._afterSelect(result, options); - }).then(function(result){ - if (result === false) { - return getPromise("_afterSelect return false", true); - }; - return result; - }) + }); }, selectAdd: function(fields, table, options){ var self = this; @@ -856,6 +845,15 @@ var Model = module.exports = Class(function(){ this._data = data; return this; }, + /** + * 设置操作选项 + * @param {[type]} options [description] + * @return {[type]} [description] + */ + options: function(options){ + this._options = options; + return this; + }, /** * 关闭数据库连接 * @return {[type]} [description] @@ -876,7 +874,7 @@ var Model = module.exports = Class(function(){ ]; methodNameList.forEach(function(item){ methods[item] = function(data){ - this.options[item] = data; + this._options[item] = data; return this; } });