-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
313 lines (262 loc) · 13.7 KB
/
Copy pathscripts.js
File metadata and controls
313 lines (262 loc) · 13.7 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
var sql="";
// value is length for int, or list for enum
var columnsParameters = {
"id" : { "type":"int","value":"11","autoincrement":"true","index":"primary key" },
"int" : { "type":"int","value":"11","autoincrement":"","index":"" },
"enum" : { "type":"enum","value":"'val1','val2'","autoincrement":"","index":"" },
"date" : { "type":"date","value":'',"autoincrement":"","index":"" },
"bool" : { "type":"tinyint","value":'1',"autoincrement":"","index":"" },
"text" : { "type":"text","value":'',"autoincrement":"","index":"" },
"varchar" : { "type":"varchar", "value":"100","autoincrement":"","index":"" },
"timestamp" : { "type":"timestamp","value":'',"autoincrement":"","index":"" }
}
// first value is possible user value, 2nd value is mapping to a preset value defined in columnOptions
var columnGuesses={
// ints
"id": "id",
"pos": "int",
"position":"int",
"user_id":"int",
// bools
"is_active":"bool",
"is_highlighted":"bool",
"is_visible":"bool",
// dates
"date":"date",
"dob":"date",
"registration_date":"date",
// varchars
"name":"varchar",
"email":"varchar",
"title":"varchar",
// texts
"comments":"text",
"comment":"text",
"text":"text",
//timestamp
"timestamp":"timestamp",
"registration":"timestamp"
}
// pass col name, it guesses type and then additionl params
function predictType(columnName){
var columnName=columnName.toLowerCase();
var columnType ;
// if could guess it
if(columnGuesses[columnName]) { columnType = columnGuesses[columnName]; }
// otherwise default to varchar
else { columnType = 'varchar' }
// get guessed column type parameters
if(columnsParameters[columnType]) {
columnParameters = columnsParameters[columnType];
}
var column = {};
column['type'] = columnType ;
column['parameters'] = columnParameters ;
return column;
}
// cleans a string
function string_to_slug(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '_') // collapse whitespace and replace by -
.replace(/-+/g, '_'); // collapse dashes
return str;
}
var dataArray = {
tables: []
};
// does all the work
function process(e,data){
var i,j,name,predictedTablesHtml="";
var tables=data.split(/\r\n|\r|\n/g); // split each table set
var columns=[];
dataArray = {
tables: []
};
for(i=0;i<tables.length;i++){ // i is the table count
name=tables[i].split(":");
tableName = string_to_slug(name[0]);
var tableData = {
table_name : tableName,
id : i,
columns: []
};
predictedTablesHtml+="<section data-table-id='"+ i +"'><div rel='table' id='t"+i+"' data-table-id='"+ i +"' class='tag table'>"+tableName+"</div>";
if(name[1]){ // if entries found after :
columns=name[1].split(","); // split column names
for(j=0;j<columns.length;j++){ // j is the column count
columnName = string_to_slug(columns[j]); // remove all white spaces and replace special chars
columnType = predictType(columnName);
var columnData = {
column_name : columnName,
id : 'c'+i+j,
properties: []
}
columnData.properties['type'] = columnType.parameters.type ;
columnData.properties['value'] = columnType.parameters.value ;
columnData.properties['autoincrement'] = columnType.parameters.autoincrement;
columnData.properties['index'] = columnType.parameters.index ;
columnData.properties['id'] = "p"+i+j;
tableData.columns.push(columnData);
// what's the col type (int, varchar, etc.)
var columnParams = "<span id='c"+i+j+"'class='columnType editableTypes'>" + columnType.parameters.type + "</span>";
// what's the col parameter value ( varchr->255, enum vals..)
if (columnType.parameters.value) {
columnParams += "<span id='p"+i+j+"' class='columnParameters editable'>"+columnType.parameters.value+"</span>";
}
predictedTablesHtml+="\
<div rel='column' data-column-id='"+ j +"'>\
<div class='tag column'>" + columnName + "</div>\
<div class='tag column_type'>" + columnParams + "</div>\
</div>";
}
predictedTablesHtml += "</section>";//close table
}
dataArray.tables.push(tableData);
}
$("#visualData").html(predictedTablesHtml);
convertToSql();
initEditables(); // reinitialize editable fields
}
function convertToSql(){
var sql = '';
$.each(dataArray.tables, function(tableIndex, table){
sql += "CREATE TABLE IF NOT EXISTS `" + table.table_name + "` (\n";
if (table.columns){
var indexes = '';
$.each(table.columns, function(columnIndex,column){
var autoIncrement = colValue = '';
if (column.properties.autoincrement == 'true') {
autoIncrement = "auto_increment";
}
if (column.properties.value) {
colValue = "(" + column.properties.value + ")";
}
if (column.properties.index) { // params for ex: varchar(255) or enum(...)
indexes += "\t " + column.properties.index + "(`" + column.column_name + "`)";
}
sql += "\t`"+column.column_name+"` " + column.properties.type + colValue +" NOT NULL " + autoIncrement + ",\n"; // autoincrement, index, type, value
});
sql += indexes;
}
sql += "\n) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;\n\n";
});
$("#sql").html('');
$("#sql").html(sql);
$("#sqlTextarea").html(sql);
$('#sql').each(function(i, e) {hljs.highlightBlock(e)});
}
function updateTextBox(){
var text = '';
$.each(dataArray.tables, function(tableIndex, table){
text += table.table_name + ": ";
if (table.columns){
$.each(table.columns, function(columnIndex,column){
text += column.column_name+",";
});
}
text += "\n";
});
$("#txt").val(text);
}
// check if parameter of selected col type are correct
function validateColumnTypeParameters(columnType, parametersElement, columnElement, numberOfColumnDetails){
// check for predifined col type
// if exists, get its default val/value
if (columnsParameters[columnType]) {
if (columnsParameters[columnType].value) {
// update previous value
if (numberOfColumnDetails == 2) { // 2 = col type + params, 1 = only col type exists
parametersElement.html(columnsParameters[columnType].value);
}
// add back params element if it doesnt exist
else {
columnElement.append("<span class='columnParameters editable'>"+columnsParameters[columnType].value+"</span>");
}
// remove params if not exist
}else {
$(parametersElement).remove();
}
// update object
var tableId = columnElement.parent().parent().attr('data-table-id');
var colId = columnElement.parent().attr('data-column-id');
updateParameterById('p' + tableId + colId,columnType);
}
// type doesnt exist
else {
alert("Invalid column type");
}
}
// reinitalize editables
function initEditables(){
$('.editable').editable(function(value, settings) {
var tableId = $(this).parent().parent().parent().attr('data-table-id');
var colId = $(this).parent().parent().attr('data-column-id');
updateParameterPropertyById('p' + tableId + colId,value);
convertToSql();
return(value);
}, {
type : 'text'
}
);
$('.editableTypes').editable(function(value, settings) {
var children = $(this).parent().children().length;
validateColumnTypeParameters(value, $(this).parent().children('.columnParameters'), $(this).parent(), children);
convertToSql();
return(value);
}, {
type : 'select',
data : {'varchar':'varchar','enum':'enum','date':'date','text':'text','boolean':'boolean','int':'int'},
onblur : 'submit'
}
);
}
function updateParameterById(id, columnType) {
$.each(dataArray.tables, function(tableIndex, table){
if (table.columns){
$.each(table.columns, function(columnIndex,column){
if (column.properties) {
if (column.properties.id == id) {
column.properties['type'] = (columnsParameters[columnType].type) ? columnsParameters[columnType].type : undefined;
column.properties['value'] = (columnsParameters[columnType].value) ? columnsParameters[columnType].value : undefined;
column.properties['autoincrement'] = (columnsParameters[columnType].autoincrement) ? columnsParameters[columnType].autoincrement: undefined;
column.properties['index'] = (columnsParameters[columnType].index) ? columnsParameters[columnType].index: undefined;
}
else {
return null; // The object was not found
}
}
});
}
});
}
function updateParameterPropertyById(id,value) {
$.each(dataArray.tables, function(tableIndex, table){
if (table.columns){
$.each(table.columns, function(columnIndex,column){
if (column.properties) {
if (column.properties.id == id) {
column.properties['value'] = value;
}
else {
return null; // The object was not found
}
}
});
}
});
}
$(document).ready(function(){
initEditables();
process(null,$("#txt").val() );
$("#txt").keyup(function(e){
process(event,this.value);
});
});