2013-03-14 01:41:37 +08:00
|
|
|
var http = require('http');
|
|
|
|
var url = require('url');
|
2014-01-28 19:04:10 +08:00
|
|
|
var _ = require('underscore');
|
2013-03-14 01:41:37 +08:00
|
|
|
|
2014-08-04 07:28:30 +08:00
|
|
|
var SQLAPIEmulator = function(port, cb) {
|
2013-03-14 01:41:37 +08:00
|
|
|
|
2013-04-04 19:15:50 +08:00
|
|
|
this.queries = [];
|
|
|
|
var that = this;
|
2014-01-30 23:45:40 +08:00
|
|
|
this.requests = [];
|
2014-01-17 00:20:30 +08:00
|
|
|
|
2013-03-14 01:41:37 +08:00
|
|
|
this.sqlapi_server = http.createServer(function(req,res) {
|
2014-01-17 00:20:30 +08:00
|
|
|
//console.log("server got request with method " + req.method);
|
|
|
|
var query;
|
2014-01-30 23:45:40 +08:00
|
|
|
|
|
|
|
that.requests.push(req);
|
|
|
|
|
2014-01-17 00:20:30 +08:00
|
|
|
if ( req.method == 'GET' ) {
|
|
|
|
query = url.parse(req.url, true).query;
|
|
|
|
that.handleQuery(query, res);
|
|
|
|
}
|
|
|
|
else if ( req.method == 'POST') {
|
|
|
|
var data = '';
|
|
|
|
req.on('data', function(chunk) {
|
|
|
|
//console.log("GOT Chunk " + chunk);
|
|
|
|
data += chunk;
|
|
|
|
});
|
|
|
|
req.on('end', function() {
|
|
|
|
//console.log("Data is: "); console.dir(data);
|
|
|
|
query = JSON.parse(data);
|
|
|
|
//console.log("handleQuery is " + that.handleQuery);
|
|
|
|
that.handleQuery(query, res);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
that.handleQuery('SQLAPIEmu does not support method' + req.method, res);
|
|
|
|
}
|
2013-03-14 01:41:37 +08:00
|
|
|
}).listen(port, cb);
|
|
|
|
};
|
|
|
|
|
2014-08-04 07:28:30 +08:00
|
|
|
SQLAPIEmulator.prototype.handleQuery = function(query, res) {
|
2014-01-17 00:20:30 +08:00
|
|
|
this.queries.push(query);
|
|
|
|
if ( query.q.match('SQLAPIERROR') ) {
|
|
|
|
res.statusCode = 400;
|
|
|
|
res.write(JSON.stringify({'error':'Some error occurred'}));
|
2014-02-27 17:26:42 +08:00
|
|
|
} else if ( query.q.match('SQLAPINOANSWER') ) {
|
2014-08-04 07:28:30 +08:00
|
|
|
console.log("SQLAPIEmulator will never respond, on request");
|
|
|
|
return;
|
|
|
|
} else if (query.q.match('tablenames')) {
|
|
|
|
var tableNames = JSON.stringify(query);
|
|
|
|
res.write(queryResult({tablenames: '{' + tableNames + '}', max: 1234567890.123}));
|
2014-01-17 00:20:30 +08:00
|
|
|
} else if ( query.q.match('EPOCH.* as max') ) {
|
|
|
|
// This is the structure of the known query sent by tiler
|
2014-08-04 07:28:30 +08:00
|
|
|
res.write(queryResult({max: 1234567890.123}));
|
2014-01-17 00:20:30 +08:00
|
|
|
} else {
|
2014-01-28 19:04:10 +08:00
|
|
|
if ( query.q.match('_private_') && query.api_key === undefined) {
|
|
|
|
res.statusCode = 403;
|
|
|
|
res.write(JSON.stringify({'error':'forbidden: ' + JSON.stringify(query)}));
|
|
|
|
} else {
|
|
|
|
var qs = JSON.stringify(query);
|
2014-08-04 07:28:30 +08:00
|
|
|
res.write(queryResult({cdb_querytables: '{' + qs + '}', max: 1234567890.123}));
|
2014-01-28 19:04:10 +08:00
|
|
|
}
|
2014-01-17 00:20:30 +08:00
|
|
|
}
|
|
|
|
res.end();
|
|
|
|
};
|
|
|
|
|
2014-08-04 07:28:30 +08:00
|
|
|
SQLAPIEmulator.prototype.close = function(cb) {
|
2013-03-14 01:41:37 +08:00
|
|
|
this.sqlapi_server.close(cb);
|
|
|
|
};
|
|
|
|
|
2014-08-04 07:28:30 +08:00
|
|
|
SQLAPIEmulator.prototype.getLastRequest = function() {
|
2014-01-30 23:45:40 +08:00
|
|
|
return this.requests.pop();
|
|
|
|
};
|
|
|
|
|
2014-08-04 07:28:30 +08:00
|
|
|
function queryResult(row) {
|
|
|
|
return JSON.stringify({
|
|
|
|
rows: [row]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SQLAPIEmulator;
|
2013-03-14 01:41:37 +08:00
|
|
|
|