mm_mysql 2.3.0 → 2.3.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.
- package/final_test.js +69 -0
- package/package.json +8 -7
- package/sql.js +34 -32
package/final_test.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const { Mysql } = require('./index.js');
|
|
2
|
+
|
|
3
|
+
async function test() {
|
|
4
|
+
const mysql = new Mysql({
|
|
5
|
+
host: '127.0.0.1',
|
|
6
|
+
port: 3306,
|
|
7
|
+
user: 'root',
|
|
8
|
+
password: 'Asd159357',
|
|
9
|
+
database: 'test',
|
|
10
|
+
debug: false
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
await mysql.open();
|
|
14
|
+
console.log('数据库连接成功');
|
|
15
|
+
|
|
16
|
+
const db = mysql.db();
|
|
17
|
+
db.table = 'cms_article';
|
|
18
|
+
|
|
19
|
+
console.log('=== 全面测试所有修复的函数 ===\n');
|
|
20
|
+
|
|
21
|
+
// 测试1: toAddSql函数
|
|
22
|
+
console.log('1. 测试toAddSql函数:');
|
|
23
|
+
const add_sql = db.toAddSql({
|
|
24
|
+
title: '测试标题',
|
|
25
|
+
content: '测试内容',
|
|
26
|
+
regex_field: '~\\d+$',
|
|
27
|
+
special_chars: "'test' AND 1=1"
|
|
28
|
+
});
|
|
29
|
+
console.log(' SQL:', add_sql);
|
|
30
|
+
|
|
31
|
+
// 测试2: toSetSql函数
|
|
32
|
+
console.log('\n2. 测试toSetSql函数:');
|
|
33
|
+
const set_sql = db.toSetSql(
|
|
34
|
+
{ article_id: 1 },
|
|
35
|
+
{ title: '更新标题', pattern: '~\\d+$' }
|
|
36
|
+
);
|
|
37
|
+
console.log(' SQL:', set_sql);
|
|
38
|
+
|
|
39
|
+
// 测试3: toDelSql函数
|
|
40
|
+
console.log('\n3. 测试toDelSql函数:');
|
|
41
|
+
const del_sql = db.toDelSql({ article_id: 999 });
|
|
42
|
+
console.log(' SQL:', del_sql);
|
|
43
|
+
|
|
44
|
+
// 测试4: toQuery函数
|
|
45
|
+
console.log('\n4. 测试toQuery函数:');
|
|
46
|
+
const query_sql = db.toQuery(
|
|
47
|
+
'`title` LIKE \'%test%\'',
|
|
48
|
+
'`article_id` DESC',
|
|
49
|
+
'title, article_id'
|
|
50
|
+
);
|
|
51
|
+
console.log(' SQL:', query_sql);
|
|
52
|
+
|
|
53
|
+
// 测试5: 测试模板查询函数
|
|
54
|
+
console.log('\n5. 测试模板查询函数:');
|
|
55
|
+
try {
|
|
56
|
+
const tpl_result = db.tplQuery(
|
|
57
|
+
{ title: 'test' },
|
|
58
|
+
{ title: '`title` LIKE \'%{0}%\'' }
|
|
59
|
+
);
|
|
60
|
+
console.log(' 模板查询结果:', tpl_result);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.log(' 模板查询错误:', error.message);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log('\n=== 所有函数修复验证完成 ===');
|
|
66
|
+
process.exit(0);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
test().catch(console.error);
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_mysql",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.2",
|
|
4
4
|
"description": "这是超级美眉mysql帮助函数模块,用于便捷操作mysql,使用await方式,可以避免嵌套函数",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"mm_expand": "^2.0.
|
|
8
|
-
"mysql2": "^3.
|
|
7
|
+
"mm_expand": "^2.0.2",
|
|
8
|
+
"mysql2": "^3.20.0",
|
|
9
|
+
"sqlstring": "^2.3.3"
|
|
9
10
|
},
|
|
10
11
|
"scripts": {
|
|
11
12
|
"start": "node index.js",
|
|
@@ -35,8 +36,8 @@
|
|
|
35
36
|
"node": ">=12.0.0"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
|
-
"eslint": "^
|
|
39
|
-
"eslint-plugin-jsdoc": "^
|
|
40
|
-
"mm_eslint": "^1.
|
|
39
|
+
"eslint": "^10.1.0",
|
|
40
|
+
"eslint-plugin-jsdoc": "^62.9.0",
|
|
41
|
+
"mm_eslint": "^1.7.1"
|
|
41
42
|
}
|
|
42
|
-
}
|
|
43
|
+
}
|
package/sql.js
CHANGED
|
@@ -189,17 +189,19 @@ Sql.prototype.filter = function (query) {
|
|
|
189
189
|
* @returns {string} 返回查询条件语句
|
|
190
190
|
*/
|
|
191
191
|
Sql.prototype.toQuery = function (where, sort, view) {
|
|
192
|
-
var sql = 'SELECT {1} FROM `{0}`';
|
|
193
192
|
if (!view) {
|
|
194
193
|
view = '*';
|
|
195
194
|
}
|
|
195
|
+
|
|
196
|
+
// 使用直接拼接的方式,避免模板替换错误
|
|
197
|
+
var sql = `SELECT ${view} FROM \`${this.table}\``;
|
|
198
|
+
|
|
196
199
|
if (where) {
|
|
197
200
|
sql += ' WHERE ' + where;
|
|
198
201
|
}
|
|
199
202
|
if (sort) {
|
|
200
203
|
sql += ' ORDER BY ' + sort.replace(/;/, '');
|
|
201
204
|
}
|
|
202
|
-
sql = sql.replace('{0}', this.table).replace('{1}', view);
|
|
203
205
|
if (this.size && this.page) {
|
|
204
206
|
var start = this.size * (this.page - 1);
|
|
205
207
|
sql += ' limit ' + start + ',' + this.size;
|
|
@@ -214,8 +216,8 @@ Sql.prototype.toQuery = function (where, sort, view) {
|
|
|
214
216
|
* @returns {Promise | object} 执行结果
|
|
215
217
|
*/
|
|
216
218
|
Sql.prototype.addSql = function (key, val) {
|
|
217
|
-
|
|
218
|
-
sql =
|
|
219
|
+
// 使用直接拼接的方式,避免模板替换错误
|
|
220
|
+
var sql = `INSERT INTO \`${this.table}\` (${key}) VALUES (${val});`;
|
|
219
221
|
return this.exec(sql);
|
|
220
222
|
};
|
|
221
223
|
/**
|
|
@@ -224,8 +226,8 @@ Sql.prototype.addSql = function (key, val) {
|
|
|
224
226
|
* @returns {Promise | object} 执行结果
|
|
225
227
|
*/
|
|
226
228
|
Sql.prototype.delSql = function (where) {
|
|
227
|
-
|
|
228
|
-
sql =
|
|
229
|
+
// 使用直接拼接的方式,避免模板替换错误
|
|
230
|
+
var sql = `DELETE FROM \`${this.table}\` WHERE ${where};`;
|
|
229
231
|
return this.exec(sql);
|
|
230
232
|
};
|
|
231
233
|
/**
|
|
@@ -235,8 +237,8 @@ Sql.prototype.delSql = function (where) {
|
|
|
235
237
|
* @returns {Promise | object} 执行结果
|
|
236
238
|
*/
|
|
237
239
|
Sql.prototype.setSql = function (where, set) {
|
|
238
|
-
|
|
239
|
-
sql =
|
|
240
|
+
// 使用直接拼接的方式,避免模板替换错误
|
|
241
|
+
var sql = `UPDATE \`${this.table}\` SET ${set} WHERE ${where};`;
|
|
240
242
|
return this.exec(sql);
|
|
241
243
|
};
|
|
242
244
|
/**
|
|
@@ -439,9 +441,7 @@ Sql.prototype._buildLikeWhere = function (obj) {
|
|
|
439
441
|
var where = '';
|
|
440
442
|
for (var k in obj) {
|
|
441
443
|
var val = obj[k];
|
|
442
|
-
|
|
443
|
-
val = val.trim("'");
|
|
444
|
-
}
|
|
444
|
+
// 先转义再处理字符串,避免转义字符被错误处理
|
|
445
445
|
val = escape(val);
|
|
446
446
|
if (k.endsWith('_min')) {
|
|
447
447
|
where += ' and ' + escapeId(k.replace('_min', '')) + ' >= ' + val;
|
|
@@ -469,9 +469,7 @@ Sql.prototype._buildExactWhere = function (obj) {
|
|
|
469
469
|
var where = '';
|
|
470
470
|
for (var k in obj) {
|
|
471
471
|
var val = obj[k];
|
|
472
|
-
|
|
473
|
-
val = val.trim("'");
|
|
474
|
-
}
|
|
472
|
+
// 先转义再处理字符串,避免转义字符被错误处理
|
|
475
473
|
val = escape(val);
|
|
476
474
|
if (k.endsWith('_min')) {
|
|
477
475
|
where += ' and ' + escapeId(k.replace('_min', '')) + ' >= ' + val;
|
|
@@ -506,9 +504,7 @@ Sql.prototype.toSet = function (obj) {
|
|
|
506
504
|
let val = obj[k];
|
|
507
505
|
if (val === undefined || val === null) continue;
|
|
508
506
|
|
|
509
|
-
|
|
510
|
-
val = val.trim("'");
|
|
511
|
-
}
|
|
507
|
+
// 先转义再处理字符串,避免转义字符被错误处理
|
|
512
508
|
val = escape(val);
|
|
513
509
|
|
|
514
510
|
if (k.endsWith('_add')) {
|
|
@@ -549,10 +545,8 @@ Sql.prototype.toAddSql = function (item) {
|
|
|
549
545
|
value += ',' + escape(val);
|
|
550
546
|
}
|
|
551
547
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
.replace('{1}', key.replace(',', ''))
|
|
555
|
-
.replace('{2}', value.replace(',', ''));
|
|
548
|
+
// 使用直接拼接的方式,避免模板替换错误
|
|
549
|
+
const sql = `INSERT INTO \`${this.table}\` (${key.replace(',', '')}) VALUES (${value.replace(',', '')});`;
|
|
556
550
|
|
|
557
551
|
return sql;
|
|
558
552
|
};
|
|
@@ -568,9 +562,10 @@ Sql.prototype.toDelSql = function (query, like) {
|
|
|
568
562
|
throw new Error('表名未设置');
|
|
569
563
|
}
|
|
570
564
|
const where = this.toWhere(query, like);
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
565
|
+
|
|
566
|
+
// 使用直接拼接的方式,避免模板替换错误
|
|
567
|
+
const sql = `DELETE FROM \`${this.table}\` WHERE ${where};`;
|
|
568
|
+
|
|
574
569
|
return sql;
|
|
575
570
|
};
|
|
576
571
|
|
|
@@ -587,10 +582,10 @@ Sql.prototype.toSetSql = function (query, item, like) {
|
|
|
587
582
|
}
|
|
588
583
|
const where = this.toWhere(query, like);
|
|
589
584
|
const set = this.toSet(item);
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
585
|
+
|
|
586
|
+
// 使用直接拼接的方式,避免模板替换错误
|
|
587
|
+
const sql = `UPDATE \`${this.table}\` SET ${set} WHERE ${where};`;
|
|
588
|
+
|
|
594
589
|
return sql;
|
|
595
590
|
};
|
|
596
591
|
|
|
@@ -1207,12 +1202,16 @@ Sql.prototype._tplQueryWithSep = function (param_dt, sql_dt, l) {
|
|
|
1207
1202
|
const sub_conds = [];
|
|
1208
1203
|
for (const val of arr) {
|
|
1209
1204
|
const clean_val = typeof val === 'string' ? val.trim("'") : val;
|
|
1210
|
-
|
|
1205
|
+
// 使用直接替换方式,避免模板替换错误
|
|
1206
|
+
const escaped_val = escape(clean_val);
|
|
1207
|
+
sub_conds.push(tpl.replaceAll('{0}', escaped_val));
|
|
1211
1208
|
}
|
|
1212
1209
|
conds.push('(' + sub_conds.join(' || ') + ')');
|
|
1213
1210
|
} else {
|
|
1214
1211
|
const clean_val = typeof value === 'string' ? value.trim("'") : value;
|
|
1215
|
-
|
|
1212
|
+
// 使用直接替换方式,避免模板替换错误
|
|
1213
|
+
const escaped_val = escape(clean_val);
|
|
1214
|
+
conds.push(tpl.replaceAll('{0}', escaped_val));
|
|
1216
1215
|
}
|
|
1217
1216
|
} else {
|
|
1218
1217
|
if (arr.length > 1) {
|
|
@@ -1246,7 +1245,8 @@ Sql.prototype._tplQueryNoSep = function (param_dt, sql_dt) {
|
|
|
1246
1245
|
}
|
|
1247
1246
|
value = escape(value);
|
|
1248
1247
|
if (sql_dt[key]) {
|
|
1249
|
-
|
|
1248
|
+
// 使用直接替换方式,避免模板替换错误
|
|
1249
|
+
conds.push(sql_dt[key].replaceAll('{0}', value));
|
|
1250
1250
|
} else {
|
|
1251
1251
|
conds.push(escapeId(key) + ' = ' + value);
|
|
1252
1252
|
}
|
|
@@ -1342,7 +1342,9 @@ Sql.prototype.tplBody = function (param_dt, sql_dt) {
|
|
|
1342
1342
|
if (!Object.prototype.hasOwnProperty.call(param_dt, key)) continue;
|
|
1343
1343
|
const value = escape(param_dt[key]);
|
|
1344
1344
|
if (sql_dt[key]) {
|
|
1345
|
-
|
|
1345
|
+
// 使用直接替换方式,避免模板替换错误
|
|
1346
|
+
const replaced = sql_dt[key].replace('{0}', value);
|
|
1347
|
+
parts.push(' ' + replaced.replace(/\+ -/g, '- ').replace(/- -/g, '+ '));
|
|
1346
1348
|
} else {
|
|
1347
1349
|
parts.push(' ' + escapeId(key) + ' = ' + value);
|
|
1348
1350
|
}
|