Jon Stevens wrote:
> SEARCHING:
>
> It would be nice to have an interface or two that defines what a search
> engine is as well as a few implementations of those interfaces. If you look
> in Jyve, there is basic %foo% style searching implemented in there. Doing
> something like that that is cleaner would be *really* nice to have.
I have been fooling around with recursive criteria to allow more complicated
SQL generated for searching. I would appreciate comments on the code below.
Basically what I did was add a vector to the Criteria class that houses
criteria. For each criteria in the vector BasePeer then generates a search
string for it recursively. I tried to minimize my changes and keep it simple.
Here is an example from my application:
Criteria c = new Criteria();
c.setDbName("db");
c.setMapName("db");
c.add(TablePeer.DESCR, "test", LIKE);
Criteria c1 = new Criteria();
c1.add(TablePeer.DESCR, "test1", LIKE);
c1.setDbName("db");
c1.setMapName("db");
Criteria c2 = new Criteria();
c2.add(TablePeer.DESCR, "test2", LIKE);
c2.setDbName("db");
c2.setMapName("db");
c.addOrCriteria(c1, c2);
c.addSelectColumn(TablePeer.DESCR);
this in turn generates:
SELECT Table.DESCR FROM Table WHERE Table.DESCR LIKE "test" AND
(Table.DESCR LIKE "test1" OR Table.DESCR LIKE "test2")
A note on the code: It does not handle all situations and is not garunteed to
generate correct SQL! (Also it is not javadoc'd).
Index: BasePeer.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/om/peer/BasePeer.java,v
retrieving revision 1.3
diff -u -r1.3 BasePeer.java
--- BasePeer.java 2000/08/09 16:26:22 1.3
+++ BasePeer.java 2000/08/22 14:09:45
@@ -683,6 +683,14 @@
*/
public static String createQueryString( Criteria criteria ) throws
Exception
{
+ return (createQuery(criteria).toString());
+ }
+
+ /**
+ * Method to create an SQL query based on values in a Criteria.
+ */
+ public static Query createQuery( Criteria criteria ) throws Exception
+ {
Query querySql = new Query();
DB db = DBBroker.getInstance().getDB( criteria.getDbName() );
DatabaseMap dbMap = DBBroker.getInstance().getDatabaseMap(
criteria.getMapName() );
@@ -757,7 +765,21 @@
// more to come ???
}
}
-
+
+ if (criteria.getOrCriteria().size() != 0)
+ {
+ Vector orCriteria = criteria.getOrCriteria();
+ StringStackBuffer orClause = new StringStackBuffer();
+ for (int i=0; i<orCriteria.size(); i++)
+ {
+ Query orQuery = createQuery((Criteria)
orCriteria.elementAt(i));
+ orClause.add(orQuery.getWhereClause().toString());
+
+ }
+ String orString = "(" + orClause.toString( " OR " ) + ")";
+ whereClause.add(orString);
+ }
+
if ( orderBy != null && orderBy.size() > 0)
{
// Check for each String/Character column and apply toUpperCase()
@@ -821,7 +843,7 @@
if (limitString != null) querySql.setLimit(limitString);
if (DEBUG) System.out.println("BasePeer.querySql= "+
querySql.toString());
- return querySql.toString();
+ return querySql;
}
Index: Criteria.java
===================================================================
RCS file:
/products/cvs/turbine/turbine/src/java/org/apache/turbine/util/db/Criteria.java,v
retrieving revision 1.2
diff -u -r1.2 Criteria.java
--- Criteria.java 2000/08/08 20:48:18 1.2
+++ Criteria.java 2000/08/22 14:26:41
@@ -96,7 +96,8 @@
public static final String IN = " IN ";
public static final String NOT_IN = " NOT IN ";
public static final String ALL = "ALL ";
-
+
+ private Vector orCriteria = new Vector();
private boolean ignoreCase = false;
private boolean singleRecord = false;
private boolean cascade = false;
@@ -522,6 +523,11 @@
.toString() );
}
+ public Vector getOrCriteria()
+ {
+ return orCriteria;
+ }
+
/**
* Overrides Hashtable get, so that the value placed in the CriteriaObject
is
* returned instead of the CriteriaObject
@@ -812,6 +818,19 @@
add(key, (Object)values, Criteria.NOT_IN);
return this;
}
+
+ public Criteria addOrCriteria(Criteria criteria1, Criteria criteria2)
+ {
+ orCriteria.addElement(criteria1);
+ orCriteria.addElement(criteria2);
+ return this;
+ }
+
+ public void addOrCriteria(Criteria criteria)
+ {
+ orCriteria.addElement(criteria);
+ }
+
/**
Adds "ALL " to the SQL statement
*/
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?: [EMAIL PROTECTED]