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.
20 function panel(index) {
22 $(".toggle_container").hide();
24 $("h2.tiger").click(function(){
25 $(this).toggleClass("active").next().slideToggle("slow");
30 * Sorts history_array[] first,using table name as the key and then generates the HTML code for history tab,
31 * clubbing all objects of same tables together
32 * This function is called whenever changes are made in history_array[]
35 * @uses history_edit()
36 * @uses history_delete()
38 * @param {int} init starting index of unsorted array
39 * @param {int} final last index of unsorted array
43 function display(init,final) {
45 // this part sorts the history array based on table name,this is needed for clubbing all object of same name together.
46 for (i = init;i < final;i++) {
47 sto = history_array[i];
48 var temp = history_array[i].get_tab() ;//+ '.' + history_array[i].get_obj_no(); for Self JOINS
50 if(temp > (history_array[j].get_tab())) {//+ '.' + history_array[j].get_obj_no())) { //for Self JOINS
51 for(k = i;k > j;k--) {
52 history_array[k] = history_array[k-1];
54 history_array[j] = sto;
59 // this part generates HTML code for history tab.adds delete,edit,and/or and detail features with objects.
60 str =''; // string to store Html code for history tab
61 for ( var i=0; i < history_array.length; i++){
62 var temp = history_array[i].get_tab(); //+ '.' + history_array[i].get_obj_no(); for Self JOIN
63 str += '<h2 class="tiger"><a href="#">' + temp + '</a></h2>';
64 str += '<div class="toggle_container">\n';
65 while((history_array[i].get_tab()) == temp) { //+ '.' + history_array[i].get_obj_no()) == temp) {
66 str +='<div class="block"> <table width ="250">';
67 str += '<thead><tr><td>';
68 if(history_array[i].get_and_or()){
69 str +='<img src="pmd/images/or_icon.png" onclick="and_or('+i+')" title="OR"/></td>';
72 str +='<img src="pmd/images/and_icon.png" onclick="and_or('+i+')" title="AND"/></td>';
74 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();
75 if (history_array[i].get_type() == "GroupBy" || history_array[i].get_type() == "OrderBy") {
76 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>';
79 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>';
82 if(i >= history_array.length) {
85 str += '</table></div><br/>';
94 * To change And/Or relation in history tab
98 * @param {int} index of history_array where change is to be made
102 function and_or(index) {
103 if (history_array[index].get_and_or()) {
104 history_array[index].set_and_or(0);
107 history_array[index].set_and_or(1);
109 var existingDiv = document.getElementById('ab');
110 existingDiv.innerHTML = display(0,0);
115 * To display details of obects(where,rename,Having,aggregate,groupby,orderby,having)
117 * @param index index of history_array where change is to be made
121 function detail (index) {
122 var type = history_array[index].get_type();
124 if (type == "Where") {
125 str = 'Where ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
127 if (type == "Rename") {
128 str = 'Rename ' + history_array[index].get_column_name() + ' To ' + history_array[index].get_obj().getrename_to();
130 if (type == "Aggregate") {
131 str = 'Select ' + history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
133 if (type == "GroupBy") {
134 str = 'GroupBy ' + history_array[index].get_column_name() ;
136 if (type == "OrderBy") {
137 str = 'OrderBy ' + history_array[index].get_column_name() ;
139 if (type == "Having") {
141 if (history_array[index].get_obj().get_operator() != 'None') {
142 str += history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
143 str += history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
146 str = 'Having ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
153 * Deletes entry in history_array
157 * @param index index of history_array[] which is to be deleted
161 function history_delete(index) {
162 for(var k =0 ;k < from_array.length;k++){
163 if(from_array[k] == history_array[index].get_tab()){
164 from_array.splice(k,1);
168 history_array.splice(index,1);
169 var existingDiv = document.getElementById('ab');
170 existingDiv.innerHTML = display(0,0);
175 * To show where,rename,aggregate,having forms to edit a object
177 * @param{int} index index of history_array where change is to be made
181 function history_edit(index) {
183 var type = history_array[index].get_type();
184 if (type == "Where") {
185 document.getElementById('eQuery').value = history_array[index].get_obj().getquery();
186 document.getElementById('erel_opt').value = history_array[index].get_obj().getrelation_operator();
187 document.getElementById('query_where').style.left = '530px';
188 document.getElementById('query_where').style.top = '130px';
189 document.getElementById('query_where').style.position = 'absolute';
190 document.getElementById('query_where').style.zIndex = '9';
191 document.getElementById('query_where').style.visibility = 'visible';
193 if (type == "Having") {
194 document.getElementById('hQuery').value = history_array[index].get_obj().getquery();
195 document.getElementById('hrel_opt').value = history_array[index].get_obj().getrelation_operator();
196 document.getElementById('hoperator').value = history_array[index].get_obj().get_operator();
197 document.getElementById('query_having').style.left = '530px';
198 document.getElementById('query_having').style.top = '130px';
199 document.getElementById('query_having').style.position = 'absolute';
200 document.getElementById('query_having').style.zIndex = '9';
201 document.getElementById('query_having').style.visibility = 'visible';
203 if (type == "Rename") {
204 document.getElementById('query_rename_to').style.left = '530px';
205 document.getElementById('query_rename_to').style.top = '130px';
206 document.getElementById('query_rename_to').style.position = 'absolute';
207 document.getElementById('query_rename_to').style.zIndex = '9';
208 document.getElementById('query_rename_to').style.visibility = 'visible';
210 if (type == "Aggregate") {
211 document.getElementById('query_Aggregate').style.left = '530px';
212 document.getElementById('query_Aggregate').style.top = '130px';
213 document.getElementById('query_Aggregate').style.position = 'absolute';
214 document.getElementById('query_Aggregate').style.zIndex = '9';
215 document.getElementById('query_Aggregate').style.visibility = 'visible';
220 * Make changes in history_array when Edit button is clicked
221 * checks for the type of object and then sets the new value
225 * @param index index of history_array where change is to be made
228 function edit(type) {
229 if (type == "Rename") {
230 if (document.getElementById('e_rename').value != "") {
231 history_array[g_index].get_obj().setrename_to(document.getElementById('e_rename').value);
232 document.getElementById('e_rename').value = "";
234 document.getElementById('query_rename_to').style.visibility = 'hidden';
236 if (type == "Aggregate") {
237 if (document.getElementById('e_operator').value != '---') {
238 history_array[g_index].get_obj().set_operator(document.getElementById('e_operator').value);
239 document.getElementById('e_operator').value = '---';
241 document.getElementById('query_Aggregate').style.visibility = 'hidden';
243 if (type == "Where") {
244 if (document.getElementById('erel_opt').value != '--' && document.getElementById('eQuery').value !="") {
245 history_array[g_index].get_obj().setquery(document.getElementById('eQuery').value);
246 history_array[g_index].get_obj().setrelation_operator(document.getElementById('erel_opt').value);
248 document.getElementById('query_where').style.visibility = 'hidden';
250 if (type == "Having") {
251 if (document.getElementById('hrel_opt').value != '--' && document.getElementById('hQuery').value !="") {
252 history_array[g_index].get_obj().setquery(document.getElementById('hQuery').value);
253 history_array[g_index].get_obj().setrelation_operator(document.getElementById('hrel_opt').value);
254 history_array[g_index].get_obj().set_operator(document.getElementById('hoperator').value);
256 document.getElementById('query_having').style.visibility = 'hidden';
258 var existingDiv = document.getElementById('ab');
259 existingDiv.innerHTML = display(0,0);
264 * history object closure
266 * @param ncolumn_name name of the column on which conditions are put
267 * @param nobj object details(where,rename,orderby,groupby,aggregate)
268 * @param ntab table name of the column on which conditions are applied
269 * @param nobj_no object no used for inner join
270 * @param ntype type of object
274 function history(ncolumn_name,nobj,ntab,nobj_no,ntype) {
281 this.set_column_name = function (ncolumn_name) {
282 column_name = ncolumn_name;
284 this.get_column_name = function() {
287 this.set_and_or = function(nand_or) {
290 this.get_and_or = function() {
293 this.get_relation = function() {
296 this.set_obj = function(nobj) {
299 this.get_obj = function() {
302 this.set_tab = function(ntab) {
305 this.get_tab = function() {
308 this.set_obj_no = function(nobj_no) {
311 this.get_obj_no = function() {
314 this.set_type = function(ntype) {
317 this.get_type = function() {
320 this.set_obj_no(nobj_no);
324 this.set_column_name(ncolumn_name);
325 this.set_type(ntype);
329 * where object closure, makes an object with all information of where
331 * @param nrelation_operator type of relation operator to be applied
332 * @param nquery stores value of value/sub-query
337 var where = function (nrelation_operator,nquery) {
338 var relation_operator;
340 this.setrelation_operator = function(nrelation_operator) {
341 relation_operator = nrelation_operator;
343 this.setquery = function(nquery) {
346 this.getquery = function() {
349 this.getrelation_operator = function() {
350 return relation_operator;
352 this.setquery(nquery);
353 this.setrelation_operator(nrelation_operator);
358 * Having object closure, makes an object with all information of where
360 * @param nrelation_operator type of relation operator to be applied
361 * @param nquery stores value of value/sub-query
365 var having = function (nrelation_operator,nquery,noperator) {
366 var relation_operator;
369 this.set_operator = function(noperator) {
370 operator = noperator;
372 this.setrelation_operator = function(nrelation_operator) {
373 relation_operator = nrelation_operator;
375 this.setquery = function(nquery) {
378 this.getquery = function() {
381 this.getrelation_operator = function() {
382 return relation_operator;
384 this.get_operator = function() {
387 this.setquery(nquery);
388 this.setrelation_operator(nrelation_operator);
389 this.set_operator(noperator);
393 * rename object closure,makes an object with all information of rename
395 * @param nrename_to new name information
399 var rename = function(nrename_to) {
401 this.setrename_to = function(nrename_to) {
402 rename_to = nrename_to;
404 this.getrename_to =function() {
407 this.setrename_to(nrename_to);
411 * aggregate object closure
413 * @param noperator aggregte operator
417 var aggregate = function(noperator) {
419 this.set_operator = function(noperator) {
420 operator = noperator;
422 this.get_operator = function() {
425 this.set_operator(noperator);
429 * This function returns unique element from an array
431 * @param arraName array from which duplicate elem are to be removed.
432 * @return unique array
435 function unique(arrayName) {
436 var newArray=new Array();
437 label:for(var i=0; i<arrayName.length;i++ )
439 for(var j=0; j<newArray.length;j++ )
441 if(newArray[j]==arrayName[i])
444 newArray[newArray.length] = arrayName[i];
450 * This function takes in array and a value as input and returns 1 if values is present in array
453 * @param arrayName array
454 * @param value value which is to be searched in the array
457 function found(arrayName,value) {
458 for(var i=0; i<arrayName.length; i++) {
459 if(arrayName[i] == value) { return 1;}
465 * This function is the main function for query building.
466 * uses history object details for this.
468 * @ uses query_where()
469 * @ uses query_groupby()
470 * @ uses query_having()
471 * @ uses query_orderby()
473 * @param formtitle title for the form
477 function build_query(formtitle, fadin) {
478 var q_select = "SELECT ";
480 for(i = 0;i < select_field.length; i++) {
481 temp = check_aggregate(select_field[i]);
484 temp = check_rename(select_field[i]);
485 q_select += temp + ",";
488 temp = check_rename(select_field[i]);
489 q_select += select_field[i] + temp +",";
492 q_select = q_select.substring(0,q_select.length - 1);
493 q_select += " FROM " + query_from();
494 if(query_where() != "") {
495 q_select +="\n WHERE";
496 q_select += query_where();
498 if(query_groupby() != "") { q_select += "\nGROUP BY " + query_groupby(); }
499 if(query_having() != "") { q_select += "\nHAVING " + query_having(); }
500 if(query_orderby() != "") { q_select += "\nORDER BY " + query_orderby(); }
501 var box = document.getElementById('box');
502 document.getElementById('filter').style.display='block';
503 var btitle = document.getElementById('boxtitle');
504 btitle.innerHTML = 'SELECT';//formtitle;
510 box.style.display='block';
512 document.getElementById('textSqlquery').innerHTML = q_select;
515 * This function builds from clause of query
516 * makes automatic joins.
525 function query_from() {
536 t_array = from_array;
538 for(i; i < history_array.length ; i++) {
539 from_array.push(history_array[i].get_tab());
541 from_array = unique( from_array );
542 tab_left = from_array;
543 temp = tab_left.shift();
546 // if master table (key2) matches with tab used get all keys and check if tab_left matches
547 //after this check if master table (key2) matches with tab left then check if any foriegn matches with master .
548 for( i =0; i<2 ; i++) {
550 for (key in contr[K]){// contr name
551 for (key2 in contr[K][key]){// table name
552 parts = key2.split(".");
553 if(found(tab_used,parts[1]) > 0) {
554 for (key3 in contr[K][key][key2]) {
555 parts1 = contr[K][key][key2][key3][0].split(".");
556 if(found(tab_left,parts1[1]) > 0) {
557 query += "\n" + 'LEFT JOIN ';
558 query += '`' + parts1[0] + '`.`' + parts1[1] + '` ON ' ;
559 query += '`' + parts[1] +'`.`' + key3 + '` = ';
560 query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` ';
561 t_tab_left.push(parts1[1]);
569 t_tab_left = unique (t_tab_left);
570 tab_used = add_array(t_tab_left,tab_used);
571 tab_left = remove_array(t_tab_left,tab_left);
574 for (key in contr[K]) {
575 for (key2 in contr[K][key]){// table name
576 parts = key2.split(".");
577 if(found(tab_left,parts[1]) > 0){
578 for (key3 in contr[K][key][key2]){
579 parts1 = contr[K][key][key2][key3][0].split(".");
580 if(found(tab_used,parts1[1]) > 0) {
581 query += "\n" + 'LEFT JOIN ';
582 query += '`' + parts[0] + '`.`' + parts[1] + '` ON ' ;
583 query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` = ';
584 query += '`' + parts[1] + '`.`' + key3 + '` ';
585 t_tab_left.push(parts[1]);
592 t_tab_left = unique (t_tab_left);
593 tab_used = add_array(t_tab_left,tab_used);
594 tab_left = remove_array(t_tab_left,tab_left);
597 for (k in tab_left) {
598 quer += " , `" + tab_left[k] + "`";
600 query = quer + query;
601 from_array = t_array;
604 /* document.write(key3+";"); //master_field
605 document.write(contr[K][key][key2][key3][0]+";"); // foreign_table
606 document.write(contr[K][key][key2][key3][1]+";"); //forieign_feild */
608 * This function concatenates two array
610 * @params add array elements of which are pushed in
611 * @params arr array in which elemnets are added
613 function add_array(add,arr){
614 for( var i=0; i<add.length; i++){
620 /* This fucntion removes all elements present in one array from the other.
622 * @params rem array from which each element is removed from other array.
623 * @params arr array from which elements are removed.
626 function remove_array(rem,arr){
627 for(var i=0; i<rem.length; i++){
628 for(var j=0; j<arr.length; j++)
629 if(rem[i] == arr[j]) { arr.splice(j,1); }
635 * This function builds the groupby clause from history object
639 function query_groupby() {
642 for(i; i < history_array.length;i++) {
643 if(history_array[i].get_type() == "GroupBy") { str +=history_array[i].get_column_name() + ", ";}
645 str = str.substr(0,str.length -1);
650 * This function builds the Having clause from the history object.
654 function query_having() {
657 for(i; i < history_array.length;i++) {
658 if(history_array[i].get_type() == "Having") {
659 if (history_array[i].get_obj().get_operator() != 'None') {
660 and += history_array[i].get_obj().get_operator() + "(" + history_array[i].get_column_name() + " ) " + history_array[i].get_obj().getrelation_operator();
661 and += " " + history_array[i].get_obj().getquery() + ", " ;
664 and += history_array[i].get_column_name() + " " + history_array[i].get_obj().getrelation_operator() + " " + history_array[i].get_obj().getquery() + ", ";
668 if (and =="(") { and = "" ;}
669 else { and = and.substr(0,and.length -2) + ")";}
675 * This function builds the orderby clause from the history object.
679 function query_orderby() {
682 for(i; i < history_array.length;i++) {
683 if(history_array[i].get_type() == "OrderBy") { str += history_array[i].get_column_name() + " , "; }
685 str = str.substr(0,str.length -1);
691 * This function builds the Where clause from the history object.
695 function query_where(){
699 for(i; i < history_array.length;i++) {
700 if(history_array[i].get_type() == "Where") {
701 if(history_array[i].get_and_or() == 0) {
702 and += "( " + history_array[i].get_column_name() + " " + history_array[i].get_obj().getrelation_operator() +" " + history_array[i].get_obj().getquery() + ")"; and += " AND ";
705 or +="( " + history_array[i].get_column_name() + " " + history_array[i].get_obj().getrelation_operator() + " " + history_array[i].get_obj().getquery() +")";
710 if ( or != "(") { or = or.substring(0,(or.length - 4 )) + ")"; }
712 if (and !="(") {and = and.substring(0,(and.length - 5)) + ")"; }
714 if ( or != "" ) { and = and + " OR " + or + " )"; }
718 function check_aggregate(id_this) {
720 for(i;i < history_array.length;i++) {
721 var temp = '`' + history_array[i].get_tab() + '`.`' +history_array[i].get_column_name() +'`';
722 if(temp == id_this && history_array[i].get_type() == "Aggregate") {
723 return history_array[i].get_obj().get_operator() + '(' + id_this +')';
729 function check_rename(id_this) {
731 for (i;i < history_array.length;i++) {
732 var temp = '`' + history_array[i].get_tab() + '`.`' +history_array[i].get_column_name() +'`';
733 if(temp == id_this && history_array[i].get_type() == "Rename") {
734 return " AS `" + history_array[i].get_obj().getrename_to() +"`";
740 function gradient(id, level)
742 var box = document.getElementById(id);
743 box.style.opacity = level;
744 box.style.MozOpacity = level;
745 box.style.KhtmlOpacity = level;
746 box.style.filter = "alpha(opacity=" + level * 100 + ")";
747 box.style.display="block";
756 setTimeout( "gradient('" + id + "'," + level + ")", (level* 1000) + 10);
763 document.getElementById('box').style.display='none';
764 document.getElementById('filter').style.display='none';