01: package org.makumba.providers.query.oql;
02:
03: import java.io.StringReader;
04: import java.util.Date;
05:
06: import org.makumba.OQLParseError;
07: import org.makumba.commons.NamedResourceFactory;
08: import org.makumba.commons.NamedResources;
09: import org.makumba.commons.RuntimeWrappedException;
10: import org.makumba.providers.QueryAnalysis;
11: import org.makumba.providers.QueryAnalysisProvider;
12:
13: public class OQLQueryAnalysisProvider implements QueryAnalysisProvider {
14: public static int parsedQueries = NamedResources.makeStaticCache(
15: "OQL parsed queries", new NamedResourceFactory() {
16:
17: private static final long serialVersionUID = 1L;
18:
19: protected Object makeResource(Object nm, Object hashName)
20: throws Exception {
21: return OQLQueryAnalysisProvider
22: .parseQueryFundamental((String) nm);
23: }
24: }, true);
25:
26: public QueryAnalysis getQueryAnalysis(String query) {
27:
28: try {
29: return (QueryAnalysis) NamedResources.getStaticCache(
30: parsedQueries).getResource(query);
31: } catch (RuntimeWrappedException e) {
32: if (e.getCause() instanceof antlr.RecognitionException) {
33: Exception f = (antlr.RecognitionException) e.getCause();
34: String s = f.getMessage();
35: if (s.startsWith("line"))
36: s = s.substring(s.indexOf(':') + 1);
37: throw new OQLParseError("\r\nin query:\r\n" + query, f);
38: }
39: throw e;
40: }
41: }
42:
43: /**
44: * Performs the analysis of an OQL query
45: * @param oqlQuery the query to analyse
46: * @return the OQL analysis correponding to the query
47: * @throws antlr.RecognitionException
48: */
49: public static QueryAnalysis parseQueryFundamental(String oqlQuery)
50: throws antlr.RecognitionException {
51: Date d = new Date();
52: OQLLexer lexer = new OQLLexer(new StringReader(oqlQuery));
53: OQLParser parser = new OQLParser(lexer);
54: // Parse the input expression
55: QueryAST t = null;
56: try {
57:
58: parser
59: .setASTNodeClass("org.makumba.providers.query.oql.OQLAST");
60: parser.queryProgram();
61: t = (QueryAST) parser.getAST();
62: t.setOQL(oqlQuery);
63: // Print the resulting tree out in LISP notation
64: // MakumbaSystem.getLogger("debug.db").severe(t.toStringTree());
65:
66: // see the tree in a window
67: /*
68: * if(t!=null) { ASTFrame frame = new ASTFrame("AST JTree Example", t); frame.setVisible(true); }
69: */
70: } catch (antlr.TokenStreamException f) {
71: java.util.logging.Logger.getLogger(
72: "org.makumba." + "db.query.compilation").warning(
73: f + ": " + oqlQuery);
74: throw new org.makumba.MakumbaError(f, oqlQuery);
75: }
76: long diff = new java.util.Date().getTime() - d.getTime();
77: java.util.logging.Logger.getLogger(
78: "org.makumba." + "db.query.compilation").fine(
79: "OQL to SQL: " + diff + " ms: " + oqlQuery);
80: return t;
81: }
82:
83: }
|