-
Notifications
You must be signed in to change notification settings - Fork 98
/
csv.js
132 lines (118 loc) · 3.55 KB
/
csv.js
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
#!/usr/bin/env node
// Copyright 2013 SAP AG.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http: //www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific
// language governing permissions and limitations under the License.
'use strict';
var fs = require('fs');
var os = require('os');
var util = require('util');
var path = require('path');
var url = require('url');
var async = require('async');
var hdb = require('../index');
var defaults = getDefaultParams();
var params;
if (process.argv.length < 3) {
params = defaults;
} else {
params = url.parse(process.argv[2], true, true);
}
var hostname = params.hostname || defaults.hostname;
var port = params.port || defaults.port;
var auth = (params.auth || defaults.auth).split(':');
var user = auth[0];
var password = auth[1];
var query = util._extend({
top: Math.pow(10, 6)
}, params.query);
var segments = params.pathname.match(/^\/?(?:([^.]+)\.)?(.*)/).slice(1);
var schema = segments[0].toUpperCase();
var tablename = segments[1].toUpperCase();
// eslint-disable-next-line no-useless-escape
var filename = tablename.replace(/^\/[^\/]+\//, '').toLowerCase() + '.csv';
filename = path.join(os.tmpdir(), filename);
var client = hdb.createClient({
host: hostname,
port: port,
user: user,
password: password
});
function onerror(err) {
console.log('Network error', err);
}
client.on('error', onerror);
async.waterfall([connect, execute, pipeRows], done);
function connect(cb) {
client.connect(cb);
}
function execute(cb) {
console.time('time');
var sql = util.format('select top %d * from "%s"."%s"',
query.top, schema, tablename);
client.execute(sql, cb);
}
function pipeRows(rs, cb) {
rs.setFetchSize(2048);
var readStream = rs.createArrayStream();
var writeStream = fs.createWriteStream(filename);
function finish(err) {
readStream.removeListener('error', finish);
writeStream.removeListener('finish', finish);
cb(err);
}
readStream.on('error', finish);
writeStream.on('finish', finish);
readStream.pipe(createCsvStringifier(rs.metadata)).pipe(writeStream);
}
function done(err) {
console.timeEnd('time');
client.end();
if (err) {
return console.error(err);
}
console.log('table %s.%s downloaded to file %s',
schema, tablename, path.resolve(filename));
}
function createCsvStringifier(metadata) {
/* jshint evil:true */
var header = metadata.map(function getName(column) {
return column.columnDisplayName;
}).join(';') + '\n';
var functionBody = metadata.reduce(function addLine(body, column) {
body += 'line += row.' + column.columnDisplayName;
if (column.dataType === 13) {
body += '.toString(\'hex\')';
}
body += ';\nline += \';\'\n';
return body;
}, 'var line = \'\';\n') + 'return line;';
return new hdb.Stringifier({
header: header,
footer: '',
seperator: '\n',
stringify: new Function('row', functionBody)
});
}
function getDefaultParams() {
var filename = path.join(__dirname, '..', 'test', 'db', 'config.json');
var config = JSON.parse(fs.readFileSync(filename));
return {
hostname: config.host,
port: config.port,
auth: [config.user, config.password].join(':'),
pathname: 'SYS.TABLES',
query: {
top: 1000
}
};
}