-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.js
198 lines (173 loc) · 6.01 KB
/
load.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
195
196
197
198
var mysql = require('mysql');
const Transform = require('./transform');
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/Accounts";
var vals = [];
var fields = [];
var HashMap = require('hashmap');
var con;
var globalFields;
var globalTypes;
var tableInfo=new HashMap();
module.exports = {
createConnection: createConnection,
checkType: checkType
}
function createConnection (host,user,pass) {
con = mysql.createConnection({
host:host,
user:user,
password:pass,
});
con.connect(function(err) {
if (err) throw err;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("contacts").findOne(function(err, res) {
if (err) throw err;
console.log("Connected!");
Transform.TempaddFields(res);
con.query("CREATE DATABASE Accounts", function (err, result) {
if (err) throw err;
console.log("Create database Accounts");
con.query("use Accounts", function (err, result) {
if (err) throw err;
console.log("Switch to Account");
var sql = "CREATE TABLE accounts ("+Transform.createTable().substring(0,Transform.createTable().length-2)+")";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table created");
db.collection("contacts").count(function (err, maxDoc) {
globalFields = Transform.getColums();
globalTypes = Transform.getTypes();
for (var i in globalFields){
tableInfo.set(globalFields[i], globalTypes[i]);
}
addData(db,0,maxDoc);
});
});
});
});
});
});
});
}
var createValue = function (res,qur) {
for (var key in res){
if (res[key] !== null && typeof res[key] === 'object' && Object.keys(res[key]).valueOf()[1]!='id'){
createValue(res[key])
}
else {
vals.push(res[key]);
fields.push(key);
}
}
}
function addData(db,docNum,maxDoc) {
db.collection("contacts").find({},{},{skip:docNum,limit:10000}, function (err, lst) {
lst.forEach(function (items) {
var qur = "";
createValue(items, qur);
for (var i in vals) {
if (vals[i].toString() == 'true') {
vals[i] = 1;
}
else if (vals[i].toString() == 'false') {
vals[i] = 0;
}
qur = qur + "'" + vals[i] + "', ";
}
vals = [];
fields=[];
var sql = "INSERT INTO accounts VALUES (" + qur.substring(0, qur.length - 2) + ")";
con.query(sql, function (err, result) {
if (err)
changeTable(con,items);
docNum = docNum + 1;
if (docNum < maxDoc && docNum % 10000 === 0) {
console.log("10000 record inserted");
console.log("Total records: "+docNum);
addData(db, docNum, maxDoc)
}
else if(docNum== maxDoc){
console.log("FINISH");
db.close();
con.end();
}
});
})
});
}
function changeTable(con,items) {
console.log("Check for different type of columns");
createValue(items);
var newRows = new HashMap();
for (var i in vals) {
newRows.set(fields[i], vals[i]);
}
for (var i in fields){
if (fields[i]!==globalFields[i] || typeof (vals[i]) !== globalTypes[i]){
newRows.set(fields[i], vals[i]);
}
}
newRows.forEach(function(value, key) {
var newkey = "mySQL"+key;
if (tableInfo.get(newkey)!=null && tableInfo.get(newkey)==typeof (newRows.get(key))){
newRows.delete(key);
}
else if (tableInfo.get(newkey)!=null && tableInfo.get(newkey)!=typeof (newRows.get(key))){
var val = newRows.get(key);
var newkey = key+"_"+typeof (newRows.get(key));
newRows.delete(key);
newRows.set(newkey,val);
for (var i in fields) {
if (fields[i]==key){
fields[i]=newkey
vals[i]=val;
}
}
}
});
if (newRows.size > 0){
newRows.forEach(function(value, key) {
var sql ="ALTER TABLE accounts ADD mySQL"+key+" " + checkType(typeof(value))+";";
console.log("Add new column");
con.query(sql, function (err, result) {
if (err) throw err;
});
tableInfo.set("mySQL"+key,typeof(value));
});
}
var map = new HashMap();
for (var i in vals) {
map.set(fields[i], vals[i]);
}
var qur1 = "";
var qur2 = "";
for (var i in fields) {
var res= map.get(fields[i]);
if (res.toString() == 'true') {
res = 1;
}
else if (res.toString() == 'false') {
res = 0;
}
qur1 = qur1 + "'" + res + "', ";
qur2 = qur2 + "mySQL"+fields[i] + ", ";
}
vals = [];
fields = [];
var sql = "INSERT INTO accounts ("+ qur2.substring(0, qur2.length - 2) +") VALUES (" + qur1.substring(0, qur1.length - 2) + ")";
con.query(sql, function (err, result) {
if (err) throw err;
});
}
function checkType (key) {
var map = new HashMap();
map.set("object", "VARCHAR(255)");
map.set("string", "VARCHAR(255)");
map.set("boolean", "Boolean");
map.set("number", "INT");
return map.get(key);
}