Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Create arrays for insert query
var names = [],
values = [],
q = [];
for (var k in columns) {
names.push(k);
if (_.isObject(attrObj[k])) {
values.push(JSON.stringify(attrObj[k]));
} else {
values.push(attrObj[k]);
}
q.push('?');
}
// Last Modified logic
if (params.lastModifiedColumn && _.isUndefined(params.disableLastModified)) {
values[_.indexOf(names, params.lastModifiedColumn)] = params.lastModifiedDateFormat ? moment().format(params.lastModifiedDateFormat) : moment().lang('en').zone('GMT').format('ddd, D MMM YYYY HH:mm:ss ZZ');
}
// Assemble create query
var sqlInsert = "INSERT INTO " + table + " (" + names.join(",") + ") VALUES (" + q.join(",") + ");";
if (!_.isUndefined(queryList) && _.isFunction(queryList.push)) {
queryList.push({
"sql" : sqlInsert,
"values" : values
});
return queryList;
} else {
// execute the query and return the response
db = Ti.Database.open(dbName);
db.execute('BEGIN;');
values.push(JSON.stringify(attrObj[k]));
} else {
values.push(attrObj[k]);
}
q.push('?');
}
// Last Modified logic
if (params.lastModifiedColumn && _.isUndefined(params.disableLastModified)) {
values[_.indexOf(names, params.lastModifiedColumn)] = params.lastModifiedDateFormat ? moment().format(params.lastModifiedDateFormat) : moment().lang('en').zone('GMT').format('ddd, D MMM YYYY HH:mm:ss ZZ');
}
// Assemble create query
var sqlInsert = "INSERT INTO " + table + " (" + names.join(",") + ") VALUES (" + q.join(",") + ");";
if (!_.isUndefined(queryList) && _.isFunction(queryList.push)) {
queryList.push({
"sql" : sqlInsert,
"values" : values
});
return queryList;
} else {
// execute the query and return the response
db = Ti.Database.open(dbName);
db.execute('BEGIN;');
db.execute(sqlInsert, values);
// get the last inserted id
if (model.id === null) {
var sqlId = "SELECT last_insert_rowid();";
var rs = db.execute(sqlId);
if (rs.isValidRow()) {
// Create arrays for insert query
var names = [],
values = [],
q = [];
for (var k in columns) {
names.push(k);
if (_.isObject(attrObj[k])) {
values.push(JSON.stringify(attrObj[k]));
} else {
values.push(attrObj[k]);
}
q.push('?');
}
// Last Modified logic
if (params.lastModifiedColumn && _.isUndefined(params.disableLastModified)) {
values[_.indexOf(names, params.lastModifiedColumn)] = params.lastModifiedDateFormat ? moment().format(params.lastModifiedDateFormat) : moment().lang('en').zone('GMT').format('ddd, D MMM YYYY HH:mm:ss ZZ');
}
// Assemble create query
var sqlInsert = "INSERT INTO " + table + " (" + names.join(",") + ") VALUES (" + q.join(",") + ");";
if (queryList) {
queryList.push({
"sql" : sqlInsert,
"values" : values
});
return queryList;
} else {
// execute the query and return the response
db = Ti.Database.open(dbName);
db.execute('BEGIN;');
if (data) {
attrObj = data;
} else {
if (!isCollection) {
attrObj = model.toJSON();
} else {
Ti.API.error("Its a collection - error!");
}
}
// Create arrays for insert query
var names = [],
values = [],
q = [];
for (var k in columns) {
if (!_.isUndefined(attrObj[k])) {//only update those who are in the data
names.push(k + '=?');
if (_.isObject(attrObj[k])) {
values.push(JSON.stringify(attrObj[k]));
} else {
values.push(attrObj[k]);
}
q.push('?');
}
}
if (params.lastModifiedColumn && _.isUndefined(params.disableLastModified)) {
values[_.indexOf(names, params.lastModifiedColumn + "=?")] = params.lastModifiedDateFormat ? moment().format(params.lastModifiedDateFormat) : moment().lang('en').zone('GMT').format('YYYY-MM-DD HH:mm:ss ZZ');
}
// compose the update query
var sql = 'UPDATE ' + table + ' SET ' + names.join(',') + ' WHERE ' + model.idAttribute + '=?';
function sqlFindItem(_id) {
if (_.isUndefined(_id)) {
return [];
}
var sql = 'SELECT ' + model.idAttribute + ' FROM ' + table + ' WHERE ' + model.idAttribute + '=?';
db = Ti.Database.open(dbName);
var rs = db.execute(sql, _id);
var output = [];
while (rs.isValidRow()) {
output.push(rs.fieldByName(model.idAttribute));
rs.next();
}
rs.close();
db.close();
return output;
}
function getParam(name) {
return _.isUndefined(params[name]) ? name : params[name];
}
function sqlFindItem(_id) {
if (_.isUndefined(_id)) {
return [];
}
var sql = 'SELECT ' + model.idAttribute + ' FROM ' + table + ' WHERE ' + model.idAttribute + '=?';
db = Ti.Database.open(dbName);
var rs = db.execute(sql, _id);
var output = [];
while (rs.isValidRow()) {
output.push(rs.fieldByName(model.idAttribute));
rs.next();
}
rs.close();
db.close();
return output;
}
function parseJSON(_response, parentNode) {
var data = _response.responseJSON;
if (!_.isUndefined(parentNode)) {
data = _.isFunction(parentNode) ? parentNode(data, opts) : traverseProperties(data, parentNode);
}
logger(DEBUG, "server response: ", data);
return data;
}
function iteration(data, i, queryList) {
i = _.isNumber(i) ? i : 0;
queryList = queryList || [];
if (_.isUndefined(data[i])){
return;
}
if (!_.isUndefined(data[i][model.deletedAttribute]) && data[i][model.deletedAttribute] == true) {
//delete item
queryList = deleteSQL(data[i][model.idAttribute], queryList);
} else if (_.indexOf(currentModels, data[i][model.idAttribute]) != -1) {
//item exists - update it
queryList = updateSQL(data[i], queryList);
} else {
//write data to local sql
queryList = createSQL(data[i], queryList);
}
if(++i < data.length) {
_.defer(iteration, data, i, queryList);
} else {
_.defer(function() {
if (queryList && queryList.length) {
db = Ti.Database.open(dbName);