Skip to content

Commit

Permalink
Simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
mevdschee committed Jan 22, 2017
1 parent ab3714e commit 8310f30
Showing 1 changed file with 4 additions and 21 deletions.
25 changes: 4 additions & 21 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,7 @@ var server = http.createServer(withDb(withBody(function (req, resp) {

// retrieve the table and key from the path
var table = req.db.escapeId(request.shift());
var key = parseInt(request.shift());

// escape the columns and values from the input object
var columns = Object.keys(input).map(function (key) {
return req.db.escapeId(key);
});
var values = Object.keys(input).map(function (key) {
var value = input[key];
if (value === null) return null;
return req.db.escape(value);
});

// build the SET part of the SQL command
var set = '';
for (i = 0; i < columns.length; i++) {
set += (i > 0 ? ',' : '') + columns[i] + '=';
set += (values[i] === null ? 'NULL' : values[i]);
}
var key = req.db.escape(request.shift());

// create SQL based on HTTP method
var sql = '';
Expand All @@ -85,18 +68,18 @@ var server = http.createServer(withDb(withBody(function (req, resp) {
sql = "select * from " + table + (key ? " where id=" + key : '');
break;
case 'PUT':
sql = "update " + table + " set " + set + " where id=" + key;
sql = "update " + table + " set ? where id=" + key;
break;
case 'POST':
sql = "insert into " + table + " set " + set;
sql = "insert into " + table + " set ?";
break;
case 'DELETE':
sql = "delete " + table + " where id=" + key;
break;
}

// execute SQL statement
req.db.query(sql, function (err, result) {
req.db.query(sql, input, function (err, result) {

// stop using mysql connection
req.db.release();
Expand Down

0 comments on commit 8310f30

Please sign in to comment.