Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions lib/Query.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ Query.prototype.sql = function (query, level) {
}

function set(data) {
var sql = "", delim="", i;
for(i in data) {
var sql = "", delim=", ", i;
for(i in data)
sql += delim + nd + i + nd + "=" + self.sql(data[i], level+1);
delim = ", ";
}
return sql;
return sql.substring(2);
}

function values(data) {
Expand Down Expand Up @@ -173,18 +171,27 @@ Query.prototype.sql = function (query, level) {
sql += " WHERE " + where(query.filters);
}
} else if(query.verb == "insert") {
sql = "INSERT INTO " + nd + query.table + nd +
" SET " + set(query.data);
var sql = "", delim="\n", i;
for(i in data)
sql += delim + "INSERT INTO " + nd + query.table[i] + nd +
" SET " + set(query.data[i]);
sql = sql.substring(1);
} else if(query.verb == "update") {
if(!query.filters) throw "Can't update without filters.";
sql = "UPDATE " + nd + query.table + nd +
" SET " + set(query.data) +
" WHERE " + where(query.filters) +
var sql = "", delim="\n", i;
for(i in data)
sql += delim + "UPDATE " + nd + query.table[i] + nd +
" SET " + set(query.data[i]) +
" WHERE " + where(query.filters[i]) +
" LIMIT 1";
sql = sql.substring(1);
} else if(query.verb == "delete") {
if(!query.filters) throw "Can't delete without filters.";
sql = "DELETE FROM " + nd + query.table + nd +
" WHERE " + where(query.filters);
var sql = "", delim="\n", i;
for(i in data)
sql += delim +"DELETE FROM " + nd + query.table[i] + nd +
" WHERE " + where(query.filters[i]);
sql = sql.substring(1);
}

if(!sql) {
Expand Down