slice (copy) columns before popping (deleting)

This commit is contained in:
AJ ONeal 2015-12-09 00:57:31 +00:00
parent db856e025a
commit 3b581ef492
1 changed files with 46 additions and 38 deletions

View File

@ -7,6 +7,7 @@ function wrap(db, dir) {
var promises = []; var promises = [];
var earr = []; var earr = [];
var dbsMap = {}; var dbsMap = {};
var debug = false;
db.escape = function (str) { db.escape = function (str) {
return (str||'').replace(/'/g, "''"); return (str||'').replace(/'/g, "''");
@ -53,8 +54,10 @@ function wrap(db, dir) {
return; return;
} }
if (debug) {
console.log('sqlite3 rows 0'); console.log('sqlite3 rows 0');
console.log(result); console.log(result);
}
function alterTable() { function alterTable() {
var column = columns.pop(); var column = columns.pop();
@ -77,8 +80,11 @@ function wrap(db, dir) {
+ db.escape(column.name) + " " + db.escape(column.type) + db.escape(column.name) + " " + db.escape(column.type)
+ " DEFAULT null" + " DEFAULT null"
; ;
if (debug) {
console.log('sqlite3 1'); console.log('sqlite3 1');
console.log(sql); console.log(sql);
}
db.all(sql, earr, function (err, results) { db.all(sql, earr, function (err, results) {
if (err) { if (err) {
@ -88,12 +94,16 @@ function wrap(db, dir) {
return; return;
} }
if (debug) {
console.log('sqlite3 rows 1'); console.log('sqlite3 rows 1');
console.log(results); console.log(results);
}
alterTable(); alterTable();
}); });
} }
columns = columns.slice(0);
alterTable(); alterTable();
}); });
} }
@ -113,29 +123,33 @@ function wrap(db, dir) {
return col; return col;
} }
function createTable(opts) { function createTable(dir) {
if (!opts.modelname && !opts.tablename) { if (!dir.modelname && !dir.tablename) {
throw new Error('Please specify opts.modelname'); throw new Error('Please specify dir.modelname');
} }
if (!opts.tablename) { if (!dir.tablename) {
opts.tablename = snakeCase(opts.modelname); dir.tablename = snakeCase(dir.modelname);
} }
if (!opts.indices) { if (!dir.modelname) {
opts.indices = []; dir.modelname = upperCamelCase(dir.tablename);
}
if (!dir.indices) {
dir.indices = [];
} }
var DB = {}; var DB = {};
var tablename = db.escape(snakeCase(opts.tablename) || 'data'); var tablename = (db.escape(dir.tablename || 'data'));
var idname = db.escape(snakeCase(opts.idname || 'id')); var idname = (db.escape(dir.idname || 'id'));
var idnameCased = (camelCase(opts.idname || 'id')); var idnameCased = (camelCase(dir.idname || 'id'));
opts.indices.forEach(normalizeColumn); dir.indices.forEach(normalizeColumn);
db = PromiseA.promisifyAll(db); db = PromiseA.promisifyAll(db);
if (opts && opts.verbose || db.verbose) { if (dir && dir.verbose || db.verbose) {
console.log('Getting Verbose up in here'); console.log('Getting Verbose up in here');
db.on('trace', function (str) { db.on('trace', function (str) {
console.log('SQL:', str); console.log('SQL:', str);
@ -204,26 +218,26 @@ function wrap(db, dir) {
}); });
}; };
DB.find = function (opts, params) { DB.find = function (obj, params) {
var sql = 'SELECT * FROM \'' + tablename + '\' '; var sql = 'SELECT * FROM \'' + tablename + '\' ';
var keys = opts && Object.keys(opts); var keys = obj && Object.keys(obj);
if (opts && keys.length) { if (obj && keys.length) {
sql += 'WHERE '; sql += 'WHERE ';
keys.forEach(function (key, i) { keys.forEach(function (key, i) {
if (i !== 0) { if (i !== 0) {
sql += 'AND '; sql += 'AND ';
} }
if (null === opts[key]) { if (null === obj[key]) {
sql += db.escape(snakeCase(key)) + " IS '" + db.escape(opts[key]) + "'"; sql += db.escape(snakeCase(key)) + " IS '" + db.escape(obj[key]) + "'";
} }
else { else {
sql += db.escape(snakeCase(key)) + " = '" + db.escape(opts[key]) + "'"; sql += db.escape(snakeCase(key)) + " = '" + db.escape(obj[key]) + "'";
} }
}); });
} }
else if (null !== opts || (params && !params.limit)) { else if (null !== obj || (params && !params.limit)) {
return PromiseA.reject(new Error("to find all you must explicitly specify find(null, { limit: <<int>> })")); return PromiseA.reject(new Error("to find all you must explicitly specify find(null, { limit: <<int>> })"));
} }
@ -356,7 +370,7 @@ function wrap(db, dir) {
var vals = []; var vals = [];
['hasOne', 'hasMany', 'hasAndBelongsToMany', 'belongsTo', 'belongsToMany'].forEach(function (relname) { ['hasOne', 'hasMany', 'hasAndBelongsToMany', 'belongsTo', 'belongsToMany'].forEach(function (relname) {
var rels = opts[relname]; var rels = dir[relname];
if (!rels) { if (!rels) {
return; return;
@ -375,7 +389,7 @@ function wrap(db, dir) {
}); });
}); });
opts.indices.forEach(function (col) { dir.indices.forEach(function (col) {
var val = data[camelCase(col.name)]; var val = data[camelCase(col.name)];
//if (col.name in data) //if (col.name in data)
@ -490,7 +504,7 @@ function wrap(db, dir) {
var indexable = [idname + ' TEXT']; var indexable = [idname + ' TEXT'];
var sql; var sql;
opts.indices.forEach(function (col) { dir.indices.forEach(function (col) {
if ('string' === typeof col) { if ('string' === typeof col) {
col = { name: col, type: 'TEXT' }; col = { name: col, type: 'TEXT' };
} }
@ -509,7 +523,7 @@ function wrap(db, dir) {
; ;
db.runAsync(sql).then(function () { db.runAsync(sql).then(function () {
sqlite3GetColumns(tablename, opts.indices, function (err) { sqlite3GetColumns(tablename, dir.indices, function (err) {
if (err) { if (err) {
console.error('[Error] dbwrap get columns'); console.error('[Error] dbwrap get columns');
console.error(err.stack); console.error(err.stack);
@ -523,16 +537,10 @@ function wrap(db, dir) {
}); });
} }
dir.forEach(function (opts) { dir.forEach(function (dir) {
promises.push(createTable(opts).then(function (dbw) { promises.push(createTable(dir).then(function (dbw) {
var modelname = opts.modelname;
if (!modelname) { dbsMap[dir.modelname] = dbw;
modelname = (opts.tablename || 'data');
modelname = upperCamelCase(modelname);
}
dbsMap[modelname] = dbw;
return dbw; return dbw;
})); }));