From e446c9db26137828dfbccddc2811b78414041ddb Mon Sep 17 00:00:00 2001 From: welefen Date: Mon, 28 Jul 2014 11:14:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9=E4=BA=8B=E5=8A=A1=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E6=95=B0=E6=8D=AE=E5=BA=93=E6=9C=AC=E8=BA=AB?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=89=8D=E8=83=BD=E4=BD=BF=E7=94=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Lib/Core/Db.js | 2 ++ lib/Lib/Core/Model.js | 24 +++++++++++++++++ lib/Lib/Driver/Db/MysqlDb.js | 50 +++++++++++++++++++++++++++++++----- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/lib/Lib/Core/Db.js b/lib/Lib/Core/Db.js index 6e4261f7..75056314 100644 --- a/lib/Lib/Core/Db.js +++ b/lib/Lib/Core/Db.js @@ -37,6 +37,8 @@ var Db = module.exports = Class(function(){ linkId: null, // 数据库连接参数配置 config: '', + // 事务次数 + transTimes: 0, /** * 初始化 * @return {[type]} [description] diff --git a/lib/Lib/Core/Model.js b/lib/Lib/Core/Model.js index b11f27c7..94f3e27c 100644 --- a/lib/Lib/Core/Model.js +++ b/lib/Lib/Core/Model.js @@ -955,6 +955,30 @@ var Model = module.exports = Class(function(){ this.initDb().setModel(self.getModelName()); return promise; }, + /** + * 启动事务 + * @return {[type]} [description] + */ + startTrans: function(){ + var self = this; + return this.initDb().commit().then(function(){ + return self.db.startTrans(); + }); + }, + /** + * 提交事务 + * @return {[type]} [description] + */ + commit: function(){ + return this.initDb().commit(); + }, + /** + * 回滚事务 + * @return {[type]} [description] + */ + rollback: function(){ + return this.initDb().rollback(); + }, /** * 设置数据对象值 * @return {[type]} [description] diff --git a/lib/Lib/Driver/Db/MysqlDb.js b/lib/Lib/Driver/Db/MysqlDb.js index 6a7292a1..64bb6e98 100644 --- a/lib/Lib/Driver/Db/MysqlDb.js +++ b/lib/Lib/Driver/Db/MysqlDb.js @@ -82,7 +82,9 @@ module.exports = Db(function(){ this.setSql(str); var self = this; return this.connect().query(str).then(function(data){ - self.lastInsertId = data.insertId; + if (data.insertId) { + self.lastInsertId = data.insertId; + } return data.affectedRows || 0; }); }, @@ -128,14 +130,38 @@ module.exports = Db(function(){ }); }, /** - * 关闭连接 + * 启动事务 * @return {[type]} [description] */ - close: function(){ - if (this.linkId) { - this.linkId.close(); - this.linkId = null; + startTrans: function(){ + if (this.transTimes === 0) { + this.transTimes++; + return this.execute('START TRANSACTION'); } + this.transTimes++; + return getPromise(); + }, + /** + * 提交事务 + * @return {[type]} [description] + */ + commit: function(){ + if (this.transTimes > 0) { + this.transTimes = 0; + return this.execute('COMMIT'); + } + return getPromise(); + }, + /** + * 回滚事务 + * @return {[type]} [description] + */ + rollback: function(){ + if (this.transTimes > 0) { + this.transTimes = 0; + return this.execute('ROLLBACK'); + } + return getPromise(); }, /** * 解析key @@ -155,6 +181,16 @@ module.exports = Db(function(){ */ getLastInsertId: function(){ return this.lastInsertId; - } + }, + /** + * 关闭连接 + * @return {[type]} [description] + */ + close: function(){ + if (this.linkId) { + this.linkId.close(); + this.linkId = null; + } + }, }; }); \ No newline at end of file