forked from jub3i/csv2sql-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (114 loc) · 3.46 KB
/
index.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
/* 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.truncateTable = options.truncateTable || false;
this.seperator = options.seperator || ',';
this.lineSeperator = options.lineSeperator || '\n';
this.columnNameSeparator = options.columnNameSeparator || '`';
//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.truncateTable !== false) {
this.push(`TRUNCATE TABLE ${this.tableName};\n`);
}
if (this.isFirstChunk) {
this.isFirstChunk = false;
}
newLinePos = this.internalBuffer.indexOf(this.lineSeperator);
let currentLine = 0;
while (newLinePos !== -1) {
if (currentLine > 100000000) {
return;
}
line = this.internalBuffer.substring(0, newLinePos);
this.internalBuffer = this.internalBuffer.substring(newLinePos + 1);
if (this.isFirstRowColumnNames) {
linePush = this.insertColumnNames(line);
currentLine++;
} else {
linePush = this.lineToInsert(line);
currentLine++;
}
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) {
const colNames = line.split(this.seperator);
const colNamesString = [
this.columnNameSeparator,
colNames.join(`${this.columnNameSeparator},${this.columnNameSeparator}`),
this.columnNameSeparator,
].join('');
this.isFirstRowColumnNames = false;
return `INSERT INTO ${this.tableName} (${colNamesString}) VALUES`;
}
/* helper */
function lineToInsert(line) {
//TODO: use a csv parser here, or write own
var dataArr = line.split(this.seperator);
var row;
// console.log('dataArr', dataArr);
//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 single quotes
row += "'" + dataArr[i] + "'";
}
// insert comma's between datums (what about numbers)?
if (i !== dataArr.length - 1) {
row += ',';
}
}
row += ')';
return row;
}