mm_sqlite 1.0.6 → 1.0.8
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/README.md +351 -1
- package/db/mm.db +0 -0
- package/db.js +583 -139
- package/index.js +687 -187
- package/package.json +16 -11
- package/sql.js +854 -125
- package/sql_builder.js +375 -0
- package/test.js +75 -648
- package/test_all_methods.js +115 -0
- package/db/test.db +0 -0
- package/index_/345/217/202/350/200/203.js +0 -299
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// 核心方法测试脚本 - 专注测试del、delList等关键方法
|
|
2
|
+
const { Sqlite } = require('./index');
|
|
3
|
+
|
|
4
|
+
async function testAllMethods() {
|
|
5
|
+
try {
|
|
6
|
+
console.log('=== 开始测试 SQLite 模块核心方法 ===');
|
|
7
|
+
|
|
8
|
+
// 初始化SQLite实例
|
|
9
|
+
const sql = new Sqlite({
|
|
10
|
+
database: './db/test_core_methods.db'
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
await sql.open();
|
|
14
|
+
console.log('数据库连接成功');
|
|
15
|
+
|
|
16
|
+
// 获取数据库操作实例
|
|
17
|
+
const db = sql.db();
|
|
18
|
+
const test_table = 'test_core_methods';
|
|
19
|
+
|
|
20
|
+
// 准备测试表
|
|
21
|
+
console.log('\n=== 1. 准备测试环境 ===');
|
|
22
|
+
await db.exec(`DROP TABLE IF EXISTS ${test_table};`);
|
|
23
|
+
await db.exec(`CREATE TABLE IF NOT EXISTS ${test_table} (
|
|
24
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
25
|
+
username VARCHAR(50) NOT NULL,
|
|
26
|
+
email VARCHAR(100),
|
|
27
|
+
age INTEGER DEFAULT 0 NOT NULL
|
|
28
|
+
);`);
|
|
29
|
+
console.log('测试表创建完成');
|
|
30
|
+
|
|
31
|
+
db.table = test_table;
|
|
32
|
+
|
|
33
|
+
// 2. 单条数据操作测试
|
|
34
|
+
console.log('\n=== 2. 单条数据操作测试 ===');
|
|
35
|
+
// 添加单条数据
|
|
36
|
+
const addResult = await db.add({
|
|
37
|
+
username: 'test_user1',
|
|
38
|
+
email: 'test1@example.com',
|
|
39
|
+
age: 25
|
|
40
|
+
});
|
|
41
|
+
console.log('✓ add方法测试通过:', addResult);
|
|
42
|
+
|
|
43
|
+
// 查询单条数据
|
|
44
|
+
const getResult = await db.get({ username: 'test_user1' });
|
|
45
|
+
console.log('✓ get方法测试通过,数据:', getResult[0]?.username);
|
|
46
|
+
|
|
47
|
+
// 修改数据
|
|
48
|
+
const setResult = await db.set(
|
|
49
|
+
{ username: 'test_user1' },
|
|
50
|
+
{ age: 26 }
|
|
51
|
+
);
|
|
52
|
+
console.log('✓ set方法测试通过:', setResult);
|
|
53
|
+
|
|
54
|
+
// 3. 批量操作测试 - 重点测试addList
|
|
55
|
+
console.log('\n=== 3. 批量添加测试 ===');
|
|
56
|
+
const batchData = [
|
|
57
|
+
{ username: 'batch_user1', email: 'batch1@example.com', age: 30 },
|
|
58
|
+
{ username: 'batch_user2', email: 'batch2@example.com', age: 31 },
|
|
59
|
+
{ username: 'batch_user3', email: 'batch3@example.com', age: 32 }
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
const addListResult = await db.addList(batchData);
|
|
63
|
+
console.log('✓ addList方法测试通过:', addListResult);
|
|
64
|
+
|
|
65
|
+
// 4. 单条删除测试 - 重点测试del
|
|
66
|
+
console.log('\n=== 4. 单条删除测试 ===');
|
|
67
|
+
const delResult = await db.del({ username: 'test_user1' });
|
|
68
|
+
console.log('✓ del方法测试通过:', delResult);
|
|
69
|
+
|
|
70
|
+
// 验证删除结果
|
|
71
|
+
const delCheck = await db.get({ username: 'test_user1' });
|
|
72
|
+
console.log(' del方法验证结果: 删除成功', delCheck.length === 0);
|
|
73
|
+
|
|
74
|
+
// 5. 批量删除测试 - 重点测试delList
|
|
75
|
+
console.log('\n=== 5. 批量删除测试 ===');
|
|
76
|
+
const deleteList = [
|
|
77
|
+
{ query: { username: 'batch_user1' } },
|
|
78
|
+
{ query: { username: 'batch_user2' } }
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
const delListResult = await db.delList(deleteList);
|
|
82
|
+
console.log('✓ delList方法测试通过:', delListResult);
|
|
83
|
+
|
|
84
|
+
// 验证批量删除结果
|
|
85
|
+
const batch1Check = await db.get({ username: 'batch_user1' });
|
|
86
|
+
const batch2Check = await db.get({ username: 'batch_user2' });
|
|
87
|
+
console.log(' delList方法验证结果: 批量删除成功',
|
|
88
|
+
batch1Check.length === 0 && batch2Check.length === 0);
|
|
89
|
+
|
|
90
|
+
// 验证剩余数据
|
|
91
|
+
const remainingData = await db.get({});
|
|
92
|
+
console.log(' 剩余数据数量:', remainingData.length);
|
|
93
|
+
|
|
94
|
+
// 6. 清理测试数据
|
|
95
|
+
console.log('\n=== 6. 清理测试环境 ===');
|
|
96
|
+
await db.exec(`DROP TABLE IF EXISTS ${test_table};`);
|
|
97
|
+
console.log('测试表清理完成');
|
|
98
|
+
|
|
99
|
+
// 关闭数据库连接
|
|
100
|
+
await sql.close();
|
|
101
|
+
console.log('\n=== 核心方法测试完成!===');
|
|
102
|
+
console.log('✓ del方法工作正常');
|
|
103
|
+
console.log('✓ delList方法工作正常');
|
|
104
|
+
console.log('✓ 其他核心方法(add, get, set, addList)工作正常');
|
|
105
|
+
|
|
106
|
+
} catch (error) {
|
|
107
|
+
console.error('测试过程中出现错误:', error.message);
|
|
108
|
+
console.error('错误堆栈:', error.stack);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 执行测试
|
|
113
|
+
(async () => {
|
|
114
|
+
await testAllMethods();
|
|
115
|
+
})();
|
package/db/test.db
DELETED
|
Binary file
|
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileOverview Mysql帮助类函数
|
|
3
|
-
* @author <a href="http://qww.elins.cn">邱文武</a>
|
|
4
|
-
* @version 1.2
|
|
5
|
-
*/
|
|
6
|
-
const {
|
|
7
|
-
createPool
|
|
8
|
-
} = require('mysql');
|
|
9
|
-
const {
|
|
10
|
-
DB
|
|
11
|
-
} = require('./db');
|
|
12
|
-
|
|
13
|
-
const Link_model = require('./link_model');
|
|
14
|
-
|
|
15
|
-
var pool = {};
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @description 数据库封装
|
|
19
|
-
*/
|
|
20
|
-
class Mysql {
|
|
21
|
-
/**
|
|
22
|
-
* @description 创建Mysql帮助类函数 (构造函数)
|
|
23
|
-
* @param {String} scope 作用域
|
|
24
|
-
* @param {String} dir 当前路径
|
|
25
|
-
* @constructor
|
|
26
|
-
*/
|
|
27
|
-
constructor(scope, dir) {
|
|
28
|
-
// 作用域
|
|
29
|
-
this.scope;
|
|
30
|
-
if (scope) {
|
|
31
|
-
this.scope = scope;
|
|
32
|
-
} else {
|
|
33
|
-
this.scope = $.val.scope + '';
|
|
34
|
-
}
|
|
35
|
-
// 当前目录
|
|
36
|
-
this.dir = __dirname;
|
|
37
|
-
if (dir) {
|
|
38
|
-
this.dir = dir;
|
|
39
|
-
}
|
|
40
|
-
// 错误提示
|
|
41
|
-
this.error;
|
|
42
|
-
/**
|
|
43
|
-
* sql语句
|
|
44
|
-
*/
|
|
45
|
-
this.sql = "";
|
|
46
|
-
// 连接器
|
|
47
|
-
this.conn;
|
|
48
|
-
|
|
49
|
-
// 数据库配置参数
|
|
50
|
-
this.config = {
|
|
51
|
-
// 服务器地址
|
|
52
|
-
host: "127.0.0.1",
|
|
53
|
-
// 端口号
|
|
54
|
-
port: 3306,
|
|
55
|
-
// 连接用户名
|
|
56
|
-
user: "root",
|
|
57
|
-
// 连接密码
|
|
58
|
-
password: "asd123",
|
|
59
|
-
// 数据库
|
|
60
|
-
database: "mm",
|
|
61
|
-
// 是否支持多个sql语句同时操作
|
|
62
|
-
multipleStatements: false,
|
|
63
|
-
// 打印SQL
|
|
64
|
-
log: false
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// 唯一标识符
|
|
68
|
-
this.identifier = this.config.host + "/" + this.config.database;
|
|
69
|
-
|
|
70
|
-
// 定义当前类, 用于数据库实例化访问
|
|
71
|
-
var $this = this;
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* @description 查询sql
|
|
75
|
-
* @param {String} sql 查询参
|
|
76
|
-
* @param {Array} val 替换值
|
|
77
|
-
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
78
|
-
*/
|
|
79
|
-
this.run = function(sql, val) {
|
|
80
|
-
var _this = this;
|
|
81
|
-
this.sql = sql;
|
|
82
|
-
if ($this.config.log) {
|
|
83
|
-
$.log.debug("SQL:", sql);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// 返回一个 Promise
|
|
87
|
-
return new Promise((resolve, reject) => {
|
|
88
|
-
$this.conn.getConnection(function(err, db) {
|
|
89
|
-
if (err) {
|
|
90
|
-
_this.error = {
|
|
91
|
-
code: 2003,
|
|
92
|
-
message: $.info(err).between('Error: ', '\n')
|
|
93
|
-
};
|
|
94
|
-
$.log.error("SQL error:", err);
|
|
95
|
-
resolve([]);
|
|
96
|
-
reject(err);
|
|
97
|
-
} else {
|
|
98
|
-
db.query(sql, val, (err, rows) => {
|
|
99
|
-
// 结束会话
|
|
100
|
-
db.release();
|
|
101
|
-
if (err) {
|
|
102
|
-
_this.error = {
|
|
103
|
-
code: err.errno,
|
|
104
|
-
message: err.sqlMessage
|
|
105
|
-
};
|
|
106
|
-
$.log.error("SQL error:", err);
|
|
107
|
-
resolve([]);
|
|
108
|
-
reject(err);
|
|
109
|
-
} else {
|
|
110
|
-
_this.error = undefined;
|
|
111
|
-
resolve(rows);
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* @description 增删改sql
|
|
121
|
-
* @param {String} sql 查询参
|
|
122
|
-
* @param {Array} val 替换值
|
|
123
|
-
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
124
|
-
*/
|
|
125
|
-
this.exec = function(sql, val) {
|
|
126
|
-
var _this = this;
|
|
127
|
-
if (this.task) {
|
|
128
|
-
this.task_sql += sql + "\r\n";
|
|
129
|
-
if (this.task === 1) {
|
|
130
|
-
return this.task_sql;
|
|
131
|
-
} else if (this.task === 2) {
|
|
132
|
-
this.task = 0;
|
|
133
|
-
sql = this.task_sql.trim();
|
|
134
|
-
this.task_sql = '';
|
|
135
|
-
} else {
|
|
136
|
-
this.task = 0;
|
|
137
|
-
this.task_sql = '';
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
this.sql = sql;
|
|
142
|
-
if ($this.config.log) {
|
|
143
|
-
$.log.debug("SQL:", sql);
|
|
144
|
-
}
|
|
145
|
-
// $this.conn.config.connectionConfig = false;
|
|
146
|
-
// console.log("测试", $this.conn.config.connectionConfig);
|
|
147
|
-
// 返回一个 Promise
|
|
148
|
-
return new Promise((resolve, reject) => {
|
|
149
|
-
$this.conn.getConnection(function(err, db) {
|
|
150
|
-
if (err) {
|
|
151
|
-
_this.error = {
|
|
152
|
-
code: 41000,
|
|
153
|
-
message: $.info(err).between('Error: ', '\n')
|
|
154
|
-
};
|
|
155
|
-
resolve(-1);
|
|
156
|
-
reject(err);
|
|
157
|
-
$.log.error("SQL error:", err);
|
|
158
|
-
} else {
|
|
159
|
-
db.query(sql, val, (err, o) => {
|
|
160
|
-
if (err) {
|
|
161
|
-
_this.error = {
|
|
162
|
-
code: err.errno,
|
|
163
|
-
message: err.sqlMessage
|
|
164
|
-
};
|
|
165
|
-
resolve(-1);
|
|
166
|
-
reject(err);
|
|
167
|
-
$.log.error("SQL error:", err);
|
|
168
|
-
} else {
|
|
169
|
-
_this.error = undefined;
|
|
170
|
-
if (o.constructor == Array) {
|
|
171
|
-
if (o.length > 0) {
|
|
172
|
-
var num = 0;
|
|
173
|
-
o.map(function(item) {
|
|
174
|
-
num += item['affectedRows'];
|
|
175
|
-
});
|
|
176
|
-
if (num === 0) {
|
|
177
|
-
resolve(o.length);
|
|
178
|
-
} else {
|
|
179
|
-
resolve(num);
|
|
180
|
-
}
|
|
181
|
-
} else {
|
|
182
|
-
resolve(1);
|
|
183
|
-
}
|
|
184
|
-
} else {
|
|
185
|
-
var num = o['affectedRows'];
|
|
186
|
-
if (num === 0) {
|
|
187
|
-
resolve(1);
|
|
188
|
-
} else {
|
|
189
|
-
resolve(num);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
// 结束会话
|
|
194
|
-
db.release();
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* @description 获取数据库管理器
|
|
203
|
-
*/
|
|
204
|
-
this.db = function() {
|
|
205
|
-
return new DB($this);
|
|
206
|
-
};
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* 获取数据库管理器
|
|
212
|
-
* @param {String} key 主键
|
|
213
|
-
* @param {String|Number} value 对象值
|
|
214
|
-
* @param {Boolean} clear_prefix 清除前缀
|
|
215
|
-
* @param {Array} arr_table 关联的数据表
|
|
216
|
-
* @return {Object} 管理模型
|
|
217
|
-
*/
|
|
218
|
-
Mysql.prototype.dbs = async function(key, value, clear_prefix, ...arr_table) {
|
|
219
|
-
var lm = new Link_model({
|
|
220
|
-
key,
|
|
221
|
-
value,
|
|
222
|
-
clear_prefix
|
|
223
|
-
});
|
|
224
|
-
lm.sql = this;
|
|
225
|
-
for (var i = 0; i < arr_table.length; i++) {
|
|
226
|
-
await lm.add(arr_table[i]);
|
|
227
|
-
}
|
|
228
|
-
return lm;
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* 设置配置参数
|
|
233
|
-
* @param {Object} cg 配置对象或配置路径
|
|
234
|
-
*/
|
|
235
|
-
Mysql.prototype.setConfig = function(cg) {
|
|
236
|
-
var obj;
|
|
237
|
-
if (typeof(cg) === "string") {
|
|
238
|
-
obj = cg.loadJson(this.dir);
|
|
239
|
-
} else {
|
|
240
|
-
obj = cg;
|
|
241
|
-
}
|
|
242
|
-
$.push(this.config, obj);
|
|
243
|
-
this.identifier = this.config.host + "/" + this.config.database;
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* @description 打开数据库, 如果没有则建立数据库连接再打开
|
|
248
|
-
*/
|
|
249
|
-
Mysql.prototype.open = function() {
|
|
250
|
-
if (!pool[this.identifier]) {
|
|
251
|
-
pool[this.identifier] = createPool(this.config);
|
|
252
|
-
}
|
|
253
|
-
this.conn = pool[this.identifier];
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
/**
|
|
257
|
-
* @description 关闭连接
|
|
258
|
-
*/
|
|
259
|
-
Mysql.prototype.close = function() {
|
|
260
|
-
if (pool[this.identifier]) {
|
|
261
|
-
pool[this.identifier] = null;
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* @description 导出Mysql帮助类
|
|
267
|
-
*/
|
|
268
|
-
exports.Mysql = Mysql;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* @description mysql连接池
|
|
273
|
-
*/
|
|
274
|
-
if (!$.pool.mysql) {
|
|
275
|
-
$.pool.mysql = {};
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* @description 缓存管理器,用于创建缓存
|
|
280
|
-
* @param {String} scope 作用域
|
|
281
|
-
* @param {String} dir 当前路径
|
|
282
|
-
* @return {Object} 返回一个缓存类
|
|
283
|
-
*/
|
|
284
|
-
function mysql_admin(scope, dir) {
|
|
285
|
-
if (!scope) {
|
|
286
|
-
scope = $.val.scope
|
|
287
|
-
}
|
|
288
|
-
var obj = $.pool.mysql[scope];
|
|
289
|
-
if (!obj) {
|
|
290
|
-
$.pool.mysql[scope] = new Mysql(scope, dir);
|
|
291
|
-
obj = $.pool.mysql[scope];
|
|
292
|
-
}
|
|
293
|
-
return obj;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* @description 导出Mysql管理函数
|
|
298
|
-
*/
|
|
299
|
-
exports.mysql_admin = mysql_admin;
|