generalized update sql templating for create

This commit is contained in:
AJ ONeal 2015-09-22 02:08:58 -06:00
parent a3fabc87d2
commit 26311eba3b
1 changed files with 76 additions and 47 deletions

View File

@ -186,13 +186,19 @@ function wrap(db, dir) {
return new PromiseA(function (resolve, reject) { return new PromiseA(function (resolve, reject) {
var json = JSON.stringify(obj); var json = JSON.stringify(obj);
var data = JSON.parse(json); var data = JSON.parse(json);
delete data[idnameCased];
json = JSON.stringify(data);
var sql = "INSERT INTO " + tablename + " (" + idname + ", json) VALUES (?, ?)"; var sql;
var values = [id, json];
db.run(sql, values, function (err) { // removes known fields from data
sql = strainUpdate(id, data, function sqlTpl(fieldable) {
return "INSERT INTO " + tablename + " (" + fieldable.join(', ') + ", " + idname + ")"
//+ " VALUES ('" + vals.join("', '") + "')"
+ " VALUES (" + fieldable.map(function () { return '?'; }).join(", ") + ", ?)"
;
});
console.log('[debug] DB.create() sql:', sql);
db.run(sql, [], function (err) {
if (err) { if (err) {
reject(err); reject(err);
return; return;
@ -215,13 +221,11 @@ function wrap(db, dir) {
}); });
}; };
DB.set = function (id, obj) { // pull indices from object
var json = JSON.stringify(obj); function strainUpdate(id, data/*, vals*/, cb) {
var data = JSON.parse(json);
return new PromiseA(function (resolve, reject) {
var sql;
var fieldable = []; var fieldable = [];
var json;
var sql;
var vals = []; var vals = [];
(opts.indices || []).forEach(function (col) { (opts.indices || []).forEach(function (col) {
@ -242,28 +246,53 @@ function wrap(db, dir) {
+ " = '" + db.escape(val) + "'" + " = '" + db.escape(val) + "'"
); );
*/ */
fieldable.push(db.escape(snakeCase(col.name)) + " = ?"); fieldable.push(db.escape(snakeCase(col.name)));
vals.push(val); vals.push(val);
} }
delete data[col.name]; delete data[col.name];
delete data[camelCase(col.name)];
}); });
delete data[idnameCased]; delete data[idnameCased];
if (!fieldable.length || Object.keys(data).length) {
json = JSON.stringify(data); json = JSON.stringify(data);
fieldable.push("json = ?"); fieldable.push("json");
//fieldable.push("json = '" + db.escape(json) + "'"); //fieldable.push("json = '" + db.escape(json) + "'");
vals.push(json); vals.push(json);
}
vals.push(id); vals.push(id);
sql = "UPDATE " + tablename + " SET " + fieldable.join(', ') + " WHERE " + idname + " = ?";
sql = cb(fieldable);
while (vals.length) { while (vals.length) {
sql = sql.replace(/\?/, "'" + db.escape(vals.shift()) + "'"); sql = sql.replace(/\?/, "'" + db.escape(vals.shift()) + "'");
} }
console.log('[debug] sql:', sql); return sql;
db.run(sql, vals, function (err) { }
DB.set = function (id, obj) {
var json = JSON.stringify(obj);
var data = JSON.parse(json);
return new PromiseA(function (resolve, reject) {
function sqlTpl(fieldable) {
// this will always at least have one fieldable value: json
return "UPDATE " + tablename + " SET "
+ (fieldable.join(' = ?, ') + " = ?")
+ " WHERE " + idname + " = ?"
;
}
//var vals = [];
// removes known fields from data
var sql = strainUpdate(id, data/*, vals*/, sqlTpl);
console.log('[debug] DB.set() sql:', sql);
db.run(sql, /*vals*/[], function (err) {
if (err) { if (err) {
reject(err); reject(err);
return; return;