mm_sqlite 1.0.4 → 1.0.6
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/LICENSE +201 -0
- package/README.md +2 -0
- package/config.json +8 -0
- package/db/mm.db +0 -0
- package/db.js +344 -94
- package/index.js +38 -8
- package/index_/345/217/202/350/200/203.js +299 -0
- package/link_model.js +132 -0
- package/package.json +38 -36
- package/sql.js +275 -51
- package/sql.json +56 -0
- package/test.js +594 -82
package/db.js
CHANGED
|
@@ -8,38 +8,86 @@ class DB extends Sql {
|
|
|
8
8
|
/**
|
|
9
9
|
* @description 数据库管理器
|
|
10
10
|
* @param {String} database 数据库名称
|
|
11
|
-
* @param {Object} run 查询函数
|
|
12
|
-
* @param {Object} exec 更改函数
|
|
13
11
|
*/
|
|
14
|
-
constructor(
|
|
15
|
-
super(run, exec);
|
|
12
|
+
constructor(sqlite) {
|
|
13
|
+
super(sqlite.run, sqlite.exec);
|
|
14
|
+
// 事务中
|
|
15
|
+
this.task = 0;
|
|
16
16
|
|
|
17
|
-
//
|
|
18
|
-
this.
|
|
17
|
+
// 事务SQL
|
|
18
|
+
this.task_sql = "";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 获取上级
|
|
22
|
+
* @return {Object} 返回上级管理器
|
|
23
|
+
*/
|
|
24
|
+
this.parent = function() {
|
|
25
|
+
return sqlite;
|
|
26
|
+
};
|
|
19
27
|
}
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
/**
|
|
23
|
-
*
|
|
24
|
-
* @
|
|
25
|
-
* @property {Promise|Number} 返回成功数
|
|
31
|
+
* 获取数据库名
|
|
32
|
+
* @return {String} 数据库
|
|
26
33
|
*/
|
|
27
|
-
DB.prototype.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
DB.prototype.database = function() {
|
|
35
|
+
return this.parent().config.database;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 构建新管理器
|
|
40
|
+
* @param {String} table 表名
|
|
41
|
+
* @param {String} key 键名
|
|
42
|
+
* @return {Object} 返回管理器
|
|
43
|
+
*/
|
|
44
|
+
DB.prototype.new = function(table, key) {
|
|
45
|
+
var db = this.parent().db();
|
|
46
|
+
db.table = table;
|
|
47
|
+
if (key) {
|
|
48
|
+
db.key = key;
|
|
49
|
+
} else {
|
|
50
|
+
var arr = table.split("_");
|
|
51
|
+
db.key = arr[arr.length - 1] + "_id";
|
|
36
52
|
}
|
|
37
|
-
return
|
|
53
|
+
return db;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 事务开始
|
|
58
|
+
*/
|
|
59
|
+
DB.prototype.start = async function(identifier = "point_1") {
|
|
60
|
+
this.task = 1;
|
|
61
|
+
return await this.exec("SET AUTOCOMMIT=0;BEGIN;");
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 提交
|
|
66
|
+
*/
|
|
67
|
+
DB.prototype.commit = async function() {
|
|
68
|
+
this.task = 2;
|
|
69
|
+
return this.exec("COMMIT;");
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 事务结束
|
|
74
|
+
*/
|
|
75
|
+
DB.prototype.end = async function() {
|
|
76
|
+
this.task = 3;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 滚回
|
|
81
|
+
*/
|
|
82
|
+
DB.prototype.back = async function(identifier = "point_1") {
|
|
83
|
+
this.task = 3;
|
|
84
|
+
await this.exec("ROLLBACK;");
|
|
85
|
+
return this.exec("SET AUTOCOMMIT=1;");
|
|
38
86
|
};
|
|
39
87
|
|
|
40
88
|
/**
|
|
41
89
|
* @description 获取所有表名
|
|
42
|
-
* @param {String} table
|
|
90
|
+
* @param {String} table 表名关键词, 可以 *table*包含、*table后缀、table*前缀 查询
|
|
43
91
|
* @return {Promise|Array} 表名数组
|
|
44
92
|
*/
|
|
45
93
|
DB.prototype.tables = async function(table) {
|
|
@@ -55,7 +103,7 @@ DB.prototype.tables = async function(table) {
|
|
|
55
103
|
* @param {String} table 表名
|
|
56
104
|
* @return {Promise|Array} 字段信息列表
|
|
57
105
|
*/
|
|
58
|
-
DB.prototype.fields = async function(table) {
|
|
106
|
+
DB.prototype.fields = async function(table, field_name) {
|
|
59
107
|
if (!table) {
|
|
60
108
|
table = this.table;
|
|
61
109
|
}
|
|
@@ -71,134 +119,336 @@ DB.prototype.fields = async function(table) {
|
|
|
71
119
|
|
|
72
120
|
/**
|
|
73
121
|
* @description 设置类型
|
|
74
|
-
* @param {String}
|
|
75
|
-
* @param {
|
|
76
|
-
* @
|
|
122
|
+
* @param {String} field 字段名
|
|
123
|
+
* @param {String} type 类型名,常用类型 mediumint, int, varchar, datetime
|
|
124
|
+
* @param {String} value 默认值
|
|
125
|
+
* @param {Boolean} not_null 是否非空字段 true为非空,false为可空
|
|
126
|
+
* @param {Boolean} auto 自动
|
|
127
|
+
* @return {String} 返回最终类型
|
|
77
128
|
*/
|
|
78
|
-
DB.prototype.setType = function(type, auto) {
|
|
129
|
+
DB.prototype.setType = function(field, type, value, not_null, auto) {
|
|
79
130
|
if (!type) {
|
|
80
|
-
type = '
|
|
131
|
+
type = 'integer';
|
|
81
132
|
}
|
|
82
133
|
switch (type) {
|
|
83
134
|
case "str":
|
|
84
135
|
case "varchar":
|
|
85
136
|
case "string":
|
|
86
|
-
type = "varchar(255)
|
|
137
|
+
type = "varchar(255)";
|
|
138
|
+
if (not_null) {
|
|
139
|
+
type += " NOT NULL";
|
|
140
|
+
}
|
|
141
|
+
if (value) {
|
|
142
|
+
type += " DEFAULT '" + value + "'";
|
|
143
|
+
} else {
|
|
144
|
+
type += " DEFAULT ''";
|
|
145
|
+
}
|
|
87
146
|
break;
|
|
88
|
-
case "int":
|
|
89
147
|
case "number":
|
|
90
|
-
type = "integer NOT NULL";
|
|
91
|
-
if (auto
|
|
148
|
+
type = "integer(11) NOT NULL";
|
|
149
|
+
if (auto) {
|
|
92
150
|
type += " autoincrement";
|
|
151
|
+
} else if (value) {
|
|
152
|
+
type += " DEFAULT " + value;
|
|
153
|
+
} else {
|
|
154
|
+
type += " DEFAULT 0";
|
|
93
155
|
}
|
|
94
156
|
break;
|
|
95
157
|
case "bool":
|
|
96
158
|
case "tinyint":
|
|
97
|
-
type = "tinyint(1) NOT NULL";
|
|
159
|
+
type = "tinyint(1) UNSIGNED NOT NULL";
|
|
160
|
+
if (value) {
|
|
161
|
+
type += " DEFAULT " + value;
|
|
162
|
+
} else {
|
|
163
|
+
type += " DEFAULT 0";
|
|
164
|
+
}
|
|
98
165
|
break;
|
|
99
|
-
|
|
166
|
+
case "datetime":
|
|
167
|
+
if (!value) {
|
|
168
|
+
value = "1970-01-01 00:00:00";
|
|
169
|
+
}
|
|
170
|
+
type += " DEFAULT '" + value + "'";
|
|
171
|
+
break;
|
|
172
|
+
case "timestamp":
|
|
173
|
+
if (auto) {
|
|
174
|
+
if (field.indexOf('update') !== -1 || field.indexOf('last') !== -1) {
|
|
175
|
+
type += " DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
|
|
176
|
+
} else {
|
|
177
|
+
type += " DEFAULT CURRENT_TIMESTAMP";
|
|
178
|
+
}
|
|
179
|
+
} else {
|
|
180
|
+
type += " DEFAULT CURRENT_TIMESTAMP";
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
case "date":
|
|
184
|
+
if (!value) {
|
|
185
|
+
value = "1970-01-01";
|
|
186
|
+
}
|
|
187
|
+
type += " NOT NULL DEFAULT '" + value + "'";
|
|
188
|
+
break;
|
|
189
|
+
case "time":
|
|
190
|
+
if (!value) {
|
|
191
|
+
value = "00:00:00";
|
|
192
|
+
}
|
|
193
|
+
type += " NOT NULL DEFAULT '" + value + "'";
|
|
194
|
+
break;
|
|
195
|
+
case "double":
|
|
196
|
+
if (type == "double") {
|
|
197
|
+
type = "double(10, 2)"
|
|
198
|
+
}
|
|
199
|
+
type += " NOT NULL";
|
|
200
|
+
if (value) {
|
|
201
|
+
type += " DEFAULT '" + value + "'";
|
|
202
|
+
} else {
|
|
203
|
+
type += " DEFAULT 0";
|
|
204
|
+
}
|
|
205
|
+
break;
|
|
206
|
+
case "float":
|
|
207
|
+
if (type == "float") {
|
|
208
|
+
type = "float(17, 8)"
|
|
209
|
+
}
|
|
100
210
|
type += " NOT NULL";
|
|
101
|
-
if (
|
|
102
|
-
|
|
211
|
+
if (value) {
|
|
212
|
+
type += " DEFAULT '" + value + "'";
|
|
213
|
+
} else {
|
|
214
|
+
type += " DEFAULT 0";
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
case "longtext":
|
|
218
|
+
case "text":
|
|
219
|
+
if (type == "text") {
|
|
220
|
+
type = "text NULL"
|
|
221
|
+
} else if (type == "longtext") {
|
|
222
|
+
type = "longtext NULL"
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
default:
|
|
226
|
+
if (type.indexOf('var') !== -1) {
|
|
227
|
+
if (not_null) {
|
|
228
|
+
type += " NOT NULL"
|
|
229
|
+
}
|
|
230
|
+
if (value) {
|
|
231
|
+
type += " DEFAULT '" + value + "'";
|
|
232
|
+
} else {
|
|
233
|
+
type += " DEFAULT ''";
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
type += " UNSIGNED NOT NULL";
|
|
237
|
+
if (auto) {
|
|
103
238
|
type += " autoincrement";
|
|
239
|
+
} else {
|
|
240
|
+
if (value) {
|
|
241
|
+
type += " DEFAULT '" + value + "'";
|
|
242
|
+
} else {
|
|
243
|
+
type += " DEFAULT 0";
|
|
244
|
+
}
|
|
104
245
|
}
|
|
105
246
|
}
|
|
106
247
|
break;
|
|
107
248
|
}
|
|
108
249
|
return type;
|
|
109
|
-
}
|
|
250
|
+
};
|
|
110
251
|
|
|
111
252
|
/**
|
|
112
253
|
* @description 创建数据表
|
|
113
254
|
* @param {String} table 表名
|
|
114
255
|
* @param {String} field 主键字段名
|
|
115
|
-
* @param {String} type
|
|
116
|
-
* @param {Boolean} auto 是否自增字段, 默认为自增字段
|
|
117
|
-
* @
|
|
256
|
+
* @param {String} type 类型名,常用类型 mediumint, int, varchar
|
|
257
|
+
* @param {Boolean} auto 是否自增字段, 默认为自增字段
|
|
258
|
+
* @param {String} commit 注释
|
|
259
|
+
* @return {Promise|Number} 创建成功返回1,失败返回0
|
|
118
260
|
*/
|
|
119
|
-
DB.prototype.addTable = async function(table, field, type, auto) {
|
|
261
|
+
DB.prototype.addTable = async function(table, field, type, auto, commit = '') {
|
|
120
262
|
if (!field) {
|
|
121
263
|
field = "id";
|
|
122
264
|
}
|
|
123
|
-
var
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
265
|
+
var sql = "CREATE TABLE IF NOT EXISTS `{0}` (`{1}` {2})".replace('{0}', table).replace(
|
|
266
|
+
'{1}', field).replace('{2}', this.setType(field, type, null, true, auto) + ' PRIMARY KEY');
|
|
267
|
+
if (commit) {
|
|
268
|
+
sql += " COMMENT = '" + commit + "';"
|
|
269
|
+
} else {
|
|
270
|
+
sql += ";"
|
|
271
|
+
}
|
|
272
|
+
var bl = await this.exec(sql);
|
|
273
|
+
return bl;
|
|
127
274
|
};
|
|
128
275
|
|
|
129
276
|
/**
|
|
130
|
-
* @description
|
|
277
|
+
* @description 清空数据表
|
|
278
|
+
* @param {Boolean} reset 是否重置自增ID
|
|
279
|
+
* @return {Promise|Number} 清空成功返回1,失败返回0
|
|
280
|
+
*/
|
|
281
|
+
DB.prototype.clearTable = async function(reset = true, table = '') {
|
|
282
|
+
var sql = reset ? "TRUNCATE table `{0}`;" : "DELETE FROM `{0}`";
|
|
283
|
+
var bl = await this.exec(sql.replace('{0}', table || this.table));
|
|
284
|
+
return bl;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* @description 添加字段
|
|
131
289
|
* @param {String} field 字段名
|
|
132
|
-
* @param {String} type
|
|
133
|
-
* @param {String|Number}
|
|
290
|
+
* @param {String} type 类型名,常用类型 mediumint, int, float, double, varchar, tinyint, text, date, datetime, time, timestamp
|
|
291
|
+
* @param {String|Number} value 默认值
|
|
292
|
+
* @param {Boolean} not_null 是否非空字段 true为非空,false为可空
|
|
293
|
+
* @param {Boolean} auto 是否自动(如果为数字类型则自增增段,如果为时间类型则默认事件)
|
|
134
294
|
* @param {Boolean} isKey 是否主键
|
|
135
|
-
* @return {Promise|
|
|
295
|
+
* @return {Promise|Number} 添加成功返回1,失败返回0
|
|
136
296
|
*/
|
|
137
|
-
DB.prototype.field_add = async function(field, type, value, isKey) {
|
|
297
|
+
DB.prototype.field_add = async function(field, type, value, not_null, auto, isKey) {
|
|
138
298
|
var sql =
|
|
139
|
-
"select count(*) as `count` from sqlite_master where `type` = 'table' and `name` = '{
|
|
140
|
-
sql = sql.replace('{
|
|
299
|
+
"select count(*) as `count` from sqlite_master where `type` = 'table' and `name` = '{1}' and `sql` like '%{2}%'";
|
|
300
|
+
sql = sql.replace('{1}', this.table).replace('{2}', field);
|
|
141
301
|
var arr = await this.run(sql);
|
|
142
302
|
if (arr && arr.length > 0) {
|
|
143
303
|
if (arr[0].count == 0) {
|
|
144
|
-
var type = this.setType(type,
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
type += " DEFAULT '" + value + "'";
|
|
148
|
-
} else {
|
|
149
|
-
type += " DEFAULT " + value;
|
|
150
|
-
}
|
|
151
|
-
} else if (type.has('varchar') || type.has('text')) {
|
|
152
|
-
type = type.replace('NOT NULL', '');
|
|
153
|
-
} else if (type.has('dateTime')) {
|
|
154
|
-
type += " DEFAULT '1970-01-01 00:00:00'";
|
|
155
|
-
} else if (type.has('date')) {
|
|
156
|
-
type += " DEFAULT '1970-01-01'";
|
|
157
|
-
} else if (type.has('time')) {
|
|
158
|
-
type += " DEFAULT '00:00:00'";
|
|
159
|
-
} else {
|
|
160
|
-
type += " DEFAULT 0";
|
|
161
|
-
}
|
|
162
|
-
var sql = "ALTER Table `{0}` ADD `{1}` {2}".replace('{0}', this.table).replace('{1}', field).replace('{2}',
|
|
163
|
-
type);
|
|
304
|
+
var type = this.setType(field, type, value, not_null, auto);
|
|
305
|
+
var sql = "ALTER Table `{0}` ADD `{1}` {2}";
|
|
306
|
+
sql = sql.replace('{0}', this.table).replace('{1}', field).replace('{2}', type);
|
|
164
307
|
if (isKey) {
|
|
165
|
-
sql += ", ADD PRIMARY KEY (`
|
|
308
|
+
sql += " , ADD PRIMARY KEY (`" + field + "`)";
|
|
309
|
+
} else {
|
|
310
|
+
sql += ";";
|
|
166
311
|
}
|
|
167
312
|
return await this.exec(sql);
|
|
168
313
|
}
|
|
169
314
|
}
|
|
170
|
-
return
|
|
315
|
+
return 0;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* @description 修改字段
|
|
320
|
+
* @param {String} field 字段名
|
|
321
|
+
* @param {String} type 类型名,常用类型 mediumint, int, float, double, varchar, tinyint, text, date, datetime, time, timestamp
|
|
322
|
+
* @param {String|Number} value 默认值
|
|
323
|
+
* @param {Boolean} not_null 是否非空字段 true为非空,false为可空
|
|
324
|
+
* @param {Boolean} auto 是否自动(如果为数字类型则自增增段,如果为时间类型则默认事件)
|
|
325
|
+
* @param {Boolean} isKey 是否主键
|
|
326
|
+
* @param {String} new_name 新名称
|
|
327
|
+
* @return {Promise|Number} 修改成功返回1,失败返回0
|
|
328
|
+
*/
|
|
329
|
+
DB.prototype.field_set = async function(field, type, value, not_null, auto, isKey, new_name) {
|
|
330
|
+
var sql =
|
|
331
|
+
"select count(*) as `count` from sqlite_master where `type` = 'table' and `name` = '{1}' and `sql` like '%{2}%'";
|
|
332
|
+
sql = sql.replace('{1}', this.table).replace('{2}', field);
|
|
333
|
+
console.log("来", sql);
|
|
334
|
+
var arr = await this.run(sql);
|
|
335
|
+
if (arr && arr.length > 0) {
|
|
336
|
+
if (arr[0].count == 0) {
|
|
337
|
+
return 0;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
var type = this.setType(field, type, value, not_null, auto);
|
|
342
|
+
if (type.has('text')) {
|
|
343
|
+
type = type.replace('NOT NULL', '').replace("DEFAULT ''", "");
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
if (!new_name) {
|
|
347
|
+
new_name = field;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
sql = "ALTER TABLE `{0}` ADD COLUMN `{2}` {3}";
|
|
351
|
+
sql = sql.replace('{0}', this.table).replace('{2}', new_name).replace('{3}', type);
|
|
352
|
+
if (isKey) {
|
|
353
|
+
sql += ", DROP PRIMARY KEY, ADD PRIMARY KEY (" + new_name + ") USING BTREE;"
|
|
354
|
+
} else {
|
|
355
|
+
sql += ";";
|
|
356
|
+
}
|
|
357
|
+
await this.exec(sql);
|
|
358
|
+
|
|
359
|
+
sql = "UPDATE TABLE `{0}` SET `{2}` = `{1}`;";
|
|
360
|
+
sql = sql.replace('{0}', this.table).replace('{1}', field).replace('{2}', new_name);
|
|
361
|
+
var ret = await this.exec(sql);
|
|
362
|
+
|
|
363
|
+
if (field !== new_name) {
|
|
364
|
+
// sql = "ALTER TABLE `{0}` DROP COLUMN `{1}`;";
|
|
365
|
+
// sql = sql.replace('{0}', this.table).replace('{1}', field);
|
|
366
|
+
// ret = await this.exec(sql);
|
|
367
|
+
// console.log("来了3", this.sql);
|
|
368
|
+
}
|
|
369
|
+
return ret;
|
|
370
|
+
// sql = "ALTER TABLE `{0}` CHANGE COLUMN `{1}` `{2}` {3}";
|
|
371
|
+
// sql = sql.replace('{0}', this.table).replace('{1}', field).replace('{2}', new_name).replace('{3}', type);
|
|
372
|
+
// if (isKey) {
|
|
373
|
+
// sql += ", DROP PRIMARY KEY, ADD PRIMARY KEY (" + new_name + ") USING BTREE;"
|
|
374
|
+
// } else {
|
|
375
|
+
// sql += ";";
|
|
376
|
+
// }
|
|
377
|
+
// return await this.exec(sql);
|
|
171
378
|
};
|
|
172
379
|
|
|
173
380
|
/**
|
|
174
381
|
* @description 删除字段
|
|
175
382
|
* @param {String} field 字段名
|
|
383
|
+
* @return {Promise|Number} 删除成功返回1,失败返回0
|
|
176
384
|
*/
|
|
177
385
|
DB.prototype.field_del = async function(field) {
|
|
178
|
-
var
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (bl) {
|
|
191
|
-
bl = await this.exec("alter table `mm_temp` rename to `{0}`;".replace('{0}', this.table));
|
|
192
|
-
if (bl) {
|
|
193
|
-
this.error = undefined;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
} else {
|
|
386
|
+
var sql =
|
|
387
|
+
"select count(*) as `count` from sqlite_master where `type` = 'table' and `name` = '{1}' and `sql` like '%{2}%'";
|
|
388
|
+
sql = sql.replace('{1}', this.table).replace('{2}', field);
|
|
389
|
+
var arr = await this.run(sql);
|
|
390
|
+
if (arr && arr.length > 0) {
|
|
391
|
+
if (arr[0].count > 0) {
|
|
392
|
+
sql = "ALTER Table `{0}` rename to `{1}`;".replace('{0}', this.table).replace('{1}', field);
|
|
393
|
+
return await this.exec(sql);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return 0;
|
|
397
|
+
};
|
|
197
398
|
|
|
399
|
+
/**
|
|
400
|
+
* @description 拼接字段信息SQL
|
|
401
|
+
* @param {Object} fd 字段信息
|
|
402
|
+
* @return {String} sql语段
|
|
403
|
+
*/
|
|
404
|
+
DB.prototype.field_sql = function(fd) {
|
|
405
|
+
var sql = "`{0}`".replace('{0}', fd.name);
|
|
406
|
+
sql += " " + fd.type;
|
|
407
|
+
if (fd.notnull) {
|
|
408
|
+
sql += " NOT NULL";
|
|
409
|
+
}
|
|
410
|
+
if (fd.auto) {
|
|
411
|
+
if (fd.dflt_value) {
|
|
412
|
+
if (fd.dflt_value === '0000-00-00 00:00:00') {
|
|
413
|
+
fd.dflt_value = 'CURRENT_TIMESTAMP';
|
|
414
|
+
}
|
|
415
|
+
sql += " DEFAULT " + fd.dflt_value;
|
|
198
416
|
}
|
|
199
|
-
|
|
417
|
+
sql += " " + fd.auto;
|
|
418
|
+
} else if (fd.dflt_value) {
|
|
419
|
+
if (fd.dflt_value === '0000-00-00 00:00:00') {
|
|
420
|
+
fd.dflt_value = '1970-01-01 00:00:00';
|
|
421
|
+
}
|
|
422
|
+
sql += " DEFAULT " + fd.dflt_value;
|
|
423
|
+
}
|
|
424
|
+
if (fd.note) {
|
|
425
|
+
sql += " COMMENT '" + fd.note + "'";
|
|
426
|
+
}
|
|
427
|
+
if (fd.pk) {
|
|
428
|
+
sql += " PRIMARY KEY";
|
|
429
|
+
}
|
|
430
|
+
return sql;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* @description 修改字段名称
|
|
435
|
+
* @param {String} field 字段名
|
|
436
|
+
* @param {String} name 变更后名称
|
|
437
|
+
* @return {Promise|Number} 修改成功返回1,失败返回0
|
|
438
|
+
*/
|
|
439
|
+
DB.prototype.field_name = async function(field, name) {
|
|
440
|
+
var fields = await this.fields(this.table, field);
|
|
441
|
+
if (!fields) {
|
|
442
|
+
return -1;
|
|
443
|
+
}
|
|
444
|
+
if (fields.length === 0) {
|
|
445
|
+
return 0;
|
|
200
446
|
}
|
|
201
|
-
|
|
447
|
+
var sql_sub = this.field_sql(fields[0]);
|
|
448
|
+
sql_sub = sql_sub.replace("`" + field + "`", "`" + name + "`").replace(' PRIMARY KEY', '');
|
|
449
|
+
var sql = "alter table `{0}` change `{1}` " + sql_sub;
|
|
450
|
+
sql = sql.replace('{0}', this.table).replace('{1}', field);
|
|
451
|
+
return await this.exec(sql);
|
|
202
452
|
};
|
|
203
453
|
|
|
204
|
-
exports.DB = DB;
|
|
454
|
+
exports.DB = DB;
|
package/index.js
CHANGED
|
@@ -4,6 +4,8 @@ const {
|
|
|
4
4
|
DB
|
|
5
5
|
} = require('./db');
|
|
6
6
|
|
|
7
|
+
const Link_model = require('./link_model');
|
|
8
|
+
|
|
7
9
|
var pool = {};
|
|
8
10
|
|
|
9
11
|
/// 数据库封装
|
|
@@ -59,8 +61,10 @@ class Sqlite {
|
|
|
59
61
|
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
60
62
|
*/
|
|
61
63
|
this.run = function(sql, val) {
|
|
64
|
+
sql = sql.replaceAll("&&", "AND").replaceAll("||", "OR").replaceAll("varchar", "text").replaceAll(" int", " integer");
|
|
62
65
|
var _this = this;
|
|
63
66
|
this.sql = sql;
|
|
67
|
+
|
|
64
68
|
// 返回一个 Promise
|
|
65
69
|
return new Promise((resolve, reject) => {
|
|
66
70
|
$this.conn.all(sql, val, function(err, rows) {
|
|
@@ -70,15 +74,15 @@ class Sqlite {
|
|
|
70
74
|
code: err.errno,
|
|
71
75
|
message: $.info(err).between('Error: ', ']')
|
|
72
76
|
};
|
|
73
|
-
resolve(rows);
|
|
77
|
+
resolve(rows || []);
|
|
74
78
|
} else {
|
|
75
79
|
_this.error = undefined;
|
|
76
|
-
resolve(rows);
|
|
80
|
+
resolve(rows || []);
|
|
77
81
|
}
|
|
78
82
|
});
|
|
79
83
|
});
|
|
80
84
|
};
|
|
81
|
-
|
|
85
|
+
|
|
82
86
|
/**
|
|
83
87
|
* @description 增删改sql
|
|
84
88
|
* @param {String} sql 查询参
|
|
@@ -86,6 +90,7 @@ class Sqlite {
|
|
|
86
90
|
* @return {Promise|Array} 异步构造器, 当await时返回执行结果
|
|
87
91
|
*/
|
|
88
92
|
this.exec = function(sql) {
|
|
93
|
+
sql = sql.replaceAll("&&", "AND").replaceAll("||", "OR").replaceAll("varchar", "text").replaceAll(" int", " integer");
|
|
89
94
|
var _this = this;
|
|
90
95
|
this.sql = sql;
|
|
91
96
|
// 返回一个 Promise
|
|
@@ -105,12 +110,12 @@ class Sqlite {
|
|
|
105
110
|
});
|
|
106
111
|
});
|
|
107
112
|
};
|
|
108
|
-
|
|
113
|
+
|
|
109
114
|
/**
|
|
110
115
|
* @description 获取数据库管理器
|
|
111
116
|
*/
|
|
112
117
|
this.db = function() {
|
|
113
|
-
return new DB($this
|
|
118
|
+
return new DB($this);
|
|
114
119
|
};
|
|
115
120
|
}
|
|
116
121
|
}
|
|
@@ -151,6 +156,7 @@ Sqlite.prototype.open = function() {
|
|
|
151
156
|
}
|
|
152
157
|
this.conn = pool[this.identifier];
|
|
153
158
|
};
|
|
159
|
+
|
|
154
160
|
/**
|
|
155
161
|
* @description 关闭连接
|
|
156
162
|
*/
|
|
@@ -161,8 +167,31 @@ Sqlite.prototype.close = function() {
|
|
|
161
167
|
}
|
|
162
168
|
};
|
|
163
169
|
|
|
164
|
-
|
|
170
|
+
/**
|
|
171
|
+
* 获取数据库管理器
|
|
172
|
+
* @param {String} key 主键
|
|
173
|
+
* @param {String|Number} value 对象值
|
|
174
|
+
* @param {Boolean} clear_prefix 清除前缀
|
|
175
|
+
* @param {Array} arr_table 关联的数据表
|
|
176
|
+
* @return {Object} 管理模型
|
|
177
|
+
*/
|
|
178
|
+
Sqlite.prototype.dbs = async function(key, value, clear_prefix, ...arr_table) {
|
|
179
|
+
var lm = new Link_model({
|
|
180
|
+
key,
|
|
181
|
+
value,
|
|
182
|
+
clear_prefix
|
|
183
|
+
});
|
|
184
|
+
lm.sql = this;
|
|
185
|
+
for (var i = 0; i < arr_table.length; i++) {
|
|
186
|
+
await lm.add(arr_table[i]);
|
|
187
|
+
}
|
|
188
|
+
return lm;
|
|
189
|
+
};
|
|
165
190
|
|
|
191
|
+
/**
|
|
192
|
+
* @description 导出Sqlite帮助类
|
|
193
|
+
*/
|
|
194
|
+
exports.Sqlite = Sqlite;
|
|
166
195
|
|
|
167
196
|
/**
|
|
168
197
|
* @description sqlite连接池
|
|
@@ -170,8 +199,9 @@ exports.Sqlite = Sqlite;
|
|
|
170
199
|
if (!$.pool.sqlite) {
|
|
171
200
|
$.pool.sqlite = {};
|
|
172
201
|
}
|
|
202
|
+
|
|
173
203
|
/**
|
|
174
|
-
* @description
|
|
204
|
+
* @description 缓存管理器,用于创建缓存
|
|
175
205
|
* @param {String} scope 作用域
|
|
176
206
|
* @param {String} dir 当前路径
|
|
177
207
|
* @return {Object} 返回一个缓存类
|
|
@@ -189,6 +219,6 @@ function sqlite_admin(scope, dir) {
|
|
|
189
219
|
}
|
|
190
220
|
|
|
191
221
|
/**
|
|
192
|
-
* @
|
|
222
|
+
* @description 导出Sqlite管理函数
|
|
193
223
|
*/
|
|
194
224
|
exports.sqlite_admin = sqlite_admin;
|