-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautomationInsert V2.js
406 lines (371 loc) · 8.17 KB
/
automationInsert V2.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Require the mysql
var mysql = require('mysql');
// Create a connection with mysql.
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: ""
});
// Stablish the connection.
connection.connect(function(err) {
if (err) {
console.warn('connection failed!', err);
} else {
//console.log('connection established! 1');
}
});
// Run the squl query to Create test data base if not exists for automation data.
connection.query("CREATE DATABASE IF NOT EXISTS aspire", function(err) {
if (err) {
console.warn('connection failed!', err);
} else {
//console.log('connection established! 2');
}
});
// If database exists then use it.
connection.query("USE aspire", function(err) {
if (err) {
console.warn('connection failed!', err);
} else {
//console.log('connection established! 3');
}
});
/**
* Run the given sql query.
* @param {String} runQuery Sql query to be run.
*/
var runSqlQuery = function(runQuery){
console.log(runQuery)
connection.query(runQuery, function(err) {
if (err) {
console.warn('connection failed!', err);
} else {
//console.log('connection established! 3');
}
});
}
// Link for GeoNames
// https://gist.github.com/harpreetkhalsagtbit/e4ac28e02b4624c144f3
// Link for CountryState_PK
// https://gist.github.com/harpreetkhalsagtbit/4d47fa713f4261437b5c
//
// Link for GeoNamesIndianState_PK
// https://gist.github.com/harpreetkhalsagtbit/84a7fcfb23d3455316f1
// Sample input json which define the table ,column and related infomation to create a table in the database.
/*
var inputFields=[
{
dbtable:"country",
dbcolumn:"name",
type:"text",
maxlength:30
},
{
dbtable:"country",
dbcolumn:"code",
type:"text",
maxlength:30
},
{
dbtable:"country",
dbcolumn:"organisation_pk",
type:"text",
maxlength:10
}
]
*/
/*
var inputFields=[
{
dbtable:"country_state",
dbcolumn:"country_pk",
type:"text",
maxlength:30
},
{
dbtable:"country_state",
dbcolumn:"state_pk",
type:"text",
maxlength:30
}
]
*/
/*
var inputFields=[
{
dbtable:"widgets",
dbcolumn:"name",
type:"text",
maxlength:30
},
{
dbtable:"widgets",
dbcolumn:"organisation_pk",
type:"text",
maxlength:10
}
]
*/
var inputFields=[
{
dbtable:"input",
dbcolumn:"type",
type:"text",
maxlength:30
},
{
dbtable:"input",
dbcolumn:"organisation_pk",
type:"text",
maxlength:10
}
]
// Input/sample data to insert in the table.ie automation data to be inserted in the database.
// var store = [
// {
// "name":"Button"
// },
// {
// "name":"DateTextBox"
// },
// {
// "name":"FilteringSelect"
// },
// {
// "name":"RadioButton"
// },
// {
// "name":"TimeTextBox"
// }
// ]
var store = [
{
"type":"Text"
},
{
"type":"Number"
},
{
"type":"Email"
},
{
"type":"Alphanumeric"
},
{
"type":"Date"
}
]
// var table = "country"
// var table = "country_state"
// var table = "widgets"
var table = "input"
// Function to get the sql data type for the attributes.
var getType = function(sqltype, len) {
switch (sqltype) {
case "number":
case "text":
{
return ("VARCHAR (" + len + ")");
}
case "enumeration":
{
return ("ENUM('" + options.join("', '") + "')")
}
case "date":
{
return ("VARCHAR (" + len + ")");
}
case "longtext":
{
return "LONGTEXT";
}
case "bool":
{
return ("bit (" + len + ")");
}
case "password":
{
return ("VARCHAR (" + len + ")");
}
case "image":
{
return "VARCHAR(10)";
}
case "alphanumeric":
{
return ("VARCHAR (" + len + ")");
}
case "url":
{
return ("VARCHAR (" + len + ")");
}
case "time":
{
return ("VARCHAR (" + len + ")");
}
case "Number":
}
}
/**
* Get the table, column and column related information.
* @param {Array} fields Define the table columns and related information
* @return {Array} Return table, column and column related information
*/
var getTableColumn = function(fields){
var allTable =[];
var dataTable={}
var tableInfo = [];
var dbInfo=[];
// Loop to get the uniquely define tables.
for(var data in fields){
if (allTable.indexOf(fields[data].dbtable) == -1) {
allTable.push(fields[data].dbtable)
dataTable={};
dataTable.table = fields[data].dbtable;
tableInfo.push(dataTable)
}
}
// Loop to get the column(s) for the respective table(s).
for (var each in tableInfo){
var finaldata={};
var columns=[];
for(var data in fields){
var columnInfo ={};
if(tableInfo[each].table == fields[data].dbtable ){
columnInfo.columnName = fields[data].dbcolumn;
columnInfo.maxlength = fields[data].maxlength;
columnInfo.type = fields[data].type;
columns.push(columnInfo)
}
}
finaldata.tableName = tableInfo[each].table;
finaldata.columns = columns;
dbInfo.push(finaldata)
}
return dbInfo;
}
/**
* Generate the CREATE sql quey statment.
* @param {Array} fields Define the table columns and related information
* @return {String} CREATE sql query to create table(s) in the database.
*/
var getCreateSqlQuery = function(fields){
// Array valiable to store the respective data ie table and columns and related information needed to create the sql query.
// eg.
// [
// {
// tableName: 'country',
// columns: [
// {
// columnName: 'country_name',
// maxlength: 30,
// type: 'text'
// }
// ]
// },
// {
// tableName: 'city',
// columns: [
// {
// columnName: 'city_id',
// maxlength: 30,
// type: 'text'
// },
// {
// columnName: 'city_name',
// maxlength: 30,
// type: 'text'
// }
// ]
// },
// {
// tableName: 'state',
// columns: [
// {
// columnName: 'state_name',
// maxlength: 30,
// type: 'text'
// }
// ]
// }
// ]
var dbInfo = getTableColumn(fields);
var queryString = '';
// Loop to create sql query which create a table in database
for(var each in dbInfo){
var table = dbInfo[each];
queryString = "CREATE TABLE IF NOT EXISTS " + table.tableName + "(" + table.tableName+"_pk INT PRIMARY KEY AUTO_INCREMENT NOT NULL,";
// Loop to concatinate columns in the sql query.
for(var eachColumn in table.columns){
var column = table.columns[eachColumn];
// When column is not last column in the table.
if(eachColumn != table.columns.length-1){
queryString = queryString + column.columnName + " " + getType(column.type, column.maxlength) +",";
}
// When column is last column in the table.
else {
queryString = queryString + column.columnName + " " + getType(column.type, column.maxlength);
}
}
queryString = queryString + " )";
// Run the sql query.
runSqlQuery(queryString);
}
}
/**
* Get the length of object.
* @param {Object} data Object whose length is to find.
* @return {Number} Return the length of object.
*/
var getLength = function(data){
var count =0;
for(var i in data){
count = count + 1;
}
return count;
}
/**
* Generate the CREATE sql quey statment
* @param {Array} store It is the data to be inserted in the database.
* @return {String} INSERT sql query to insert data in database.
*/
var getInsertSqlQuery = function(data, isRelationShipTable){
var insertQuery= 'INSERT INTO ' + table +' (';
for(each in data) {
insertQuery = insertQuery + each + ",";
}
if(isRelationShipTable) {
insertQuery = insertQuery.substr(0,insertQuery.length-1)
} else {
insertQuery = insertQuery + "organisation_pk" ;
}
insertQuery = insertQuery + ')';
insertQuery = insertQuery + ' VALUES (';
for(each in data) {
if(data[each] == "") {
data[each] = null;
}
insertQuery = insertQuery +'"'+data[each] + '"' + ",";
}
if(isRelationShipTable) {
insertQuery = insertQuery.substr(0,insertQuery.length-1)
} else {
insertQuery = insertQuery + '"*"' ;
}
insertQuery = insertQuery + ')';
return insertQuery;
}
/**
* Insert the data in database.
* @param {Array} store Define the data to be inserted.
*/
var insertData = function (store){
console.log(store)
for (var data in store){
var insertQuery = getInsertSqlQuery(store[data]);
console.log(insertQuery)
runSqlQuery(insertQuery);
}
}
// getCreateSqlQuery(inputFields);
insertData(store);