mm_sqlite 1.1.4 → 1.1.5
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 +168 -102
- package/db.js +94 -115
- package/index.js +741 -548
- package/package.json +2 -2
- package/sql.js +245 -372
- package/sql_builder.js +0 -375
package/sql_builder.js
DELETED
|
@@ -1,375 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SQL构建器类 - 提供与mm_mysql兼容的链式SQL构建接口
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* SQL构建器类
|
|
7
|
-
*/
|
|
8
|
-
class SqlBuilder {
|
|
9
|
-
/**
|
|
10
|
-
* 构造函数
|
|
11
|
-
* @param {Object} sqlite - Sqlite实例
|
|
12
|
-
*/
|
|
13
|
-
constructor(sqlite) {
|
|
14
|
-
this.sqlite = sqlite;
|
|
15
|
-
this.tableName = '';
|
|
16
|
-
this.fields = '*';
|
|
17
|
-
this.whereConditions = [];
|
|
18
|
-
this.orderByField = '';
|
|
19
|
-
this.orderDirection = 'ASC';
|
|
20
|
-
this.limitValue = null;
|
|
21
|
-
this.offsetValue = null;
|
|
22
|
-
this.groupByField = '';
|
|
23
|
-
this.havingCondition = '';
|
|
24
|
-
this.joinClauses = [];
|
|
25
|
-
this.insertData = null;
|
|
26
|
-
this.updateData = null;
|
|
27
|
-
this.deleteFlag = false;
|
|
28
|
-
this.transaction = null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 设置表名
|
|
33
|
-
* @param {String} table - 表名
|
|
34
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
35
|
-
*/
|
|
36
|
-
table(table) {
|
|
37
|
-
this.tableName = table;
|
|
38
|
-
return this;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 设置查询字段
|
|
43
|
-
* @param {String|Array} fields - 查询字段
|
|
44
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
45
|
-
*/
|
|
46
|
-
select(fields = '*') {
|
|
47
|
-
this.fields = fields;
|
|
48
|
-
return this;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 设置查询条件
|
|
53
|
-
* @param {String|Object} conditions - 查询条件
|
|
54
|
-
* @param {*} value - 条件值
|
|
55
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
56
|
-
*/
|
|
57
|
-
where(conditions, value = null) {
|
|
58
|
-
if (typeof conditions === 'object') {
|
|
59
|
-
// 对象形式的条件
|
|
60
|
-
for (const key in conditions) {
|
|
61
|
-
this.whereConditions.push(`${key} = ?`);
|
|
62
|
-
}
|
|
63
|
-
} else if (typeof conditions === 'string') {
|
|
64
|
-
// 字符串形式的条件
|
|
65
|
-
if (value !== null) {
|
|
66
|
-
this.whereConditions.push(`${conditions} = ?`);
|
|
67
|
-
} else {
|
|
68
|
-
this.whereConditions.push(conditions);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return this;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 设置OR查询条件
|
|
76
|
-
* @param {String|Object} conditions - 查询条件
|
|
77
|
-
* @param {*} value - 条件值
|
|
78
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
79
|
-
*/
|
|
80
|
-
orWhere(conditions, value = null) {
|
|
81
|
-
// 简化实现,实际应该支持OR逻辑
|
|
82
|
-
return this.where(conditions, value);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 设置LIKE查询
|
|
87
|
-
* @param {String} field - 字段名
|
|
88
|
-
* @param {String} value - 查询值
|
|
89
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
90
|
-
*/
|
|
91
|
-
like(field, value) {
|
|
92
|
-
this.whereConditions.push(`${field} LIKE ?`);
|
|
93
|
-
return this;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* 设置IN查询
|
|
98
|
-
* @param {String} field - 字段名
|
|
99
|
-
* @param {Array} values - 值数组
|
|
100
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
101
|
-
*/
|
|
102
|
-
in(field, values) {
|
|
103
|
-
const placeholders = values.map(() => '?').join(',');
|
|
104
|
-
this.whereConditions.push(`${field} IN (${placeholders})`);
|
|
105
|
-
return this;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* 设置排序
|
|
110
|
-
* @param {String} field - 排序字段
|
|
111
|
-
* @param {String} direction - 排序方向
|
|
112
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
113
|
-
*/
|
|
114
|
-
orderBy(field, direction = 'ASC') {
|
|
115
|
-
this.orderByField = field;
|
|
116
|
-
this.orderDirection = direction.toUpperCase();
|
|
117
|
-
return this;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 设置分组
|
|
122
|
-
* @param {String} field - 分组字段
|
|
123
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
124
|
-
*/
|
|
125
|
-
groupBy(field) {
|
|
126
|
-
this.groupByField = field;
|
|
127
|
-
return this;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 设置HAVING条件
|
|
132
|
-
* @param {String} condition - HAVING条件
|
|
133
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
134
|
-
*/
|
|
135
|
-
having(condition) {
|
|
136
|
-
this.havingCondition = condition;
|
|
137
|
-
return this;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 设置限制数量
|
|
142
|
-
* @param {Number} limit - 限制数量
|
|
143
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
144
|
-
*/
|
|
145
|
-
limit(limit) {
|
|
146
|
-
this.limitValue = limit;
|
|
147
|
-
return this;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 设置偏移量
|
|
152
|
-
* @param {Number} offset - 偏移量
|
|
153
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
154
|
-
*/
|
|
155
|
-
offset(offset) {
|
|
156
|
-
this.offsetValue = offset;
|
|
157
|
-
return this;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 设置插入数据
|
|
162
|
-
* @param {Object} data - 插入数据
|
|
163
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
164
|
-
*/
|
|
165
|
-
insert(data) {
|
|
166
|
-
this.insertData = data;
|
|
167
|
-
return this;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* 设置更新数据
|
|
172
|
-
* @param {Object} data - 更新数据
|
|
173
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
174
|
-
*/
|
|
175
|
-
update(data) {
|
|
176
|
-
this.updateData = data;
|
|
177
|
-
return this;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* 设置删除操作
|
|
182
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
183
|
-
*/
|
|
184
|
-
delete() {
|
|
185
|
-
this.deleteFlag = true;
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* 设置JOIN操作
|
|
191
|
-
* @param {String} table - 连接表名
|
|
192
|
-
* @param {String} condition - 连接条件
|
|
193
|
-
* @param {String} type - 连接类型
|
|
194
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
195
|
-
*/
|
|
196
|
-
join(table, condition, type = 'INNER') {
|
|
197
|
-
this.joinClauses.push({
|
|
198
|
-
table,
|
|
199
|
-
condition,
|
|
200
|
-
type: type.toUpperCase()
|
|
201
|
-
});
|
|
202
|
-
return this;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* 设置事务
|
|
207
|
-
* @param {Object} transaction - 事务对象
|
|
208
|
-
* @returns {SqlBuilder} 返回自身实例
|
|
209
|
-
*/
|
|
210
|
-
setTransaction(transaction) {
|
|
211
|
-
this.transaction = transaction;
|
|
212
|
-
return this;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* 构建SELECT SQL语句
|
|
217
|
-
* @returns {String} SQL语句
|
|
218
|
-
*/
|
|
219
|
-
_buildSelectSql() {
|
|
220
|
-
let sql = 'SELECT ';
|
|
221
|
-
|
|
222
|
-
// 处理字段
|
|
223
|
-
if (Array.isArray(this.fields)) {
|
|
224
|
-
sql += this.fields.join(', ');
|
|
225
|
-
} else {
|
|
226
|
-
sql += this.fields;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
sql += ` FROM ${this.tableName}`;
|
|
230
|
-
|
|
231
|
-
// 处理JOIN
|
|
232
|
-
for (const join of this.joinClauses) {
|
|
233
|
-
sql += ` ${join.type} JOIN ${join.table} ON ${join.condition}`;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// 处理WHERE条件
|
|
237
|
-
if (this.whereConditions.length > 0) {
|
|
238
|
-
sql += ' WHERE ' + this.whereConditions.join(' AND ');
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// 处理GROUP BY
|
|
242
|
-
if (this.groupByField) {
|
|
243
|
-
sql += ` GROUP BY ${this.groupByField}`;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// 处理HAVING
|
|
247
|
-
if (this.havingCondition) {
|
|
248
|
-
sql += ` HAVING ${this.havingCondition}`;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// 处理ORDER BY
|
|
252
|
-
if (this.orderByField) {
|
|
253
|
-
sql += ` ORDER BY ${this.orderByField} ${this.orderDirection}`;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
// 处理LIMIT和OFFSET
|
|
257
|
-
if (this.limitValue !== null) {
|
|
258
|
-
sql += ` LIMIT ${this.limitValue}`;
|
|
259
|
-
if (this.offsetValue !== null) {
|
|
260
|
-
sql += ` OFFSET ${this.offsetValue}`;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return sql;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* 构建INSERT SQL语句
|
|
269
|
-
* @returns {String} SQL语句
|
|
270
|
-
*/
|
|
271
|
-
_buildInsertSql() {
|
|
272
|
-
if (!this.insertData) {
|
|
273
|
-
throw new Error('No insert data provided');
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
const keys = Object.keys(this.insertData);
|
|
277
|
-
const values = Object.values(this.insertData);
|
|
278
|
-
const placeholders = keys.map(() => '?').join(', ');
|
|
279
|
-
|
|
280
|
-
return `INSERT INTO ${this.tableName} (${keys.join(', ')}) VALUES (${placeholders})`;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* 构建UPDATE SQL语句
|
|
285
|
-
* @returns {String} SQL语句
|
|
286
|
-
*/
|
|
287
|
-
_buildUpdateSql() {
|
|
288
|
-
if (!this.updateData) {
|
|
289
|
-
throw new Error('No update data provided');
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const setClause = Object.keys(this.updateData)
|
|
293
|
-
.map(key => `${key} = ?`)
|
|
294
|
-
.join(', ');
|
|
295
|
-
|
|
296
|
-
let sql = `UPDATE ${this.tableName} SET ${setClause}`;
|
|
297
|
-
|
|
298
|
-
if (this.whereConditions.length > 0) {
|
|
299
|
-
sql += ' WHERE ' + this.whereConditions.join(' AND ');
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return sql;
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* 构建DELETE SQL语句
|
|
307
|
-
* @returns {String} SQL语句
|
|
308
|
-
*/
|
|
309
|
-
_buildDeleteSql() {
|
|
310
|
-
let sql = `DELETE FROM ${this.tableName}`;
|
|
311
|
-
|
|
312
|
-
if (this.whereConditions.length > 0) {
|
|
313
|
-
sql += ' WHERE ' + this.whereConditions.join(' AND ');
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
return sql;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* 执行查询并返回结果
|
|
321
|
-
* @returns {Promise<Array>} 查询结果
|
|
322
|
-
*/
|
|
323
|
-
async get() {
|
|
324
|
-
const sql = this._buildSelectSql();
|
|
325
|
-
return await this.sqlite.run(sql);
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* 获取第一条记录
|
|
330
|
-
* @returns {Promise<Object>} 第一条记录
|
|
331
|
-
*/
|
|
332
|
-
async first() {
|
|
333
|
-
this.limit(1);
|
|
334
|
-
const results = await this.get();
|
|
335
|
-
return results.length > 0 ? results[0] : null;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* 统计记录数量
|
|
340
|
-
* @returns {Promise<Number>} 记录数量
|
|
341
|
-
*/
|
|
342
|
-
async count() {
|
|
343
|
-
const originalFields = this.fields;
|
|
344
|
-
this.fields = 'COUNT(*) as count';
|
|
345
|
-
const results = await this.get();
|
|
346
|
-
this.fields = originalFields;
|
|
347
|
-
return results[0].count;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
/**
|
|
351
|
-
* 执行插入操作
|
|
352
|
-
* @returns {Promise<Object>} 插入结果
|
|
353
|
-
*/
|
|
354
|
-
async exec() {
|
|
355
|
-
if (this.insertData) {
|
|
356
|
-
const sql = this._buildInsertSql();
|
|
357
|
-
return await this.sqlite.exec(sql, Object.values(this.insertData));
|
|
358
|
-
} else if (this.updateData) {
|
|
359
|
-
const sql = this._buildUpdateSql();
|
|
360
|
-
const params = Object.values(this.updateData);
|
|
361
|
-
return await this.sqlite.exec(sql, params);
|
|
362
|
-
} else if (this.deleteFlag) {
|
|
363
|
-
const sql = this._buildDeleteSql();
|
|
364
|
-
return await this.sqlite.exec(sql);
|
|
365
|
-
} else {
|
|
366
|
-
throw new Error('No operation specified');
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
// 导出SqlBuilder类
|
|
372
|
-
module.exports = { SqlBuilder };
|
|
373
|
-
|
|
374
|
-
// 兼容CommonJS和ES6模块导出
|
|
375
|
-
exports.SqlBuilder = SqlBuilder;
|