mm_mysql 2.5.0 → 2.5.2

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.
Files changed (3) hide show
  1. package/index.js +14 -14
  2. package/package.json +4 -4
  3. package/sql.js +8 -0
package/index.js CHANGED
@@ -41,7 +41,7 @@ class Mysql extends Base {
41
41
  this._pool = null;
42
42
  // 移除单连接支持,统一使用连接池
43
43
 
44
- // 错误信息
44
+ // 错误信息(注意:同 scope 下为单例共享,并发时以最后一次写入为准)
45
45
  this.error = null;
46
46
  // 最近执行的SQL语句
47
47
  this.sql = '';
@@ -136,13 +136,12 @@ Mysql.prototype.open = async function () {
136
136
  * @returns {Promise<boolean>}
137
137
  */
138
138
  Mysql.prototype.close = async function () {
139
+ if (!this._pool) {
140
+ return true;
141
+ }
139
142
  try {
140
- // 先关闭连接池(避免连接状态问题)
141
- if (this._pool) {
142
- await this._pool.end();
143
- this._pool = null;
144
- }
145
-
143
+ await this._pool.end();
144
+ this._pool = null;
146
145
  this.log('info', '数据库连接池已关闭');
147
146
  return true;
148
147
  } catch (error) {
@@ -157,12 +156,15 @@ Mysql.prototype.close = async function () {
157
156
  * @throws {TypeError} 当连接池不存在时
158
157
  */
159
158
  Mysql.prototype.getConn = async function () {
160
- // 参数校验
159
+ // 连接池不存在则创建
161
160
  if (!this._pool) {
162
161
  await this.open();
163
162
  }
163
+ // 连接池可能已被外部关闭,检测并重建
164
+ if (this._pool._closed) {
165
+ await this.open();
166
+ }
164
167
 
165
- // 统一从连接池获取连接
166
168
  return await this._pool.getConnection();
167
169
  };
168
170
 
@@ -187,14 +189,13 @@ Mysql.prototype.run = async function (sql, params = []) {
187
189
  let conn = null;
188
190
  let ret = [];
189
191
  try {
190
- // 获取连接
191
192
  conn = await this.getConn();
192
193
  this.sql = sql;
193
194
  let [rows] = await conn.query(sql, params);
194
195
  ret = rows;
195
196
  } catch (err) {
196
197
  console.error('SQL执行失败', err);
197
- this.sql = err.sql;
198
+ this.sql = err.sql || sql;
198
199
  this.error = {
199
200
  code: err.errno,
200
201
  message: err.sqlMessage
@@ -226,14 +227,13 @@ Mysql.prototype.exec = async function (sql, params = []) {
226
227
  let conn = null;
227
228
  let ret = 0;
228
229
  try {
229
- // 获取连接
230
230
  conn = await this.getConn();
231
231
  this.sql = sql;
232
232
  let [result] = await conn.execute(sql, params);
233
233
  ret = result.insertId || result.affectedRows || result.changedRows || 1;
234
234
  } catch (error) {
235
235
  console.error('SQL执行失败', error);
236
- this.sql = error.sql;
236
+ this.sql = error.sql || sql;
237
237
  this.error = {
238
238
  code: error.errno,
239
239
  message: error.sqlMessage
@@ -394,7 +394,7 @@ Mysql.prototype.getPoolStats = function () {
394
394
  */
395
395
  Mysql.prototype.healthCheck = async function () {
396
396
  try {
397
- if (!this._pool && !this._conneced) {
397
+ if (!this._pool) {
398
398
  return {
399
399
  status: 'error',
400
400
  message: '数据库连接未初始化'
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "mm_mysql",
3
- "version": "2.5.0",
3
+ "version": "2.5.2",
4
4
  "description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
- "mm_expand": "^2.4.7",
7
+ "mm_expand": "^2.5.1",
8
8
  "mysql2": "^3.23.2",
9
9
  "sqlstring": "^2.3.3"
10
10
  },
@@ -36,8 +36,8 @@
36
36
  "node": ">=12.0.0"
37
37
  },
38
38
  "devDependencies": {
39
- "eslint": "^10.8.0",
40
- "eslint-plugin-jsdoc": "^63.3.3",
39
+ "eslint": "^10.8.1",
40
+ "eslint-plugin-jsdoc": "^64.0.1",
41
41
  "mm_eslint": "^1.7.5"
42
42
  }
43
43
  }
package/sql.js CHANGED
@@ -441,6 +441,10 @@ Sql.prototype._buildLikeWhere = function (obj) {
441
441
  var where = '';
442
442
  for (var k in obj) {
443
443
  var val = obj[k];
444
+ // 防御:如果 String.prototype 被污染导致 .slice 丢失,强制重建原始字符串
445
+ if (typeof val === 'string') {
446
+ val = String(val);
447
+ }
444
448
  // 先转义再处理字符串,避免转义字符被错误处理
445
449
  val = escape(val);
446
450
  if (k.endsWith('_min')) {
@@ -469,6 +473,10 @@ Sql.prototype._buildExactWhere = function (obj) {
469
473
  var where = '';
470
474
  for (var k in obj) {
471
475
  var val = obj[k];
476
+ // 防御:如果 String.prototype 被污染导致 .slice 丢失,强制重建原始字符串
477
+ if (typeof val === 'string') {
478
+ val = String(val);
479
+ }
472
480
  // 先转义再处理字符串,避免转义字符被错误处理
473
481
  val = escape(val);
474
482
  if (k.endsWith('_min')) {