-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCsql.js
325 lines (221 loc) · 7.15 KB
/
PCsql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const whereCompare = {
//等于
"=": function (a, b) {
return a == b;
},
//大于
">": function (a, b) {
return a > b;
},
//大于等于
">=": function (a, b) {
return a >= b;
},
//小于
"<": function (a, b) {
return a < b;
},
//小于等于
"<=": function (a, b) {
return a <= b;
},
//不等于
"!=": function (a, b) {
return a != b;
},
//模糊查询
"like": function (a, b) {
return new RegExp(b, "i").test(a);
}
}
class Sql {
constructor(dbName) {
/*
本地缓存 => 为了提高资源利用率
1: 先将数据保存到本地模拟缓存 cache 中
2: 最后在 save 里进行数据库储存操作
*/
let db = {
dbName,
cache: {
add: {
data: []
}
}
}
// 将数据库与this绑定合并一起
Object.assign(this, db);
}
//静态方法查询本地数据库存在资源
static getSql(dbName) {
let db = window.localStorage.getItem(dbName) || [];
// console.log(db)
if (typeof db == "string") {
return JSON.parse(db);
} else {
return db;
}
}
//更新方法
updata(Obj) {
if (/object/i.test(Obj)) {
// console.log(Obj);
if (!this.whereFn) {
throw Error("请在此操作之前,进行where()操作!!");
} else {
this.cache.updata = {
data: Obj,
where: this.whereFn
}
}
} else {
throw Error("updata 只支持传入对象");
}
return this;
}
//数据库插入操作
add(items) {
// console.log(items);
// 参数设计为对象 如果用户给我非对象的结构 throw Error("参数类型错误,")
//由此 需要判断参数 的传入类型
if (Array.isArray(items)) { //参数类型 支持 [{...},{...}]
//利用递归解析数组内部元素
items.map(item => {
this.add(item);
})
} else if (/object/.test(typeof items)) { //参数为对象时
// console.log("传入参数为对象");
//将合格的 数据插入到数据缓存中待定
this.cache.add.data.push(items);
} else {
throw Error("传入参数不在参数支持类型分为内")
}
return this; //方便链式操作
}
//删除数据项
del() {
if (!this.whereFn) {
throw Error("请在此操作之前,进行where()操作!!");
} else {
this.cache.del = {
where: this.whereFn
}
}
return this;
}
//构造查询器 => where 只是构造查询函数的作用 给予 select 和 find 方法调用
where(...res) {
//参数范围为 2~3 位
if (res.length >= 2 && res.length <= 3) {
let [key, compare, value] = res;
if (value == undefined) {
value = compare;
compare = "="
}
// console.log(key,compare,value);
//排除 异端查询 嘿嘿嘿
if (!whereCompare[compare]) {
throw Error(`where方法不支持${compare}查询`);
} else {
//利用闭包 事先导入查询所需的参数 key,value [赋予其访问该变量的权限] => 构建查询函数
this.whereFn = (item) => {
return whereCompare[compare](item[key], value); //该出代码解析如下 =>
/*
当 compare == "=" => true 成立时
"=": function(a,b){
return a == b;
},
执行获取 [ a == b]; 该代码片段
*/
}
}
// console.log(this.whereFn)
} else {
throw Error("参数个数不正确,正确示范=>where('a','like',1)||where('a',1)");
}
return this;
}
//排序方法 构造排序方法
order(key, sort = "asc") {
//默认正序
// console.log("倒序=>desc 正序=> asc ");
this.sortFn = (a, b) => {
return /desc/i.test(sort) ? b[key] - a[key] : a[key] - b[key];
}
return this;
}
//设置用户所需数据的量 参数 2位 limit(起点,[步长]) 构造截取数据函数
limit(start, step) {
this.limitFn = (Arr) => {
// 传参为一个时
if (step === undefined) {
step = start;
start = 0;
} else { //传参为 2 个时
--start;
step += start;
}
return Arr.slice(start, step)
}
return this;
}
//查询多条数据
select() {
//获取数据库的内容
let list = Sql.getSql(this.dbName);
// console.log(list)
//利用 数组自带的 filter 过滤掉没有用的数据
let res = list.filter(this.whereFn) || [];
// console.log(this.sortFn);
//数组截取
this.limitFn && (res = this.limitFn(res));
// console.log(res)
//数组排序
this.sortFn && res.sort(this.sortFn);
// console.log(res)
return res;
}
//where 构建查询 函数后 进行数据逐条整理 返回给用户 查询单条
find() {
//数据库查询的条目
let list = Sql.getSql(this.dbName);
// console.log(list)
if (!this.whereFn) {
throw Error("缺少匹配条件,请先进行 .where(...) 操作在用 find()")
}
//如果匹配不到数据 则返回一个空数组
let res = list.find(this.whereFn) || [];
// res => 匹配到的结果 是一个 Array
// console.log("主人!查询数据如下");
// console.log(res)
//后期可对 res 做各种逻辑处理
return res;
}
//保存阶段
save() {
let list = Sql.getSql(this.dbName);
// 检查是否存在删除函数
if (this.cache.del) {
list = list.filter(item => {
return !this.cache.del.where(item);
});
}
//检查是否存在更新函数
if (this.cache.updata) {
list.map(item => {
if (this.cache.updata.where(item)) { //判断是否符合查询内容
Object.assign(item, this.cache.updata.data)
}
})
}
list.push(...this.cache.add.data)
if (list.length == 0) {
throw Error("不可插入空的数据")
}
window.localStorage.setItem(this.dbName, JSON.stringify(list));
//存入数据库后 清除数据缓存
this.cache.add.data = [];
console.log("主人!数据以存入数据库,缓存清空完成!")
return this;
}
}