-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.dba.js
69 lines (61 loc) · 1.73 KB
/
mod.dba.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
/* global sqlite */
const DBA = function (db) {
var T = this;
var t;
if (typeof db === "string") {
t = true;
}
T.get = function (table, cb) {
var q = "select * from " + table;
if (t) {
sqlite().query(db, q, function (r) {
cb(r);
});
return;
}
var r = db.query(q);
cb(r);
};
T.query = function (table, key, val) {
if (typeof val === "string") {
val = "'" + val + "'";
}
var q = "select * from " + table + " where " + key + "=" + val;
if (t) {
sqlite().query(db, q, function (r) {
cb(r);
});
return;
}
return db.query(q);
};
T.insert = function (table, data, cb) {
cb = cb || function () {};
var s = "INSERT INTO " + table + "(";
var v = "VALUES(";
data.forEach(function (k, i) {
s += k + ",";
if (typeof data[k] === "string") {
v += "'" + (data[k]) + "',";
} else {
if (isNaN(data[k]) || typeof data[k] === "undefined" || data[k] === null) {
v += ",";
} else {
v += "" + (data[k]).toString() + ",";
}
}
});
if (v.slice(-1) === ",") {
v = v.slice(0, -1);
}
var query = s.replaceAt(s.length - 1, ')') + " " + v + ')' + ";";
if (t) {
sqlite().query(db, query, function (r) {
cb(r);
});
return;
}
return db.query(query);
};
return T;
};