2012-08-08 17:42:11 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
// Command line tool for CartoDB SQL API
|
|
|
|
//
|
|
|
|
// https://github.com/Vizzuality/CartoDB-SQL-API
|
2012-08-08 20:28:07 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
var http = require('http');
|
|
|
|
|
|
|
|
var nodevers = process.versions.node.split('.');
|
2012-08-08 17:42:11 +08:00
|
|
|
|
2012-08-08 20:28:07 +08:00
|
|
|
// NOTE: readline is also available in 0.4 but doesn't work
|
|
|
|
var hasReadline = parseInt(nodevers[0]) > 0 || parseInt(nodevers[1]) >= 8;
|
|
|
|
//console.log('Node version ' + nodevers.join(',') + ( hasReadline ? ' has' : ' does not have' ) + ' readline support');
|
|
|
|
|
|
|
|
var readline = hasReadline ? require('readline') : null;
|
2012-11-24 00:18:39 +08:00
|
|
|
var tty = require('tty');
|
2012-08-08 17:42:11 +08:00
|
|
|
|
|
|
|
var me = process.argv[1];
|
|
|
|
|
2013-07-01 18:23:35 +08:00
|
|
|
process.argv.shift(); // this will be "node" (argv[0])
|
|
|
|
process.argv.shift(); // this will be "benchmark.js" (argv[1])
|
|
|
|
|
|
|
|
var batch_mode = false;
|
|
|
|
var format = 'json';
|
|
|
|
var username = 'dev';
|
|
|
|
var domain = 'localhost';
|
|
|
|
var port = 8080;
|
|
|
|
var api_version = 2;
|
|
|
|
var api_key;
|
|
|
|
var sql;
|
|
|
|
var decimal_places;
|
|
|
|
var echo_queries = false;
|
|
|
|
var skipfields;
|
2013-07-02 22:40:18 +08:00
|
|
|
var verbose = 0;
|
2013-07-01 18:23:35 +08:00
|
|
|
|
2012-08-08 17:42:11 +08:00
|
|
|
function usage(exit_code) {
|
|
|
|
console.log("Usage: " + me + " [OPTIONS] <query>");
|
2012-08-08 20:28:07 +08:00
|
|
|
if ( hasReadline ) {
|
|
|
|
console.log(" " + me + " [OPTIONS]");
|
|
|
|
}
|
2012-08-08 17:42:11 +08:00
|
|
|
console.log("Options:");
|
|
|
|
console.log(" -v verbose operations (off)");
|
|
|
|
console.log(" --help print this help");
|
2013-07-01 18:23:35 +08:00
|
|
|
console.log(" --user <string> cartodb username (" + username + ")");
|
|
|
|
console.log(" --domain <string> service domain (" + domain + ")");
|
|
|
|
console.log(" --port <num> service tcp port number (" + port + ")");
|
|
|
|
console.log(" --api-version <num> API version (" + api_version +")");
|
|
|
|
console.log(" --key <string> API authentication key (" + api_key + ")");
|
2012-08-08 18:18:04 +08:00
|
|
|
console.log(" --format <string> Response format (json)");
|
2012-08-08 18:27:57 +08:00
|
|
|
console.log(" --dp <num> Decimal places in geojson format (unspecified)");
|
2013-05-29 18:35:08 +08:00
|
|
|
console.log(" --skipfields <string> Comma-separated list of fields to skip (none)");
|
2012-11-24 00:18:39 +08:00
|
|
|
console.log(" --echo-queries echo commands sent to server");
|
2012-08-08 21:02:43 +08:00
|
|
|
if ( hasReadline ) {
|
|
|
|
console.log(" --batch Send all read queries at once (off)");
|
|
|
|
}
|
2012-08-08 17:42:11 +08:00
|
|
|
process.exit(exit_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
var arg;
|
|
|
|
while ( arg = process.argv.shift() ) {
|
|
|
|
if ( arg == '-v' ) {
|
|
|
|
++verbose;
|
|
|
|
}
|
|
|
|
else if ( arg == '--help' ) {
|
|
|
|
usage(0);
|
|
|
|
}
|
|
|
|
else if ( arg == '--key' ) {
|
|
|
|
api_key = process.argv.shift();
|
|
|
|
}
|
2012-08-08 17:58:03 +08:00
|
|
|
else if ( arg == '--domain' ) {
|
|
|
|
domain = process.argv.shift();
|
2012-08-08 17:42:11 +08:00
|
|
|
}
|
|
|
|
else if ( arg == '--user' ) {
|
|
|
|
username = process.argv.shift();
|
|
|
|
}
|
|
|
|
else if ( arg == '--port' ) {
|
|
|
|
port = process.argv.shift();
|
|
|
|
}
|
|
|
|
else if ( arg == '--api-version' ) {
|
|
|
|
api_version = process.argv.shift();
|
|
|
|
}
|
2012-08-08 18:18:04 +08:00
|
|
|
else if ( arg == '--format' ) {
|
|
|
|
format = process.argv.shift();
|
|
|
|
}
|
2012-08-08 18:27:57 +08:00
|
|
|
else if ( arg == '--dp' ) {
|
|
|
|
decimal_places = process.argv.shift();
|
|
|
|
}
|
2012-08-08 21:02:43 +08:00
|
|
|
else if ( arg == '--batch' ) {
|
|
|
|
batch_mode = true;
|
|
|
|
}
|
2013-05-29 18:35:08 +08:00
|
|
|
else if ( arg == '--skipfields' ) {
|
|
|
|
skipfields = process.argv.shift();
|
|
|
|
}
|
2012-11-24 00:18:39 +08:00
|
|
|
else if ( arg == '--echo-queries' ) {
|
|
|
|
echo_queries = true;
|
|
|
|
}
|
2012-08-08 17:42:11 +08:00
|
|
|
else if ( ! sql ) {
|
|
|
|
sql = arg;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var hostname = username + '.' + domain;
|
|
|
|
|
2012-11-24 00:18:39 +08:00
|
|
|
if ( ! tty.isatty(process.stdin) || ! tty.isatty(process.stdout) ) {
|
|
|
|
batch_mode = true;
|
|
|
|
}
|
|
|
|
|
2012-08-08 20:28:07 +08:00
|
|
|
if ( ! sql ) {
|
2012-08-08 21:02:43 +08:00
|
|
|
|
2012-11-24 00:18:39 +08:00
|
|
|
if ( ! batch_mode ) {
|
|
|
|
|
|
|
|
if ( readline ) {
|
|
|
|
|
|
|
|
var rl = readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout,
|
|
|
|
});
|
2012-08-08 21:02:43 +08:00
|
|
|
|
|
|
|
rl.setPrompt(hostname + '> ');
|
|
|
|
rl.prompt();
|
|
|
|
|
2012-11-24 00:18:39 +08:00
|
|
|
sql = '';
|
|
|
|
rl.on('line', function(line) {
|
|
|
|
sql += line;
|
2012-08-08 21:02:43 +08:00
|
|
|
// TODO: some sanity checking, like trim the line or check if it ends with semicolon
|
|
|
|
if ( sql.length ) {
|
|
|
|
processQuery(sql, function() {
|
|
|
|
sql = '';
|
|
|
|
rl.prompt();
|
|
|
|
});
|
|
|
|
} else rl.prompt();
|
2012-11-24 00:18:39 +08:00
|
|
|
}).on('close', function() {
|
2012-08-08 21:02:43 +08:00
|
|
|
if ( sql.length ) {
|
|
|
|
console.warn("Unprocessed sql left: [" + sql + "]");
|
|
|
|
}
|
|
|
|
console.log("Good bye");
|
2012-11-24 00:18:39 +08:00
|
|
|
}).on('SIGCONT', function() {
|
|
|
|
// this is needed so not to exit on stop/resume
|
|
|
|
rl.prompt();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
usage(1);
|
|
|
|
}
|
|
|
|
} else { // batch mode
|
|
|
|
process.stdin.resume();
|
|
|
|
process.stdin.setEncoding('utf8');
|
|
|
|
sql = '';
|
|
|
|
process.stdin.on('data', function(chunk) {
|
|
|
|
// TODO: some sanity checking, like trim the line or check if it ends with semicolon
|
|
|
|
processQuery(chunk);
|
2012-08-08 20:28:07 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
processQuery(sql);
|
|
|
|
}
|
2012-08-08 17:42:11 +08:00
|
|
|
|
2012-08-08 20:28:07 +08:00
|
|
|
// -- Perform the request
|
2012-08-08 17:42:11 +08:00
|
|
|
|
2012-08-08 21:02:43 +08:00
|
|
|
function processQuery(sql, callback)
|
2012-08-08 20:28:07 +08:00
|
|
|
{
|
2012-08-08 23:27:29 +08:00
|
|
|
|
|
|
|
var post_data = 'q=' + encodeURIComponent(sql);
|
|
|
|
|
2012-08-08 20:28:07 +08:00
|
|
|
var opt = {
|
|
|
|
host: hostname,
|
|
|
|
port: port,
|
2012-08-08 23:27:29 +08:00
|
|
|
path: '/api/v' + api_version + '/sql?format=' + encodeURIComponent(format),
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
|
'Content-Length': post_data.length
|
|
|
|
}
|
2012-08-08 20:28:07 +08:00
|
|
|
};
|
|
|
|
|
2012-08-08 22:49:15 +08:00
|
|
|
if ( typeof(api_key) != 'undefined' ) opt.path += '&api_key=' + api_key;
|
2012-08-08 20:28:07 +08:00
|
|
|
if ( typeof(decimal_places) != 'undefined' ) opt.path += '&dp=' + decimal_places;
|
2013-05-29 18:35:08 +08:00
|
|
|
if ( typeof(skipfields) != 'undefined' ) opt.path += '&skipfields=' + encodeURIComponent(skipfields);
|
2012-08-08 20:28:07 +08:00
|
|
|
|
|
|
|
var body = '';
|
2012-08-08 21:02:43 +08:00
|
|
|
var request = 'http://' + opt.host + ':' + opt.port + opt.path;
|
2013-07-02 22:40:18 +08:00
|
|
|
if ( verbose ) {
|
|
|
|
console.log("Posting " + post_data + " to ", request);
|
|
|
|
}
|
2012-08-08 20:28:07 +08:00
|
|
|
|
2012-08-08 23:27:29 +08:00
|
|
|
var req = http.request(opt, function(res) {
|
2012-08-08 21:02:43 +08:00
|
|
|
//console.log("Response status: " + res.statusCode);
|
2012-08-08 20:28:07 +08:00
|
|
|
res.on('data', function(chunk) {
|
|
|
|
body += chunk;
|
|
|
|
//console.log("data: "); console.dir(json);
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on('end', function() {
|
2012-08-08 21:02:43 +08:00
|
|
|
console.log("Request:", request);
|
2012-08-08 23:27:29 +08:00
|
|
|
var sqlprint = sql.length > 100 ? sql.substring(0, 100) + ' ... [truncated ' + (sql.length-100) + ' bytes]' : sql;
|
|
|
|
sqlprint = sqlprint.split('\n').join(' ');
|
2012-11-24 00:18:39 +08:00
|
|
|
if ( echo_queries ) console.log("Query:", sqlprint);
|
2012-08-08 21:02:43 +08:00
|
|
|
console.log("Response status: " + res.statusCode);
|
|
|
|
console.log('Response body:');
|
2012-08-08 20:28:07 +08:00
|
|
|
console.dir(body);
|
2012-08-08 21:02:43 +08:00
|
|
|
if ( callback ) callback();
|
2012-08-08 20:28:07 +08:00
|
|
|
});
|
2012-08-08 21:02:43 +08:00
|
|
|
|
2012-08-08 20:28:07 +08:00
|
|
|
}).on('error', function(e) {
|
2012-08-08 21:02:43 +08:00
|
|
|
console.log("Request:", request);
|
|
|
|
console.log("Error: " + e.message);
|
|
|
|
if ( callback ) callback();
|
2012-08-08 17:42:11 +08:00
|
|
|
});
|
|
|
|
|
2012-08-08 23:27:29 +08:00
|
|
|
req.write(post_data);
|
|
|
|
req.end();
|
|
|
|
|
2012-08-08 20:28:07 +08:00
|
|
|
}
|