from and having clause added with auto left joins

This commit is contained in:
ankitg
2010-08-12 05:20:44 +05:30
parent 638cee4dec
commit e6bb88117f
3 changed files with 358 additions and 8 deletions

View File

@@ -100,7 +100,7 @@ function and_or(index) {
} }
/** /**
* To display details of obects(where,rename,aggregate,groupby,orderby) * To display details of obects(where,rename,aggregate,groupby,orderby,having)
* *
* @param index index of history_array where change is to be made * @param index index of history_array where change is to be made
* *
@@ -124,6 +124,16 @@ function detail (index) {
if (type == "OrderBy") { if (type == "OrderBy") {
str = 'OrderBy ' + history_array[index].get_column_name() ; str = 'OrderBy ' + history_array[index].get_column_name() ;
} }
if (type == "Having") {
str = 'Having ';
if (history_array[index].get_obj().get_operator() != 'None') {
str += history_array[index].get_obj().get_operator() + '( ' + history_array[index].get_column_name() + ' )';
str += history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
}
else {
str = 'Having ' + history_array[index].get_column_name() + history_array[index].get_obj().getrelation_operator() + history_array[index].get_obj().getquery();
}
}
return str; return str;
} }
@@ -137,6 +147,12 @@ function detail (index) {
**/ **/
function history_delete(index) { function history_delete(index) {
for(var k =0 ;k < from_array.length;k++){
if(from_array[k] == history_array[index].get_tab()){
from_array.splice(k,1);
break;
}
}
history_array.splice(index,1); history_array.splice(index,1);
var existingDiv = document.getElementById('ab'); var existingDiv = document.getElementById('ab');
existingDiv.innerHTML = display(0,0); existingDiv.innerHTML = display(0,0);
@@ -162,6 +178,16 @@ function history_edit(index) {
document.getElementById('query_where').style.zIndex = '9'; document.getElementById('query_where').style.zIndex = '9';
document.getElementById('query_where').style.visibility = 'visible'; document.getElementById('query_where').style.visibility = 'visible';
} }
if (type == "Having") {
document.getElementById('hQuery').value = history_array[index].get_obj().getquery();
document.getElementById('hrel_opt').value = history_array[index].get_obj().getrelation_operator();
document.getElementById('hoperator').value = history_array[index].get_obj().get_operator();
document.getElementById('query_having').style.left = '230px';
document.getElementById('query_having').style.top = '330px';
document.getElementById('query_having').style.position = 'absolute';
document.getElementById('query_having').style.zIndex = '9';
document.getElementById('query_having').style.visibility = 'visible';
}
if (type == "Rename") { if (type == "Rename") {
document.getElementById('query_rename_to').style.left = '230px'; document.getElementById('query_rename_to').style.left = '230px';
document.getElementById('query_rename_to').style.top = '330px'; document.getElementById('query_rename_to').style.top = '330px';
@@ -209,6 +235,14 @@ function edit(type) {
} }
document.getElementById('query_where').style.visibility = 'hidden'; document.getElementById('query_where').style.visibility = 'hidden';
} }
if (type == "Having") {
if (document.getElementById('hrel_opt').value != '--' && document.getElementById('hQuery').value !="") {
history_array[g_index].get_obj().setquery(document.getElementById('hQuery').value);
history_array[g_index].get_obj().setrelation_operator(document.getElementById('hrel_opt').value);
history_array[g_index].get_obj().set_operator(document.getElementById('hoperator').value);
}
document.getElementById('query_having').style.visibility = 'hidden';
}
var existingDiv = document.getElementById('ab'); var existingDiv = document.getElementById('ab');
existingDiv.innerHTML = display(0,0); existingDiv.innerHTML = display(0,0);
panel(1); panel(1);
@@ -287,6 +321,7 @@ function history(ncolumn_name,nobj,ntab,nobj_no,ntype) {
* *
**/ **/
var where = function (nrelation_operator,nquery) { var where = function (nrelation_operator,nquery) {
var relation_operator; var relation_operator;
var query; var query;
@@ -306,6 +341,33 @@ var where = function (nrelation_operator,nquery) {
this.setrelation_operator(nrelation_operator); this.setrelation_operator(nrelation_operator);
}; };
var having = function (nrelation_operator,nquery,noperator) {
var relation_operator;
var query;
var operator;
this.set_operator = function(noperator) {
operator = noperator;
};
this.setrelation_operator = function(nrelation_operator) {
relation_operator = nrelation_operator;
};
this.setquery = function(nquery) {
query = nquery;
};
this.getquery = function() {
return query;
};
this.getrelation_operator = function() {
return relation_operator;
};
this.get_operator = function() {
return operator;
};
this.setquery(nquery);
this.setrelation_operator(nrelation_operator);
this.set_operator(noperator);
};
/** /**
* rename object closure,makes an object with all information of rename * rename object closure,makes an object with all information of rename
* *
@@ -342,7 +404,29 @@ var aggregate = function(noperator) {
this.set_operator(noperator); this.set_operator(noperator);
}; };
function unique(arrayName) {
var newArray=new Array();
label:for(var i=0; i<arrayName.length;i++ )
{
for(var j=0; j<newArray.length;j++ )
{
if(newArray[j]==arrayName[i])
continue label;
}
newArray[newArray.length] = arrayName[i];
}
return newArray;
}
function found(arrayName,value) {
for(var i=0; i<arrayName.length; i++) {
if(arrayName[i] == value) { return 1;}
}
return -1;
}
function build_query(formtitle, fadin) { function build_query(formtitle, fadin) {
var q_select = "SELECT "; var q_select = "SELECT ";
var temp; var temp;
for(i = 0;i < select_field.length; i++) { for(i = 0;i < select_field.length; i++) {
@@ -358,10 +442,14 @@ function build_query(formtitle, fadin) {
} }
} }
q_select = q_select.substring(0,q_select.length - 1); q_select = q_select.substring(0,q_select.length - 1);
q_select += " FROM WHERE "; q_select += " FROM " + query_from();
if(query_where() != "") {
q_select +="\n WHERE";
q_select += query_where(); q_select += query_where();
if(query_groupby() != "") { q_select += "GROUP BY" + query_groupby(); } }
if(query_orderby() != "") { q_select += "ORDER BY" + query_orderby(); } if(query_groupby() != "") { q_select += "\nGROUP BY " + query_groupby(); }
if(query_having() != "") { q_select += "\nHAVING " + query_having(); }
if(query_orderby() != "") { q_select += "\nORDER BY " + query_orderby(); }
var box = document.getElementById('box'); var box = document.getElementById('box');
document.getElementById('filter').style.display='block'; document.getElementById('filter').style.display='block';
var btitle = document.getElementById('boxtitle'); var btitle = document.getElementById('boxtitle');
@@ -379,6 +467,107 @@ function build_query(formtitle, fadin) {
// document.getElementById('hint').style.visibility = "visible"; // document.getElementById('hint').style.visibility = "visible";
} }
function query_from() {
var i =0;
var tab_left = [];
var tab_used = [];
var t_tab_used = [];
var t_tab_left = [];
var temp;
var query = "";
var quer = "";
var parts = [];
var t_array = [];
t_array = from_array;
var K = 0;
for(i; i < history_array.length ; i++) {
from_array.push(history_array[i].get_tab());
}
from_array = unique( from_array );
tab_left = from_array;
temp = tab_left.shift();
quer = temp;
tab_used.push(temp);
for( i =0; i<2 ; i++) {
for (K in contr){
for (key in contr[K]){// contr name
// if master table (key2) matches with tab used get all keys and check if tab_left matches
//after this check if master table (key2) matches with tab left then check if any foriegn matches with master .
for (key2 in contr[K][key]){// table name
parts = key2.split(".");
if(found(tab_used,parts[1]) > 0) {
for (key3 in contr[K][key][key2]) {
parts1 = contr[K][key][key2][key3][0].split(".");
if(found(tab_left,parts1[1]) > 0) {
query += "\n" + 'LEFT JOIN ';
query += '`' + parts1[0] + '`.`' + parts1[1] + '` ON ' ;
query += '`' + parts[1] +'`.`' + key3 + '` = ';
query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` ';
t_tab_left.push(parts1[1]);
}
}
}
}
}
}
K = 0;
t_tab_left = unique (t_tab_left);
tab_used = add_array(t_tab_left,tab_used);
tab_left = remove_array(t_tab_left,tab_left);
t_tab_left = [];
for (K in contr) {
for (key in contr[K]) {
for (key2 in contr[K][key]){// table name
parts = key2.split(".");
if(found(tab_left,parts[1]) > 0)
{
for (key3 in contr[K][key][key2]) // field name
{
parts1 = contr[K][key][key2][key3][0].split(".");
if(found(tab_used,parts1[1]) > 0) {
query += "\n" + 'LEFT JOIN ';
query += '`' + parts[0] + '`.`' + parts[1] + '` ON ' ;
query += '`' + parts1[1] + '`.`' + contr[K][key][key2][key3][1] + '` = ';
query += '`' + parts[1] + '`.`' + key3 + '` ';
t_tab_left.push(parts[1]);
}
}
}
}
}
}
t_tab_left = unique (t_tab_left);
tab_used = add_array(t_tab_left,tab_used);
tab_left = remove_array(t_tab_left,tab_left);
t_tab_left = [];
}
for (k in tab_left) {
quer += " , `" + tab_left[k] + "`";
}
query = quer + query;
from_array = t_array;
return query;
}
/* document.write(key3+";"); //master_field
document.write(contr[K][key][key2][key3][0]+";"); // foreign_table
document.write(contr[K][key][key2][key3][1]+";"); //forieign_feild */
function add_array(add,arr){
for( var i=0; i<add.length; i++){
arr.push(add[i]);
}
return arr;
}
function remove_array(rem,arr){
for(var i=0; i<rem.length; i++){
for(var j=0; j<arr.length; j++)
if(rem[i] == arr[j]) { arr.splice(j,1); }
}
return arr;
}
function query_groupby() { function query_groupby() {
var i = 0; var i = 0;
var str = ""; var str = "";
@@ -389,6 +578,25 @@ function query_groupby() {
return str; return str;
} }
function query_having() {
var i = 0;
var and = "(";
for(i; i < history_array.length;i++) {
if(history_array[i].get_type() == "Having") {
if (history_array[i].get_obj().get_operator() != 'None') {
and += history_array[i].get_obj().get_operator() + "(" + history_array[i].get_column_name() + " ) " + history_array[i].get_obj().getrelation_operator();
and += " " + history_array[i].get_obj().getquery() + ", " ;
}
else {
and += history_array[i].get_column_name() + " " + history_array[i].get_obj().getrelation_operator() + " " + history_array[i].get_obj().getquery() + ", ";
}
}
}
if (and =="(") { and = "" ;}
else { and = and.substr(0,and.length -2) + ")";}
return and;
}
function query_orderby() { function query_orderby() {
var i = 0; var i = 0;
var str = "" ; var str = "" ;
@@ -417,7 +625,7 @@ function query_where(){
else { or = "" ;} else { or = "" ;}
if (and !="(") {and = and.substring(0,(and.length - 5)) + ")"; } if (and !="(") {and = and.substring(0,(and.length - 5)) + ")"; }
else {and = "" ;} else {and = "" ;}
and = and + " OR " + or + " )"; if ( or != "" ) { and = and + " OR " + or + " )"; }
return and; return and;
} }

View File

@@ -33,6 +33,7 @@ var timeoutID;
var layer_menu_cur_click = 0; var layer_menu_cur_click = 0;
var step = 10; var step = 10;
var old_class; var old_class;
var from_array = [];
var downer; var downer;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -989,6 +990,7 @@ function Select_all(id_this,owner)
var parent= document.form1; var parent= document.form1;
downer =owner; downer =owner;
var i; var i;
var tab = [];
for(i = 0; i < parent.elements.length; i++) { for(i = 0; i < parent.elements.length; i++) {
if (parent.elements[i].type == "checkbox" && parent.elements[i].id.substring(0,(9 + id_this.length)) == 'select_' + id_this + '._') { if (parent.elements[i].type == "checkbox" && parent.elements[i].id.substring(0,(9 + id_this.length)) == 'select_' + id_this + '._') {
if(document.getElementById('select_all_' + id_this).checked == true) { if(document.getElementById('select_all_' + id_this).checked == true) {
@@ -1004,11 +1006,21 @@ function Select_all(id_this,owner)
} }
if(document.getElementById('select_all_' + id_this).checked == true) { if(document.getElementById('select_all_' + id_this).checked == true) {
select_field.push('`' + id_this.substring(owner.length +1) + '`.*'); select_field.push('`' + id_this.substring(owner.length +1) + '`.*');
tab = id_this.split(".");
from_array.push(tab[1]);
} }
else { else {
for (i =0; i < select_field.length; i++) { for (i =0; i < select_field.length; i++) {
if (select_field[i] == ('`' + id_this.substring(owner.length +1) + '`.*')) { if (select_field[i] == ('`' + id_this.substring(owner.length +1) + '`.*')) {
select_field.splice(i,1); select_field.splice(i,1);
}
}
for(k =0 ;k < from_array.length;k++){
if(from_array[k] == id_this){
from_array.splice(k,1);
break;
} }
} }
} }
@@ -1033,8 +1045,10 @@ function Table_onover(id_this,val)
*/ */
function store_column(id_this,owner,col) { function store_column(id_this,owner,col) {
var i = 0; var i = 0;
var k = 0;
if (document.getElementById('select_' + owner + '.' + id_this + '._' + col).checked == true) { if (document.getElementById('select_' + owner + '.' + id_this + '._' + col).checked == true) {
select_field.push('`' + id_this + '`.`' + col +'`'); select_field.push('`' + id_this + '`.`' + col +'`');
from_array.push(id_this);
} }
else { else {
for(i; i < select_field.length ;i++) { for(i; i < select_field.length ;i++) {
@@ -1043,6 +1057,12 @@ function store_column(id_this,owner,col) {
break; break;
} }
} }
for(k =0 ;k < from_array.length;k++){
if(from_array[k] == id_this){
from_array.splice(k,1);
break;
}
}
} }
} }
@@ -1095,6 +1115,20 @@ function add_object() {
document.getElementById('groupby').checked = false; document.getElementById('groupby').checked = false;
//make groupby //make groupby
} }
if (document.getElementById('h_rel_opt').value != '--') {
if (document.getElementById('having').value == "") {
document.getElementById('hint').innerHTML = "value/subQuery is empty" ;
document.getElementById('hint').style.visibility = "visible";
return;
}
var p = document.getElementById('having');
var where_obj = new having(document.getElementById('h_rel_opt').value,p.value,document.getElementById('h_operator').value);//make where object
history_array.push(new history(col_name,where_obj,tab_name,h_tabs[downer + '.' + tab_name],"Having"));
sum = sum + 1;
document.getElementById('h_rel_opt').value = '--';
document.getElementById('h_operator').value = '---';
p.value = ""; //make having
}
if (document.getElementById('orderby').checked == true) { if (document.getElementById('orderby').checked == true) {
history_array.push(new history(col_name,'OrderBy',tab_name,h_tabs[downer + '.' + tab_name],"OrderBy")); history_array.push(new history(col_name,'OrderBy',tab_name,h_tabs[downer + '.' + tab_name],"OrderBy"));
sum = sum + 1; sum = sum + 1;

View File

@@ -27,7 +27,6 @@ $hidden = "hidden";
<link rel="icon" href="pmd/images/favicon.ico" type="image/x-icon" /> <link rel="icon" href="pmd/images/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="pmd/images/favicon.ico" type="image/x-icon" /> <link rel="shortcut icon" href="pmd/images/favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="pmd/styles/<?php echo $GLOBALS['PMD']['STYLE'] ?>/style1.css" /> <link rel="stylesheet" type="text/css" href="pmd/styles/<?php echo $GLOBALS['PMD']['STYLE'] ?>/style1.css" />
<link rel="stylesheet" type="text/css" href="../../../Users/Ankit/Desktop/lightbox-form.css" />
<title>Designer</title> <title>Designer</title>
<script src="./js/jquery/jquery-1.4.2.js" type="text/javascript"></script> <script src="./js/jquery/jquery-1.4.2.js" type="text/javascript"></script>
<script type="text/javascript"> <script type="text/javascript">
@@ -491,7 +490,7 @@ for ($i = 0; $i < count($GLOBALS['PMD']["TABLE_NAME"]); $i++) {
<option value="min"> Min </option> <option value="min"> Min </option>
<option value="max"> Max </option> <option value="max"> Max </option>
<option value="avg"> Avg </option> <option value="avg"> Avg </option>
<option value="avg"> Count </option> <option value="count"> Count </option>
</select> </select>
</td></tr> </td></tr>
<tr> <tr>
@@ -502,6 +501,39 @@ for ($i = 0; $i < count($GLOBALS['PMD']["TABLE_NAME"]); $i++) {
<td nowrap="nowrap" width="58" align="center"><b>Order By</b></td> <td nowrap="nowrap" width="58" align="center"><b>Order By</b></td>
<td><input type="checkbox" value="orderby" id="orderby"/></td> <td><input type="checkbox" value="orderby" id="orderby"/></td>
</tr> </tr>
<tr><td align="center" nowrap="nowrap"><b>Having</b></td></tr>
<tr>
<td width="58" nowrap="nowrap">Operator</td>
<td width="102"><select name="h_operator" id="h_operator">
<option value="---" selected="selected">---</option>
<option value="None" > None </option>
<option value="sum" > Sum </option>
<option value="min"> Min </option>
<option value="max"> Max </option>
<option value="avg"> Avg </option>
<option value="count"> Count </option>
</select>
</td></tr>
<tr>
<td width="58" nowrap="nowrap">Relation operator</td>
<td width="102"><select name="h_rel_opt" id="h_rel_opt">
<option value="--" selected="selected"> -- </option>
<option value="=" > = </option>
<option value=">"> > </option>
<option value="<"> < </option>
<option value=">="> >= </option>
<option value="<="> <= </option>
<option value="NOT"> NOT </option>
<option value="IN"> IN </option>
<option value="EXCEPT"> Except </option>
<option value="NOT IN"> Not In </option>
</select>
</td>
</tr>
<tr>
<td width="58" nowrap="nowrap">Value/<br/>subQuery</td>
<td width="102"><textarea id="having" value="" cols="18"></textarea></td>
</tr>
</tbody> </tbody>
<tbody> <tbody>
<tr> <tr>
@@ -574,6 +606,82 @@ for ($i = 0; $i < count($GLOBALS['PMD']["TABLE_NAME"]); $i++) {
</tbody> </tbody>
</table> </table>
<table id="query_having" style="visibility:<?php echo $hidden ?>;"
width="5%" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td class="frams1" width="10px"></td>
<td class="frams5" width="99%" ></td>
<td class="frams2" width="10px"><div class="bor"></div></td>
</tr>
<tr>
<td class="frams8"></td>
<td class="input_tab">
<table width="168" border="0" align="center" cellpadding="2" cellspacing="0">
<thead>
<tr>
<td colspan="2" align="center" nowrap="nowrap"><strong>Having</strong></td>
</tr>
</thead>
<tbody id="rename_to">
<tr>
<td width="58" nowrap="nowrap">Operator</td>
<td width="102"><select name="hoperator" id="hoperator">
<option value="---" selected="selected">---</option>
<option value="None" > None </option>
<option value="sum" > Sum </option>
<option value="min"> Min </option>
<option value="max"> Max </option>
<option value="avg"> Avg </option>
<option value="count"> Count </option>
</select>
</td></tr>
<tr>
<tr>
<td width="58" nowrap="nowrap">Opeartor</td>
<td width="102"><select name="hrel_opt" id="hrel_opt">
<option value="--" selected="selected"> -- </option>
<option value="=" > = </option>
<option value=">"> > </option>
<option value="<"> < </option>
<option value=">="> >= </option>
<option value="<="> <= </option>
<option value="NOT"> NOT </option>
<option value="IN"> IN </option>
<option value="EXCEPT"> Except </option>
<option value="NOT IN"> Not In </option>
</select>
</td>
</tr>
<tr>
<td nowrap="nowrap">Value/<br />Subquery</td>
<td><textarea id="hQuery" value="" cols="18"></textarea>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td colspan="2" align="center" nowrap="nowrap">
<input type="button" class="butt" name="Button"
value="<?php echo __('OK'); ?>" onclick="edit('Having')" />
<input type="button" class="butt" name="Button"
value="<?php echo __('Cancel'); ?>"
onclick="document.getElementById('query_having').style.visibility = 'hidden';" />
</td>
</tr>
</tbody>
</table>
</td>
<td class="frams6"></td>
</tr>
<tr>
<td class="frams4"><div class="bor"></div></td>
<td class="frams7"></td>
<td class="frams3"></td>
</tr>
</tbody>
</table>
<table id="query_Aggregate" style="visibility:<?php echo $hidden ?>;" <table id="query_Aggregate" style="visibility:<?php echo $hidden ?>;"
width="5%" border="0" cellpadding="0" cellspacing="0"> width="5%" border="0" cellpadding="0" cellspacing="0">
<tbody> <tbody>