mm_sql 1.4.2 → 1.4.4
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/admin_vs_direct_test.js +14 -14
- package/debug_error_test.js +98 -60
- package/detailed_error_test.js +105 -67
- package/index.js +41 -1
- package/mysql_error_test.js +72 -46
- package/package.json +3 -3
package/admin_vs_direct_test.js
CHANGED
|
@@ -2,10 +2,10 @@ const { mysqlAdmin } = require('../mm_mysql');
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* 测试run方法对不存在的表的处理
|
|
5
|
-
* @param {
|
|
6
|
-
* @param {
|
|
5
|
+
* @param {object} admin_mysql - mysqlAdmin实例
|
|
6
|
+
* @param {object} direct_mysql - 直接Mysql实例
|
|
7
7
|
*/
|
|
8
|
-
async function
|
|
8
|
+
async function runMethod(admin_mysql, direct_mysql) {
|
|
9
9
|
console.log('\n测试1: 比较对不存在的表的处理...');
|
|
10
10
|
|
|
11
11
|
// mysqlAdmin实例测试
|
|
@@ -29,10 +29,10 @@ async function testRunMethod(admin_mysql, direct_mysql) {
|
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
31
|
* 比较实例类型和属性
|
|
32
|
-
* @param {
|
|
33
|
-
* @param {
|
|
32
|
+
* @param {object} admin_mysql - mysqlAdmin实例
|
|
33
|
+
* @param {object} direct_mysql - 直接Mysql实例
|
|
34
34
|
*/
|
|
35
|
-
function
|
|
35
|
+
function instanceType(admin_mysql, direct_mysql) {
|
|
36
36
|
console.log('\n测试2: 比较实例类型和属性...');
|
|
37
37
|
console.log('mysqlAdmin实例类型:', admin_mysql.constructor.name);
|
|
38
38
|
console.log('直接Mysql实例类型:', direct_mysql.constructor.name);
|
|
@@ -42,10 +42,10 @@ function compareInstanceType(admin_mysql, direct_mysql) {
|
|
|
42
42
|
|
|
43
43
|
/**
|
|
44
44
|
* 检查缓存问题
|
|
45
|
-
* @param {
|
|
46
|
-
* @param {
|
|
45
|
+
* @param {object} config - 数据库配置
|
|
46
|
+
* @param {object} admin_mysql - mysqlAdmin实例
|
|
47
47
|
*/
|
|
48
|
-
async function
|
|
48
|
+
async function cache(config, admin_mysql) {
|
|
49
49
|
console.log('\n测试3: 检查缓存问题...');
|
|
50
50
|
const admin_mysql2 = mysqlAdmin('compare_test', config); // 相同scope
|
|
51
51
|
const admin_mysql3 = mysqlAdmin('compare_test2', config); // 不同scope
|
|
@@ -87,9 +87,9 @@ async function compareAdminDirect() {
|
|
|
87
87
|
const direct_mysql = new Mysql(config);
|
|
88
88
|
console.log('✅ 直接Mysql实例创建成功');
|
|
89
89
|
|
|
90
|
-
await
|
|
91
|
-
|
|
92
|
-
await
|
|
90
|
+
await runMethod(admin_mysql, direct_mysql);
|
|
91
|
+
instanceType(admin_mysql, direct_mysql);
|
|
92
|
+
await cache(config, admin_mysql);
|
|
93
93
|
|
|
94
94
|
} catch (error) {
|
|
95
95
|
console.log('❌ 实例创建失败:', error.message);
|
|
@@ -99,7 +99,7 @@ async function compareAdminDirect() {
|
|
|
99
99
|
/**
|
|
100
100
|
* 测试mm_sql模块中的适配器行为
|
|
101
101
|
*/
|
|
102
|
-
async function
|
|
102
|
+
async function sqlAdapter() {
|
|
103
103
|
console.log('\n=== 测试mm_sql模块中的适配器行为 ===');
|
|
104
104
|
|
|
105
105
|
try {
|
|
@@ -151,7 +151,7 @@ async function main() {
|
|
|
151
151
|
console.log('开始比较mysqlAdmin和直接Mysql实例的行为...\n');
|
|
152
152
|
|
|
153
153
|
await compareAdminDirect();
|
|
154
|
-
await
|
|
154
|
+
await sqlAdapter();
|
|
155
155
|
|
|
156
156
|
console.log('\n比较测试完成');
|
|
157
157
|
}
|
package/debug_error_test.js
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
const { Sql } = require('./index.js');
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* 测试适配器直接调用
|
|
5
|
+
* @param {object} mysql_sql - MySQL实例
|
|
6
|
+
*/
|
|
7
|
+
async function adapterDirect(mysql_sql) {
|
|
8
|
+
console.log('\n测试1: 直接调用适配器run方法...');
|
|
9
|
+
try {
|
|
10
|
+
await mysql_sql._adapter.run('SELECT * FROM non_existent_table_xyz');
|
|
11
|
+
console.log('❌ 适配器应该对无效SQL抛出错误,但没有抛出');
|
|
12
|
+
} catch (error) {
|
|
13
|
+
console.log('✅ 适配器正确抛出错误:', error.message);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 测试mm_sql的run方法调用
|
|
19
|
+
* @param {object} mysql_sql - MySQL实例
|
|
20
|
+
*/
|
|
21
|
+
async function sqlRun(mysql_sql) {
|
|
22
|
+
console.log('\n测试2: 通过mm_sql的run方法调用...');
|
|
23
|
+
try {
|
|
24
|
+
await mysql_sql.run('SELECT * FROM non_existent_table_xyz');
|
|
25
|
+
console.log('❌ mm_sql应该对无效SQL抛出错误,但没有抛出');
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.log('✅ mm_sql正确抛出错误:', error.message);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 测试参数验证
|
|
33
|
+
* @param {object} mysql_sql - MySQL实例
|
|
34
|
+
*/
|
|
35
|
+
async function paramValid(mysql_sql) {
|
|
36
|
+
console.log('\n测试3: 测试参数验证...');
|
|
37
|
+
try {
|
|
38
|
+
await mysql_sql.run(123); // 无效参数类型
|
|
39
|
+
console.log('❌ mm_sql应该对无效参数抛出TypeError,但没有抛出');
|
|
40
|
+
} catch (error) {
|
|
41
|
+
if (error instanceof TypeError) {
|
|
42
|
+
console.log('✅ mm_sql正确抛出TypeError:', error.message);
|
|
43
|
+
} else {
|
|
44
|
+
console.log('❌ mm_sql抛出错误但不是TypeError:', error.message);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
3
49
|
/**
|
|
4
50
|
* 调试MySQL错误传播
|
|
5
51
|
*/
|
|
@@ -20,42 +66,61 @@ async function debugMysqlError() {
|
|
|
20
66
|
console.log('✅ mm_sql MySQL实例创建成功');
|
|
21
67
|
console.log('适配器类型:', mysql_sql._adapter?.constructor?.name);
|
|
22
68
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
await mysql_sql._adapter.run('SELECT * FROM non_existent_table_xyz');
|
|
27
|
-
console.log('❌ 适配器应该对无效SQL抛出错误,但没有抛出');
|
|
28
|
-
} catch (error) {
|
|
29
|
-
console.log('✅ 适配器正确抛出错误:', error.message);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// 测试2: 通过mm_sql的run方法调用
|
|
33
|
-
console.log('\n测试2: 通过mm_sql的run方法调用...');
|
|
34
|
-
try {
|
|
35
|
-
await mysql_sql.run('SELECT * FROM non_existent_table_xyz');
|
|
36
|
-
console.log('❌ mm_sql应该对无效SQL抛出错误,但没有抛出');
|
|
37
|
-
} catch (error) {
|
|
38
|
-
console.log('✅ mm_sql正确抛出错误:', error.message);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// 测试3: 测试参数验证
|
|
42
|
-
console.log('\n测试3: 测试参数验证...');
|
|
43
|
-
try {
|
|
44
|
-
await mysqlSql.run(123); // 无效参数类型
|
|
45
|
-
console.log('❌ mm_sql应该对无效参数抛出TypeError,但没有抛出');
|
|
46
|
-
} catch (error) {
|
|
47
|
-
if (error instanceof TypeError) {
|
|
48
|
-
console.log('✅ mm_sql正确抛出TypeError:', error.message);
|
|
49
|
-
} else {
|
|
50
|
-
console.log('❌ mm_sql抛出错误但不是TypeError:', error.message);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
69
|
+
await adapterDirect(mysql_sql);
|
|
70
|
+
await sqlRun(mysql_sql);
|
|
71
|
+
await paramValid(mysql_sql);
|
|
53
72
|
|
|
54
73
|
} catch (error) {
|
|
55
74
|
console.log('❌ mm_sql实例创建失败:', error.message);
|
|
56
75
|
}
|
|
57
76
|
}
|
|
58
77
|
|
|
78
|
+
/**
|
|
79
|
+
* 测试SQLite适配器直接调用
|
|
80
|
+
* @param {object} sqlite_sql - SQLite实例
|
|
81
|
+
*/
|
|
82
|
+
async function sqliteAdapter(sqlite_sql) {
|
|
83
|
+
console.log('\n测试1: 直接调用适配器run方法...');
|
|
84
|
+
try {
|
|
85
|
+
await sqlite_sql._adapter.run('SELECT * FROM non_existent_table_xyz');
|
|
86
|
+
console.log('❌ 适配器应该对无效SQL抛出错误,但没有抛出');
|
|
87
|
+
} catch (error) {
|
|
88
|
+
console.log('✅ 适配器正确抛出错误:', error.message);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* 测试SQLite的run方法调用
|
|
94
|
+
* @param {object} sqlite_sql - SQLite实例
|
|
95
|
+
*/
|
|
96
|
+
async function sqliteRun(sqlite_sql) {
|
|
97
|
+
console.log('\n测试2: 通过mm_sql的run方法调用...');
|
|
98
|
+
try {
|
|
99
|
+
await sqlite_sql.run('SELECT * FROM non_existent_table_xyz');
|
|
100
|
+
console.log('❌ mm_sql应该对无效SQL抛出错误,但没有抛出');
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.log('✅ mm_sql正确抛出错误:', error.message);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 测试SQLite参数验证
|
|
108
|
+
* @param {object} sqlite_sql - SQLite实例
|
|
109
|
+
*/
|
|
110
|
+
async function sqliteParam(sqlite_sql) {
|
|
111
|
+
console.log('\n测试3: 测试参数验证...');
|
|
112
|
+
try {
|
|
113
|
+
await sqlite_sql.run(123); // 无效参数类型
|
|
114
|
+
console.log('❌ mm_sql应该对无效参数抛出TypeError,但没有抛出');
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (error instanceof TypeError) {
|
|
117
|
+
console.log('✅ mm_sql正确抛出TypeError:', error.message);
|
|
118
|
+
} else {
|
|
119
|
+
console.log('❌ mm_sql抛出错误但不是TypeError:', error.message);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
59
124
|
/**
|
|
60
125
|
* 调试SQLite错误传播
|
|
61
126
|
*/
|
|
@@ -73,36 +138,9 @@ async function debugSqliteError() {
|
|
|
73
138
|
console.log('✅ mm_sql SQLite实例创建成功');
|
|
74
139
|
console.log('适配器类型:', sqlite_sql._adapter?.constructor?.name);
|
|
75
140
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
await sqlite_sql._adapter.run('SELECT * FROM non_existent_table_xyz');
|
|
80
|
-
console.log('❌ 适配器应该对无效SQL抛出错误,但没有抛出');
|
|
81
|
-
} catch (error) {
|
|
82
|
-
console.log('✅ 适配器正确抛出错误:', error.message);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// 测试2: 通过mm_sql的run方法调用
|
|
86
|
-
console.log('\n测试2: 通过mm_sql的run方法调用...');
|
|
87
|
-
try {
|
|
88
|
-
await sqlite_sql.run('SELECT * FROM non_existent_table_xyz');
|
|
89
|
-
console.log('❌ mm_sql应该对无效SQL抛出错误,但没有抛出');
|
|
90
|
-
} catch (error) {
|
|
91
|
-
console.log('✅ mm_sql正确抛出错误:', error.message);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// 测试3: 测试参数验证
|
|
95
|
-
console.log('\n测试3: 测试参数验证...');
|
|
96
|
-
try {
|
|
97
|
-
await sqlite_sql.run(123); // 无效参数类型
|
|
98
|
-
console.log('❌ mm_sql应该对无效参数抛出TypeError,但没有抛出');
|
|
99
|
-
} catch (error) {
|
|
100
|
-
if (error instanceof TypeError) {
|
|
101
|
-
console.log('✅ mm_sql正确抛出TypeError:', error.message);
|
|
102
|
-
} else {
|
|
103
|
-
console.log('❌ mm_sql抛出错误但不是TypeError:', error.message);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
141
|
+
await testSqliteAdapter(sqlite_sql);
|
|
142
|
+
await testSqliteRun(sqlite_sql);
|
|
143
|
+
await testSqliteParam(sqlite_sql);
|
|
106
144
|
|
|
107
145
|
} catch (error) {
|
|
108
146
|
console.log('❌ mm_sql实例创建失败:', error.message);
|
package/detailed_error_test.js
CHANGED
|
@@ -1,5 +1,69 @@
|
|
|
1
1
|
const { mysqlAdmin } = require('../mm_mysql');
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* 测试MySQL语法错误
|
|
5
|
+
* @param {Object} mysql - MySQL实例
|
|
6
|
+
*/
|
|
7
|
+
async function testMysqlSyntax(mysql) {
|
|
8
|
+
console.log('\n测试1: 语法错误的SQL...');
|
|
9
|
+
try {
|
|
10
|
+
await mysql.run('SELECT * FROM'); // 语法错误
|
|
11
|
+
console.log('❌ MySQL应该对语法错误SQL抛出错误,但没有抛出');
|
|
12
|
+
} catch (error) {
|
|
13
|
+
console.log('✅ MySQL正确抛出语法错误:', error.message);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 测试MySQL不存在的表
|
|
19
|
+
* @param {Object} mysql - MySQL实例
|
|
20
|
+
*/
|
|
21
|
+
async function testMysqlTable(mysql) {
|
|
22
|
+
console.log('\n测试2: 不存在的表...');
|
|
23
|
+
try {
|
|
24
|
+
const result = await mysql.run('SELECT * FROM non_existent_table_xyz');
|
|
25
|
+
console.log('MySQL返回结果:', result);
|
|
26
|
+
console.log('结果类型:', typeof result);
|
|
27
|
+
console.log('结果长度:', Array.isArray(result) ? result.length : 'N/A');
|
|
28
|
+
|
|
29
|
+
if (Array.isArray(result) && result.length === 0) {
|
|
30
|
+
console.log('⚠️ MySQL对不存在的表返回空数组而不是抛出错误');
|
|
31
|
+
} else {
|
|
32
|
+
console.log('❌ MySQL应该对不存在的表抛出错误,但没有抛出');
|
|
33
|
+
}
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.log('✅ MySQL正确抛出表不存在错误:', error.message);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 测试MySQL不存在的数据库
|
|
41
|
+
* @param {Object} mysql - MySQL实例
|
|
42
|
+
*/
|
|
43
|
+
async function testMysqlDb(mysql) {
|
|
44
|
+
console.log('\n测试3: 不存在的数据库...');
|
|
45
|
+
try {
|
|
46
|
+
await mysql.run('SELECT * FROM non_existent_db.non_existent_table');
|
|
47
|
+
console.log('❌ MySQL应该对不存在的数据库抛出错误,但没有抛出');
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.log('✅ MySQL正确抛出数据库不存在错误:', error.message);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 测试MySQL权限错误
|
|
55
|
+
* @param {Object} mysql - MySQL实例
|
|
56
|
+
*/
|
|
57
|
+
async function testMysqlPerm(mysql) {
|
|
58
|
+
console.log('\n测试4: 权限错误...');
|
|
59
|
+
try {
|
|
60
|
+
await mysql.run('DROP TABLE IF EXISTS important_system_table');
|
|
61
|
+
console.log('❌ MySQL应该对权限错误抛出错误,但没有抛出');
|
|
62
|
+
} catch (error) {
|
|
63
|
+
console.log('✅ MySQL正确抛出权限错误:', error.message);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
3
67
|
/**
|
|
4
68
|
* 详细测试MySQL错误处理
|
|
5
69
|
*/
|
|
@@ -18,52 +82,49 @@ async function mysqlErrorTest() {
|
|
|
18
82
|
|
|
19
83
|
console.log('✅ MySQL实例创建成功');
|
|
20
84
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
console.log('❌ MySQL应该对语法错误SQL抛出错误,但没有抛出');
|
|
26
|
-
} catch (error) {
|
|
27
|
-
console.log('✅ MySQL正确抛出语法错误:', error.message);
|
|
28
|
-
}
|
|
85
|
+
await testMysqlSyntax(mysql);
|
|
86
|
+
await testMysqlTable(mysql);
|
|
87
|
+
await testMysqlDb(mysql);
|
|
88
|
+
await testMysqlPerm(mysql);
|
|
29
89
|
|
|
30
|
-
|
|
31
|
-
console.log('
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
90
|
+
} catch (error) {
|
|
91
|
+
console.log('❌ MySQL实例创建失败:', error.message);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 测试SQLite语法错误
|
|
97
|
+
* @param {Object} sqlite - SQLite实例
|
|
98
|
+
*/
|
|
99
|
+
async function testSqliteSyntax(sqlite) {
|
|
100
|
+
console.log('\n测试1: 语法错误的SQL...');
|
|
101
|
+
try {
|
|
102
|
+
await sqlite.run('SELECT * FROM'); // 语法错误
|
|
103
|
+
console.log('❌ SQLite应该对语法错误SQL抛出错误,但没有抛出');
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.log('✅ SQLite正确抛出语法错误:', error.message);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 测试SQLite不存在的表
|
|
111
|
+
* @param {Object} sqlite - SQLite实例
|
|
112
|
+
*/
|
|
113
|
+
async function testSqliteTable(sqlite) {
|
|
114
|
+
console.log('\n测试2: 不存在的表...');
|
|
115
|
+
try {
|
|
116
|
+
const result = await sqlite.run('SELECT * FROM non_existent_table_xyz');
|
|
117
|
+
console.log('SQLite返回结果:', result);
|
|
118
|
+
console.log('结果类型:', typeof result);
|
|
119
|
+
console.log('结果长度:', Array.isArray(result) ? result.length : 'N/A');
|
|
55
120
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
console.log('❌ MySQL应该对权限错误抛出错误,但没有抛出');
|
|
61
|
-
} catch (error) {
|
|
62
|
-
console.log('✅ MySQL正确抛出权限错误:', error.message);
|
|
121
|
+
if (Array.isArray(result) && result.length === 0) {
|
|
122
|
+
console.log('⚠️ SQLite对不存在的表返回空数组而不是抛出错误');
|
|
123
|
+
} else {
|
|
124
|
+
console.log('❌ SQLite应该对不存在的表抛出错误,但没有抛出');
|
|
63
125
|
}
|
|
64
|
-
|
|
65
126
|
} catch (error) {
|
|
66
|
-
console.log('
|
|
127
|
+
console.log('✅ SQLite正确抛出表不存在错误:', error.message);
|
|
67
128
|
}
|
|
68
129
|
}
|
|
69
130
|
|
|
@@ -84,31 +145,8 @@ async function sqliteErrorTest() {
|
|
|
84
145
|
|
|
85
146
|
console.log('✅ SQLite实例创建成功');
|
|
86
147
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
await sqlite.run('SELECT * FROM'); // 语法错误
|
|
91
|
-
console.log('❌ SQLite应该对语法错误SQL抛出错误,但没有抛出');
|
|
92
|
-
} catch (error) {
|
|
93
|
-
console.log('✅ SQLite正确抛出语法错误:', error.message);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// 测试2: 不存在的表(语法正确但表不存在)
|
|
97
|
-
console.log('\n测试2: 不存在的表...');
|
|
98
|
-
try {
|
|
99
|
-
const result = await sqlite.run('SELECT * FROM non_existent_table_xyz');
|
|
100
|
-
console.log('SQLite返回结果:', result);
|
|
101
|
-
console.log('结果类型:', typeof result);
|
|
102
|
-
console.log('结果长度:', Array.isArray(result) ? result.length : 'N/A');
|
|
103
|
-
|
|
104
|
-
if (Array.isArray(result) && result.length === 0) {
|
|
105
|
-
console.log('⚠️ SQLite对不存在的表返回空数组而不是抛出错误');
|
|
106
|
-
} else {
|
|
107
|
-
console.log('❌ SQLite应该对不存在的表抛出错误,但没有抛出');
|
|
108
|
-
}
|
|
109
|
-
} catch (error) {
|
|
110
|
-
console.log('✅ SQLite正确抛出表不存在错误:', error.message);
|
|
111
|
-
}
|
|
148
|
+
await testSqliteSyntax(sqlite);
|
|
149
|
+
await testSqliteTable(sqlite);
|
|
112
150
|
|
|
113
151
|
} catch (error) {
|
|
114
152
|
console.log('❌ SQLite实例创建失败:', error.message);
|
package/index.js
CHANGED
|
@@ -304,4 +304,44 @@ Sql.prototype.db = function () {
|
|
|
304
304
|
};
|
|
305
305
|
|
|
306
306
|
// 导出模块
|
|
307
|
-
exports.Sql = Sql;
|
|
307
|
+
exports.Sql = Sql;
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 确保连接池对象存在
|
|
312
|
+
*/
|
|
313
|
+
if (!$.pool) {
|
|
314
|
+
$.pool = {};
|
|
315
|
+
}
|
|
316
|
+
if (!$.pool.sql) {
|
|
317
|
+
$.pool.sql = {};
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* @description Sql管理器,用于创建缓存(线程安全版)
|
|
322
|
+
* @param {string} scope 作用域
|
|
323
|
+
* @param {object} config 配置参数
|
|
324
|
+
* @returns {object} 返回一个Sql类实例
|
|
325
|
+
*/
|
|
326
|
+
function sqlAdmin(scope, config) {
|
|
327
|
+
if (!scope) {
|
|
328
|
+
scope = 'sys';
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// 检查是否已存在实例
|
|
332
|
+
if ($.pool.sql[scope]) {
|
|
333
|
+
return $.pool.sql[scope];
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// 同步方式创建实例(无需锁,因为Node.js是单线程的)
|
|
337
|
+
if (!$.pool.sql[scope]) {
|
|
338
|
+
$.pool.sql[scope] = new Sql(config);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return $.pool.sql[scope];
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* @module 导出Sql管理器
|
|
346
|
+
*/
|
|
347
|
+
exports.sqlAdmin = sqlAdmin;
|
package/mysql_error_test.js
CHANGED
|
@@ -3,7 +3,7 @@ const { mysqlAdmin } = require('../mm_mysql');
|
|
|
3
3
|
/**
|
|
4
4
|
* 测试mysqlAdmin的错误传播
|
|
5
5
|
*/
|
|
6
|
-
async function
|
|
6
|
+
async function testMysqlAdminError() {
|
|
7
7
|
console.log('=== 测试mysqlAdmin错误传播 ===');
|
|
8
8
|
|
|
9
9
|
try {
|
|
@@ -18,37 +18,50 @@ async function testMysqlAdminErrorPropagation() {
|
|
|
18
18
|
|
|
19
19
|
console.log('✅ mysqlAdmin实例创建成功');
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
console.log('测试无效SQL...');
|
|
24
|
-
await mysql.run('SELECT * FROM non_existent_table_xyz');
|
|
25
|
-
console.log('❌ mysqlAdmin应该对无效SQL抛出错误,但没有抛出');
|
|
26
|
-
} catch (error) {
|
|
27
|
-
console.log('✅ mysqlAdmin正确抛出错误:', error.message);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// 测试无效参数 - 应该抛出TypeError
|
|
31
|
-
try {
|
|
32
|
-
console.log('测试无效参数...');
|
|
33
|
-
await mysql.run('SELECT * FROM test', 'invalid_param');
|
|
34
|
-
console.log('❌ mysqlAdmin应该对无效参数抛出TypeError,但没有抛出');
|
|
35
|
-
} catch (error) {
|
|
36
|
-
if (error instanceof TypeError) {
|
|
37
|
-
console.log('✅ mysqlAdmin正确抛出TypeError:', error.message);
|
|
38
|
-
} else {
|
|
39
|
-
console.log('❌ mysqlAdmin抛出错误但不是TypeError:', error.message);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
21
|
+
await sql(mysql);
|
|
22
|
+
await param(mysql);
|
|
42
23
|
|
|
43
24
|
} catch (error) {
|
|
44
25
|
console.log('❌ mysqlAdmin实例创建失败:', error.message);
|
|
45
26
|
}
|
|
46
27
|
}
|
|
47
28
|
|
|
29
|
+
/**
|
|
30
|
+
* 测试无效SQL
|
|
31
|
+
* @param {object} mysql - mysql实例
|
|
32
|
+
*/
|
|
33
|
+
async function sql(mysql) {
|
|
34
|
+
try {
|
|
35
|
+
console.log('测试无效SQL...');
|
|
36
|
+
await mysql.run('SELECT * FROM non_existent_table_xyz');
|
|
37
|
+
console.log('❌ mysqlAdmin应该对无效SQL抛出错误,但没有抛出');
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.log('✅ mysqlAdmin正确抛出错误:', error.message);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 测试无效参数
|
|
45
|
+
* @param {object} mysql - mysql实例
|
|
46
|
+
*/
|
|
47
|
+
async function param(mysql) {
|
|
48
|
+
try {
|
|
49
|
+
console.log('测试无效参数...');
|
|
50
|
+
await mysql.run('SELECT * FROM test', 'invalid_param');
|
|
51
|
+
console.log('❌ mysqlAdmin应该对无效参数抛出TypeError,但没有抛出');
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (error instanceof TypeError) {
|
|
54
|
+
console.log('✅ mysqlAdmin正确抛出TypeError:', error.message);
|
|
55
|
+
} else {
|
|
56
|
+
console.log('❌ mysqlAdmin抛出错误但不是TypeError:', error.message);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
48
61
|
/**
|
|
49
62
|
* 测试直接使用Mysql类的错误传播
|
|
50
63
|
*/
|
|
51
|
-
async function
|
|
64
|
+
async function testDirectMysqlError() {
|
|
52
65
|
console.log('\n=== 测试直接Mysql类错误传播 ===');
|
|
53
66
|
|
|
54
67
|
try {
|
|
@@ -65,41 +78,54 @@ async function testDirectMysqlErrorPropagation() {
|
|
|
65
78
|
|
|
66
79
|
console.log('✅ 直接Mysql实例创建成功');
|
|
67
80
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
console.log('测试无效SQL...');
|
|
71
|
-
await mysql.run('SELECT * FROM non_existent_table_xyz');
|
|
72
|
-
console.log('❌ 直接Mysql应该对无效SQL抛出错误,但没有抛出');
|
|
73
|
-
} catch (error) {
|
|
74
|
-
console.log('✅ 直接Mysql正确抛出错误:', error.message);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// 测试无效参数 - 应该抛出TypeError
|
|
78
|
-
try {
|
|
79
|
-
console.log('测试无效参数...');
|
|
80
|
-
await mysql.run('SELECT * FROM test', 'invalid_param');
|
|
81
|
-
console.log('❌ 直接Mysql应该对无效参数抛出TypeError,但没有抛出');
|
|
82
|
-
} catch (error) {
|
|
83
|
-
if (error instanceof TypeError) {
|
|
84
|
-
console.log('✅ 直接Mysql正确抛出TypeError:', error.message);
|
|
85
|
-
} else {
|
|
86
|
-
console.log('❌ 直接Mysql抛出错误但不是TypeError:', error.message);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
81
|
+
await directSql(mysql);
|
|
82
|
+
await direct(mysql);
|
|
89
83
|
|
|
90
84
|
} catch (error) {
|
|
91
85
|
console.log('❌ 直接Mysql实例创建失败:', error.message);
|
|
92
86
|
}
|
|
93
87
|
}
|
|
94
88
|
|
|
89
|
+
/**
|
|
90
|
+
* 测试直接Mysql实例的无效SQL
|
|
91
|
+
* @param {object} mysql - mysql实例
|
|
92
|
+
*/
|
|
93
|
+
async function directSql(mysql) {
|
|
94
|
+
try {
|
|
95
|
+
console.log('测试无效SQL...');
|
|
96
|
+
await mysql.run('SELECT * FROM non_existent_table_xyz');
|
|
97
|
+
console.log('❌ 直接Mysql应该对无效SQL抛出错误,但没有抛出');
|
|
98
|
+
} catch (error) {
|
|
99
|
+
console.log('✅ 直接Mysql正确抛出错误:', error.message);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 测试直接Mysql实例的无效参数
|
|
105
|
+
* @param {object} mysql - mysql实例
|
|
106
|
+
*/
|
|
107
|
+
async function direct(mysql) {
|
|
108
|
+
try {
|
|
109
|
+
console.log('测试无效参数...');
|
|
110
|
+
await mysql.run('SELECT * FROM test', 'invalid_param');
|
|
111
|
+
console.log('❌ 直接Mysql应该对无效参数抛出TypeError,但没有抛出');
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (error instanceof TypeError) {
|
|
114
|
+
console.log('✅ 直接Mysql正确抛出TypeError:', error.message);
|
|
115
|
+
} else {
|
|
116
|
+
console.log('❌ 直接Mysql抛出错误但不是TypeError:', error.message);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
95
121
|
/**
|
|
96
122
|
* 主测试函数
|
|
97
123
|
*/
|
|
98
124
|
async function main() {
|
|
99
125
|
console.log('开始测试MySQL错误传播...\n');
|
|
100
126
|
|
|
101
|
-
await
|
|
102
|
-
await
|
|
127
|
+
await testMysqlAdminError();
|
|
128
|
+
await testDirectMysqlError();
|
|
103
129
|
|
|
104
130
|
console.log('\n测试完成');
|
|
105
131
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_sql",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "一个通用的SQL帮助类,支持通过切换db_type实现对不同数据库的操作,包括MySQL和SQLite等",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"node": ">=12.0.0"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"mm_mysql": "^2.2.
|
|
48
|
-
"mm_sqlite": "^1.2.
|
|
47
|
+
"mm_mysql": "^2.2.8",
|
|
48
|
+
"mm_sqlite": "^1.2.8"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"eslint": "^9.39.2",
|