-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconverter.js
131 lines (115 loc) · 3.2 KB
/
converter.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
const jsonfile = require('jsonfile');
const fs = require('fs');
/**
* ===========================
* Command line interface
* ===========================
*/
// Extract command line arguments
const input = process.argv.splice(2);
const [jsonFilename, sqlFilename] = input;
parseIfNotExist();
/**
* ===========================
* Implementation
* ===========================
*/
function parseIfNotExist(){
fs.open(sqlFilename, 'r', function (fileNotExist, _) {
if (fileNotExist) {
converter(input);
} else {
console.log("output file already exists!");
}
})
}
function converter(input) {
// exit if json or sql files are not specified
if (!jsonFilename || !sqlFilename) return 'Error';
const tables = [];
var columns = [];
var columnTypes = [];
var columnInfo = [];
var values = [];
const valueInserts = [];
const createTables = [];
// use jsonfile module to read json file
jsonfile.readFile(jsonFilename, (err, data) => {
if (err) return console.error(err);
const source = data.Data.Data;
fetchTables(source);
for (let i = 0; i < tables.length; i++) {
const tableItem = source[tables[i]];
if (Array.isArray(tableItem)) {
parseArray(tableItem, i);
}
else if (typeof (tableItem) == "object") {
parseObject(tableItem, i);
}
}
const creates = toSql(createTables);
const inserts = toSql(valueInserts);
const combinedSql = creates.concat(`\n` + inserts)
writeOutput(combinedSql)
});
function fetchTables(source) {
for (var i in source) {
tables.push(i);
}
}
function parseArray(tableItem, index) {
for (var i = 0; i < tableItem.length; i++) {
convertObject(tableItem[i]);
if (i == 1) {
columnInfo = []
parseColumnInfo()
createTables.push(`CREATE TABLE IF NOT EXISTS ${tables[index]} (${columnInfo})`)
}
const query = `INSERT INTO ${tables[index]} (${columns}) VALUES (${values})`;
valueInserts.push(query)
}
}
function parseObject(tableItem, index) {
convertObject(tableItem)
parseColumnInfo()
createTables.push(`CREATE TABLE IF NOT EXISTS ${tables[index]} (${columnInfo})`)
const query = `INSERT INTO ${tables[index]} (${columns}) VALUES (${values})`
valueInserts.push(query)
}
function convertObject(item) {
columns = [];
values = [];
for (var i in item) {
columns.push(i);
let value = item[i]
if (typeof value === 'string') {
value = "\"" + value + "\"";
}
if (value == null) {
value = "\"\""
}
values.push(value);
}
}
function parseColumnInfo() {
for (var i = 0; i < columns.length; i++) {
if (typeof (values[i]) == "string") {
columnTypes = "TEXT"
columnInfo.push(`${columns[i]} ${columnTypes}`)
}
else if (typeof (values[i]) == "number") {
columnTypes = "INTERGER"
columnInfo.push(`${columns[i]} ${columnTypes}`)
}
}
}
function toSql(queries) {
return queries.join(`;\n`) + ';';
}
function writeOutput(combinedSql) {
fs.writeFile(sqlFilename, combinedSql, (err2) => {
if (err2) return console.error(err2);
console.log('Done');
});
}
}