-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.js
More file actions
393 lines (349 loc) · 14.5 KB
/
Copy pathsql.js
File metadata and controls
393 lines (349 loc) · 14.5 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/bin/env js
/*
* Copyright (C) Vadim Shchukin (vsshchukin@gmail.com) 2016
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
function Application() {
}
Application.prototype.parseParameters = function(parameters) {
this.parameters = {};
var hostname, port, location;
var username, password;
var inputFile;
var verbose;
var printTableName;
var printTableColumns;
var formatAsCSV;
var delimiter;
for (var index = 0; index < parameters.length; index++) {
if (parameters[index].match(/^(-h)|(--help)$/)) {
this.parameters.help = true;
this.printHelp();
return;
}
if (parameters[index].match(/^(-t)|(--print-table-name)$/)) {
printTableName = true; continue;
}
if (parameters[index].match(/^(-c)|(--print-table-columns)$/)) {
printTableColumns = true; continue;
}
if (parameters[index].match(/^(-x)|(--format-as-csv)$/)) {
formatAsCSV = true; continue;
}
if (parameters[index].match(/^(-v)|(--verbose)$/)) {
verbose = true; continue;
}
if (hostname === '') {
hostname = parameters[index]; continue;
} else if (port === '') {
port = parameters[index]; continue;
} else if (location === '') {
location = parameters[index]; continue;
} else if (username === '') {
username = parameters[index]; continue;
} else if (password === '') {
password = parameters[index]; continue;
} else if (inputFile === '') {
inputFile = parameters[index]; continue;
} else if (delimiter === '') {
delimiter = parameters[index]; continue;
}
var match = parameters[index].match(/^(?:(?:-s)|(?:--hostname=))(.+)?$/);
if (match) {
hostname = match[1] ? match[1] : ''; continue;
}
var match = parameters[index].match(/^(?:(?:-p)|(?:--port=))(.+)?$/);
if (match) {
port = match[1] ? match[1] : ''; continue;
}
var match = parameters[index].match(/^(?:(?:-l)|(?:--location=))(.+)?$/);
if (match) {
location = match[1] ? match[1] : ''; continue;
}
var match = parameters[index].match(/^(?:(?:-u)|(?:--username=))(.+)?$/);
if (match) {
username = match[1] ? match[1] : ''; continue;
}
var match = parameters[index].match(/^(?:(?:-a)|(?:--password=))(.+)?$/);
if (match) {
password = match[1] ? match[1] : ''; continue;
}
var match = parameters[index].match(/^(?:(?:-i)|(?:--input-file=))(.+)?$/);
if (match) {
inputFile = match[1] ? match[1] : ''; continue;
}
var match = parameters[index].match(/^(?:(?:-d)|(?:--csv-delimiter=))(.+)?$/);
if (match) {
delimiter = match[1] ? match[1] : ''; continue;
}
throw new Error("invalid option '" + parameters[index] + "'\nsee 'sql.js -h' for usage");
}
if (!port || !location || !password) {
var missingOptions = [];
if (!port) {
missingOptions.push('port');
}
if (!location) {
missingOptions.push('location');
}
if (!password) {
missingOptions.push('password');
}
throw new Error("missing options: " + missingOptions.join(', ') + "\nsee 'sql.js -h' for usage");
}
if (!username) {
username = java.lang.System.getProperty('user.name');
}
if (!delimiter) {
delimiter = ',';
}
this.parameters = {};
this.parameters.hostname = hostname;
this.parameters.port = port;
this.parameters.location = location;
this.parameters.username = username;
this.parameters.password = password;
this.parameters.inputFile = inputFile;
this.parameters.verbose = verbose;
this.parameters.printTableName = printTableName;
this.parameters.printTableColumns = printTableColumns;
this.parameters.formatAsCSV = formatAsCSV;
this.parameters.delimiter = delimiter;
}
Application.prototype.printHelp = function() {
var help = 'usage: sql.js options\n\noptions:\n';
help += ' -h, --help print this help and exit.\n';
help += ' -s HOSTNAME, --hostname=HOSTNAME subsystem hostname. If not specified \n';
help += " then 'localhost' will be used.\n";
help += ' -p PORT, --port=PORT subsystem port.\n';
help += ' -l LOCATION, --location=LOCATION subsystem location.\n';
help += ' -u USERNAME, --username=USERNAME DB2 username. If not specified then \n';
help += ' current user account name will be used.\n';
help += ' -a PASSWORD, --password=PASSWORD DB2 password.\n';
help += ' -i INPUT, --input-file=INPUT input file containing SQL statements. \n';
help += ' If not specified then SQL statements \n';
help += ' would be read from the standard input.\n';
help += ' -v, --verbose enable verbose mode.\n';
help += ' -t, --print-table-name print table name.\n';
help += ' -c, --print-table-columns print table columns.\n';
help += ' -x, --format-as-csv use CSV format.\n';
help += ' -d, --csv-delimiter=DELIMITER CSV delimiter. If not specified then "," will be used.';
print(help);
};
Application.prototype.connectToDB2 = function() {
var parameters = this.parameters;
var properties = new java.util.Properties();
properties.setProperty('user', parameters.username);
properties.setProperty('password', parameters.password);
properties.setProperty('retrieveMessagesFromServerOnGetMessage', true);
var hostname = parameters.hostname ? parameters.hostname : 'localhost';
var port = new java.lang.Integer(parameters.port);
var connectionURL = 'jdbc:db2://' + hostname + ':' + port + '/' + parameters.location;
java.lang.System.setProperty('db2.jcc.charsetDecoderEncoder', '3');
java.lang.Class.forName('com.ibm.db2.jcc.DB2Driver');
if (this.parameters.verbose) {
java.lang.System.out.format("connecting to '%s:%d/%s' host%n", hostname, port, parameters.location);
}
this.connection = java.sql.DriverManager.getConnection(connectionURL, properties);
};
Application.prototype.printResultSetAsTable = function(resultSet) {
var metaData = resultSet.getMetaData();
var columnCount = metaData.getColumnCount();
var columnNames = [];
var columnWidths = [];
for (var columnIndex = 0; columnIndex < columnCount; columnIndex++) {
var columnName = metaData.getColumnName(columnIndex + 1);
columnNames.push(columnName);
columnWidths.push(columnName.length());
}
var rowStrings = [];
while (resultSet.next()) {
var columnStrings = [];
for (var columnIndex = 0; columnIndex < columnCount; columnIndex++) {
var columnString = resultSet.getString(columnIndex + 1);
columnStrings.push(columnString);
if (!resultSet.wasNull() && columnString.length() > columnWidths[columnIndex]) {
columnWidths[columnIndex] = columnString.length();
}
}
rowStrings.push(columnStrings);
}
var tableName = metaData.getTableName(1);
var schemaName = metaData.getSchemaName(1);
if (schemaName.length()) {
tableName = String(java.lang.String.format('%s.%s', schemaName, tableName));
}
var columnWidthsSum = columnWidths.reduce(function(previous, current) {
return previous + current;
}, 0);
if (tableName.length > columnWidthsSum) {
columnWidths[columnWidths.length - 1] = tableName.length;
}
var paddedColumnNames = [];
for (var columnIndex = 0; columnIndex < columnCount; columnIndex++) {
var columnName = columnNames[columnIndex];
var format = "%-" + columnWidths[columnIndex] + "s";
paddedColumnNames.push(java.lang.String.format(format, columnName));
}
var lineLength = paddedColumnNames.join(' | ').length;
var format = "| %-" + lineLength + "s |";
var tableNameLine = java.lang.String.format(format, tableName);
var headerLine = java.lang.String.format('| %s |', paddedColumnNames.join(' | '));
var separatorLine = java.lang.String.format('+-%s-+', new Array(lineLength + 1).join('-'));
if (this.parameters.printTableName) {
print(separatorLine);
print(tableNameLine);
}
if (this.parameters.printTableColumns) {
print(separatorLine);
print(headerLine);
}
print(separatorLine);
for (var rowIndex = 0; rowIndex < rowStrings.length; rowIndex++) {
var columnStrings = rowStrings[rowIndex];
var paddedColumnStrings = [];
for (var columnIndex = 0; columnIndex < columnCount; columnIndex++) {
var columnString = columnStrings[columnIndex];
var format = "%-" + columnWidths[columnIndex] + "s";
paddedColumnStrings.push(java.lang.String.format(format, columnString));
}
print(java.lang.String.format('| %s |', paddedColumnStrings.join(' | ')));
}
print(separatorLine);
};
Application.prototype.printResultSetAsCSV = function(resultSet) {
function formatStringAsCSV(string) {
if (string.indexOf('\n') !== -1 || string.indexOf('"') !== -1 || string.indexOf(delimiter) !== -1) {
string = '"' + string.replace(/"/g, '""') + '"';
}
return string;
}
var delimiter = this.parameters.delimiter;
var metaData = resultSet.getMetaData();
var columnCount = metaData.getColumnCount();
if (this.parameters.printTableName) {
var tableName = metaData.getTableName(1);
var schemaName = metaData.getSchemaName(1);
if (schemaName.length()) {
tableName = schemaName + '.' + tableName;
}
print(tableName);
}
if (this.parameters.printTableColumns) {
var outputLine = '';
for (var columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
if (columnIndex > 1) {
outputLine += delimiter;
}
outputLine += formatStringAsCSV(String(metaData.getColumnName(columnIndex)));
}
print(outputLine);
}
while (resultSet.next()) {
var outputLine = '';
for (var columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
if (columnIndex > 1) {
outputLine += delimiter;
}
outputLine += formatStringAsCSV(String(resultSet.getString(columnIndex)));
}
print(outputLine);
}
};
Application.prototype.executeCommand = function(command) {
var statement = this.connection.createStatement();
if (command.match(new RegExp('^\s*SELECT', 'i'))) {
if (this.parameters.verbose) {
print('executing query:');
print(command);
}
var resultSet = statement.executeQuery(command);
if (this.parameters.formatAsCSV) {
this.printResultSetAsCSV(resultSet);
} else {
this.printResultSetAsTable(resultSet);
}
} else {
if (this.parameters.verbose) {
print('executing statement:');
print(command);
}
var rowCount = new java.lang.Integer(statement.executeUpdate(command));
if (this.parameters.verbose) {
java.lang.System.out.format("%d rows affected%n", rowCount);
}
}
};
Application.prototype.process = function(parameters) {
/* process command line parameters */
this.parseParameters(parameters);
var parameters = this.parameters;
if (parameters.help) {
return 0;
}
this.connectToDB2();
var reader;
if (parameters.inputFile) {
reader = new java.io.FileReader(parameters.inputFile);
} else {
reader = new java.io.InputStreamReader(java.lang.System.in);
}
reader = new java.io.BufferedReader(reader);
var command = '';
var commands = [];
var line;
while (line = reader.readLine()) {
var match = line.match(/;\s*$/);
if (match) {
command += line;
this.executeCommand(command);
command = '';
} else {
if (command) {
command += '\n';
}
command += line;
}
}
if (command) {
this.executeCommand(command);
}
return 0;
};
Application.prototype.run = function(parameters) {
try {
return this.process(parameters);
} catch (error if error.javaException) {
var javaException = error.javaException;
var errorMessage = javaException.getMessage();
if (javaException instanceof java.sql.SQLException) {
var errorExpression = /(?:\[.*?\])* *(.+) (?:ERRORCODE|SQLCODE)=(-?\d+), SQLSTATE=([0-9ABCDEF]+)(, DRIVER=([0-9\.]+))?/;
var messageMatch = String(errorMessage).match(errorExpression);
var errorTitle;
if (messageMatch) {
errorMessage = messageMatch[1];
var errorCode = messageMatch[2];
errorTitle = 'SQL error ' + errorCode;
} else {
errorMessage = error.message;
errorTitle = 'SQL error';
}
}
java.lang.System.err.println(errorTitle + ':');
java.lang.System.err.println(errorMessage);
return 8;
} catch (error) {
java.lang.System.err.println(error.message);
return 8;
}
};
java.lang.System.exit(new Application().run(arguments));