cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sidney Chong" <sid...@krdl.org.sg>
Subject SQLQueryCreator feature add
Date Thu, 02 Dec 1999 04:25:36 GMT
Hi, I needed to use some session parameters (in addition to request parms)
in my SQL queries but the stock SQLQueryCreator that came with cocoon-1.5
can only do request parameters (although the master_default_query_props did
include session variable delimiters) hence I've decided to build it in
myself. The code is attached below. It goes into the SQLQueryCreator class
and replaces the getQuery() mtd.

Hope this helps.

Cheers
Sidney

  /**
   * Variable type selection
   */
  protected static final int SESSION=0;
  protected static final int REQUEST=1;

  public String getQuery(Connection conn, String query, Element
query_element, Properties query_props, Dictionary parameters) throws
Exception {
   HttpServletRequest req = (HttpServletRequest)parameters.get("request");
   query = this.substitute(query, req,
query_props.getProperty("session-variable-left-delimiter"),
query_props.getProperty("session-variable-right-delimiter"), SESSION);
   query = this.substitute(query, req,
query_props.getProperty("variable-left-delimiter"),
query_props.getProperty("variable-right-delimiter"), REQUEST);
   return query;
  }

  protected String substitute(String query, HttpServletRequest req, String
ldelim, String rdelim, int type) {
   int llength = ldelim.length();
   int rlength = rdelim.length();
   int offset = 0;
   StringBuffer buf = new StringBuffer(query);
   while (true) {
    int lindex = query.indexOf(ldelim,offset);
    if (lindex < 0) break;
    int rindex = query.indexOf(rdelim,offset+llength);
    if (rindex < 0 || rindex < lindex) break;
    String name = query.substring(lindex+llength,rindex);
    String value = this.getValue(req, name, type);
    if (value == null) break;
    buf = buf.replace(lindex, rindex+rlength, value);
    offset = lindex+value.length();
   }
   return buf.toString();
  }

  protected String getValue(HttpServletRequest req, String name, int type) {
   String val = null;
   switch ( type ) {
    case SESSION :
     val = (String) (req.getSession(true).getValue(name));
     break;
    case REQUEST :
     val = req.getParameter(name);
     break;
    default :
     val = "";
   }
  return val;
  }



Mime
View raw message