-
Notifications
You must be signed in to change notification settings - Fork 98
/
app5.js
150 lines (128 loc) · 3.51 KB
/
app5.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/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 util = require('util');
var path = require('path');
var async = require('async');
var EventEmitter = require('events').EventEmitter;
var client = require('./client');
var fstream = require('fstream');
var concatStream = require('concat-stream');
var home = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
var dirname = process.argv[2] || path.join(home, 'tmp', 'lobs');
var schema = client.get('user');
async.waterfall([connect, init, prepare, copyDir], done);
function connect(cb) {
client.connect(cb);
}
function dropTable(cb) {
var sql = 'drop table TEST_LOBS';
client.exec(sql, cb);
}
function init(cb) {
dropTable(function droped(err) {
/* jshint unused:false */
// ignore error
createTable(cb);
});
}
function createTable(cb) {
var sql = [
'create column table TEST_LOBS (',
'"NAME" NVARCHAR(256) NOT NULL,',
'"DATA" BLOB ST_MEMORY_LOB,',
'PRIMARY KEY ("NAME"))'
].join('\n');
client.exec(sql, cb);
}
function prepare(cb) {
var sql = 'insert into TEST_LOBS values (?, ?)';
client.prepare(sql, cb);
}
function copyDir(statement, cb) {
console.time('time');
function isChildFile() {
/* jshint validthis:true */
return this.parent === r && this.type === 'File' || this === r;
}
var r = fstream.Reader({
path: dirname,
filter: isChildFile
});
function getParams(props, data) {
return [props.basename, data];
}
var adapter = new FstreamAdapter(statement, getParams);
function finish(err) {
adapter.removeListener('error', finish);
adapter.removeListener('close', finish);
cb(err);
}
adapter.once('error', finish);
adapter.once('close', finish);
r.pipe(adapter);
}
function done(err) {
console.timeEnd('time');
if (err) {
console.error('Error', err);
} else {
console.log('Copied dir %s to table "%s"."TEST_LOBS"', dirname, schema);
}
client.end();
}
util.inherits(FstreamAdapter, EventEmitter);
function FstreamAdapter(statement, createParams) {
EventEmitter.call(this);
this._end = false;
this._busy = false;
this._statement = statement;
this._createParams = createParams;
}
FstreamAdapter.prototype.execStatement = function execStatement(entry, data) {
var self = this;
var params = this._createParams(entry.props, data);
function handleResult(err) {
self._busy = false;
if (err) {
return self.emit('error', err);
}
if (self._end) {
return self.emit('close');
}
entry.resume();
}
this._statement.exec(params, handleResult);
};
FstreamAdapter.prototype.add = function add(entry) {
var self = this;
if (this._end) {
return;
}
function handleData(data) {
entry.pause();
self._busy = true;
self.execStatement(entry, data);
}
entry.pipe(concatStream(handleData));
return true;
};
FstreamAdapter.prototype.end = function end() {
this._end = true;
this.emit('end');
if (!this._busy) {
this.emit('close');
}
};