Index: /main/trunk/greenstone2/macros/query.dm
===================================================================
--- /main/trunk/greenstone2/macros/query.dm	(revision 22052)
+++ /main/trunk/greenstone2/macros/query.dm	(revision 22053)
@@ -22,4 +22,6 @@
 _advformlist_ {}
 _regformlist_ {}
+_sqladvformlist_ {}
+_sqlregformlist_ {}
 #_fqfselection_ {}
 
@@ -71,5 +73,5 @@
 #######################################################################
 
-_pagescriptextra_ {_If_("_cgiargqt_" eq "1", _formpagescriptextra_, _selectpagescriptextra_)
+_pagescriptextra_ {_If_("_cgiargqt_" ge "1", _formpagescriptextra_, _selectpagescriptextra_)
 _If_("_cgiarghd_" ne "0",_historypagescriptextra_) }
 
@@ -88,5 +90,8 @@
   var args="";
   args = "&fqa=0&fqv="+argfqv+"&fqf="+argfqf;
-  _If_(_cgiargb_,args += "&fqk="+argfqk+"&fqs="+argfqs+"&fqc="+argfqc;)
+  _If_(_cgiargb_,
+     args += "&fqk="+argfqk+"&fqs="+argfqs+"&fqc="+argfqc;
+  )
+  _If_("_cgiarga_" eq "sqlq",args += "&sqlfqf="+argsqlfqf + "&sqlfqc="+argsqlfqc;)
 
   return args;
@@ -97,5 +102,6 @@
 function getqueryargs () \{
   return "&fqa=1"+"&q="+argq+"&fqv="+argfqv+"&fqf="+argfqf+
-	 "&fqk="+argfqk+"&fqs="+argfqs+"&fqc="+argfqc ;
+	 "&fqk="+argfqk+"&fqs="+argfqs+"&fqc="+argfqc+
+         "&sqlfqf="+argsqlfqf + "&sqlfqc="+argsqlfqc ;
 \}
 
@@ -152,5 +158,7 @@
     _If_(_jselection_,_getjarg_)
     _If_(_nselection_,_getnarg_)
+    _If_(_sqlsfselection_,_getsqlsfarg_)
     _If_(_sfselection_,_getsfarg_)
+      
     if (document.QueryForm.t != null) \{
       value = document.QueryForm.t.options[document.QueryForm.t.selectedIndex].value;
@@ -180,7 +188,18 @@
 }
 
+# currently only supported in lucene
 _getsfarg_{
+_If_("_cgiarga__cgiargct_" eq "q2",
     value = document.QueryForm.sf.options[document.QueryForm.sf.selectedIndex].value;
     if (value != "_cgiargsf_") stdarg += "&sf="+value;
+)
+}
+
+# only when in sql mode
+_getsqlsfarg_{
+_If_("_cgiarga__cgiargqt_" eq "sqlq2",
+    value = document.QueryForm.sqlsf.options[document.QueryForm.sqlsf.selectedIndex].value;
+    if (value != "_cgiargsqlsf_") stdarg += "&sqlsf="+value;
+)
 }
 
@@ -192,4 +211,8 @@
 argfqs="_cgiargfqs_";
 argfqc="_cgiargfqc_";
+_If_("_cgiarga_" eq "sqlq",
+argsqlfqf="_cgiargsqlfqf_";	
+argsqlfqc="_cgiargsqlfqc_";
+)
 argq = "";
 
@@ -201,4 +224,8 @@
    if (document.QueryForm.fqk != null) \{initfqk(); \}
    if (document.QueryForm.fqc != null) \{initfqc(); \}
+_If_("_cgiarga_" eq "sqlq",
+   if (document.QueryForm.sqlfqf != null) \{initsqlfqf(); \}
+   if (document.QueryForm.sqlfqc != null) \{initsqlfqc(); \}
+)
    if (document.QueryForm.q != null) \{initq(); \}   
 \}
@@ -272,4 +299,61 @@
 \}
 
+   
+// sqlfqf - the field selection box
+function initsqlfqf() \{
+   var i;
+   sqlfqf = argsqlfqf.split(",");
+   num_opts = document.QueryForm.sqlfqf[0].options.length; // assumes all have the same  options   
+   for (i=0; i<sqlfqf.length && i<document.QueryForm.sqlfqf.length; i++) \{
+     if (sqlfqf[i] != "")\{			
+       for (j=0;j<num_opts;j++) \{
+	   if (sqlfqf[i]== document.QueryForm.sqlfqf[i].options[j].value) \{
+	      document.QueryForm.sqlfqf[i].options[j].selected = true;
+	      break;	
+	   \}
+	\}
+     \}
+   \}
+   if (sqlfqf.length < document.QueryForm.sqlfqf.length) \{ // initialize remaining ones
+      for (i=sqlfqf.length;i<document.QueryForm.sqlfqf.length;i++) \{
+	  var z=i;
+	  while (z >= num_opts) \{
+	     z = z-(num_opts);
+	  \}
+	  document.QueryForm.sqlfqf[i].options[z].selected = true;
+	  
+      \}
+   \}
+   updatesqlfqf();
+
+\}
+
+function updatesqlfqf() \{
+
+   var i;
+   argsqlfqf = "";
+   var j = document.QueryForm.sqlfqf[0].selectedIndex;
+   argsqlfqf += document.QueryForm.sqlfqf[0].options[j].value;
+   for (i=1; i<document.QueryForm.sqlfqf.length;i++) \{
+      j = document.QueryForm.sqlfqf[i].selectedIndex;
+      argsqlfqf += "," + document.QueryForm.sqlfqf[i].options[j].value;
+   \}
+
+\}
+
+
+function clearsqlfqf() \{
+   var i;
+   for (i=0;i<document.QueryForm.sqlfqf.length;i++) \{
+       var z = i;
+       if (i >= document.QueryForm.sqlfqf[i].options.length) z = z-document.QueryForm.sqlfqf[i].options.length;
+       document.QueryForm.sqlfqf[i].options[z].selected = true;
+   \}
+   updatesqlfqf();
+\}
+
+
+
+
 // fqc - the boolean operator selection box
 function initfqc() \{
@@ -332,4 +416,69 @@
 \}
 
+
+
+// sqlfqc - the SQL operator selection box
+function initsqlfqc() \{
+   var i,j;
+   sqlfqc = argsqlfqc.split(",");
+   if (_cgiargsqlfqn_ == 2) \{ // there will only be one sqlfqc element
+       for (j=0;j<document.QueryForm.sqlfqc.options.length;j++) \{
+	   if (sqlfqc[0] == document.QueryForm.sqlfqc.options[j].value) \{
+	      document.QueryForm.sqlfqc.options[j].selected = true;
+	      break;
+	   \}
+       \}   
+   \}
+   else \{   
+   for (i=0; i<sqlfqc.length;i++) \{
+       for (j=0;j<document.QueryForm.sqlfqc[i].options.length;j++) \{
+	   if (sqlfqc[i] == document.QueryForm.sqlfqc[i].options[j].value) \{
+	      document.QueryForm.sqlfqc[i].options[j].selected = true;
+	      break;
+	   \}
+       \}
+   \}
+   \}
+   updatesqlfqc();
+
+\}
+
+function updatesqlfqc() \{
+  
+   var i,j;
+   argsqlfqc = "";
+   if (_cgiargsqlfqn_ == 2) \{
+      j = document.QueryForm.sqlfqc.selectedIndex;
+      argsqlfqc += document.QueryForm.sqlfqc.options[j].value;
+   \}
+   else \{
+	j = document.QueryForm.sqlfqc[0].selectedIndex;
+	argsqlfqc += document.QueryForm.sqlfqc[0].options[j].value;
+	for (i=1;i<document.QueryForm.sqlfqc.length;i++) \{
+	    j = document.QueryForm.sqlfqc[i].selectedIndex;
+	    argsqlfqc += "," + document.QueryForm.sqlfqc[i].options[j].value;
+	\}
+   \}
+
+\}
+
+
+function clearsqlfqc() \{
+   
+   var i;
+   if (_cgiargsqlfqn_ == 2) \{
+       document.QueryForm.sqlfqc.options[0].selected = true;
+   \}
+   else \{
+	for (i=0; i<document.QueryForm.sqlfqc.length; i++) \{
+	    document.QueryForm.sqlfqc[i].options[0].selected = true;
+	\}
+   \}
+   updatesqlfqc();
+\}
+
+
+
+
 // fqv - the query word/phrase text box       
 function initfqv() \{
@@ -473,5 +622,5 @@
        ch=str.charAt(j);
        if (ch == " "|| ch == ",") \{
-           if ( flag == 0) \{     
+           if (flag == 0) \{     
 	      out += "+";
 	      flag=1;
@@ -480,5 +629,6 @@
 	\}
 	if (ch == ";" || ch == ":" || ch == "/" || ch == "?" ||
-            ch == "@" || ch == "&" || ch == "=" || ch == "#") \{
+            ch == "@" || ch == "&" || ch == "=" || ch == "#" ||
+	    ch == "%") \{
 	    out += escape(ch);
 	    flag=0;
@@ -500,5 +650,5 @@
 
 function beginSearch() \{
-window.location="_httpquery_"+getstdargs()+"_advformargs_"+getsearchargs();
+  window.location="_httpquery_"+getstdargs()+"_advformargs_"+getsearchargs();
 \}
 
@@ -509,4 +659,7 @@
 function clearForm() \{
    clearfqf();
+_If_("_cgiarga_" eq "sqlq",
+   clearsqlfqf();
+)
    clearfqv();
 
@@ -515,4 +668,7 @@
       clearfqs();
       clearfqc();
+_If_("_cgiarga_" eq "sqlq",
+      clearsqlfqc();
+)
    \}
 
@@ -551,5 +707,12 @@
 <div class="document">
 <div class="queryform">
-_If_(_cgiargct_,_selectqueryform_,_queryform_)
+
+_If_("_cgiarga_" eq "sqlq",
+
+  _If_("_cgiargqt_" eq "2",_sqlfieldqueryform_,_sqlqueryform_)
+, 
+  _If_(_cgiargct_,_selectqueryform_,_queryform_)
+)
+
 </div>
 
@@ -567,5 +730,5 @@
 }
 
-_selectqueryform_{_If_("_cgiargqt_" eq "1",_fieldqueryform_,_queryform_)}
+_selectqueryform_{_If_("_cgiargqt_" ge "1",_fieldqueryform_,_queryform_)}
 
 _queryform_ {
@@ -584,4 +747,22 @@
 }
 
+
+_sqlqueryform_ {
+
+<!-- simple query box that requires you to type SQL where clause directly -->
+<!-- sqlquery form (\_query:plainqueryform\_) -->
+<form name="QueryForm" method="get" action="_gwcgi_">
+<p>
+<input type="hidden" name="a" value="sqlq">
+<input type="hidden" name="r" value="1">
+<input type="hidden" name="hs" value="1">
+<input type="hidden" name="e" value="_decodedcompressedoptions_">
+_sqlqueryformcontent_
+</p>
+</form>
+<!-- end of sqlquery form -->
+}
+
+
 _ifeellucky_ { <br><input type="checkbox" name="ifl" value="1">_textifeellucky_ }
 _useifeellucky_ { }  # Set this to _ifeellucky_ if you want this functionality available
@@ -597,4 +778,12 @@
 _If_(_cgiargqb_,_largequerybox_,_smallquerybox_)
 _useifeellucky_
+</span>
+}
+
+
+_sqlqueryformcontent_ {
+<span class="querybox">
+_If_(_cgiargqb_,_query:largequerybox_,_query:smallquerybox_)
+_query:useifeellucky_
 </span>
 }
@@ -648,4 +837,5 @@
 
 <table><tr><td align="left">
+
 _textformselect_
 </td></tr>
@@ -660,4 +850,30 @@
 <!-- end of query form -->
 }
+
+_sqlfieldqueryform_ {
+sf = _cgiargsf_ <br>
+sqlsf = _cgiargsqlsf_ <br>
+
+<noscript>
+<p><b>_textnojsformwarning_</b></p>
+</noscript>
+<!--sql  field query form (\_query:fieldqueryform\_) -->
+<form name="QueryForm" method="get" action="_gwcgi_">
+
+<table><tr><td align="left">
+_textformselect_
+</td></tr>
+<tr><td>
+_If_(_cgiargb_,_sqladvancedforms_,_sqlsimpleforms_)
+</td></tr>
+</table>
+_If_(_cgiargb_,<div class="divbar">&nbsp;</div>
+_advancedformextra_)
+
+</form>
+<!-- end of sql query form -->
+}
+
+
 
 _advancedforms_{
@@ -672,4 +888,26 @@
 }
 
+_sqladvancedforms_{
+<table border="0" cellspacing="0" cellpadding="0" width="90%">
+  <tr>
+    <th></th>
+    <th align="left">_textfieldphrase_</th>
+    <th></th>
+    <th align="center">&nbsp;&nbsp;_textinwords_</th>
+  </tr>	
+    _sqladvformlist_
+  <tr>
+    <td colspan="2" align="left">
+      <input type="button" value="_textclearform_" onClick="clearForm();">
+    </td>
+    <td colspan="3" align="right">
+      <input type="button" value="_textbeginsearch_" onClick="beginSearch();">
+    </td>
+  </tr>
+</table>
+}
+
+
+
 _advancedformextra_{
 <table>
@@ -679,4 +917,15 @@
 <input type="button" value="_textrunquery_" onClick="runQuery();"></td></tr>
 </table>
+
+
+_If_("_cgiarga_" eq "sqlq",
+<p>For example:<br>
+<i>
+  element in ('dc.Keywords') AND value='Farming'<br>
+  element in ('dls.Title') AND value LIKE 'F%'<br>
+  element in ('dls.Title') AND value<'F%'<br>
+</i>
+)
+
 }
 
@@ -691,4 +940,27 @@
 </table>}
 
+_sqlsimpleforms_{
+<table border="0" cellspacing="0" cellpadding="0" width="90%">
+  <tr>
+    <th align="left">_textfieldphrase_</th>
+    <th></th>
+    <th align="left">&nbsp;&nbsp;_textinwords_</th>
+  </tr>	
+    _sqlregformlist_
+  <tr>
+    <td align="left">
+      <input type="button" value="_textclearform_" onClick="clearForm();">
+    </td>
+    <td align="right">
+      <input type="button" value="_textbeginsearch_" onClick="beginSearch();">
+    </td>
+  </tr>
+</table>
+}
+
+#
+# Full-text versions
+#
+
 _regformelement_{
 <tr><td><input type="text" size="39" name="fqv" onChange="updatefqv();" onkeypress="updatefqv(); entersubmit(event);"></td>
@@ -718,8 +990,95 @@
 </select>}
 
-_textselect_ {_If_(_cgiargb_,_chooseadvancedsearch_,_textsimplesearch_)}
-_chooseadvancedsearch_ {_If_(_cgiargct_,_If_("_cgiargct_" eq "2",_textadvancedlucenesearch_,_textadvancedmgppsearch_),_textadvancedsearch_}
-_textformselect_ {_If_(_cgiargb_,_chooseformadvancedsearch_,_textformsimplesearch_)}
-_chooseformadvancedsearch_ {_If_("_cgiargct_" eq "1",_textformadvancedsearchmgpp_)_If_("_cgiargct_" eq "2",_textformadvancedsearchlucene_)}
+
+_andorfqcselection_ {
+<select name="fqc" onChange="updatefqc();">
+<option value="and">_textand_
+<option value="or">_textor_
+</select>}
+
+
+#
+# SQL versions of regformelement, firstsqladvformelement, advformelement
+#
+
+_sqlregformelement_{
+<tr>
+  <td>
+    _sqlfqfselection_
+  </td>
+  <td>
+    _sqlfqcselection_
+  </td>
+  <td>
+    <input type="text" size="39" name="fqv" onChange="updatefqv();" 
+           onkeypress="updatefqv(); entersubmit(event);">
+  </td>
+</tr>
+}
+
+_firstsqladvformelement_{
+<tr>
+  <td>
+  </td>
+  <td align="right">
+    _sqlfqfselection_
+  </td>
+  <td>
+    _sqlfqcselection_
+  </td>
+  <td>
+    <input type="text" size="31" name="fqv" onChange="updatefqv();" 
+           onkeypress="updatefqv(); entersubmit(event);">
+  </td>
+</tr>
+}
+
+_sqladvformelement_{
+<tr>
+  <td>
+    _andorfqcselection_
+  </td>
+  <td align="right">
+    _sqlfqfselection_
+  </td>
+  <td>
+    _sqlfqcselection_
+  </td>
+  <td>
+    <input type="text" size="31" name="fqv" onChange="updatefqv();" onkeypress="updatefqv(); entersubmit(event);">
+  </td>
+</tr>
+}
+
+
+_sqlfqcselection_ {
+<select name="sqlfqc" onChange="updatesqlfqc();">
+  <option value="=">matches
+  <option value="&lt;">&lt; 
+  <option value="&lt;=">&lt;= 
+  <option value="LIKE">like
+  <option value="&gt;=">&gt;= 
+  <option value="&gt;">&gt;
+  <option value="&lt;&gt;">&lt;&gt;
+
+</select>}
+
+#
+# end of SQL version
+#
+
+_textselect_ {_If_(_cgiargb_,_chooseadvancedsearch_,_choosesimplesearch_)}
+
+_choosesimplesearch_ {_If_("_cgiargqt_" eq "2",_textsimplesqlsearch_,_textsimplesearch_)}
+
+_chooseadvancedsearch_ {_If_("_cgiarqt_" eq "2",_textadvancedsqlsearch_,_choosesimplesearchct_)}
+_chooseadvancedsearchct_ {_If_(_cgiargct_,_If_("_cgiargct_" eq "2",_textadvancedlucenesearch_,_textadvancedmgppsearch_),_textadvancedsearch_}
+
+# formed based versions
+_textformselect_ {_If_(_cgiargb_,_chooseformadvancedsearch_,_chooseformsimplesearch_)}
+_chooseformsimplesearch_ {_If_("_cgiargqt_" eq "2",_textformsimplesearchsql_,_textformsimplesearch_)}
+
+_chooseformadvancedsearch_ {_If_("_cgiargqt_" eq "2",_textformadvancedsearchsql_,_chooseformadvancedsearchct_)}
+_chooseformadvancedsearchct_ {_If_("_cgiargct_" eq "1",_textformadvancedsearchmgpp_)_If_("_cgiargct_" eq "2",_textformadvancedsearchlucene_)}
 
 # mg uses hselection for index, mgpp uses fqfselection 
