Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
logger(DEBUG, "\n******************************\nCollection total BEFORE read from db: " + model.length + " models\n******************************");
}
var sql = opts.query || 'SELECT * FROM ' + table;
// we want the exact server response returned by the adapter
if (params.returnExactServerResponse && data) {
opts.sql = opts.sql || {};
opts.sql.where = opts.sql.where || {};
if (_.isEmpty(data)) {
// No result
opts.sql.where[model.idAttribute] = "1=2";
} else {
// Find all idAttribute in the server response
var ids = [];
_.each(data, function(element) {
ids.push(element[model.idAttribute]);
});
// this will select IDs in the sql query
opts.sql.where[model.idAttribute] = ids;
}
}
// execute the select query
db = Ti.Database.open(dbName);
// run a specific sql query if defined
if (opts.query) {
if (opts.query.params) {
var rs = db.execute(opts.query.sql, opts.query.params);
} else {
var rs = db.execute(opts.query.sql);
logger(DEBUG, "\n******************************\nCollection total BEFORE read from db: " + model.length + " models\n******************************");
}
var sql = opts.query || 'SELECT * FROM ' + table;
// we want the exact server response returned by the adapter
if (params.returnExactServerResponse && data) {
opts.sql = opts.sql || {};
opts.sql.where = opts.sql.where || {};
if (_.isEmpty(data)) {
// No result
opts.sql.where[model.idAttribute] = "1=2";
} else {
// Find all idAttribute in the server response
var ids = [];
_.each(data, function(element) {
ids.push(element[model.idAttribute]);
});
// this will select IDs in the sql query
opts.sql.where[model.idAttribute] = ids;
}
}
// execute the select query
db = Ti.Database.open(dbName);
// run a specific sql query if defined
if (opts.query) {
if (opts.query.params) {
var rs = db.execute(opts.query.sql, opts.query.params);
} else {
var rs = db.execute(opts.query.sql);
// use the where not operator
wherenot = whereBuilder(wherenot, opts.wherenot, " != ");
wherenot = wherenot.join(' AND ');
} else {
wherenot = opts.wherenot;
}
sql += ' AND ' + wherenot;
}
// LIKE
if (opts.like) {
var like;
if ( typeof opts.like === 'object') {
like = [];
_.each(opts.like, function(value, f) {
like.push(f + ' LIKE "%' + value + '%"');
});
like = like.join(' AND ');
sql += ' AND ' + like;
}
}
// LIKE OR
if (opts.likeor) {
var likeor;
if ( typeof opts.likeor === 'object') {
likeor = [];
_.each(opts.likeor, function(value, f) {
likeor.push(f + ' LIKE "%' + value + '%"');
});
likeor = likeor.join(' OR ');
}
}
config.columns = columns;
// make sure we have a unique id field
if (config.adapter.idAttribute) {
if (!_.contains(_.keys(config.columns), config.adapter.idAttribute)) {
throw 'config.adapter.idAttribute "' + config.adapter.idAttribute + '" not found in list of columns for table "' + table + '"\n' + 'columns: [' + _.keys(config.columns).join(',') + ']';
}
} else {
Ti.API.info('No config.adapter.idAttribute specified for table "' + table + '"');
Ti.API.info('Adding "' + ALLOY_ID_DEFAULT + '" to uniquely identify rows');
var fullStrings = [],
colStrings = [];
_.each(config.columns, function(type, name) {
colStrings.push(name);
fullStrings.push(name + ' ' + type);
});
var colsString = colStrings.join(',');
db.execute('ALTER TABLE ' + table + ' RENAME TO ' + table + '_temp;');
db.execute('CREATE TABLE ' + table + '(' + fullStrings.join(',') + ',' + ALLOY_ID_DEFAULT + ' TEXT UNIQUE);');
db.execute('INSERT INTO ' + table + '(' + colsString + ',' + ALLOY_ID_DEFAULT + ') SELECT ' + colsString + ',CAST(_ROWID_ AS TEXT) FROM ' + table + '_temp;');
db.execute('DROP TABLE ' + table + '_temp;');
config.columns[ALLOY_ID_DEFAULT] = 'TEXT UNIQUE';
config.adapter.idAttribute = ALLOY_ID_DEFAULT;
}
// close the db handle
db.close();
}
function Sync(method, model, opts) {
var prefix = model.config.adapter.collection_name ? model.config.adapter.collection_name : "default", regex = new RegExp("^(" + prefix + ")\\-(.+)$"), resp = null;
if ("read" === method) if (opts.parse) {
var list = [];
_.each(TAP.listProperties(), function(prop) {
var match = prop.match(regex);
null !== match && list.push(TAP.getObject(prop));
});
model.reset(list);
resp = list;
} else {
var obj = TAP.getObject(prefix + "-" + model.id);
model.set(obj);
resp = model.toJSON();
} else if ("create" === method || "update" === method) {
if (!model.id) {
model.id = guid();
model.set(model.idAttribute, model.id);
}
TAP.setObject(prefix + "-" + model.id, model.toJSON() || {});
resp = model.toJSON();
config.adapter.idAttribute ? config.adapter.idAttribute : ALLOY_ID_DEFAULT;
for (var k in config.columns) {
cName = k;
cType = config.columns[k];
cName !== ALLOY_ID_DEFAULT || config.adapter.idAttribute ? k === config.adapter.idAttribute && (cType += " UNIQUE") : config.adapter.idAttribute = ALLOY_ID_DEFAULT;
columns[cName] = cType;
}
}
config.columns = columns;
if (config.adapter.idAttribute) {
if (!_.contains(_.keys(config.columns), config.adapter.idAttribute)) throw 'config.adapter.idAttribute "' + config.adapter.idAttribute + '" not found in list of columns for table "' + table + '"\n' + "columns: [" + _.keys(config.columns).join(",") + "]";
} else {
Ti.API.info('No config.adapter.idAttribute specified for table "' + table + '"');
Ti.API.info('Adding "' + ALLOY_ID_DEFAULT + '" to uniquely identify rows');
var fullStrings = [], colStrings = [];
_.each(config.columns, function(type, name) {
colStrings.push(name);
fullStrings.push(name + " " + type);
});
var colsString = colStrings.join(",");
db.execute("ALTER TABLE " + table + " RENAME TO " + table + "_temp;");
db.execute("CREATE TABLE " + table + "(" + fullStrings.join(",") + "," + ALLOY_ID_DEFAULT + " TEXT UNIQUE);");
db.execute("INSERT INTO " + table + "(" + colsString + "," + ALLOY_ID_DEFAULT + ") SELECT " + colsString + ",CAST(_ROWID_ AS TEXT) FROM " + table + "_temp;");
db.execute("DROP TABLE " + table + "_temp;");
config.columns[ALLOY_ID_DEFAULT] = "TEXT UNIQUE";
config.adapter.idAttribute = ALLOY_ID_DEFAULT;
}
db.close();
}
}
for (var k in config.columns) {
cName = k;
cType = config.columns[k];
cName !== ALLOY_ID_DEFAULT || config.adapter.idAttribute ? k === config.adapter.idAttribute && (cType += " UNIQUE") : config.adapter.idAttribute = ALLOY_ID_DEFAULT;
columns[cName] = cType;
}
}
config.columns = columns;
if (config.adapter.idAttribute) {
if (!_.contains(_.keys(config.columns), config.adapter.idAttribute)) throw 'config.adapter.idAttribute "' + config.adapter.idAttribute + '" not found in list of columns for table "' + table + '"\ncolumns: [' + _.keys(config.columns).join(",") + "]";
} else {
Ti.API.info('No config.adapter.idAttribute specified for table "' + table + '"');
Ti.API.info('Adding "' + ALLOY_ID_DEFAULT + '" to uniquely identify rows');
var fullStrings = [], colStrings = [];
_.each(config.columns, function(type, name) {
colStrings.push(name);
fullStrings.push(name + " " + type);
});
var colsString = colStrings.join(",");
db.execute("ALTER TABLE " + table + " RENAME TO " + table + "_temp;");
db.execute("CREATE TABLE " + table + "(" + fullStrings.join(",") + "," + ALLOY_ID_DEFAULT + " TEXT UNIQUE);");
db.execute("INSERT INTO " + table + "(" + colsString + "," + ALLOY_ID_DEFAULT + ") SELECT " + colsString + ",CAST(_ROWID_ AS TEXT) FROM " + table + "_temp;");
db.execute("DROP TABLE " + table + "_temp;");
config.columns[ALLOY_ID_DEFAULT] = "TEXT UNIQUE";
config.adapter.idAttribute = ALLOY_ID_DEFAULT;
}
db.close();
}
function whereBuilder(where, data, operator) {
var whereOperator = operator || " = ";
_.each(data, function(v, f) {
if (_.isArray(v)) {//select multiple items
var innerWhere = [];
_.each(v, function(value) {
innerWhere.push(f + whereOperator + _valueType(value));
});
where.push(innerWhere.join(' OR '));
} else if (_.isObject(v)) {
where = whereBuilder(where, v, whereOperator);
} else {
where.push(f + whereOperator + _valueType(v));
}
});
return where;
}