-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (106 loc) · 3.15 KB
/
Copy pathindex.js
File metadata and controls
140 lines (106 loc) · 3.15 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
/* requires */
var util = require('util');
var Transform = require('stream').Transform;
/* constructor */
function CSV2SQL(options) {
// allow use without new
if (!(this instanceof CSV2SQL)) {
return new CSV2SQL(options);
}
this.internalBuffer = '';
this.isFirstDataRow = true;
this.isFirstRowColumnNames = true;
this.isFirstChunk = true;
this.tableName = options.tableName || 'undefined';
this.dbName = options.dbName || false;
this.dropTable = options.dropTable || false;
this.seperator = options.seperator || ',';
this.lineSeperator = options.lineSeperator || '\n';
//helper functions
this.insertColumnNames = insertColumnNames;
this.lineToInsert = lineToInsert;
//init Transform, call super constructor
Transform.call(this, options);
}
util.inherits(CSV2SQL, Transform);
/* implement transform stream */
//TODO: encoding not 'sticking'
CSV2SQL.prototype._transform = function(chunk, enc, cb) {
this.internalBuffer += chunk.toString();
var newLinePos;
var line;
var linePush;
if (this.isFirstChunk && this.dbName !== false) {
this.push('USE ' + this.dbName + ';\n');
}
if (this.isFirstChunk && this.dropTable !== false) {
this.push('DROP TABLE IF EXISTS ' + this.tableName + ';\n');
}
if (this.isFirstChunk) {
this.isFirstChunk = false;
}
newLinePos = this.internalBuffer.indexOf(this.lineSeperator);
while (newLinePos !== -1) {
line = this.internalBuffer.substring(0, newLinePos);
this.internalBuffer = this.internalBuffer.substring(newLinePos + 1);
if (this.isFirstRowColumnNames) {
linePush = this.insertColumnNames(line);
} else {
linePush = this.lineToInsert(line);
}
newLinePos = this.internalBuffer.indexOf(this.lineSeperator);
this.push(linePush + '\n');
}
cb();
};
/* implement transform flush 'event' */
//after all the chunks have been processed, put a ';' to finish off the INSERT
CSV2SQL.prototype._flush = function(cb) {
this.push(';');
cb();
};
/* export */
module.exports = CSV2SQL;
/* helper */
function insertColumnNames(line) {
var columnNamesArr = line.split(this.seperator);
var columnNames = '(';
for (var i = 0; i < columnNamesArr.length; i++) {
columnNames += columnNamesArr[i] + ',';
}
//remove trailing comma
columnNames = columnNames.substring(0, columnNames.length - 1);
columnNames += ')';
var insert = 'INSERT INTO ' + this.tableName + ' ' + columnNames + ' ' +
'VALUES';
this.isFirstRowColumnNames = false;
return insert;
}
/* helper */
function lineToInsert(line) {
//TODO: use a csv parser here, or write own
var dataArr = line.split(this.seperator);
var row;
//insert comma's between VALUES (..), (..), ... , (..)
if (this.isFirstDataRow) {
row = '(';
this.isFirstDataRow = false;
} else {
row = ',(';
}
//build up the row (a, b, ... , c)
for (var i = 0; i < dataArr.length; i++) {
if (dataArr[i] === '' || dataArr[i] === 'NULL') {
row += 'NULL';
} else {
//enclose datums in quotes
row += '"' + dataArr[i] + '"';
}
//insert comma's between datums
if (i !== dataArr.length - 1) {
row += ',';
}
}
row += ')';
return row;
}