doomiwork 3.4.1 → 3.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/database/mysqlbase.js +87 -64
- package/package.json +1 -1
|
@@ -6,6 +6,7 @@ const apiResult = require('../actionresult');
|
|
|
6
6
|
const logHelper = require('doomi-helper').logHelper;
|
|
7
7
|
const PoolManager = require('./poolmanager')
|
|
8
8
|
const mysql = require('mysql');
|
|
9
|
+
const executeSqlWithConnection = Symbol("executeSqlWithConnection");
|
|
9
10
|
class Database {
|
|
10
11
|
/**
|
|
11
12
|
*
|
|
@@ -21,29 +22,31 @@ class Database {
|
|
|
21
22
|
* 防Sql注入进行编码
|
|
22
23
|
* @param {*} value
|
|
23
24
|
*/
|
|
24
|
-
escape(value){
|
|
25
|
+
escape(value) {
|
|
25
26
|
return mysql.escape(value);
|
|
26
27
|
}
|
|
27
28
|
/**
|
|
28
29
|
* 记录错误日志
|
|
29
30
|
*/
|
|
30
|
-
logError(scene,error,user=null){
|
|
31
|
+
logError(scene, error, user = null) {
|
|
31
32
|
///不需要记录错误日志
|
|
32
|
-
if (this.ingoreErrorLog === true || !this.actionLogger || typeof (this.actionLogger.logError)!=='function'){
|
|
33
|
-
return
|
|
33
|
+
if (this.ingoreErrorLog === true || !this.actionLogger || typeof (this.actionLogger.logError) !== 'function') {
|
|
34
|
+
return;
|
|
34
35
|
}
|
|
35
36
|
///如果没有传场景,则记录数据库的业务值
|
|
36
37
|
if (!scene) scene = this.constructor.name;
|
|
37
|
-
this.actionLogger.logError({ scene, error, user})
|
|
38
|
+
this.actionLogger.logError({ scene, error, user })
|
|
38
39
|
}
|
|
39
40
|
/**
|
|
40
41
|
* 检查是否存在数据
|
|
41
42
|
* @param {*} sqlCommand
|
|
42
43
|
* @param {*} parameters
|
|
43
44
|
*/
|
|
44
|
-
async existedSqlData(sqlCommand, parameters){
|
|
45
|
-
let sqlExecute = await this.executeSql(sqlCommand, parameters);
|
|
46
|
-
return { successed:
|
|
45
|
+
async existedSqlData(sqlCommand, parameters, conn, autoclose = true) {
|
|
46
|
+
let sqlExecute = await this.executeSql(sqlCommand, parameters, conn, autoclose);
|
|
47
|
+
if (!sqlExecute.successed) return { successed: false }
|
|
48
|
+
if (!sqlExecute.rows || sqlExecute.rows.length == 0) return { success: true, existed: false }
|
|
49
|
+
return { successed: true, existed: true, ...sqlExecute.rows[0] }
|
|
47
50
|
}
|
|
48
51
|
/**
|
|
49
52
|
* 获取到第一行记录的所有字段的值
|
|
@@ -51,87 +54,107 @@ class Database {
|
|
|
51
54
|
* @param {*} parameters
|
|
52
55
|
* @returns
|
|
53
56
|
*/
|
|
54
|
-
async getComputedRowValue(sqlCommand, parameters) {
|
|
55
|
-
let sqlExecute = await this.executeSql(sqlCommand, parameters);
|
|
56
|
-
if (sqlExecute.successed || sqlExecute.rows===0) return {successed:false,errcode
|
|
57
|
+
async getComputedRowValue(sqlCommand, parameters, conn, autoclose = true) {
|
|
58
|
+
let sqlExecute = await this.executeSql(sqlCommand, parameters, conn, autoclose);
|
|
59
|
+
if (sqlExecute.successed || sqlExecute.rows === 0) return { successed: false, errcode: -1, errmsg: '没有任何记录' }
|
|
57
60
|
return { successed: true, ...sqlExecute.rows[0] }
|
|
58
61
|
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 单独获得一个连接串
|
|
65
|
+
* @returns
|
|
66
|
+
*/
|
|
67
|
+
getConnection() {
|
|
68
|
+
return new Promise((success, failed) => {
|
|
69
|
+
if (!this.pool) {
|
|
70
|
+
return success(null);
|
|
71
|
+
}
|
|
72
|
+
this.pool.getConnection(function (err, connection) {
|
|
73
|
+
if (err) return success(null);;
|
|
74
|
+
return success(connection)
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
59
78
|
/**
|
|
60
79
|
* 不事务执行SQL命令
|
|
61
80
|
* @param {*} sqlCommand
|
|
62
81
|
* @param {*} parameters 参数
|
|
63
82
|
*/
|
|
64
|
-
executeSql(sqlCommand, parameters) {
|
|
83
|
+
executeSql(sqlCommand, parameters, conn, autoclose = true) {
|
|
65
84
|
let logger = this.logger;
|
|
66
|
-
let that = this;
|
|
85
|
+
// let that = this;
|
|
86
|
+
if (conn) {
|
|
87
|
+
return this[executeSqlWithConnection](sqlCommand, parameters, conn, autoclose)
|
|
88
|
+
}
|
|
67
89
|
return new Promise((success, failed) => {
|
|
68
90
|
if (!this.pool) {
|
|
69
91
|
return success(apiResult.DB_CONNECTED_FAILED);
|
|
70
92
|
}
|
|
71
93
|
///创建出数据库连接,准备执行
|
|
72
|
-
this.pool.getConnection(
|
|
94
|
+
this.pool.getConnection(async (err, connection) => {
|
|
73
95
|
if (err) {
|
|
74
|
-
logger.error("Database Connected Failed :"+ err);
|
|
96
|
+
logger.error("Database Connected Failed :" + err);
|
|
75
97
|
return success(apiResult.DB_CONNECTED_FAILED);
|
|
76
98
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
connection.query(sqlCommand, parameters, function (err, rows) {
|
|
80
|
-
connection.release();
|
|
81
|
-
if (err) {
|
|
82
|
-
logger.error("Database Query Error :" + err, sqlCommand);
|
|
83
|
-
that.logError(null, '数据库操作错误:' + err + sqlCommand)
|
|
84
|
-
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
85
|
-
}
|
|
86
|
-
return success({ successed: true, rows: rows });//,fields:fields});
|
|
87
|
-
});
|
|
88
|
-
}
|
|
99
|
+
let sqlResult = await this[executeSqlWithConnection](sqlCommand, parameters, connection, autoclose)
|
|
100
|
+
return success(sqlResult)
|
|
89
101
|
});
|
|
90
102
|
});
|
|
91
103
|
}
|
|
92
|
-
|
|
93
104
|
/**
|
|
94
|
-
*
|
|
105
|
+
* 使用一个已存在的链接Query Sql
|
|
95
106
|
* @param {*} sqlCommand
|
|
96
|
-
* @param {*} parameters
|
|
107
|
+
* @param {*} parameters
|
|
108
|
+
* @param {*} conn
|
|
109
|
+
* @param {*} closeAfterQuery 执行完毕后是否关闭
|
|
97
110
|
*/
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return new Promise((success
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
111
|
+
[executeSqlWithConnection](sqlCommand, parameters, conn, autoclose = true) {
|
|
112
|
+
if (!conn) return Promise.resolve(apiResult.DB_CONNECTED_FAILED);
|
|
113
|
+
return new Promise((success) => {
|
|
114
|
+
this.logger.trace("SqlCommand:", sqlCommand, "Parameters:", parameters);
|
|
115
|
+
return conn.query(sqlCommand, parameters, (err, rows) => {
|
|
116
|
+
if (autoclose) conn.release();
|
|
104
117
|
if (err) {
|
|
105
|
-
logger.error("Database
|
|
106
|
-
|
|
118
|
+
this.logger.error("Database Query Error :" + err, sqlCommand);
|
|
119
|
+
this.logError(null, '数据库操作错误:' + err + sqlCommand)
|
|
120
|
+
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
107
121
|
}
|
|
108
|
-
|
|
109
|
-
////开启事务保护
|
|
110
|
-
connection.beginTransaction(transerr=>{
|
|
111
|
-
if (transerr) return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: transerr.message }));
|
|
112
|
-
connection.query(sqlCommand, parameters, function (err, rows) {
|
|
113
|
-
////执行出错,事务回滚
|
|
114
|
-
if (err) {
|
|
115
|
-
logger.error("Database Query Error :"+ err);
|
|
116
|
-
return connection.rollback(() => {
|
|
117
|
-
connection.release();
|
|
118
|
-
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
119
|
-
})
|
|
120
|
-
}
|
|
121
|
-
///执行成功,事务提交
|
|
122
|
-
connection.commit(commiterror => {
|
|
123
|
-
if(commiterror) {
|
|
124
|
-
logger.error('事务提交失败 :',commiterror);
|
|
125
|
-
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
126
|
-
}
|
|
127
|
-
connection.release();
|
|
128
|
-
return success({ successed: true, rows: rows });
|
|
129
|
-
})
|
|
130
|
-
});
|
|
131
|
-
})
|
|
132
|
-
|
|
122
|
+
return success({ successed: true, rows: rows });//,fields:fields});
|
|
133
123
|
});
|
|
134
|
-
})
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* 启动事务进行保护性操作
|
|
128
|
+
* @param {*} conn
|
|
129
|
+
* @param {*} func
|
|
130
|
+
*/
|
|
131
|
+
async executeSqlWithTransaction(conn, func) {
|
|
132
|
+
if (!conn) {
|
|
133
|
+
conn = await this.getConnection();
|
|
134
|
+
}
|
|
135
|
+
if (!conn) return Promise.resolve({ successed: false });
|
|
136
|
+
return new Promise(success => {
|
|
137
|
+
conn.beginTransaction(async err => {
|
|
138
|
+
if (err) return success({ successed: false, errcode: -1, errmessage: '【事务启动错误】' + err.message });
|
|
139
|
+
let result = { successed: true }
|
|
140
|
+
if (func && typeof (func) === 'function') result = await func(conn);
|
|
141
|
+
if (result.successed) {
|
|
142
|
+
conn.commit(commiterror => {
|
|
143
|
+
if (commiterror) {
|
|
144
|
+
console.log('事务提交失败 :', commiterror);
|
|
145
|
+
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: commiterror.message }));
|
|
146
|
+
}
|
|
147
|
+
conn.release();
|
|
148
|
+
return success(result);
|
|
149
|
+
})
|
|
150
|
+
} else {
|
|
151
|
+
conn.rollback(() => {
|
|
152
|
+
conn.release();
|
|
153
|
+
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: '撤回操作' }));
|
|
154
|
+
})
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
})
|
|
135
158
|
}
|
|
136
159
|
}
|
|
137
160
|
module.exports = Database;
|