KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > jdbc > parser > SqlParser


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.controls.system.jdbc.parser;
20
21 import org.apache.beehive.controls.api.ControlException;
22
23 import java.util.HashMap JavaDoc;
24 import java.io.StringReader JavaDoc;
25
26 /**
27  * The SqlParser class is a thread-safe class which parses a string containing a SQL statement
28  * with JdbcControl substitituion delimiters. It is important to note that the SQL is not parsed/validated - only
29  * the sections within the SQL string which are delimited by '{' and '}' are parsed.
30  * <p/>
31  * Parsing is accomplished using the JavaCC grammar file <tt>SqlGrammer.jj</tt>. As the string is parsed it is broken
32  * into fragments by the parser. Any portion of the string which is not between '{' and '}' delimiters becomes a
33  * <tt>LiteralFragment</tt>. The portions of the SQL string which fall between the start and end delimiters are categorized as
34  * either <tt>JdbcFragment</tt>, <tt>ReflectionFragment</tt>, or <tt>SqlSubstitutionFragment</tt>.
35  * <p/>
36  * Fragments which subclass <tt>SqlFragmentContainer</tt> may contain other fragments as children. Fragements subclassed
37  * from <tt>SqlFragment</tt> my not contain child fragments. Upon completion of parsing a <tt>SqlStatement</tt> is
38  * returned to the caller. The <tt>SqlStatement</tt> contains the heirarchary of fragments which have been derived
39  * from the orignal SQL string.
40  * <p/>
41  * The parser will also cache all <tt>SqlStatements</tt> which contain non-volitale SQL. Only <tt>SqlEscapeFragments</tt>
42  * contain volitile SQL at this point.
43  */

44 public final class SqlParser {
45
46     // maintain a cache of SQLStatements which have already been parsed
47
private HashMap JavaDoc<String JavaDoc, SqlStatement> _cachedSqlStatements;
48
49     /**
50      * Create a new instance of the SqlParser.
51      */

52     public SqlParser() {
53         _cachedSqlStatements = new HashMap JavaDoc<String JavaDoc, SqlStatement>();
54     }
55
56     /**
57      * Parse the sql and return an SqlStatement.
58      *
59      * @param sql A String contianing the sql to parse.
60      * @return A SqlStatement instance.
61      */

62     public SqlStatement parse(String JavaDoc sql) {
63
64         // does a cached parse result exist for this statement?
65
if (_cachedSqlStatements.containsKey(sql)) {
66             return _cachedSqlStatements.get(sql);
67         }
68
69         SqlGrammar _parser = new SqlGrammar(new StringReader JavaDoc(sql));
70         SqlStatement parsed = null;
71         try {
72             parsed = _parser.parse();
73         } catch (ParseException e) {
74             throw new ControlException("Error parsing SQL statment." + e.getMessage(), e);
75         } catch (TokenMgrError tme) {
76             throw new ControlException("Error parsing SQL statment. " + tme.getMessage(), tme);
77         }
78
79         if (parsed.isCacheable()) {
80             _cachedSqlStatements.put(sql, parsed);
81         }
82         return parsed;
83     }
84 }
85
Popular Tags