From 26311eba3bc2049efddae3f2d1a42d244e9f7029 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 22 Sep 2015 02:08:58 -0600 Subject: [PATCH] generalized update sql templating for create --- lib/dbwrap.js | 123 +++++++++++++++++++++++++++++++------------------- 1 file changed, 76 insertions(+), 47 deletions(-) diff --git a/lib/dbwrap.js b/lib/dbwrap.js index 56a2d83..e90c3b7 100644 --- a/lib/dbwrap.js +++ b/lib/dbwrap.js @@ -186,13 +186,19 @@ function wrap(db, dir) { return new PromiseA(function (resolve, reject) { var json = JSON.stringify(obj); var data = JSON.parse(json); - delete data[idnameCased]; - json = JSON.stringify(data); - var sql = "INSERT INTO " + tablename + " (" + idname + ", json) VALUES (?, ?)"; - var values = [id, json]; + var sql; - 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) { reject(err); return; @@ -215,55 +221,78 @@ function wrap(db, dir) { }); }; + // pull indices from object + function strainUpdate(id, data/*, vals*/, cb) { + var fieldable = []; + var json; + var sql; + var vals = []; + + (opts.indices || []).forEach(function (col) { + if ('string' === typeof col) { + col = { name: col, type: 'TEXT' }; + } + if (!col.type) { + col.type = 'TEXT'; + } + + var val = data[camelCase(col.name)]; + + //if (col.name in data) + if ('undefined' !== typeof val) { + /* + fieldable.push( + db.escape(snakeCase(col.name)) + + " = '" + db.escape(val) + "'" + ); + */ + fieldable.push(db.escape(snakeCase(col.name))); + vals.push(val); + } + + delete data[col.name]; + delete data[camelCase(col.name)]; + }); + + delete data[idnameCased]; + + if (!fieldable.length || Object.keys(data).length) { + json = JSON.stringify(data); + fieldable.push("json"); + //fieldable.push("json = '" + db.escape(json) + "'"); + vals.push(json); + } + + vals.push(id); + + sql = cb(fieldable); + + while (vals.length) { + sql = sql.replace(/\?/, "'" + db.escape(vals.shift()) + "'"); + } + + return sql; + } + DB.set = function (id, obj) { var json = JSON.stringify(obj); var data = JSON.parse(json); return new PromiseA(function (resolve, reject) { - var sql; - var fieldable = []; - var vals = []; - - (opts.indices || []).forEach(function (col) { - if ('string' === typeof col) { - col = { name: col, type: 'TEXT' }; - } - if (!col.type) { - col.type = 'TEXT'; - } - - var val = data[camelCase(col.name)]; - - //if (col.name in data) - if ('undefined' !== typeof val) { - /* - fieldable.push( - db.escape(snakeCase(col.name)) - + " = '" + db.escape(val) + "'" - ); - */ - fieldable.push(db.escape(snakeCase(col.name)) + " = ?"); - vals.push(val); - } - - delete data[col.name]; - }); - delete data[idnameCased]; - - json = JSON.stringify(data); - fieldable.push("json = ?"); - //fieldable.push("json = '" + db.escape(json) + "'"); - vals.push(json); - - vals.push(id); - sql = "UPDATE " + tablename + " SET " + fieldable.join(', ') + " WHERE " + idname + " = ?"; - - while (vals.length) { - sql = sql.replace(/\?/, "'" + db.escape(vals.shift()) + "'"); + function sqlTpl(fieldable) { + // this will always at least have one fieldable value: json + return "UPDATE " + tablename + " SET " + + (fieldable.join(' = ?, ') + " = ?") + + " WHERE " + idname + " = ?" + ; } - console.log('[debug] sql:', sql); - db.run(sql, vals, function (err) { + //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) { reject(err); return;