-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconverter.js
159 lines (129 loc) · 3.93 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
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
var Converter = (function () {
var _fs = null;
var _fileLines = null;
var _currentLine = null;
var _collections = [];
var reportError = function (errorText) {
console.log('ERROR: ' + errorText);
process.exit(1);
};
var getNextLine = function () {
return _fileLines.shift().trim();
};
var hasMoreLines = function () {
return _fileLines.length > 0;
};
var readFile = function (fileName) {
var fileAsString = _fs.readFileSync(fileName, 'utf8');
_fileLines = fileAsString.split('\n');
};
var startsWith = function (str, textToFind) {
return str.trim().indexOf(textToFind.trim()) === 0;
};
var convertData = function (data, type) {
if(startsWith(type, 'varchar') || startsWith(type, 'text') || startsWith(type, 'date')) {
return data;
}
else if(startsWith(type, 'int') || startsWith(type, 'decimal')) {
return Number(data);
}
else if(startsWith(type, 'tinyint')) {
return data == 1;
}
else {
console.log('Don\'t know this type: ' + type);
return data;
}
};
var readNextTableDef = function () {
while(hasMoreLines()) {
var currentLine = getNextLine();
if(startsWith(currentLine, 'CREATE TABLE')) {
var tableName = currentLine.split('`')[1];
console.log('Converting table: ' + tableName);
currentLine = getNextLine();
var fields = [];
while(startsWith(currentLine, '`')) {
var parts = currentLine.split('`');
var fieldName = parts[1];
var fieldType = parts[2].split(' ')[1];
if(fieldName === 'id') {
fieldName = '_id';
fieldType = 'text';
}
fields.push({
name: fieldName,
type: fieldType
});
currentLine = getNextLine();
}
_collections.push({
name: tableName,
fields: fields
});
return;
}
}
};
var readTableValues = function () {
var currentCollection = _collections[_collections.length - 1];
var tableName = currentCollection.name;
var fields = currentCollection.fields;
while(hasMoreLines()) {
var currentLine = getNextLine();
if(startsWith(currentLine, 'INSERT INTO')) {
currentLine = currentLine.replace('INSERT INTO `' + tableName + '` VALUES ', '');
var index = 1;
var valueId = 0
var insideString = false;
var currentValue = '';
var values = [];
var pair = {};
while(index < currentLine.length) {
var previousChar = currentLine.charAt(index - 1);
var currentChar = currentLine.charAt(index);
if((currentChar === ',' || currentChar === ')') && !insideString) {
var field = fields[valueId];
pair[field.name] = convertData(currentValue, field.type);
valueId++;
currentValue = '';
if(currentChar === ')') {
index += 2;
values.push(pair);
pair = {};
valueId = 0;
}
}
else if(currentChar === "'" && previousChar !== '\\') {
insideString = !insideString;
}
else {
currentValue = currentValue + currentChar;
}
index++;
}
_collections[_collections.length - 1].values = values;
return;
}
}
};
return {
init: function () {
if(process.argv.length != 3) {
reportError(':-)Please specify exactly one mysqldump input file');
}
_fs = require('fs');
var fileName = process.argv[2];
readFile(fileName);
while(hasMoreLines()) {
readNextTableDef();
readTableValues();
}
for(var i = 0; i < _collections.length; i++) {
_fs.writeFileSync(_collections[i].name + '.json', JSON.stringify(_collections[i].values, undefined, 2));
}
process.exit();
}
};
})();
Converter.init();