forked from CartoDB/CartoDB-SQL-API
/
cdbsql
executable file
·196 lines (170 loc) · 5.09 KB
/
cdbsql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env node
// Command line tool for CartoDB SQL API
//
// https://github.com/Vizzuality/CartoDB-SQL-API
//
var http = require('http');
var nodevers = process.versions.node.split('.');
// 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;
var me = process.argv[1];
function usage(exit_code) {
console.log("Usage: " + me + " [OPTIONS] <query>");
if ( hasReadline ) {
console.log(" " + me + " [OPTIONS]");
}
console.log("Options:");
console.log(" -v verbose operations (off)");
console.log(" --help print this help");
console.log(" --user <string> cartodb username (none)");
console.log(" --domain <string> service domain (localhost)");
console.log(" --port <num> service tcp port number (8080)");
console.log(" --api-version <num> API version (1)");
console.log(" --key <string> API authentication key (none)");
console.log(" --format <string> Response format (json)");
console.log(" --dp <num> Decimal places in geojson format (unspecified)");
if ( hasReadline ) {
console.log(" --batch Send all read queries at once (off)");
}
process.exit(exit_code);
}
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;
var domain = 'localhost';
var port = 8080;
var api_version = 1;
var api_key;
var sql;
var decimal_places;
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();
}
else if ( arg == '--domain' ) {
domain = process.argv.shift();
}
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();
}
else if ( arg == '--format' ) {
format = process.argv.shift();
}
else if ( arg == '--dp' ) {
decimal_places = process.argv.shift();
}
else if ( arg == '--batch' ) {
batch_mode = true;
}
else if ( ! sql ) {
sql = arg;
}
else {
usage(1);
}
}
var hostname = username + '.' + domain;
if ( ! sql ) {
if ( readline ) {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
if ( ! batch_mode ) {
rl.setPrompt(hostname + '> ');
rl.prompt();
}
sql = '';
rl.on('line', function(line) {
sql += line;
if ( ! batch_mode ) {
// 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();
}
}).on('close', function() {
if ( batch_mode ) {
if ( sql.length ) {
processQuery(sql);
sql = '';
}
} else {
if ( sql.length ) {
console.warn("Unprocessed sql left: [" + sql + "]");
}
console.log("Good bye");
}
}).on('SIGCONT', function() {
// this is needed so not to exit on stop/resume
rl.prompt();
});
} else {
usage(1);
}
} else {
processQuery(sql);
}
// -- Perform the request
function processQuery(sql, callback)
{
var post_data = 'q=' + encodeURIComponent(sql);
var opt = {
host: hostname,
port: port,
path: '/api/v' + api_version + '/sql?format=' + encodeURIComponent(format),
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
}
};
if ( typeof(api_key) != 'undefined' ) opt.path += '&api_key=' + api_key;
if ( typeof(decimal_places) != 'undefined' ) opt.path += '&dp=' + decimal_places;
var body = '';
var request = 'http://' + opt.host + ':' + opt.port + opt.path;
//console.log("Sending request:", request);
var req = http.request(opt, function(res) {
//console.log("Response status: " + res.statusCode);
res.on('data', function(chunk) {
body += chunk;
//console.log("data: "); console.dir(json);
});
res.on('end', function() {
console.log("Request:", request);
var sqlprint = sql.length > 100 ? sql.substring(0, 100) + ' ... [truncated ' + (sql.length-100) + ' bytes]' : sql;
sqlprint = sqlprint.split('\n').join(' ');
console.log("Query:", sqlprint);
console.log("Response status: " + res.statusCode);
console.log('Response body:');
console.dir(body);
if ( callback ) callback();
});
}).on('error', function(e) {
console.log("Request:", request);
console.log("Error: " + e.message);
if ( callback ) callback();
});
req.write(post_data);
req.end();
}