Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// see if it already has the ALLOY_ID_DEFAULT
if (cName === ALLOY_ID_DEFAULT && !config.adapter.idAttribute) {
config.adapter.idAttribute = ALLOY_ID_DEFAULT;
} else if (k === config.adapter.idAttribute) {
cType += " UNIQUE";
}
columns[cName] = cType;
}
}
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;');
attrObj[model.idAttribute] = attrObj.id;
} else {
// idAttribute not assigned by alloy. Leave it empty and
// allow sqlite to process as null, which is the
// expected value for an AUTOINCREMENT field.
attrObj[model.idAttribute] = null;
}
}
//validate the item
if (params.useStrictValidation) {
for (var c in columns) {
if (c == model.idAttribute) {
continue;
}
if (!_.contains(_.keys(attrObj), c)) {
Ti.API.error("[SQL REST API] ITEM NOT VALID - REASON: " + c + " is not present");
return;
}
}
}
// 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]);
this.deleteRow = function(columns) {
var sql = "DELETE FROM " + this.table;
var keys = _.keys(columns);
var len = keys.length;
var conditions = [];
var values = [];
len && (sql += " WHERE ");
for (var i = 0; len > i; i++) {
conditions.push(keys[i] + " = ?");
values.push(columns[keys[i]]);
}
sql += conditions.join(" AND ");
this.db.execute(sql, values);
};
}
cName !== ALLOY_ID_DEFAULT || config.adapter.idAttribute || (config.adapter.idAttribute = ALLOY_ID_DEFAULT);
rs.next();
}
rs.close();
} else {
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;
}
this.deleteRow = function(columns) {
var sql = "DELETE FROM " + this.table;
var keys = _.keys(columns);
var len = keys.length;
var conditions = [];
var values = [];
len && (sql += " WHERE ");
for (var i = 0; len > i; i++) {
conditions.push(keys[i] + " = ?");
values.push(columns[keys[i]]);
}
sql += conditions.join(" AND ");
this.db.execute(sql, values);
};
}
var dbName = config.adapter.db_name = match[2];
Ti.API.debug('Installing sql database "' + dbFile + '" with name "' + dbName + '"');
var db = Ti.Database.install(dbFile, dbName);
var rs = db.execute('pragma table_info("' + table + '");');
var columns = {};
while (rs.isValidRow()) {
var cName = rs.fieldByName("name");
var cType = rs.fieldByName("type");
columns[cName] = cType;
cName !== ALLOY_ID_DEFAULT || config.adapter.idAttribute || (config.adapter.idAttribute = ALLOY_ID_DEFAULT);
rs.next();
}
config.columns = columns;
rs.close();
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;
}
this.deleteRow = function(columns) {
var sql = "DELETE FROM " + this.table;
var keys = _.keys(columns);
var len = keys.length;
var conditions = [];
var values = [];
len && (sql += " WHERE ");
for (var i = 0; len > i; i++) {
conditions.push(keys[i] + " = ?");
values.push(columns[keys[i]]);
}
sql += conditions.join(" AND ");
this.db.execute(sql, values);
};
}
}
rs.close();
} else {
{
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 + '"\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;
}