Revision 402
Added by berkley over 24 years ago
src/edu/ucsb/nceas/metacat/QuerySpecification.java | ||
---|---|---|
29 | 29 |
import org.xml.sax.helpers.XMLReaderFactory; |
30 | 30 |
import org.xml.sax.helpers.DefaultHandler; |
31 | 31 |
|
32 |
/**
|
|
32 |
/** |
|
33 | 33 |
* A Class that represents a structured query, and can be |
34 | 34 |
* constructed from an XML serialization conforming to @see pathquery.dtd. |
35 | 35 |
* The printSQL() method can be used to print a SQL serialization of the query. |
36 | 36 |
*/ |
37 | 37 |
public class QuerySpecification extends DefaultHandler { |
38 | 38 |
|
39 |
private boolean containsExtendedSQL=false; |
|
40 |
|
|
39 | 41 |
// Query data structures |
40 | 42 |
private String meta_file_id; |
41 | 43 |
private String querytitle; |
42 | 44 |
private Vector doctypeList; |
45 |
private Vector returnFieldList; |
|
43 | 46 |
private QueryGroup query = null; |
44 | 47 |
|
45 | 48 |
private Stack elementStack; |
... | ... | |
59 | 62 |
public QuerySpecification( Reader queryspec, String parserName ) |
60 | 63 |
throws IOException { |
61 | 64 |
super(); |
62 |
|
|
65 |
|
|
63 | 66 |
// Initialize the class variables |
64 | 67 |
doctypeList = new Vector(); |
65 | 68 |
elementStack = new Stack(); |
66 | 69 |
queryStack = new Stack(); |
70 |
returnFieldList = new Vector(); |
|
67 | 71 |
this.parserName = parserName; |
68 | 72 |
|
69 | 73 |
// Initialize the parser and read the queryspec |
... | ... | |
115 | 119 |
|
116 | 120 |
} |
117 | 121 |
} |
122 |
|
|
123 |
/** |
|
124 |
* Returns true if the parsed query contains and extended xml query |
|
125 |
* (i.e. there is at least one <returnfield> in the pathquery document) |
|
126 |
*/ |
|
127 |
public boolean containsExtendedSQL() |
|
128 |
{ |
|
129 |
if(containsExtendedSQL) |
|
130 |
{ |
|
131 |
return true; |
|
132 |
} |
|
133 |
else |
|
134 |
{ |
|
135 |
return false; |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* Accessor method to return a vector of the extended return fields as |
|
141 |
* defined in the <returnfield> tag in the pathquery dtd. |
|
142 |
*/ |
|
143 |
public Vector getReturnFieldList() |
|
144 |
{ |
|
145 |
return this.returnFieldList; |
|
146 |
} |
|
118 | 147 |
|
119 | 148 |
/** |
120 | 149 |
* Set up the SAX parser for reading the XML serialized query |
... | ... | |
221 | 250 |
currentPathexpr = inputString; |
222 | 251 |
} else if (currentTag.equals("returndoctype")) { |
223 | 252 |
doctypeList.add(inputString); |
253 |
} else if (currentTag.equals("returnfield")) { |
|
254 |
returnFieldList.add(inputString); |
|
255 |
containsExtendedSQL = true; |
|
224 | 256 |
} |
225 | 257 |
} |
226 | 258 |
|
... | ... | |
231 | 263 |
public String printSQL() { |
232 | 264 |
StringBuffer self = new StringBuffer(); |
233 | 265 |
|
234 |
// This determines the returned results
|
|
235 |
self.append("SELECT docid,docname,doctype,doctitle ");
|
|
266 |
self.append("SELECT docid,docname,doctype,doctitle,");
|
|
267 |
self.append("date_created, date_updated ");
|
|
236 | 268 |
self.append("FROM xml_documents WHERE docid IN ("); |
237 | 269 |
|
238 | 270 |
// This determines the documents that meet the query conditions |
239 | 271 |
self.append(query.printSQL()); |
240 | 272 |
|
241 | 273 |
self.append(") "); |
242 |
|
|
274 |
|
|
243 | 275 |
// Add SQL to filter for doctypes requested in the query |
244 | 276 |
if (!doctypeList.isEmpty()) { |
245 | 277 |
boolean firstdoctype = true; |
... | ... | |
248 | 280 |
while (en.hasMoreElements()) { |
249 | 281 |
String currentDoctype = (String)en.nextElement(); |
250 | 282 |
if (firstdoctype) { |
251 |
firstdoctype = false; |
|
252 |
self.append(" doctype = '" + currentDoctype + "'"); |
|
283 |
firstdoctype = false;
|
|
284 |
self.append(" doctype = '" + currentDoctype + "'");
|
|
253 | 285 |
} else { |
254 | 286 |
self.append(" OR doctype = '" + currentDoctype + "'"); |
255 | 287 |
} |
256 | 288 |
} |
257 | 289 |
self.append(") "); |
258 | 290 |
} |
259 |
|
|
291 |
|
|
260 | 292 |
return self.toString(); |
261 | 293 |
} |
294 |
|
|
295 |
/** |
|
296 |
* This method prints sql based upon the <returnfield> tag in the |
|
297 |
* pathquery document. This allows for customization of the |
|
298 |
* returned fields |
|
299 |
* The parameters of the query are changed to upper case before the query |
|
300 |
* so that givenName is queryied the same as givenname. |
|
301 |
*/ |
|
302 |
public String printExtendedSQL() |
|
303 |
{ |
|
304 |
StringBuffer self = new StringBuffer(); |
|
305 |
self.append("select A.docid, A.nodename, B.nodedata from xml_nodes A, "); |
|
306 |
self.append("xml_nodes B where A.nodeid = B.parentnodeid "); |
|
307 |
self.append("and B.nodeid in "); |
|
308 |
self.append("(select distinct nodeid from xml_nodes where parentnodeid in "); |
|
309 |
self.append("(select nodeid from xml_index where UPPER(path) like '"); |
|
310 |
boolean firstfield = true; |
|
311 |
//put the returnfields into the query |
|
312 |
//the for loop allows for multiple fields |
|
313 |
for(int i=0; i<returnFieldList.size(); i++) |
|
314 |
{ |
|
315 |
if(firstfield) |
|
316 |
{ |
|
317 |
firstfield = false; |
|
318 |
self.append(((String)returnFieldList.elementAt(i)).toUpperCase()); |
|
319 |
self.append("' "); |
|
320 |
} |
|
321 |
else |
|
322 |
{ |
|
323 |
self.append("or UPPER(path) like '"); |
|
324 |
self.append(((String)returnFieldList.elementAt(i)).toUpperCase()); |
|
325 |
self.append("' "); |
|
326 |
} |
|
327 |
} |
|
328 |
self.append("))"); |
|
329 |
self.append(" AND B.docid in ("); |
|
330 |
self.append(query.printSQL()); |
|
331 |
self.append(")"); |
|
332 |
self.append(" AND B.nodetype = 'TEXT'"); |
|
333 |
|
|
334 |
return self.toString(); |
|
335 |
} |
|
262 | 336 |
|
263 | 337 |
/** |
264 | 338 |
* create a String description of the query that this instance represents. |
... | ... | |
483 | 557 |
|
484 | 558 |
/** |
485 | 559 |
* '$Log$ |
560 |
* 'Revision 1.11 2000/08/14 20:53:34 jones |
|
561 |
* 'Added "release" keyword to all metacat source files so that the release |
|
562 |
* 'number will be evident in software distributions. |
|
563 |
* ' |
|
486 | 564 |
* 'Revision 1.10 2000/06/26 10:35:05 jones |
487 | 565 |
* 'Merged in substantial changes to DBWriter and associated classes and to |
488 | 566 |
* 'the MetaCatServlet in order to accomodate the new UPDATE and DELETE |
Also available in: Unified diff
added support for the returnfield parameter
-QuerySpecification now sets a flag (containsExtendedSQL) when there are returnfield items in the pathquery document.
the accessor method containsExtendedSQL() can be called by other classes to check for extended return parameters
-getReturnFields returns a Vector of the names of each specified return field.
-printExtendedSQL returns a string of the extra SQL statements required for the query.
-a calling class should first check containsExtendedSQL to make sure that there are extra fields being returned, then call printExtendedSQL to
insert the extra SQL into the query. (Note that this is how DBQuery implements this.)