2013-03-14 01:41:37 +08:00
|
|
|
var http = require('http');
|
|
|
|
var url = require('url');
|
|
|
|
|
|
|
|
var o = function(port, cb) {
|
|
|
|
|
2013-04-04 19:15:50 +08:00
|
|
|
this.queries = [];
|
|
|
|
var that = this;
|
2013-03-14 01:41:37 +08:00
|
|
|
this.sqlapi_server = http.createServer(function(req,res) {
|
|
|
|
var query = url.parse(req.url, true).query;
|
2013-04-04 19:15:50 +08:00
|
|
|
that.queries.push(query);
|
2013-03-14 01:41:37 +08:00
|
|
|
if ( query.q.match('SQLAPIERROR') ) {
|
|
|
|
res.statusCode = 400;
|
|
|
|
res.write(JSON.stringify({'error':'Some error occurred'}));
|
2013-04-04 19:15:50 +08:00
|
|
|
} else if ( query.q.match('EPOCH.* as max') ) {
|
|
|
|
// This is the structure of the known query sent by tiler
|
|
|
|
var row = {
|
|
|
|
'max': 1234567890123
|
|
|
|
};
|
|
|
|
res.write(JSON.stringify({rows: [ row ]}));
|
2013-03-14 01:41:37 +08:00
|
|
|
} else {
|
|
|
|
var qs = JSON.stringify(query);
|
|
|
|
var row = {
|
|
|
|
// This is the structure of the known query sent by tiler
|
|
|
|
'cdb_querytables': '{' + qs + '}',
|
|
|
|
'max': qs
|
|
|
|
};
|
|
|
|
res.write(JSON.stringify({rows: [ row ]}));
|
|
|
|
}
|
|
|
|
res.end();
|
|
|
|
}).listen(port, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
o.prototype.close = function(cb) {
|
|
|
|
this.sqlapi_server.close(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = o;
|
|
|
|
|