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