Skip to content

Commit 8dd525d

Browse files
authored
Merge pull request #164 from OpenAF/db2
ow.ch.db
2 parents d31913b + 4718380 commit 8dd525d

File tree

1 file changed

+192
-0
lines changed

1 file changed

+192
-0
lines changed

js/owrap.ch.js

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,199 @@ OpenWrap.ch.prototype.__types = {
139139
this.__channels[aName].remove(ak);
140140
}
141141
},
142+
// DB channel implementation
143+
//
144+
/**
145+
* <odoc>
146+
* <key>ow.ch.types.db</key>
147+
* This OpenAF implementation wraps access to a db table. The creation options are:\
148+
* \
149+
* - db (Database) The database object to access the database table.\
150+
* - from (String) The name of the database table or object (don't use double quotes).\
151+
* - keys (Array) An array of fields keys to use (don't use double quotes).\
152+
* - cs (Boolean) Determines if the database is case sensitive for table and field names (defaults to false).\
153+
* \
154+
* </odoc>
155+
*/
142156
db: {
157+
__options: {},
158+
create: function(aName, shouldCompress, options) {
159+
options = _$(options, "options").isMap().default({});
160+
161+
_$(options.db, "options.db").$_();
162+
options.from = _$(options.from, "options.from").$_();
163+
options.keys = _$(options.keys, "options.keys").isArray().default(void 0);
164+
165+
options.cs = _$(options.cs, "options.cs").isBoolean().default(false);
166+
if (options.cs && options.from.trim().length > 0 && options.from.trim()[0] != "(") {
167+
options.from = "\"" + options.from + "\"";
168+
};
169+
170+
if (options.cs && isDef(options.keys)) {
171+
options.keys = options.keys.map(k => "\"" + k + "\"");
172+
}
173+
174+
this.__options[aName] = options;
175+
},
176+
destroy: function(aName) {
177+
delete this.__options[aName];
178+
},
179+
size: function(aName) {
180+
var options = this.__options[aName];
181+
try {
182+
var res = options.db.q("select count(1) as C from " + options.from);
183+
return Number(res.results[0].C);
184+
} catch(e) {
185+
return String(e);
186+
}
187+
},
188+
forEach: function(aName, aFunction, x) {
189+
var i = this.getKeys(aName);
190+
for(var j in i) {
191+
aFunction(i[j], this.get(aName, i[j]));
192+
}
193+
},
194+
getKeys: function(aName, full) {
195+
var options = this.__options[aName];
196+
full = _$(full, "extra").isString().default(void 0);
197+
198+
var lst = (isDef(options.keys) ? options.keys.join(", ") : "*");
199+
200+
try {
201+
var res = options.db.q("select " + lst + " from " + options.from + (isDef(full) ? " where " + full : ""));
202+
return res.results;
203+
} catch(e) {
204+
return String(e);
205+
}
206+
},
207+
getSortedKeys: function(aName, full) {
208+
return this.getKeys(aName, full);
209+
},
210+
getSet: function getSet(aName, aMatch, aK, aV, aTimestamp) {
211+
/*var res;
212+
try {
213+
res = this.__db[aName].qs("select key from " + this.__table[aName] + " where key = ? for update", [stringify(aK)], true).results[0].KEY;
214+
if (isDef(res) && ($stream([JSON.parse(res)]).anyMatch(aMatch)) ) {
215+
this.set(aName, aK, aV, aTimestamp);
216+
}
217+
this.__db[aName].commit();
218+
return res;
219+
} catch(e) {
220+
this.__db[aName].rollback();
221+
throw e;
222+
}*/
223+
},
224+
set: function(aName, aK, aV, aTimestamp, x) {
225+
var options = this.__options[aName];
226+
227+
var i = this.get(aName, aK);
228+
try {
229+
var wset = [], wku = [], wv = [], wk = [];
230+
231+
if (isDef(i)) {
232+
for(var ii in aV) {
233+
wset.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
234+
wv.push(aV[ii]);
235+
}
236+
for(var ii in aK) {
237+
if (isDef(options.keys) && options.keys.indexOf((options.cs ? "\"" + ii + "\"" : ii)) < 0) continue;
238+
wku.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
239+
wv.push(aK[ii]);
240+
}
241+
options.db.us("update " + options.from + " set " + wset.join(", ") + " where " + wku.join(" AND "), wv, true);
242+
} else {
243+
for(var ii in aV) {
244+
wk.push((options.cs ? "\"" + ii + "\"" : ii));
245+
wv.push(aV[ii]);
246+
}
247+
options.db.us("insert into " + options.from + " (" + wk.join(", ") + ") values (" + wv.map(r => "?").join(", ") + ")", wv, true);
248+
}
249+
options.db.commit();
250+
} catch(e) {
251+
options.db.rollback();
252+
throw e;
253+
}
254+
return aK;
255+
},
256+
setAll: function(aName, aKs, aVs, aTimestamp) {
257+
for(var i in aVs) {
258+
this.set(aName, ow.loadObj().filterKeys(aKs, aVs[i]), aVs[i], aTimestamp);
259+
}
260+
},
261+
unsetAll: function(aName, aKs, aVs, aTimestamp) {
262+
for(var i in aVs) {
263+
this.unset(aName, ow.loadObj().filterKeys(aKs, aVs[i]), aTimestamp);
264+
}
265+
},
266+
get: function(aName, aK, x) {
267+
var options = this.__options[aName];
268+
var lst = "*";
269+
var w = [], wv = [];
270+
for(var ii in aK) {
271+
if (isDef(options.keys) && options.keys.indexOf((options.cs ? "\"" + ii + "\"" : ii)) < 0) continue;
272+
w.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
273+
wv.push(aK[ii]);
274+
}
275+
276+
var res;
277+
try {
278+
var res = options.db.qs("select " + lst + " from " + options.from + " where " + w.join(" and "), wv, true);
279+
if (isDef(res) && isArray(res.results) && res.results.length > 0) {
280+
return res.results[0];
281+
} else {
282+
return void 0;
283+
}
284+
} catch(e) {
285+
return String(e);
286+
}
287+
},
288+
getAll: function(aName, full) {
289+
var options = this.__options[aName];
290+
var res = [], wv = [], wk = [], w = "";
291+
if (isDef(full) && isMap(full)) {
292+
for(var ii in full) {
293+
wk.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
294+
wv.push(full[ii]);
295+
}
296+
w = " where " + wk.join(" and ");
297+
}
298+
299+
try {
300+
res = options.db.qs("select * from " + options.from + w, wv, true).results;
301+
} catch(e) {
302+
return String(e);
303+
}
304+
return res;
305+
},
306+
pop: function(aName) {
307+
var aKs = this.getSortedKeys(aName);
308+
var aK = aKs[aKs.length - 1];
309+
return aK;
310+
},
311+
shift: function(aName) {
312+
var aK = this.getSortedKeys(aName)[0];
313+
return aK;
314+
},
315+
unset: function(aName, aK, aTimestamp) {
316+
var options = this.__options[aName];
317+
318+
var w = [], wv = [];
319+
for(var ii in aK) {
320+
if (isDef(options.keys) && options.keys.indexOf((options.cs ? "\"" + ii + "\"" : ii)) < 0) continue;
321+
w.push((options.cs ? "\"" + ii + "\"" : ii) + " = ?");
322+
wv.push(aK[ii]);
323+
}
324+
325+
try {
326+
options.db.us("delete " + options.from + " where " + w.join(" and "), wv, true);
327+
options.db.commit();
328+
} catch(e) {
329+
options.db.rollback();
330+
throw e;
331+
}
332+
}
333+
},
334+
dbOld: {
143335
__db: {},
144336
__table: {},
145337
create: function(aName, shouldCompress, options) {

0 commit comments

Comments
 (0)