Skip to content

Commit d3981a6

Browse files
committed
ow.ch: db to dbold a new db implementation
1 parent 5760594 commit d3981a6

File tree

1 file changed

+59
-20
lines changed

1 file changed

+59
-20
lines changed

js/owrap.ch.js

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ OpenWrap.ch.prototype.__types = {
139139
this.__channels[aName].remove(ak);
140140
}
141141
},
142-
db2: {
142+
db: {
143143
__options: {},
144144
create: function(aName, shouldCompress, options) {
145145
options = _$(options, "options").isMap().default({});
@@ -208,19 +208,36 @@ OpenWrap.ch.prototype.__types = {
208208
}*/
209209
},
210210
set: function(aName, aK, aV, aTimestamp, x) {
211-
/*var i = this.get(aName, aK);
211+
var options = this.__options[aName];
212+
213+
var i = this.get(aName, aK);
212214
try {
215+
var wset = [], wku = [], wv = [], wk = [];
216+
213217
if (isDef(i)) {
214-
this.__db[aName].us("update " + this.__table[aName] + " set value = ?, ts = ? where key = ?", [stringify(aV), aTimestamp, stringify(aK)], true);
218+
for(var ii in aV) {
219+
wset.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
220+
wv.push(aV[ii]);
221+
}
222+
for(var ii in aK) {
223+
if (isDef(options.keys) && options.keys.indexOf((options.cs ? "\"" + ii + "\"" : ii)) < 0) continue;
224+
wku.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
225+
wv.push(aK[ii]);
226+
}
227+
options.db.us("update " + options.from + " set " + wset.join(", ") + " where " + wku.join(" AND "), wv, true);
215228
} else {
216-
this.__db[aName].us("insert into " + this.__table[aName] + " (key, ts, value) values (?, ?, ?)", [stringify(aK), aTimestamp, stringify(aV)], true);
229+
for(var ii in aV) {
230+
wk.push((options.cs ? "\"" + ii + "\"" : ii));
231+
wv.push(aV[ii]);
232+
}
233+
options.db.us("insert into " + options.from + " (" + wk.join(", ") + ") values (" + wv.map(r => "?").join(", ") + ")", wv, true);
217234
}
218-
this.__db[aName].commit();
235+
options.db.commit();
219236
} catch(e) {
220-
this.__db[aName].rollback();
237+
options.db.rollback();
221238
throw e;
222239
}
223-
return aK;*/
240+
return aK;
224241
},
225242
setAll: function(aName, aKs, aVs, aTimestamp) {
226243
for(var i in aVs) {
@@ -234,51 +251,73 @@ OpenWrap.ch.prototype.__types = {
234251
},
235252
get: function(aName, aK, x) {
236253
var options = this.__options[aName];
237-
var lst = (isDef(options.keys) ? options.keys.join(", ") : "*");
238-
var w = "";
254+
var lst = "*";
255+
var w = [], wv = [];
239256
for(var ii in aK) {
240-
w += " " + (options.cs ? "\"" + ii + "\"" : ii) + " = " + (isNumber(aK[ii]) ? aK[ii] : "'" + aK[ii] + "'");
241-
w += ",";
257+
if (isDef(options.keys) && options.keys.indexOf((options.cs ? "\"" + ii + "\"" : ii)) < 0) continue;
258+
w.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
259+
wv.push(aK[ii]);
242260
}
243-
w = w.substr(0, w.length -1);
244261

245262
var res;
246263
try {
247-
var res = options.db.q("select " + lst + " from " + options.from + " where " + w);
248-
if (isDef(res) && isArray(res) && res.length > 0) {
249-
return res[0];
264+
var res = options.db.qs("select " + lst + " from " + options.from + " where " + w.join(" and "), wv, true);
265+
if (isDef(res) && isArray(res.results) && res.results.length > 0) {
266+
return res.results[0];
250267
} else {
251268
return void 0;
252269
}
253270
} catch(e) {
254271
return String(e);
255272
}
273+
},
274+
getAll: function(aName, full) {
275+
var options = this.__options[aName];
276+
var res = [], wv = [], wk = [], w = "";
277+
if (isDef(full) && isMap(full)) {
278+
for(var ii in full) {
279+
wk.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
280+
wv.push(full[ii]);
281+
}
282+
w = " where " + wk.join(" and ");
283+
}
256284

285+
try {
286+
res = options.db.qs("select * from " + options.from + w, wv, true).results;
287+
} catch(e) {
288+
return String(e);
289+
}
257290
return res;
258-
},
291+
},
259292
pop: function(aName) {
260293
var aKs = this.getSortedKeys(aName);
261294
var aK = aKs[aKs.length - 1];
262-
var aV = this.get(aName, aK);
263295
return aK;
264296
},
265297
shift: function(aName) {
266298
var aK = this.getSortedKeys(aName)[0];
267-
var aV = this.get(aName, aK);
268299
return aK;
269300
},
270301
unset: function(aName, aK, aTimestamp) {
271302
var options = this.__options[aName];
303+
304+
var w = [], wv = [];
305+
for(var ii in aK) {
306+
if (isDef(options.keys) && options.keys.indexOf((options.cs ? "\"" + ii + "\"" : ii)) < 0) continue;
307+
w.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
308+
wv.push(aK[ii]);
309+
}
310+
272311
try {
273-
options.db.us("delete " + options.from + " where key = ?", [stringify(aK)], true);
312+
options.db.us("delete " + options.from + " where " + w.join(" and "), wv, true);
274313
options.db.commit();
275314
} catch(e) {
276315
options.db.rollback();
277316
throw e;
278317
}
279318
}
280319
},
281-
db: {
320+
dbOld: {
282321
__db: {},
283322
__table: {},
284323
create: function(aName, shouldCompress, options) {

0 commit comments

Comments
 (0)