-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.js
194 lines (181 loc) · 5.23 KB
/
sql.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
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
/*
* SQL
*/
(function(exports) {
"use strict";
var fs = require('fs')
,util = require('util')
,stream = require('stream')
,es = require('event-stream');
var separator;
var cols = ['seq', 'first', 'last', 'age', 'street', 'city', 'state', 'zip', 'dollar', 'pick'];
const pattern = /[^(가-힣ㄱ-ㅎㅏ-ㅣa-zA-Z0-9)]/gi;
// Error Handler
function errorHandler(error){
this.errorMessage = error;
}
function getIndex(arr, value){
var left = value.split(".");
var v = (typeof left[1] !== 'undefined') ? left[1] : value;
for(var index in arr){
if(arr[index] == v) return index;
}
}
// Table Grouping
function checkDuplicateAlias(files){
try{
var alias = [];
files.forEach(function (d) {
var name = d.toString().split("/");
var filename = name[name.length-1];
if(typeof alias[filename] === 'undefined') alias[filename] = d;
else throw new errorHandler('Deplicate File Alias : ' + d);
});
return alias;
} catch(e) {
console.log("error :" + e.errorMessage);
return null;
}
}
// Query Execute
function exec (queryData, fileAlias) {
// Laod Table
var process;
// SQL
switch(queryData.cmd){
case "SELECT":
// Table
console.log(queryData);
process = __condition(queryData, fileAlias);
if(!process) process = __table(fileAlias, null);
else process.then(function(condition){ return __table(fileAlias, condition) }) // where
.then(function(res){ return __operator(res); }) // Joining
.then(function(res){ return __print(res); }); // result
break;
}
}
// Query Print(Select)
var __cols = function(d, reocrd){
return new Promise(function(__callback, reject){
setTimeout(function(){
//console.log(reocrd);
}, 1000);
});
}
// Query Condition(Col)
var __print = function(d){
return new Promise(function(__callback, reject){
setTimeout(function(){
//console.log(d);
}, 1000);
});
}
var __operator = function(group){
var pop = [];
group.forEach(function(d, i){
if(pop.length < 1){
pop = d.data;
}
else {
if(d.logic == 'and'){
// and
var res = [];
d.data.forEach(function(row){
pop.forEach(function(p){
if(JSON.stringify(row) === JSON.stringify(p)){
res.push(row);
}
});
});
pop = res;
} else {
// Or
var res = [];
d.data.forEach(function(row){
pop.forEach(function(p, i){
if(JSON.stringify(row) === JSON.stringify(p)) pop.splice(i, 1);
});
});
}
}
});
return pop;
}
function ConditionParser(cond, logic){
var stack = [];
cond.terms.forEach(function(c){
if(typeof c.logic !== 'undefined'){
if(c.terms.length > 1){
c.terms.forEach(function(t){
stack.push({ logic : c.logic, operator: t.operator, column: t.left, idx: getIndex(cols, t.left), value: t.right, data: [] });
});
} else stack.push(ConditionParser(c, logic));
}
else {
stack.push({ logic : logic, operator: c.operator, column: c.left, idx: getIndex(cols, c.left), value: c.right, data: [] });
}
})
return stack;
}
// Query Condition(Where)
var __condition = function(d, fileAlias){
return new Promise(function(__callback, reject){
setTimeout(function(){
try{
var stack = ConditionParser(d.condition, d.condition.logic);
return __callback(stack);
}catch(e){
console.log("Invalid Query : " + e);
return null;
}
}, 1000);
});
}
// Query Table(From)
var __table = function(f, condition){
return new Promise(function(_callback, reject){
setTimeout(function(){
for(var i in f){
var stream = fs.createReadStream(f[i]);
stream.pipe(es.split())
.pipe(es.mapSync(function(line){
stream.pause();
var record = line.split(separator);
condition.forEach(function(c, i){
var value = c.value.replace(pattern, "")
if(record[c.idx] == value){
condition[i].data.push(record);
}
});
stream.resume();
}))
.on('error', function(){
return reject('ReadStream Error');
})
.on('end', function(){
return _callback(condition);
});
}
}, 1000);
});
}
// Get Data
exports.loadData = function (queryData, files, delimiter){
try{
separator = delimiter;
var fileAlias = checkDuplicateAlias(files);
var tables = queryData.table;
for(var t in tables){
var check = false;
for(var f in fileAlias){
if(tables[t].table == f) check = true;
}
if(!check) throw new errorHandler('Not Found File: ' + tables[t].table);
}
return exec(queryData, fileAlias);
}catch(e){
console.log("ERROR : " + e.errorMessage);
return null;
}
}
}(typeof exports === "undefined" ? (this.sql = {}) : exports));