doomiwork 3.4.0 → 3.5.0
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 +92 -51
- package/package.json +1 -1
|
@@ -36,82 +36,123 @@ class Database {
|
|
|
36
36
|
if (!scene) scene = this.constructor.name;
|
|
37
37
|
this.actionLogger.logError({ scene, error, user})
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* 检查是否存在数据
|
|
41
|
+
* @param {*} sqlCommand
|
|
42
|
+
* @param {*} parameters
|
|
43
|
+
*/
|
|
44
|
+
async existedSqlData(sqlCommand, parameters){
|
|
45
|
+
let sqlExecute = await this.executeSql(sqlCommand, parameters);
|
|
46
|
+
if (!sqlExecute.successed) return {successed:false}
|
|
47
|
+
if (!sqlExecute.rows || sqlExecute.rows.length == 0) return { success: true, existed:false }
|
|
48
|
+
return { successed: true, existed: true, ...sqlExecute.rows[0] }
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 获取到第一行记录的所有字段的值
|
|
52
|
+
* @param {*} sqlCommand
|
|
53
|
+
* @param {*} parameters
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
async getComputedRowValue(sqlCommand, parameters) {
|
|
57
|
+
let sqlExecute = await this.executeSql(sqlCommand, parameters);
|
|
58
|
+
if (sqlExecute.successed || sqlExecute.rows===0) return {successed:false,errcode:-1,errmsg:'没有任何记录'}
|
|
59
|
+
return { successed: true, ...sqlExecute.rows[0] }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 单独获得一个连接串
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
getConnection(){
|
|
67
|
+
return new Promise((success, failed) => {
|
|
68
|
+
if (!this.pool) {
|
|
69
|
+
return success(null);
|
|
70
|
+
}
|
|
71
|
+
this.pool.getConnection(function (err, connection) {
|
|
72
|
+
if (err) return success(null);;
|
|
73
|
+
return success(connection)
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
39
77
|
/**
|
|
40
78
|
* 不事务执行SQL命令
|
|
41
79
|
* @param {*} sqlCommand
|
|
42
80
|
* @param {*} parameters 参数
|
|
43
81
|
*/
|
|
44
|
-
executeSql(sqlCommand, parameters) {
|
|
82
|
+
executeSql(sqlCommand, parameters,conn,autoclose=true) {
|
|
45
83
|
let logger = this.logger;
|
|
46
84
|
let that = this;
|
|
85
|
+
if (conn){
|
|
86
|
+
return this.executeSqlWithConnection(sqlCommand, parameters, conn, autoclose)
|
|
87
|
+
}
|
|
47
88
|
return new Promise((success, failed) => {
|
|
48
89
|
if (!this.pool) {
|
|
49
90
|
return success(apiResult.DB_CONNECTED_FAILED);
|
|
50
91
|
}
|
|
51
92
|
///创建出数据库连接,准备执行
|
|
52
|
-
this.pool.getConnection(
|
|
93
|
+
this.pool.getConnection((err, connection)=> {
|
|
53
94
|
if (err) {
|
|
54
95
|
logger.error("Database Connected Failed :"+ err);
|
|
55
96
|
return success(apiResult.DB_CONNECTED_FAILED);
|
|
56
97
|
}
|
|
57
|
-
|
|
58
|
-
logger.trace("SqlCommand:", sqlCommand, "Parameters:", parameters);
|
|
59
|
-
connection.query(sqlCommand, parameters, function (err, rows) {
|
|
60
|
-
connection.release();
|
|
61
|
-
if (err) {
|
|
62
|
-
logger.error("Database Query Error :" + err, sqlCommand);
|
|
63
|
-
that.logError(null, '数据库操作错误:' + err + sqlCommand)
|
|
64
|
-
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
65
|
-
}
|
|
66
|
-
return success({ successed: true, rows: rows });//,fields:fields});
|
|
67
|
-
});
|
|
68
|
-
}
|
|
98
|
+
return this.executeSqlWithConnection(sqlCommand, parameters, connection, autoclose)
|
|
69
99
|
});
|
|
70
100
|
});
|
|
71
101
|
}
|
|
72
|
-
|
|
73
102
|
/**
|
|
74
|
-
*
|
|
103
|
+
* 使用一个已存在的链接Query Sql
|
|
75
104
|
* @param {*} sqlCommand
|
|
76
|
-
* @param {*} parameters
|
|
105
|
+
* @param {*} parameters
|
|
106
|
+
* @param {*} conn
|
|
107
|
+
* @param {*} closeAfterQuery 执行完毕后是否关闭
|
|
77
108
|
*/
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return new Promise(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
109
|
+
executeSqlWithConnection(sqlCommand, parameters, conn, autoclose =true){
|
|
110
|
+
if (!conn) return Promise.resolve(apiResult.DB_CONNECTED_FAILED);
|
|
111
|
+
return new Promise(success=>{
|
|
112
|
+
this.logger.trace("SqlCommand:", sqlCommand, "Parameters:", parameters);
|
|
113
|
+
conn.query(sqlCommand, parameters, (err, rows) => {
|
|
114
|
+
if (autoclose) conn.release();
|
|
84
115
|
if (err) {
|
|
85
|
-
logger.error("Database
|
|
86
|
-
|
|
116
|
+
this.logger.error("Database Query Error :" + err, sqlCommand);
|
|
117
|
+
this.logError(null, '数据库操作错误:' + err + sqlCommand)
|
|
118
|
+
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
87
119
|
}
|
|
88
|
-
|
|
89
|
-
////开启事务保护
|
|
90
|
-
connection.beginTransaction(transerr=>{
|
|
91
|
-
if (transerr) return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: transerr.message }));
|
|
92
|
-
connection.query(sqlCommand, parameters, function (err, rows) {
|
|
93
|
-
////执行出错,事务回滚
|
|
94
|
-
if (err) {
|
|
95
|
-
logger.error("Database Query Error :"+ err);
|
|
96
|
-
return connection.rollback(() => {
|
|
97
|
-
connection.release();
|
|
98
|
-
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
///执行成功,事务提交
|
|
102
|
-
connection.commit(commiterror => {
|
|
103
|
-
if(commiterror) {
|
|
104
|
-
logger.error('事务提交失败 :',commiterror);
|
|
105
|
-
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
106
|
-
}
|
|
107
|
-
connection.release();
|
|
108
|
-
return success({ successed: true, rows: rows });
|
|
109
|
-
})
|
|
110
|
-
});
|
|
111
|
-
})
|
|
112
|
-
|
|
120
|
+
return success({ successed: true, rows: rows });//,fields:fields});
|
|
113
121
|
});
|
|
114
|
-
})
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* 启动事务进行保护性操作
|
|
126
|
+
* @param {*} conn
|
|
127
|
+
* @param {*} func
|
|
128
|
+
*/
|
|
129
|
+
async executeSqlWithTransaction(func,conn){
|
|
130
|
+
if (!conn) {
|
|
131
|
+
conn = await this.getConnection();
|
|
132
|
+
}
|
|
133
|
+
if (!conn) return Promise.resolve({successed:false});
|
|
134
|
+
return new Promise(success=>{
|
|
135
|
+
conn.beginTransaction(async err=>{
|
|
136
|
+
if (err) return success({successed:false,errcode:-1, errmessage: '【事务启动错误】'+err.message });
|
|
137
|
+
let result = {successed:true}
|
|
138
|
+
if (func && typeof (func)==='function') result = await func(params);
|
|
139
|
+
if (result.successed) {
|
|
140
|
+
conn.commit(commiterror => {
|
|
141
|
+
if (commiterror) {
|
|
142
|
+
console.log('事务提交失败 :', commiterror);
|
|
143
|
+
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
144
|
+
}
|
|
145
|
+
conn.release();
|
|
146
|
+
return success(result);
|
|
147
|
+
})
|
|
148
|
+
}else{
|
|
149
|
+
conn.rollback(() => {
|
|
150
|
+
conn.release();
|
|
151
|
+
return success(Object.assign(apiResult.DB_EXECUTE_FAILED, { errmessage: err.message }));
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
})
|
|
155
|
+
})
|
|
115
156
|
}
|
|
116
157
|
}
|
|
117
158
|
module.exports = Database;
|