Passed
Pull Request — master (#14042)
by Maurício
15:45 queued 06:17
created

history.js ➔ build_query   C

Complexity

Conditions 10
Paths 128

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
nc 128
nop 2
dl 0
loc 83
rs 5.1351
c 1
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A history.js ➔ ... ➔ button_options[PMA_messages.strClose] 0 3 1
A history.js ➔ ... ➔ button_options[PMA_messages.strSubmit] 0 8 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like history.js ➔ build_query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/* vim: set expandtab sw=4 ts=4 sts=4: */
2
/**
3
 * @fileoverview    function used in this file builds history tab and generates query.
4
  *
5
  * @requires    jQuery
6
  * @requires    moves.js
7
  * @version $Id$
8
  */
9
10
var history_array = []; // Global array to store history objects
11
var select_field = [];  // Global array to store informaation for columns which are used in select clause
12
var g_index;
13
var vqb_editor = null;
14
15
/**
16
 * To display details of objects(where,rename,Having,aggregate,groupby,orderby,having)
17
 *
18
 * @param index index of history_array where change is to be made
19
 *
20
**/
21
22
function detail (index) {
23
    var type = history_array[index].get_type();
24
    var str;
25
    if (type === 'Where') {
26
        str = 'Where ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
27
    }
28
    if (type === 'Rename') {
29
        str = 'Rename ' + history_array[index].get_column_name() + ' To ' + history_array[index].get_obj().getrename_to();
30
    }
31
    if (type === 'Aggregate') {
32
        str = 'Select ' + history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
33
    }
34
    if (type === 'GroupBy') {
35
        str = 'GroupBy ' + history_array[index].get_column_name();
36
    }
37
    if (type === 'OrderBy') {
38
        str = 'OrderBy ' + history_array[index].get_column_name() + ' ' + history_array[index].get_obj().get_order();
39
    }
40
    if (type === 'Having') {
41
        str = 'Having ';
42
        if (history_array[index].get_obj().get_operator() !== 'None') {
43
            str += history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
44
            str += history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
45
        } else {
46
            str = 'Having ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
47
        }
48
    }
49
    return str;
0 ignored issues
show
Bug introduced by
The variable str does not seem to be initialized in case type === "Where" on line 25 is false. Are you sure this can never be the case?
Loading history...
50
}
51
52
/**
53
 * Sorts history_array[] first,using table name as the key and then generates the HTML code for history tab,
54
 * clubbing all objects of same tables together
55
 * This function is called whenever changes are made in history_array[]
56
 *
57
 *
58
 * @param {int}  init starting index of unsorted array
59
 * @param {int} finit   last index of unsorted array
60
 *
61
**/
62
63
function display (init, finit) {
64
    var str;
65
    var i;
66
    var j;
67
    var k;
68
    var sto;
69
    var temp;
70
    // this part sorts the history array based on table name,this is needed for clubbing all object of same name together.
71
    for (i = init; i < finit; i++) {
72
        sto = history_array[i];
73
        temp = history_array[i].get_tab();// + '.' + history_array[i].get_obj_no(); for Self JOINS
74
        for (j = 0; j < i; j++) {
75
            if (temp > (history_array[j].get_tab())) {// + '.' + history_array[j].get_obj_no())) { //for Self JOINS
76
                for (k = i; k > j; k--) {
77
                    history_array[k] = history_array[k - 1];
78
                }
79
                history_array[j] = sto;
80
                break;
81
            }
82
        }
83
    }
84
    // this part generates HTML code for history tab.adds delete,edit,and/or and detail features with objects.
85
    str = ''; // string to store Html code for history tab
86
    for (i = 0; i < history_array.length; i++) {
87
        temp = history_array[i].get_tab(); // + '.' + history_array[i].get_obj_no(); for Self JOIN
88
        str += '<h3 class="tiger"><a href="#">' + temp + '</a></h3>';
89
        str += '<div class="toggle_container">\n';
90
        while ((history_array[i].get_tab()) === temp) { // + '.' + history_array[i].get_obj_no()) === temp) {
91
            str += '<div class="block"> <table width ="250">';
92
            str += '<thead><tr><td>';
93
            if (history_array[i].get_and_or()) {
94
                str += '<img src="' + pmaThemeImage + 'designer/or_icon.png" onclick="and_or(' + i + ')" title="OR"/></td>';
0 ignored issues
show
Bug introduced by
The variable pmaThemeImage seems to be never declared. If this is a global, consider adding a /** global: pmaThemeImage */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
95
            } else {
96
                str += '<img src="' + pmaThemeImage + 'designer/and_icon.png" onclick="and_or(' + i + ')" title="AND"/></td>';
97
            }
98
            str += '<td style="padding-left: 5px;" class="right">' + PMA_getImage('b_sbrowse', 'column name') + '</td>' +
99
                '<td width="175" style="padding-left: 5px">' + history_array[i].get_column_name() + '<td>';
100
            if (history_array[i].get_type() === 'GroupBy' || history_array[i].get_type() === 'OrderBy') {
101
                str += '<td class="center">' + PMA_getImage('s_info', detail(i)) + '</td>' +
102
                    '<td title="' + detail(i) + '">' + history_array[i].get_type() + '</td>' +
103
                    '<td onclick=history_delete(' + i + ')>' + PMA_getImage('b_drop', PMA_messages.strDelete) + '</td>';
0 ignored issues
show
Bug introduced by
The variable PMA_messages seems to be never declared. If this is a global, consider adding a /** global: PMA_messages */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
104
            } else {
105
                str += '<td class="center">' + PMA_getImage('s_info', detail(i)) + '</td>' +
106
                    '<td title="' + detail(i) + '">' + history_array[i].get_type() + '</td>' +
107
                    '<td onclick=history_edit(' + i + ')>' + PMA_getImage('b_edit', PMA_messages.strEdit) + '</td>' +
108
                    '<td onclick=history_delete(' + i + ')>' + PMA_getImage('b_drop', PMA_messages.strDelete) + '</td>';
109
            }
110
            str += '</tr></thead>';
111
            i++;
112
            if (i >= history_array.length) {
113
                break;
114
            }
115
            str += '</table></div>';
116
        }
117
        i--;
118
        str += '</div>';
119
    }
120
    return str;
121
}
122
123
/**
124
 * To change And/Or relation in history tab
125
 *
126
 *
127
 * @param {int} index of history_array where change is to be made
128
 *
129
**/
130
131
function and_or (index) {
132
    if (history_array[index].get_and_or()) {
133
        history_array[index].set_and_or(0);
134
    } else {
135
        history_array[index].set_and_or(1);
136
    }
137
    var existingDiv = document.getElementById('ab');
138
    existingDiv.innerHTML = display(0, 0);
139
    $('#ab').accordion('refresh');
140
}
141
142
/**
143
 * Deletes entry in history_array
144
 *
145
 * @param index index of history_array[] which is to be deleted
146
 *
147
**/
148
149
function history_delete (index) {
150
    for (var k = 0; k < from_array.length; k++) {
0 ignored issues
show
Bug introduced by
The variable from_array seems to be never declared. If this is a global, consider adding a /** global: from_array */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
151
        if (from_array[k] === history_array[index].get_tab()) {
152
            from_array.splice(k, 1);
153
            break;
154
        }
155
    }
156
    history_array.splice(index, 1);
157
    var existingDiv = document.getElementById('ab');
158
    existingDiv.innerHTML = display(0, 0);
159
    $('#ab').accordion('refresh');
160
}
161
162
/**
163
 * To show where,rename,aggregate,having forms to edit a object
164
 *
165
 * @param{int} index index of history_array where change is to be made
166
 *
167
**/
168
169
function history_edit (index) {
170
    g_index = index;
171
    var type = history_array[index].get_type();
172
    if (type === 'Where') {
173
        document.getElementById('eQuery').value = history_array[index].get_obj().getquery();
174
        document.getElementById('erel_opt').value = history_array[index].get_obj().getrelation_operator();
175
        document.getElementById('query_where').style.left =  '530px';
176
        document.getElementById('query_where').style.top  = '130px';
177
        document.getElementById('query_where').style.position  = 'absolute';
178
        document.getElementById('query_where').style.zIndex = '103';
179
        document.getElementById('query_where').style.visibility = 'visible';
180
        document.getElementById('query_where').style.display = 'block';
181
    }
182
    if (type === 'Having') {
183
        document.getElementById('hQuery').value = history_array[index].get_obj().getquery();
184
        document.getElementById('hrel_opt').value = history_array[index].get_obj().getrelation_operator();
185
        document.getElementById('hoperator').value = history_array[index].get_obj().get_operator();
186
        document.getElementById('query_having').style.left =  '530px';
187
        document.getElementById('query_having').style.top  = '130px';
188
        document.getElementById('query_having').style.position  = 'absolute';
189
        document.getElementById('query_having').style.zIndex = '103';
190
        document.getElementById('query_having').style.visibility = 'visible';
191
        document.getElementById('query_having').style.display = 'block';
192
    }
193
    if (type === 'Rename') {
194
        document.getElementById('e_rename').value = history_array[index].get_obj().getrename_to();
195
        document.getElementById('query_rename_to').style.left =  '530px';
196
        document.getElementById('query_rename_to').style.top  = '130px';
197
        document.getElementById('query_rename_to').style.position  = 'absolute';
198
        document.getElementById('query_rename_to').style.zIndex = '103';
199
        document.getElementById('query_rename_to').style.visibility = 'visible';
200
        document.getElementById('query_rename_to').style.display = 'block';
201
    }
202
    if (type === 'Aggregate') {
203
        document.getElementById('e_operator').value = history_array[index].get_obj().get_operator();
204
        document.getElementById('query_Aggregate').style.left = '530px';
205
        document.getElementById('query_Aggregate').style.top  = '130px';
206
        document.getElementById('query_Aggregate').style.position  = 'absolute';
207
        document.getElementById('query_Aggregate').style.zIndex = '103';
208
        document.getElementById('query_Aggregate').style.visibility = 'visible';
209
        document.getElementById('query_Aggregate').style.display = 'block';
210
    }
211
}
212
213
/**
214
 * Make changes in history_array when Edit button is clicked
215
 * checks for the type of object and then sets the new value
216
 *
217
 * @param index index of history_array where change is to be made
218
**/
219
220
function edit (type) {
221
    if (type === 'Rename') {
222
        if (document.getElementById('e_rename').value !== '') {
223
            history_array[g_index].get_obj().setrename_to(document.getElementById('e_rename').value);
224
            document.getElementById('e_rename').value = '';
225
        }
226
        document.getElementById('query_rename_to').style.visibility = 'hidden';
227
    }
228
    if (type === 'Aggregate') {
229
        if (document.getElementById('e_operator').value !== '---') {
230
            history_array[g_index].get_obj().set_operator(document.getElementById('e_operator').value);
231
            document.getElementById('e_operator').value = '---';
232
        }
233
        document.getElementById('query_Aggregate').style.visibility = 'hidden';
234
    }
235
    if (type === 'Where') {
236
        if (document.getElementById('erel_opt').value !== '--' && document.getElementById('eQuery').value !== '') {
237
            history_array[g_index].get_obj().setquery(document.getElementById('eQuery').value);
238
            history_array[g_index].get_obj().setrelation_operator(document.getElementById('erel_opt').value);
239
        }
240
        document.getElementById('query_where').style.visibility = 'hidden';
241
    }
242
    if (type === 'Having') {
243
        if (document.getElementById('hrel_opt').value !== '--' && document.getElementById('hQuery').value !== '') {
244
            history_array[g_index].get_obj().setquery(document.getElementById('hQuery').value);
245
            history_array[g_index].get_obj().setrelation_operator(document.getElementById('hrel_opt').value);
246
            history_array[g_index].get_obj().set_operator(document.getElementById('hoperator').value);
247
        }
248
        document.getElementById('query_having').style.visibility = 'hidden';
249
    }
250
    var existingDiv = document.getElementById('ab');
251
    existingDiv.innerHTML = display(0, 0);
252
    $('#ab').accordion('refresh');
253
}
254
255
/**
256
 * history object closure
257
 *
258
 * @param ncolumn_name  name of the column on which conditions are put
259
 * @param nobj          object details(where,rename,orderby,groupby,aggregate)
260
 * @param ntab          table name of the column on which conditions are applied
261
 * @param nobj_no       object no used for inner join
262
 * @param ntype         type of object
263
 *
264
**/
265
266
function history_obj (ncolumn_name, nobj, ntab, nobj_no, ntype) {
267
    var and_or;
268
    var obj;
269
    var tab;
270
    var column_name;
271
    var obj_no;
272
    var type;
273
    this.set_column_name = function (ncolumn_name) {
274
        column_name = ncolumn_name;
275
    };
276
    this.get_column_name = function () {
277
        return column_name;
278
    };
279
    this.set_and_or = function (nand_or) {
280
        and_or = nand_or;
281
    };
282
    this.get_and_or = function () {
283
        return and_or;
284
    };
285
    this.get_relation = function () {
286
        return and_or;
287
    };
288
    this.set_obj = function (nobj) {
289
        obj = nobj;
290
    };
291
    this.get_obj = function () {
292
        return obj;
293
    };
294
    this.set_tab = function (ntab) {
295
        tab = ntab;
296
    };
297
    this.get_tab = function () {
298
        return tab;
299
    };
300
    this.set_obj_no = function (nobj_no) {
301
        obj_no = nobj_no;
302
    };
303
    this.get_obj_no = function () {
304
        return obj_no;
305
    };
306
    this.set_type = function (ntype) {
307
        type = ntype;
308
    };
309
    this.get_type = function () {
310
        return type;
311
    };
312
    this.set_obj_no(nobj_no);
313
    this.set_tab(ntab);
314
    this.set_and_or(0);
315
    this.set_obj(nobj);
316
    this.set_column_name(ncolumn_name);
317
    this.set_type(ntype);
318
}
319
320
/**
321
 * where object closure, makes an object with all information of where
322
 *
323
 * @param nrelation_operator type of relation operator to be applied
324
 * @param nquery             stores value of value/sub-query
325
 *
326
**/
327
328
329
var where = function (nrelation_operator, nquery) {
330
    var relation_operator;
331
    var query;
332
    this.setrelation_operator = function (nrelation_operator) {
333
        relation_operator = nrelation_operator;
334
    };
335
    this.setquery = function (nquery) {
336
        query = nquery;
337
    };
338
    this.getquery = function () {
339
        return query;
340
    };
341
    this.getrelation_operator = function () {
342
        return relation_operator;
343
    };
344
    this.setquery(nquery);
345
    this.setrelation_operator(nrelation_operator);
346
};
347
348
/**
349
 * Orderby object closure
350
 *
351
 * @param norder order, ASC or DESC
352
 */
353
var orderby = function (norder) {
354
    var order;
355
    this.set_order = function (norder) {
356
        order = norder;
357
    };
358
    this.get_order = function () {
359
        return order;
360
    };
361
    this.set_order(norder);
362
};
363
364
/**
365
 * Having object closure, makes an object with all information of where
366
 *
367
 * @param nrelation_operator type of relation operator to be applied
368
 * @param nquery             stores value of value/sub-query
369
 * @param noperator          operator
370
**/
371
372
var having = function (nrelation_operator, nquery, noperator) {
373
    var relation_operator;
374
    var query;
375
    var operator;
376
    this.set_operator = function (noperator) {
377
        operator = noperator;
378
    };
379
    this.setrelation_operator = function (nrelation_operator) {
380
        relation_operator = nrelation_operator;
381
    };
382
    this.setquery = function (nquery) {
383
        query = nquery;
384
    };
385
    this.getquery = function () {
386
        return query;
387
    };
388
    this.getrelation_operator = function () {
389
        return relation_operator;
390
    };
391
    this.get_operator = function () {
392
        return operator;
393
    };
394
    this.setquery(nquery);
395
    this.setrelation_operator(nrelation_operator);
396
    this.set_operator(noperator);
397
};
398
399
/**
400
 * rename object closure,makes an object with all information of rename
401
 *
402
 * @param nrename_to new name information
403
 *
404
**/
405
406
var rename = function (nrename_to) {
407
    var rename_to;
408
    this.setrename_to = function (nrename_to) {
409
        rename_to = nrename_to;
410
    };
411
    this.getrename_to = function () {
412
        return rename_to;
413
    };
414
    this.setrename_to(nrename_to);
415
};
416
417
/**
418
 * aggregate object closure
419
 *
420
 * @param noperator aggregte operator
421
 *
422
**/
423
424
var aggregate = function (noperator) {
425
    var operator;
426
    this.set_operator = function (noperator) {
427
        operator = noperator;
428
    };
429
    this.get_operator = function () {
430
        return operator;
431
    };
432
    this.set_operator(noperator);
433
};
434
435
/**
436
 * This function returns unique element from an array
437
 *
438
 * @param arrayName array from which duplicate elem are to be removed.
439
 * @return unique array
440
 */
441
442
function unique (arrayName) {
443
    var newArray = [];
444
    uniquetop:
445
    for (var i = 0; i < arrayName.length; i++) {
446
        for (var j = 0; j < newArray.length; j++) {
447
            if (newArray[j] === arrayName[i]) {
448
                continue uniquetop;
449
            }
450
        }
451
        newArray[newArray.length] = arrayName[i];
452
    }
453
    return newArray;
454
}
455
456
/**
457
 * This function takes in array and a value as input and returns 1 if values is present in array
458
 * else returns -1
459
 *
460
 * @param arrayName array
461
 * @param value  value which is to be searched in the array
462
 */
463
464
function found (arrayName, value) {
465
    for (var i = 0; i < arrayName.length; i++) {
466
        if (arrayName[i] === value) {
467
            return 1;
468
        }
469
    }
470
    return -1;
471
}
472
473
/**
474
 * This function concatenates two array
475
 *
476
 * @params add array elements of which are pushed in
477
 * @params arr array in which elements are added
478
 */
479
function add_array (add, arr) {
480
    for (var i = 0; i < add.length; i++) {
481
        arr.push(add[i]);
482
    }
483
    return arr;
484
}
485
486
/* This function removes all elements present in one array from the other.
487
 *
488
 * @params rem array from which each element is removed from other array.
489
 * @params arr array from which elements are removed.
490
 *
491
 */
492
function remove_array (rem, arr) {
493
    for (var i = 0; i < rem.length; i++) {
494
        for (var j = 0; j < arr.length; j++) {
495
            if (rem[i] === arr[j]) {
496
                arr.splice(j, 1);
497
            }
498
        }
499
    }
500
    return arr;
501
}
502
503
/**
504
 * This function builds the groupby clause from history object
505
 *
506
 */
507
508
function query_groupby () {
509
    var i;
510
    var str = '';
511
    for (i = 0; i < history_array.length;i++) {
512
        if (history_array[i].get_type() === 'GroupBy') {
513
            str += '`' + history_array[i].get_column_name() + '`, ';
514
        }
515
    }
516
    str = str.substr(0, str.length - 2);
517
    return str;
518
}
519
520
/**
521
 * This function builds the Having clause from the history object.
522
 *
523
 */
524
525
function query_having () {
526
    var i;
527
    var and = '(';
528
    for (i = 0; i < history_array.length;i++) {
529
        if (history_array[i].get_type() === 'Having') {
530
            if (history_array[i].get_obj().get_operator() !== 'None') {
531
                and += history_array[i].get_obj().get_operator() + '(`' + history_array[i].get_column_name() + '`) ' + history_array[i].get_obj().getrelation_operator();
532
                and += ' ' + history_array[i].get_obj().getquery() + ', ';
533
            } else {
534
                and += '`' + history_array[i].get_column_name() + '` ' + history_array[i].get_obj().getrelation_operator() + ' ' + history_array[i].get_obj().getquery() + ', ';
535
            }
536
        }
537
    }
538
    if (and === '(') {
539
        and = '';
540
    } else {
541
        and = and.substr(0, and.length - 2) + ')';
542
    }
543
    return and;
544
}
545
546
547
/**
548
 * This function builds the orderby clause from the history object.
549
 *
550
 */
551
552
function query_orderby () {
553
    var i;
554
    var str = '';
555
    for (i = 0; i < history_array.length;i++) {
556
        if (history_array[i].get_type() === 'OrderBy') {
557
            str += '`' + history_array[i].get_column_name() + '` ' +
558
                history_array[i].get_obj().get_order() + ', ';
559
        }
560
    }
561
    str = str.substr(0, str.length - 2);
562
    return str;
563
}
564
565
566
/**
567
 * This function builds the Where clause from the history object.
568
 *
569
 */
570
571
function query_where () {
572
    var i;
573
    var and = '(';
574
    var or = '(';
575
    for (i = 0; i < history_array.length;i++) {
576
        if (history_array[i].get_type() === 'Where') {
577
            if (history_array[i].get_and_or() === 0) {
578
                and += '( `' + history_array[i].get_column_name() + '` ' + history_array[i].get_obj().getrelation_operator() + ' ' + history_array[i].get_obj().getquery() + ')';
579
                and += ' AND ';
580
            } else {
581
                or += '( `' + history_array[i].get_column_name() + '` ' + history_array[i].get_obj().getrelation_operator() + ' ' + history_array[i].get_obj().getquery() + ')';
582
                or += ' OR ';
583
            }
584
        }
585
    }
586
    if (or !== '(') {
587
        or = or.substring(0, (or.length - 4)) + ')';
588
    } else {
589
        or = '';
590
    }
591
    if (and !== '(') {
592
        and = and.substring(0, (and.length - 5)) + ')';
593
    } else {
594
        and = '';
595
    }
596
    if (or !== '') {
597
        and = and + ' OR ' + or + ' )';
598
    }
599
    return and;
600
}
601
602
function check_aggregate (id_this) {
603
    var i;
604
    for (i = 0; i < history_array.length; i++) {
605
        var temp = '`' + history_array[i].get_tab() + '`.`' + history_array[i].get_column_name() + '`';
606
        if (temp === id_this && history_array[i].get_type() === 'Aggregate') {
607
            return history_array[i].get_obj().get_operator() + '(' + id_this + ')';
608
        }
609
    }
610
    return '';
611
}
612
613
function check_rename (id_this) {
614
    var i;
615
    for (i = 0; i < history_array.length; i++) {
616
        var temp = '`' + history_array[i].get_tab() + '`.`' + history_array[i].get_column_name() + '`';
617
        if (temp === id_this && history_array[i].get_type() === 'Rename') {
618
            return ' AS `' + history_array[i].get_obj().getrename_to() + '`';
619
        }
620
    }
621
    return '';
622
}
623
624
/**
625
  * This function builds from clause of query
626
  * makes automatic joins.
627
  *
628
  *
629
  */
630
function query_from () {
631
    var i;
632
    var tab_left = [];
0 ignored issues
show
Unused Code introduced by
The assignment to variable tab_left seems to be never used. Consider removing it.
Loading history...
633
    var tab_used = [];
634
    var t_tab_used = [];
0 ignored issues
show
Unused Code introduced by
The variable t_tab_used seems to be never used. Consider removing it.
Loading history...
635
    var t_tab_left = [];
636
    var temp;
637
    var query = '';
638
    var quer = '';
639
    var parts = [];
0 ignored issues
show
Unused Code introduced by
The assignment to variable parts seems to be never used. Consider removing it.
Loading history...
640
    var t_array = [];
0 ignored issues
show
Unused Code introduced by
The assignment to variable t_array seems to be never used. Consider removing it.
Loading history...
641
    t_array = from_array;
0 ignored issues
show
Bug introduced by
The variable from_array seems to be never declared. If this is a global, consider adding a /** global: from_array */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
642
    var K = 0;
643
    var k;
644
    var key;
645
    var key2;
646
    var key3;
647
    var parts1;
648
649
    // the constraints that have been used in the LEFT JOIN
650
    var constraints_added = [];
651
652
    for (i = 0; i < history_array.length; i++) {
653
        from_array.push(history_array[i].get_tab());
654
    }
655
    from_array = unique(from_array);
0 ignored issues
show
Bug introduced by
The variable from_array seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.from_array.
Loading history...
656
    tab_left = from_array;
657
    temp = tab_left.shift();
658
    quer = '`' + temp + '`';
659
    tab_used.push(temp);
660
661
    // if master table (key2) matches with tab used get all keys and check if tab_left matches
662
    // after this check if master table (key2) matches with tab left then check if any foreign matches with master .
663
    for (i = 0; i < 2; i++) {
664
        for (K in contr) {
0 ignored issues
show
Bug introduced by
The variable contr seems to be never declared. If this is a global, consider adding a /** global: contr */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
665
            for (key in contr[K]) {// contr name
666
                for (key2 in contr[K][key]) {// table name
667
                    parts = key2.split('.');
668
                    if (found(tab_used, parts[1]) > 0) {
669
                        for (key3 in contr[K][key][key2]) {
670
                            parts1 = contr[K][key][key2][key3][0].split('.');
671
                            if (found(tab_left, parts1[1]) > 0) {
672
                                if (found(constraints_added, key) > 0) {
673
                                    query += ' AND ' + '`' + parts[1] + '`.`' + key3 + '` = ';
674
                                    query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` ';
675
                                } else {
676
                                    query += '\n' + 'LEFT JOIN ';
677
                                    query += '`' + parts[1] + '` ON ';
678
                                    query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` = ';
679
                                    query += '`' + parts[1] + '`.`' + key3 + '` ';
680
681
                                    constraints_added.push(key);
682
                                }
683
                                t_tab_left.push(parts[1]);
684
                            }
685
                        }
686
                    }
687
                }
688
            }
689
        }
690
        K = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to variable K seems to be never used. Consider removing it.
Loading history...
691
        t_tab_left = unique(t_tab_left);
692
        tab_used = add_array(t_tab_left, tab_used);
693
        tab_left = remove_array(t_tab_left, tab_left);
694
        t_tab_left = [];
695
        for (K in contr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
696
            for (key in contr[K]) {
697
                for (key2 in contr[K][key]) {// table name
698
                    parts = key2.split('.');
699
                    if (found(tab_left, parts[1]) > 0) {
700
                        for (key3 in contr[K][key][key2]) {
701
                            parts1 = contr[K][key][key2][key3][0].split('.');
702
                            if (found(tab_used, parts1[1]) > 0) {
703
                                if (found(constraints_added, key) > 0) {
704
                                    query += ' AND ' + '`' + parts[1] + '`.`' + key3 + '` = ';
705
                                    query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` ';
706
                                } else {
707
                                    query += '\n' + 'LEFT JOIN ';
708
                                    query += '`' + parts[1] + '` ON ';
709
                                    query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` = ';
710
                                    query += '`' + parts[1] + '`.`' + key3 + '` ';
711
712
                                    constraints_added.push(key);
713
                                }
714
                                t_tab_left.push(parts[1]);
715
                            }
716
                        }
717
                    }
718
                }
719
            }
720
        }
721
        t_tab_left = unique(t_tab_left);
722
        tab_used = add_array(t_tab_left, tab_used);
723
        tab_left = remove_array(t_tab_left, tab_left);
724
        t_tab_left = [];
725
    }
726
    for (k in tab_left) {
727
        quer += ' , `' + tab_left[k] + '`';
728
    }
729
    query = quer + query;
730
    from_array = t_array;
731
    return query;
732
}
733
734
/**
735
 * This function is the main function for query building.
736
 * uses history object details for this.
737
 *
738
 * @ uses query_where()
739
 * @ uses query_groupby()
740
 * @ uses query_having()
741
 * @ uses query_orderby()
742
 *
743
 * @param formtitle title for the form
744
 * @param fadin
745
 */
746
747
function build_query (formtitle, fadin) {
0 ignored issues
show
Unused Code introduced by
The parameter fadin is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter formtitle is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
748
    var q_select = 'SELECT ';
749
    var temp;
750
    if (select_field.length > 0) {
751
        for (var i = 0; i < select_field.length; i++) {
752
            temp = check_aggregate(select_field[i]);
753
            if (temp !== '') {
754
                q_select += temp;
755
                temp = check_rename(select_field[i]);
756
                q_select += temp + ', ';
757
            } else {
758
                temp = check_rename(select_field[i]);
759
                q_select += select_field[i] + temp + ', ';
760
            }
761
        }
762
        q_select = q_select.substring(0, q_select.length - 2);
763
    } else {
764
        q_select += '* ';
765
    }
766
767
    q_select += '\nFROM ' + query_from();
768
769
    var q_where = query_where();
770
    if (q_where !== '') {
771
        q_select += '\nWHERE ' + q_where;
772
    }
773
774
    var q_groupby = query_groupby();
775
    if (q_groupby !== '') {
776
        q_select += '\nGROUP BY ' + q_groupby;
777
    }
778
779
    var q_having = query_having();
780
    if (q_having !== '') {
781
        q_select += '\nHAVING ' + q_having;
782
    }
783
784
    var q_orderby = query_orderby();
785
    if (q_orderby !== '') {
786
        q_select += '\nORDER BY ' + q_orderby;
787
    }
788
789
    /**
790
     * @var button_options Object containing options
791
     *                     for jQueryUI dialog buttons
792
     */
793
    var button_options = {};
794
    button_options[PMA_messages.strClose] = function () {
0 ignored issues
show
Bug introduced by
The variable PMA_messages seems to be never declared. If this is a global, consider adding a /** global: PMA_messages */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
795
        $(this).dialog('close');
796
    };
797
    button_options[PMA_messages.strSubmit] = function () {
0 ignored issues
show
Bug introduced by
The variable PMA_messages seems to be never declared. If this is a global, consider adding a /** global: PMA_messages */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
798
        if (vqb_editor) {
799
            var $elm = $ajaxDialog.find('textarea');
800
            vqb_editor.save();
801
            $elm.val(vqb_editor.getValue());
802
        }
803
        $('#vqb_form').submit();
804
    };
805
806
    var $ajaxDialog = $('#box').dialog({
0 ignored issues
show
Bug introduced by
'$ajaxDialog' was used before it was defined.
Loading history...
807
        appendTo: '#page_content',
808
        width: 500,
809
        buttons: button_options,
810
        modal: true,
811
        title: 'SELECT'
812
    });
813
    // Attach syntax highlighted editor to query dialog
814
    /**
815
     * @var $elm jQuery object containing the reference
816
     *           to the query textarea.
817
     */
818
    var $elm = $ajaxDialog.find('textarea');
819
    if (! vqb_editor) {
820
        vqb_editor = PMA_getSQLEditor($elm);
821
    }
822
    if (vqb_editor) {
823
        vqb_editor.setValue(q_select);
824
        vqb_editor.focus();
825
    } else {
826
        $elm.val(q_select);
827
        $elm.focus();
828
    }
829
}
830
831
AJAX.registerTeardown('designer/history.js', function () {
0 ignored issues
show
Bug introduced by
The variable AJAX seems to be never declared. If this is a global, consider adding a /** global: AJAX */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
832
    vqb_editor = null;
833
    history_array = [];
834
    select_field = [];
835
    $('#ok_edit_rename').off('click');
836
    $('#ok_edit_having').off('click');
837
    $('#ok_edit_Aggr').off('click');
838
    $('#ok_edit_where').off('click');
839
});
840
841
AJAX.registerOnload('designer/history.js', function () {
0 ignored issues
show
Bug introduced by
The variable AJAX seems to be never declared. If this is a global, consider adding a /** global: AJAX */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
842
    $('#ok_edit_rename').click(function () {
843
        edit('Rename');
844
    });
845
    $('#ok_edit_having').click(function () {
846
        edit('Having');
847
    });
848
    $('#ok_edit_Aggr').click(function () {
849
        edit('Aggregate');
850
    });
851
    $('#ok_edit_where').click(function () {
852
        edit('Where');
853
    });
854
    $('#ab').accordion({ collapsible : true, active : 'none' });
855
});
856