Ignore:
Timestamp:
2010-05-06T12:55:03+12:00 (14 years ago)
Author:
davidb
Message:

Changes necessary to support new sql-query action

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/runtime-src/src/recpt/querytools.cpp

    r20602 r22046  
    8282    args.setintarg("qto", type);
    8383  }
    84  
     84
    8585  if (args["qt"].empty()) {
    8686    int arg_qto = args.getintarg("qto");
     
    9191    }
    9292  }
     93
     94
     95  // decide if sqlqto should be set or not
     96  unsigned int sql_type = 0;
     97  text_t infodb_type = cinfo->infodbType;
     98  if ((infodb_type == "sqlite") || (infodb_type == "mssql")) {
     99    if (findword(search_types.begin(), search_types.end(), "sqlform") != search_types.end()) {
     100      sql_type = 1;
     101    }
     102  }
     103
     104  if (sql_type) {
     105    args["sqlqto"] = "1";
     106  }
     107  else {
     108    args["sqlqto"] = "0";
     109  }
     110
     111
    93112}
    94113
     
    109128}
    110129
     130
     131
     132void set_basequeryfilter_options (FilterRequest_t &request,
     133                  cgiargsclass &args)
     134{
     135
     136  OptionValue_t option;
     137  int arg_m = args.getintarg("m");
     138 
     139  option.name = "Maxdocs";
     140  option.value = arg_m;
     141  request.filterOptions.push_back (option);
     142
     143  //  option.name = "StartResults";
     144  //  option.value = args["r"];
     145  //  request.filterOptions.push_back (option);
     146
     147  //  option.name = "EndResults";
     148  //  int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
     149  //  if ((endresults > arg_m) && (arg_m != -1)) endresults = arg_m;
     150  //  option.value = endresults;
     151  //  request.filterOptions.push_back (option);
     152}
     153
     154
    111155// request.filterResultOptions and request.fields (if required) should
    112156// be set from the calling code
    113 void set_queryfilter_options (FilterRequest_t &request,
    114                   const text_t &querystring,
    115                   cgiargsclass &args) {
     157void set_fulltext_queryfilter_options (FilterRequest_t &request,
     158                       const text_t &querystring,
     159                       cgiargsclass &args)
     160{
     161  // better if this function, and the two-query companion function
     162  // was implemented in queryaction.cpp
     163  // Has to be done here to documentaction.cpp can call it directly
    116164
    117165  request.filterName = "QueryFilter";
     
    190238  }
    191239
    192   set_more_queryfilter_options (request, args);
    193 }
    194 
    195 void set_queryfilter_options (FilterRequest_t &request,
    196                   const text_t &querystring1,
    197                   const text_t &querystring2, cgiargsclass &args) {
    198 
    199   set_queryfilter_options (request, querystring1, args);
     240  set_basequeryfilter_options(request, args);
     241}
     242
     243
     244
     245void set_fulltext_queryfilter_options (FilterRequest_t &request,
     246                       const text_t &querystring1,
     247                       const text_t &querystring2,
     248                       cgiargsclass &args)
     249{
     250
     251  set_fulltext_queryfilter_options (request, querystring1, args);
    200252
    201253  // fill in the second query if needed
     
    245297    }
    246298  }
    247   set_more_queryfilter_options (request, args);
    248 }
    249 
    250 void set_more_queryfilter_options (FilterRequest_t &request,
    251                    cgiargsclass &args) {
    252 
    253   OptionValue_t option;
    254   int arg_m = args.getintarg("m");
    255  
    256   option.name = "Maxdocs";
    257   option.value = arg_m;
    258   request.filterOptions.push_back (option);
    259 
    260   //  option.name = "StartResults";
    261   //  option.value = args["r"];
    262   //  request.filterOptions.push_back (option);
    263 
    264   //  option.name = "EndResults";
    265   //  int endresults = args.getintarg("o") + (args.getintarg("r") - 1);
    266   //  if ((endresults > arg_m) && (arg_m != -1)) endresults = arg_m;
    267   //  option.value = endresults;
    268   //  request.filterOptions.push_back (option);
    269 }
     299
     300  // this is probably redundant, as first line to this method will have
     301  // already caused it to invoke set_basequeryfilter_options
     302
     303  set_basequeryfilter_options(request, args);
     304}
     305
     306
     307
     308// request.filterResultOptions and request.fields (if required) should
     309// be set from the calling code
     310void set_sql_queryfilter_options (FilterRequest_t &request,
     311                  cgiargsclass &args)
     312{
     313  if (!args["sqlsf"].empty()) { // sort field for lucene
     314    OptionValue_t option;
     315
     316    option.name = "SortField";
     317    option.value = args["sqlsf"];
     318    request.filterOptions.push_back (option);
     319  }
     320
     321  set_basequeryfilter_options(request, args);
     322}
     323
    270324
    271325bool is_special_character(int indexer_type, unsigned short character) {
     
    551605    if (!values[i].empty()) {
    552606      text_t this_value = values[i];
     607
    553608      // remove operators for simple search, segments text if necessary
    554609      format_querystring(this_value, argb, segment);
     610           
    555611      // add tag info for this field (and other processing)
    556612      format_field_info(this_value, fields[i], argct, argt, argb);
     613
    557614      // add into query string
    558615      if (argct == 2) {
     
    644701  }
    645702}
     703
     704
     705// SQL versions for parsing query form
     706
     707void parse_sqlreg_query_form(text_t &querystring, cgiargsclass &args, bool segment)
     708{
     709  querystring.clear();
     710
     711  int argt = args.getintarg("t");// t=0 -and, t=1 - or
     712  int argb = args.getintarg("b");
     713   
     714  text_t combine;
     715
     716  if (argt == 0) combine = "AND";
     717  else combine = "OR";
     718 
     719  text_t field = args["sqlfqf"];
     720  if (field.empty()) return; // no query
     721  text_tarray fields;
     722  splitchar(field.begin(), field.end(), ',', fields);
     723
     724  text_t sqlcomb = args["sqlfqc"];
     725  if (sqlcomb.empty()) return; //somethings wrong
     726  text_tarray sqlcombs;
     727  splitchar(sqlcomb.begin(), sqlcomb.end(), ',', sqlcombs);
     728 
     729  text_t value = args["fqv"];
     730  if (value.empty()) return; // somethings wrong
     731  text_tarray values;
     732  splitchar(value.begin(), value.end(), ',', values);
     733
     734
     735  for (int i=0; i< values.size(); ++i) {
     736    if (!values[i].empty()) {
     737      text_t this_value = values[i];
     738
     739      // remove operators for simple search, segments text if necessary
     740      format_querystring(this_value, argb, segment);
     741           
     742      // add tag info for this field (and other processing)
     743      format_field_info_sql(this_value, fields[i], sqlcombs[i], argt, argb);
     744
     745      const text_t DISTINCT_SELECT_WHERE
     746    = "SELECT DISTINCT docOID FROM document_metadata WHERE ";
     747
     748      if (querystring.empty()) {
     749    // first query term
     750    querystring = DISTINCT_SELECT_WHERE + this_value;
     751      }
     752      else {
     753    this_value = DISTINCT_SELECT_WHERE + this_value;
     754
     755    if (combine=="AND") {   
     756      // INNER JOIN to restrict to only matching docOIDs
     757      querystring = "SELECT docOID FROM (" + querystring + ")"
     758        + " INNER JOIN (" + this_value +") USING (docOID)";
     759    }
     760    else if (combine=="OR") {
     761      // Union to allow union of the two
     762      querystring = querystring + " UNION " + this_value;
     763    }
     764      }
     765    }
     766  }
     767}
     768
     769
     770void parse_sqladv_query_form(text_t &querystring, cgiargsclass &args,
     771                 bool segment)
     772{
     773  querystring.clear();
     774
     775  int argt = 0; // set it to 0 = AND, by default
     776  int argb = args.getintarg("b");
     777  text_t combine = "AND";
     778
     779  text_t field = args["sqlfqf"];
     780
     781  if (field.empty()) return; // no query
     782  text_tarray fields;
     783  splitchar(field.begin(), field.end(), ',', fields);
     784 
     785  text_t sqlcomb = args["sqlfqc"];
     786  if (sqlcomb.empty()) return; //somethings wrong
     787  text_tarray sqlcombs;
     788  splitchar(sqlcomb.begin(), sqlcomb.end(), ',', sqlcombs);
     789
     790  text_t value = args["fqv"];
     791  if (value.empty()) return; // somethings wrong
     792  text_tarray values;
     793  splitchar(value.begin(), value.end(), ',', values);
     794
     795  text_t comb = args["fqc"];
     796  if (comb.empty()) return; //somethings wrong
     797  text_tarray combs;
     798  splitchar(comb.begin(), comb.end(), ',', combs);
     799
     800  for(int i=0; i< values.size(); ++i) {
     801    if (!values[i].empty()) {
     802      if (i>0) {
     803    if (combs[i-1]=="and") { combine = "AND"; }
     804    else if (combs[i-1]=="or") { combine = "OR"; }
     805    else if (combs[i-1]=="not") { combine = "NOT"; }
     806      }
     807      text_t this_value = values[i];
     808
     809      // remove operators for simple search, segments text if necessary
     810      format_querystring(this_value, argb, segment);
     811
     812      // add tag info for this field (and other processing)
     813      format_field_info_sql(this_value, fields[i], sqlcombs[i], argt, argb);
     814
     815      // add into query string
     816
     817      const text_t DISTINCT_SELECT_WHERE
     818    = "SELECT DISTINCT docOID FROM document_metadata WHERE ";
     819
     820      if (querystring.empty()) {
     821    // first query term
     822    querystring = DISTINCT_SELECT_WHERE + this_value;
     823      }
     824      else {
     825    this_value = DISTINCT_SELECT_WHERE + this_value;
     826
     827    if (combine=="AND") {   
     828      // INNER JOIN to restrict to only matching docOIDs
     829      querystring = "SELECT docOID FROM (" + querystring + ")"
     830        + " INNER JOIN (" + this_value +") USING (docOID)";
     831    }
     832    else if (combine=="OR") {
     833      // Union to allow union of the two
     834      querystring = querystring + " UNION " + this_value;
     835    }
     836    else {
     837      cerr << "Unsupported combination operation: " << combine << endl;
     838    }
     839      }
     840     
     841    }
     842  } 
     843}
     844
     845
     846
    646847
    647848// Extended addqueryelem for Human Info project
     
    738939    querystring = tag+":("+querystring+")";
    739940  }
     941   
     942}
     943
     944
     945void add_field_info_sql(text_t &querystring, const text_t &tagseq,
     946            const text_t& sqlcomb)
     947{
     948
     949  if (tagseq == "") return; // do nothing
     950
     951  text_t element_in = "(element IN (";
     952
     953  text_tlist mdterms;
     954
     955  splitword(tagseq.begin(), tagseq.end(), "/", mdterms);
     956
     957  text_t tags_in = "";
     958
     959  while (!mdterms.empty()) {
     960    text_t tag = mdterms.front();
     961    mdterms.pop_front();
     962
     963    if (!tag.empty()) {
     964
     965      if (tag.size()>3 && (substr(tag.begin(), tag.begin()+3) == "ex.")) {
     966    tag = substr (tag.begin()+3, tag.end());
     967      }
     968
     969      if (!tags_in.empty()) {
     970    tags_in += ",";
     971      }
     972     
     973      tags_in += "'" + tag + "'";
     974    }
     975  }
     976
     977  element_in += tags_in + ") AND (";
     978
     979  if (sqlcomb == "=") {
     980    // override what it means to do equality, to make it more like full text
     981    // searching
     982
     983    text_t orterms = "";
     984    text_t term = "";
     985    bool in_phrase = false;
     986   
     987    text_t::const_iterator here = querystring.begin();
     988    text_t::const_iterator end = querystring.end();
     989    while (here != end) {
     990      if (is_unicode_letdig(*here)) {
     991    term.push_back(*here);
     992      }
     993      else if (*here == '"') {
     994    term.push_back(*here);
     995    if (!in_phrase) {
     996      in_phrase = true;
     997    } else {
     998      in_phrase = false;
     999    }
     1000      }     
     1001      else if (in_phrase) {
     1002        // Found word boundary, but in a phrase, so does not complete term
     1003    term.push_back(*here);
     1004      }
     1005      else {
     1006        // Found a word boundary
     1007    if (!orterms.empty()) {
     1008      orterms += " OR ";
     1009    }
     1010    orterms += "value LIKE '%" + term + "%'";
     1011    term.clear();
     1012      }
     1013      ++here;
     1014    }
     1015
     1016    if (!term.empty()) {
     1017    if (!orterms.empty()) {
     1018      orterms += " OR ";
     1019    }
     1020        orterms += "value LIKE '%" + term + "%'";
     1021    }
     1022
     1023    element_in += orterms;
     1024  }
     1025  else {
     1026    // search on value is "as is" querystring
     1027    element_in += "value " + sqlcomb + " '" + querystring+"'";
     1028  }
     1029
     1030 
     1031  querystring = element_in + "))";
    7401032   
    7411033}
     
    9011193    finalquery.push_back(' ');
    9021194  }
    903 
     1195 
    9041196  querystring  = finalquery;
     1197}
     1198
     1199
     1200void format_field_info_sql(text_t &querystring, const text_t &tagseq,
     1201               const text_t &sqlcomb,
     1202               int argt, int argb)
     1203{
     1204  add_field_info_sql(querystring, tagseq, sqlcomb);
    9051205}
    9061206
Note: See TracChangeset for help on using the changeset viewer.