1 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 * @fileoverview function used in this file builds history tab and generates query.
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
15 * function for panel, hides and shows toggle_container <div>,which is for history elements uses {@link JQuery}.
17 * @param index has value 1 or 0,decides wheter to hide toggle_container on load.
23 $(".toggle_container").hide();
25 $("h2.tiger").click(function(){
26 $(this).toggleClass("active").next().slideToggle("slow");
31 * Sorts history_array[] first,using table name as the key and then generates the HTML code for history tab,
32 * clubbing all objects of same tables together
33 * This function is called whenever changes are made in history_array[]
36 * @uses history_edit()
37 * @uses history_delete()
39 * @param {int} init starting index of unsorted array
40 * @param {int} finit last index of unsorted array
44 function display(init
,finit
)
47 // this part sorts the history array based on table name,this is needed for clubbing all object of same name together.
48 for (i
= init
;i
< finit
;i
++) {
49 sto
= history_array
[i
];
50 var temp
= history_array
[i
].get_tab() ;//+ '.' + history_array[i].get_obj_no(); for Self JOINS
52 if(temp
> (history_array
[j
].get_tab())) {//+ '.' + history_array[j].get_obj_no())) { //for Self JOINS
53 for(k
= i
;k
> j
;k
--) {
54 history_array
[k
] = history_array
[k
-1];
56 history_array
[j
] = sto
;
61 // this part generates HTML code for history tab.adds delete,edit,and/or and detail features with objects.
62 str
=''; // string to store Html code for history tab
63 for ( var i
=0; i
< history_array
.length
; i
++){
64 var temp
= history_array
[i
].get_tab(); //+ '.' + history_array[i].get_obj_no(); for Self JOIN
65 str
+= '<h2 class="tiger"><a href="#">' + temp
+ '</a></h2>';
66 str
+= '<div class="toggle_container">\n';
67 while((history_array
[i
].get_tab()) == temp
) { //+ '.' + history_array[i].get_obj_no()) == temp) {
68 str
+='<div class="block"> <table width ="250">';
69 str
+= '<thead><tr><td>';
70 if(history_array
[i
].get_and_or()){
71 str
+='<img src="pmd/images/or_icon.png" onclick="and_or('+i
+')" title="OR"/></td>';
74 str
+='<img src="pmd/images/and_icon.png" onclick="and_or('+i
+')" title="AND"/></td>';
76 str
+='<td style="padding-left: 5px;" align="right"><img class="icon ic_b_sbrowse" src="themes/dot.gif" title="column name"/></td><td width="175" style="padding-left: 5px">' + history_array
[i
].get_column_name();
77 if (history_array
[i
].get_type() == "GroupBy" || history_array
[i
].get_type() == "OrderBy") {
78 str
+= '</td><td align="center"><img class="icon ic_b_info" src="themes/dot.gif" title="'+detail(i
)+'"/><td title="' + detail(i
) +'">' + history_array
[i
].get_type() + '</td></td><td onmouseover="this.className=\'history_table\';" onmouseout="this.className=\'history_table2\'" onclick=history_delete('+ i
+')><img class="icon ic_b_drop" src="themes/dot.gif" title="Delete"></td></tr></thead>';
81 str
+= '</td><td align="center"><img class="icon ic_b_info" src="themes/dot.gif" title="'+detail(i
)+'"/></td><td title="' + detail(i
) +'">' + history_array
[i
]. get_type() + '</td><td <td onmouseover="this.className=\'history_table\';" onmouseout="this.className=\'history_table2\'" onclick=history_edit('+ i
+')><img class="icon ic_b_edit" src="themes/dot.gif" title="Edit"/></td><td onmouseover="this.className=\'history_table\';" onmouseout="this.className=\'history_table2\'" onclick=history_delete('+ i
+')><img src="themes/original/img/b_drop.png" title="Delete"></td></tr></thead>';
84 if(i
>= history_array
.length
) {
87 str
+= '</table></div><br/>';
96 * To change And/Or relation in history tab
100 * @param {int} index of history_array where change is to be made
104 function and_or(index
)
106 if (history_array
[index
].get_and_or()) {
107 history_array
[index
].set_and_or(0);
110 history_array
[index
].set_and_or(1);
112 var existingDiv
= document
.getElementById('ab');
113 existingDiv
.innerHTML
= display(0,0);
118 * To display details of obects(where,rename,Having,aggregate,groupby,orderby,having)
120 * @param index index of history_array where change is to be made
124 function detail (index
)
126 var type
= history_array
[index
].get_type();
128 if (type
== "Where") {
129 str
= 'Where ' + history_array
[index
].get_column_name() + history_array
[index
].get_obj().getrelation_operator() + history_array
[index
].get_obj().getquery();
131 if (type
== "Rename") {
132 str
= 'Rename ' + history_array
[index
].get_column_name() + ' To ' + history_array
[index
].get_obj().getrename_to();
134 if (type
== "Aggregate") {
135 str
= 'Select ' + history_array
[index
].get_obj().get_operator() + '( ' + history_array
[index
].get_column_name() + ' )';
137 if (type
== "GroupBy") {
138 str
= 'GroupBy ' + history_array
[index
].get_column_name() ;
140 if (type
== "OrderBy") {
141 str
= 'OrderBy ' + history_array
[index
].get_column_name() ;
143 if (type
== "Having") {
145 if (history_array
[index
].get_obj().get_operator() != 'None') {
146 str
+= history_array
[index
].get_obj().get_operator() + '( ' + history_array
[index
].get_column_name() + ' )';
147 str
+= history_array
[index
].get_obj().getrelation_operator() + history_array
[index
].get_obj().getquery();
150 str
= 'Having ' + history_array
[index
].get_column_name() + history_array
[index
].get_obj().getrelation_operator() + history_array
[index
].get_obj().getquery();
157 * Deletes entry in history_array
161 * @param index index of history_array[] which is to be deleted
165 function history_delete(index
)
167 for(var k
=0 ;k
< from_array
.length
;k
++){
168 if(from_array
[k
] == history_array
[index
].get_tab()){
169 from_array
.splice(k
,1);
173 history_array
.splice(index
,1);
174 var existingDiv
= document
.getElementById('ab');
175 existingDiv
.innerHTML
= display(0,0);
180 * To show where,rename,aggregate,having forms to edit a object
182 * @param{int} index index of history_array where change is to be made
186 function history_edit(index
)
189 var type
= history_array
[index
].get_type();
190 if (type
== "Where") {
191 document
.getElementById('eQuery').value
= history_array
[index
].get_obj().getquery();
192 document
.getElementById('erel_opt').value
= history_array
[index
].get_obj().getrelation_operator();
193 document
.getElementById('query_where').style
.left
= '530px';
194 document
.getElementById('query_where').style
.top
= '130px';
195 document
.getElementById('query_where').style
.position
= 'absolute';
196 document
.getElementById('query_where').style
.zIndex
= '9';
197 document
.getElementById('query_where').style
.visibility
= 'visible';
199 if (type
== "Having") {
200 document
.getElementById('hQuery').value
= history_array
[index
].get_obj().getquery();
201 document
.getElementById('hrel_opt').value
= history_array
[index
].get_obj().getrelation_operator();
202 document
.getElementById('hoperator').value
= history_array
[index
].get_obj().get_operator();
203 document
.getElementById('query_having').style
.left
= '530px';
204 document
.getElementById('query_having').style
.top
= '130px';
205 document
.getElementById('query_having').style
.position
= 'absolute';
206 document
.getElementById('query_having').style
.zIndex
= '9';
207 document
.getElementById('query_having').style
.visibility
= 'visible';
209 if (type
== "Rename") {
210 document
.getElementById('query_rename_to').style
.left
= '530px';
211 document
.getElementById('query_rename_to').style
.top
= '130px';
212 document
.getElementById('query_rename_to').style
.position
= 'absolute';
213 document
.getElementById('query_rename_to').style
.zIndex
= '9';
214 document
.getElementById('query_rename_to').style
.visibility
= 'visible';
216 if (type
== "Aggregate") {
217 document
.getElementById('query_Aggregate').style
.left
= '530px';
218 document
.getElementById('query_Aggregate').style
.top
= '130px';
219 document
.getElementById('query_Aggregate').style
.position
= 'absolute';
220 document
.getElementById('query_Aggregate').style
.zIndex
= '9';
221 document
.getElementById('query_Aggregate').style
.visibility
= 'visible';
226 * Make changes in history_array when Edit button is clicked
227 * checks for the type of object and then sets the new value
231 * @param index index of history_array where change is to be made
236 if (type
== "Rename") {
237 if (document
.getElementById('e_rename').value
!= "") {
238 history_array
[g_index
].get_obj().setrename_to(document
.getElementById('e_rename').value
);
239 document
.getElementById('e_rename').value
= "";
241 document
.getElementById('query_rename_to').style
.visibility
= 'hidden';
243 if (type
== "Aggregate") {
244 if (document
.getElementById('e_operator').value
!= '---') {
245 history_array
[g_index
].get_obj().set_operator(document
.getElementById('e_operator').value
);
246 document
.getElementById('e_operator').value
= '---';
248 document
.getElementById('query_Aggregate').style
.visibility
= 'hidden';
250 if (type
== "Where") {
251 if (document
.getElementById('erel_opt').value
!= '--' && document
.getElementById('eQuery').value
!="") {
252 history_array
[g_index
].get_obj().setquery(document
.getElementById('eQuery').value
);
253 history_array
[g_index
].get_obj().setrelation_operator(document
.getElementById('erel_opt').value
);
255 document
.getElementById('query_where').style
.visibility
= 'hidden';
257 if (type
== "Having") {
258 if (document
.getElementById('hrel_opt').value
!= '--' && document
.getElementById('hQuery').value
!="") {
259 history_array
[g_index
].get_obj().setquery(document
.getElementById('hQuery').value
);
260 history_array
[g_index
].get_obj().setrelation_operator(document
.getElementById('hrel_opt').value
);
261 history_array
[g_index
].get_obj().set_operator(document
.getElementById('hoperator').value
);
263 document
.getElementById('query_having').style
.visibility
= 'hidden';
265 var existingDiv
= document
.getElementById('ab');
266 existingDiv
.innerHTML
= display(0,0);
271 * history object closure
273 * @param ncolumn_name name of the column on which conditions are put
274 * @param nobj object details(where,rename,orderby,groupby,aggregate)
275 * @param ntab table name of the column on which conditions are applied
276 * @param nobj_no object no used for inner join
277 * @param ntype type of object
281 function history(ncolumn_name
,nobj
,ntab
,nobj_no
,ntype
)
289 this.set_column_name = function (ncolumn_name
) {
290 column_name
= ncolumn_name
;
292 this.get_column_name = function() {
295 this.set_and_or = function(nand_or
) {
298 this.get_and_or = function() {
301 this.get_relation = function() {
304 this.set_obj = function(nobj
) {
307 this.get_obj = function() {
310 this.set_tab = function(ntab
) {
313 this.get_tab = function() {
316 this.set_obj_no = function(nobj_no
) {
319 this.get_obj_no = function() {
322 this.set_type = function(ntype
) {
325 this.get_type = function() {
328 this.set_obj_no(nobj_no
);
332 this.set_column_name(ncolumn_name
);
333 this.set_type(ntype
);
337 * where object closure, makes an object with all information of where
339 * @param nrelation_operator type of relation operator to be applied
340 * @param nquery stores value of value/sub-query
345 var where = function (nrelation_operator
,nquery
) {
346 var relation_operator
;
348 this.setrelation_operator = function(nrelation_operator
) {
349 relation_operator
= nrelation_operator
;
351 this.setquery = function(nquery
) {
354 this.getquery = function() {
357 this.getrelation_operator = function() {
358 return relation_operator
;
360 this.setquery(nquery
);
361 this.setrelation_operator(nrelation_operator
);
366 * Having object closure, makes an object with all information of where
368 * @param nrelation_operator type of relation operator to be applied
369 * @param nquery stores value of value/sub-query
373 var having = function (nrelation_operator
,nquery
,noperator
) {
374 var relation_operator
;
377 this.set_operator = function(noperator
) {
378 operator
= noperator
;
380 this.setrelation_operator = function(nrelation_operator
) {
381 relation_operator
= nrelation_operator
;
383 this.setquery = function(nquery
) {
386 this.getquery = function() {
389 this.getrelation_operator = function() {
390 return relation_operator
;
392 this.get_operator = function() {
395 this.setquery(nquery
);
396 this.setrelation_operator(nrelation_operator
);
397 this.set_operator(noperator
);
401 * rename object closure,makes an object with all information of rename
403 * @param nrename_to new name information
407 var rename = function(nrename_to
) {
409 this.setrename_to = function(nrename_to
) {
410 rename_to
= nrename_to
;
412 this.getrename_to =function() {
415 this.setrename_to(nrename_to
);
419 * aggregate object closure
421 * @param noperator aggregte operator
425 var aggregate = function(noperator
) {
427 this.set_operator = function(noperator
) {
428 operator
= noperator
;
430 this.get_operator = function() {
433 this.set_operator(noperator
);
437 * This function returns unique element from an array
439 * @param arraName array from which duplicate elem are to be removed.
440 * @return unique array
443 function unique(arrayName
)
445 var newArray
=new Array();
446 label
:for(var i
=0; i
<arrayName
.length
;i
++ )
448 for(var j
=0; j
<newArray
.length
;j
++ )
450 if(newArray
[j
]==arrayName
[i
])
453 newArray
[newArray
.length
] = arrayName
[i
];
459 * This function takes in array and a value as input and returns 1 if values is present in array
462 * @param arrayName array
463 * @param value value which is to be searched in the array
466 function found(arrayName
,value
)
468 for(var i
=0; i
<arrayName
.length
; i
++) {
469 if(arrayName
[i
] == value
) { return 1;}
475 * This function is the main function for query building.
476 * uses history object details for this.
478 * @ uses query_where()
479 * @ uses query_groupby()
480 * @ uses query_having()
481 * @ uses query_orderby()
483 * @param formtitle title for the form
487 function build_query(formtitle
, fadin
)
489 var q_select
= "SELECT ";
491 for (i
= 0;i
< select_field
.length
; i
++) {
492 temp
= check_aggregate(select_field
[i
]);
495 temp
= check_rename(select_field
[i
]);
496 q_select
+= temp
+ ",";
499 temp
= check_rename(select_field
[i
]);
500 q_select
+= select_field
[i
] + temp
+",";
503 q_select
= q_select
.substring(0,q_select
.length
- 1);
504 q_select
+= " FROM " + query_from();
505 if(query_where() != "") {
506 q_select
+="\n WHERE";
507 q_select
+= query_where();
509 if(query_groupby() != "") { q_select
+= "\nGROUP BY " + query_groupby(); }
510 if(query_having() != "") { q_select
+= "\nHAVING " + query_having(); }
511 if(query_orderby() != "") { q_select
+= "\nORDER BY " + query_orderby(); }
512 var box
= document
.getElementById('box');
513 document
.getElementById('filter').style
.display
='block';
514 var btitle
= document
.getElementById('boxtitle');
515 btitle
.innerHTML
= 'SELECT';//formtitle;
521 box
.style
.display
='block';
523 document
.getElementById('textSqlquery').innerHTML
= q_select
;
526 * This function builds from clause of query
527 * makes automatic joins.
536 function query_from()
548 t_array
= from_array
;
550 for (i
= 0; i
< history_array
.length
; i
++) {
551 from_array
.push(history_array
[i
].get_tab());
553 from_array
= unique( from_array
);
554 tab_left
= from_array
;
555 temp
= tab_left
.shift();
558 // if master table (key2) matches with tab used get all keys and check if tab_left matches
559 //after this check if master table (key2) matches with tab left then check if any foriegn matches with master .
560 for( i
=0; i
<2 ; i
++) {
562 for (key
in contr
[K
]){// contr name
563 for (key2
in contr
[K
][key
]){// table name
564 parts
= key2
.split(".");
565 if(found(tab_used
,parts
[1]) > 0) {
566 for (key3
in contr
[K
][key
][key2
]) {
567 parts1
= contr
[K
][key
][key2
][key3
][0].split(".");
568 if(found(tab_left
,parts1
[1]) > 0) {
569 query
+= "\n" + 'LEFT JOIN ';
570 query
+= '`' + parts1
[0] + '`.`' + parts1
[1] + '` ON ' ;
571 query
+= '`' + parts
[1] +'`.`' + key3
+ '` = ';
572 query
+= '`' + parts1
[1] + '`.`' + contr
[K
][key
][key2
][key3
][1] + '` ';
573 t_tab_left
.push(parts1
[1]);
581 t_tab_left
= unique (t_tab_left
);
582 tab_used
= add_array(t_tab_left
,tab_used
);
583 tab_left
= remove_array(t_tab_left
,tab_left
);
586 for (key
in contr
[K
]) {
587 for (key2
in contr
[K
][key
]){// table name
588 parts
= key2
.split(".");
589 if(found(tab_left
,parts
[1]) > 0){
590 for (key3
in contr
[K
][key
][key2
]){
591 parts1
= contr
[K
][key
][key2
][key3
][0].split(".");
592 if(found(tab_used
,parts1
[1]) > 0) {
593 query
+= "\n" + 'LEFT JOIN ';
594 query
+= '`' + parts
[0] + '`.`' + parts
[1] + '` ON ' ;
595 query
+= '`' + parts1
[1] + '`.`' + contr
[K
][key
][key2
][key3
][1] + '` = ';
596 query
+= '`' + parts
[1] + '`.`' + key3
+ '` ';
597 t_tab_left
.push(parts
[1]);
604 t_tab_left
= unique (t_tab_left
);
605 tab_used
= add_array(t_tab_left
,tab_used
);
606 tab_left
= remove_array(t_tab_left
,tab_left
);
609 for (k
in tab_left
) {
610 quer
+= " , `" + tab_left
[k
] + "`";
612 query
= quer
+ query
;
613 from_array
= t_array
;
618 * This function concatenates two array
620 * @params add array elements of which are pushed in
621 * @params arr array in which elemnets are added
623 function add_array(add
,arr
)
625 for( var i
=0; i
<add
.length
; i
++){
631 /* This fucntion removes all elements present in one array from the other.
633 * @params rem array from which each element is removed from other array.
634 * @params arr array from which elements are removed.
637 function remove_array(rem
,arr
)
639 for(var i
=0; i
<rem
.length
; i
++){
640 for(var j
=0; j
<arr
.length
; j
++)
641 if(rem
[i
] == arr
[j
]) { arr
.splice(j
,1); }
647 * This function builds the groupby clause from history object
651 function query_groupby()
655 for (i
= 0; i
< history_array
.length
;i
++) {
656 if(history_array
[i
].get_type() == "GroupBy") { str
+=history_array
[i
].get_column_name() + ", ";}
658 str
= str
.substr(0,str
.length
-1);
663 * This function builds the Having clause from the history object.
667 function query_having()
671 for (i
= 0; i
< history_array
.length
;i
++) {
672 if(history_array
[i
].get_type() == "Having") {
673 if (history_array
[i
].get_obj().get_operator() != 'None') {
674 and
+= history_array
[i
].get_obj().get_operator() + "(" + history_array
[i
].get_column_name() + " ) " + history_array
[i
].get_obj().getrelation_operator();
675 and
+= " " + history_array
[i
].get_obj().getquery() + ", " ;
678 and
+= history_array
[i
].get_column_name() + " " + history_array
[i
].get_obj().getrelation_operator() + " " + history_array
[i
].get_obj().getquery() + ", ";
682 if (and
=="(") { and
= "" ;}
683 else { and
= and
.substr(0,and
.length
-2) + ")";}
689 * This function builds the orderby clause from the history object.
693 function query_orderby()
697 for (i
= 0; i
< history_array
.length
;i
++) {
698 if(history_array
[i
].get_type() == "OrderBy") { str
+= history_array
[i
].get_column_name() + " , "; }
700 str
= str
.substr(0,str
.length
-1);
706 * This function builds the Where clause from the history object.
710 function query_where()
715 for (i
= 0; i
< history_array
.length
;i
++) {
716 if(history_array
[i
].get_type() == "Where") {
717 if(history_array
[i
].get_and_or() == 0) {
718 and
+= "( " + history_array
[i
].get_column_name() + " " + history_array
[i
].get_obj().getrelation_operator() +" " + history_array
[i
].get_obj().getquery() + ")"; and
+= " AND ";
721 or
+="( " + history_array
[i
].get_column_name() + " " + history_array
[i
].get_obj().getrelation_operator() + " " + history_array
[i
].get_obj().getquery() +")";
727 or
= or
.substring(0,(or
.length
- 4 )) + ")";
732 and
= and
.substring(0,(and
.length
- 5)) + ")";
737 and
= and
+ " OR " + or
+ " )";
742 function check_aggregate(id_this
)
745 for (i
= 0;i
< history_array
.length
;i
++) {
746 var temp
= '`' + history_array
[i
].get_tab() + '`.`' +history_array
[i
].get_column_name() +'`';
747 if(temp
== id_this
&& history_array
[i
].get_type() == "Aggregate") {
748 return history_array
[i
].get_obj().get_operator() + '(' + id_this
+')';
754 function check_rename(id_this
)
757 for (i
= 0;i
< history_array
.length
;i
++) {
758 var temp
= '`' + history_array
[i
].get_tab() + '`.`' +history_array
[i
].get_column_name() +'`';
759 if(temp
== id_this
&& history_array
[i
].get_type() == "Rename") {
760 return " AS `" + history_array
[i
].get_obj().getrename_to() +"`";
766 function gradient(id
, level
)
768 var box
= document
.getElementById(id
);
769 box
.style
.opacity
= level
;
770 box
.style
.MozOpacity
= level
;
771 box
.style
.KhtmlOpacity
= level
;
772 box
.style
.filter
= "alpha(opacity=" + level
* 100 + ")";
773 box
.style
.display
="block";
782 setTimeout( "gradient('" + id
+ "'," + level
+ ")", (level
* 1000) + 10);
789 document
.getElementById('box').style
.display
='none';
790 document
.getElementById('filter').style
.display
='none';