01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: * $Header:$
18: */
19:
20: package org.apache.beehive.controls.system.jdbc.parser;
21:
22: import org.apache.beehive.controls.api.ControlException;
23:
24: import java.util.HashMap;
25: import java.io.StringReader;
26:
27: /**
28: * The SqlParser class is a thread-safe class which parses a string containing a SQL statement
29: * with JdbcControl substitituion delimiters. It is important to note that the SQL is not parsed/validated - only
30: * the sections within the SQL string which are delimited by '{' and '}' are parsed.
31: * <p/>
32: * Parsing is accomplished using the JavaCC grammar file <tt>SqlGrammer.jj</tt>. As the string is parsed it is broken
33: * into fragments by the parser. Any portion of the string which is not between '{' and '}' delimiters becomes a
34: * <tt>LiteralFragment</tt>. The portions of the SQL string which fall between the start and end delimiters are categorized as
35: * either <tt>JdbcFragment</tt>, <tt>ReflectionFragment</tt>, or <tt>SqlSubstitutionFragment</tt>.
36: * <p/>
37: * Fragments which subclass <tt>SqlFragmentContainer</tt> may contain other fragments as children. Fragements subclassed
38: * from <tt>SqlFragment</tt> my not contain child fragments. Upon completion of parsing a <tt>SqlStatement</tt> is
39: * returned to the caller. The <tt>SqlStatement</tt> contains the heirarchary of fragments which have been derived
40: * from the orignal SQL string.
41: * <p/>
42: * The parser will also cache all <tt>SqlStatements</tt> which contain non-volitale SQL. Only <tt>SqlEscapeFragments</tt>
43: * contain volitile SQL at this point.
44: */
45: public final class SqlParser {
46:
47: // maintain a cache of SQLStatements which have already been parsed
48: private HashMap<String, SqlStatement> _cachedSqlStatements;
49:
50: /**
51: * Create a new instance of the SqlParser.
52: */
53: public SqlParser() {
54: _cachedSqlStatements = new HashMap<String, SqlStatement>();
55: }
56:
57: /**
58: * Parse the sql and return an SqlStatement.
59: *
60: * @param sql A String contianing the sql to parse.
61: * @return A SqlStatement instance.
62: */
63: public SqlStatement parse(String sql) {
64:
65: // does a cached parse result exist for this statement?
66: if (_cachedSqlStatements.containsKey(sql)) {
67: return _cachedSqlStatements.get(sql);
68: }
69:
70: SqlGrammar _parser = new SqlGrammar(new StringReader(sql));
71: SqlStatement parsed = null;
72: try {
73: parsed = _parser.parse();
74: } catch (ParseException e) {
75: throw new ControlException("Error parsing SQL statment."
76: + e.getMessage(), e);
77: } catch (TokenMgrError tme) {
78: throw new ControlException("Error parsing SQL statment. "
79: + tme.getMessage(), tme);
80: }
81:
82: if (parsed.isCacheable()) {
83: _cachedSqlStatements.put(sql, parsed);
84: }
85: return parsed;
86: }
87: }
|