mm_sqlite 1.4.0 → 1.4.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/db.js +200 -264
- package/index.js +43 -54
- package/package.json +1 -1
- package/pool.js +11 -16
- package/sql.js +311 -414
- package/test_db/test_basic.db +0 -0
- package/test_db/test_core_methods.db +0 -0
package/index.js
CHANGED
|
@@ -152,13 +152,8 @@ SQLite.prototype.getConn = async function (timeout) {
|
|
|
152
152
|
await this.open(acquire_timeout);
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return await this._pool.getConnection(acquire_timeout);
|
|
158
|
-
} catch (error) {
|
|
159
|
-
this.log('error', '获取连接失败', error);
|
|
160
|
-
return null;
|
|
161
|
-
}
|
|
155
|
+
// 统一从连接池获取连接
|
|
156
|
+
return await this._pool.getConnection(acquire_timeout);
|
|
162
157
|
};
|
|
163
158
|
|
|
164
159
|
/**
|
|
@@ -196,7 +191,7 @@ SQLite.prototype.run = async function (sql, params = [], timeout = 30000) {
|
|
|
196
191
|
timeout_id = setTimeout(() => {
|
|
197
192
|
reject(new Error(`SQL查询超时: ${timeout}ms`));
|
|
198
193
|
}, timeout);
|
|
199
|
-
|
|
194
|
+
|
|
200
195
|
// 执行SQL查询
|
|
201
196
|
conn.all(sql, params || [], (error, rows) => {
|
|
202
197
|
// 清除超时定时器
|
|
@@ -316,59 +311,53 @@ SQLite.prototype.read = async function (table, condition, options) {
|
|
|
316
311
|
throw new TypeError('options must be object');
|
|
317
312
|
}
|
|
318
313
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// 构建基础SQL查询
|
|
326
|
-
let sql = `SELECT * FROM ${table}`;
|
|
327
|
-
const params = [];
|
|
314
|
+
// 检查连接状态
|
|
315
|
+
if (this._status !== 'connected') {
|
|
316
|
+
throw new Error('数据库连接未建立');
|
|
317
|
+
}
|
|
328
318
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
for (const [field, value] of Object.entries(condition)) {
|
|
333
|
-
where_clauses.push(`${field} = ?`);
|
|
334
|
-
params.push(value);
|
|
335
|
-
}
|
|
336
|
-
sql += ` WHERE ${where_clauses.join(' AND ')}`;
|
|
337
|
-
}
|
|
319
|
+
// 构建基础SQL查询
|
|
320
|
+
let sql = `SELECT * FROM ${table}`;
|
|
321
|
+
const params = [];
|
|
338
322
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
323
|
+
// 处理条件
|
|
324
|
+
if (condition && Object.keys(condition).length > 0) {
|
|
325
|
+
const where_clauses = [];
|
|
326
|
+
for (const [field, value] of Object.entries(condition)) {
|
|
327
|
+
where_clauses.push(`${field} = ?`);
|
|
328
|
+
params.push(value);
|
|
342
329
|
}
|
|
330
|
+
sql += ` WHERE ${where_clauses.join(' AND ')}`;
|
|
331
|
+
}
|
|
343
332
|
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
333
|
+
// 处理排序
|
|
334
|
+
if (options && options.order_by) {
|
|
335
|
+
sql += ` ORDER BY ${options.order_by}`;
|
|
336
|
+
}
|
|
348
337
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
}
|
|
338
|
+
// 处理分页
|
|
339
|
+
if (options && options.limit) {
|
|
340
|
+
sql += ` LIMIT ${options.limit}`;
|
|
341
|
+
}
|
|
352
342
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
}
|
|
343
|
+
if (options && options.offset) {
|
|
344
|
+
sql += ` OFFSET ${options.offset}`;
|
|
345
|
+
}
|
|
357
346
|
|
|
358
|
-
|
|
359
|
-
|
|
347
|
+
// 记录查询日志
|
|
348
|
+
if (this.config.debug) {
|
|
349
|
+
this.log('debug', '查询数据', { sql, params });
|
|
350
|
+
}
|
|
360
351
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
}
|
|
352
|
+
// 执行查询
|
|
353
|
+
const results = await this.run(sql, params);
|
|
364
354
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
} catch (error) {
|
|
368
|
-
this.log('error', '查询失败', error);
|
|
369
|
-
// 返回空数组作为默认值,保持返回值类型一致
|
|
370
|
-
return [];
|
|
355
|
+
if (this.config.debug) {
|
|
356
|
+
this.log('debug', '查询成功', { count: Array.isArray(results) ? results.length : 1 });
|
|
371
357
|
}
|
|
358
|
+
|
|
359
|
+
// 确保返回数组格式
|
|
360
|
+
return Array.isArray(results) ? results : [results];
|
|
372
361
|
};
|
|
373
362
|
|
|
374
363
|
/**
|
|
@@ -415,7 +404,7 @@ SQLite.prototype.db = function () {
|
|
|
415
404
|
* 开始事务
|
|
416
405
|
* @returns {Promise<object>} 事务连接对象
|
|
417
406
|
*/
|
|
418
|
-
SQLite.prototype.
|
|
407
|
+
SQLite.prototype.beginTransaction = async function () {
|
|
419
408
|
try {
|
|
420
409
|
// 检查连接状态
|
|
421
410
|
if (this._status !== 'connected') {
|
|
@@ -515,7 +504,7 @@ SQLite.prototype.beginTrans = async function () {
|
|
|
515
504
|
* @returns {Promise<*>} 回调函数的返回值
|
|
516
505
|
* @throws {TypeError} 当callback参数无效时
|
|
517
506
|
*/
|
|
518
|
-
SQLite.prototype.
|
|
507
|
+
SQLite.prototype.transaction = async function (callback) {
|
|
519
508
|
// 参数校验
|
|
520
509
|
if (typeof callback !== 'function') {
|
|
521
510
|
throw new TypeError('callback must be function');
|
|
@@ -525,7 +514,7 @@ SQLite.prototype.trans = async function (callback) {
|
|
|
525
514
|
|
|
526
515
|
try {
|
|
527
516
|
// 开始事务
|
|
528
|
-
trans = await this.
|
|
517
|
+
trans = await this.beginTransaction();
|
|
529
518
|
|
|
530
519
|
// 执行回调函数,传入事务对象
|
|
531
520
|
const result = await callback(trans);
|
package/package.json
CHANGED
package/pool.js
CHANGED
|
@@ -33,24 +33,19 @@ class SQLitePool {
|
|
|
33
33
|
return;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
36
|
+
// 创建最小连接数
|
|
37
|
+
for (let i = 0; i < this.config.pool_min; i++) {
|
|
38
|
+
const conn = await this._createConnection();
|
|
39
|
+
this._pool_connections.push(conn);
|
|
40
|
+
this._pool_available.push(conn);
|
|
41
|
+
}
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
// 启动连接回收器
|
|
44
|
+
this._pool_reaper = setInterval(() => {
|
|
45
|
+
this._reapIdleConnections();
|
|
46
|
+
}, this.config.pool_reap_interval);
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
} catch (error) {
|
|
51
|
-
this.log('error', '连接池初始化失败', error);
|
|
52
|
-
throw error;
|
|
53
|
-
}
|
|
48
|
+
this._is_initialized = true;
|
|
54
49
|
}
|
|
55
50
|
|
|
56
51
|
/**
|