KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > SQLQueryExecutor


1 /**
2  * com.mckoi.database.interpret.SQLQueryExecutor 25 Mar 2002
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.interpret;
26
27 import com.mckoi.database.*;
28 import com.mckoi.database.jdbc.SQLQuery;
29 import com.mckoi.database.sql.SQL;
30 import com.mckoi.database.sql.ParseException;
31
32 import java.io.StringReader JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 /**
36  * An object used to execute SQL queries against a given DatabaseConnection
37  * object. The object maintains an SQL parser object as state which is
38  * reused as necessary.
39  * <p>
40  * This object is a convenient way to execute SQL queries.
41  *
42  * @author Tobias Downer
43  */

44
45 public class SQLQueryExecutor {
46
47   /**
48    * The SQL parser state.
49    */

50   private static SQL sql_parser;
51
52   static {
53     // Set up the sql parser.
54
sql_parser = new SQL(new StringReader JavaDoc(""));
55   }
56   
57   /**
58    * Constructs the executor.
59    */

60   public SQLQueryExecutor() {
61   }
62
63   /**
64    * Executes the given SQLQuery object on the given DatabaseConnection object.
65    * Returns a Table object that contains the result of the execution.
66    * <p>
67    * Note that this method does not perform any locking. Any locking must have
68    * happened before this method is called.
69    * <p>
70    * Also note that the returned Table object is onld valid within the
71    * life-time of the lock unless the root lock requirements are satisified.
72    */

73   public Table execute(DatabaseConnection connection, SQLQuery query)
74                throws SQLException JavaDoc, DatabaseException, TransactionException,
75                       ParseException {
76
77     // StatementTree caching
78

79     // Create a new parser and set the parameters...
80
String JavaDoc query_str = query.getQuery();
81     StatementTree statement_tree = null;
82     StatementCache statement_cache =
83                                   connection.getSystem().getStatementCache();
84
85     if (statement_cache != null) {
86       // Is this query cached?
87
statement_tree = statement_cache.get(query_str);
88     }
89     if (statement_tree == null) {
90       synchronized (sql_parser) {
91         sql_parser.ReInit(new StringReader JavaDoc(query_str));
92         sql_parser.reset();
93         // Parse the statement.
94
statement_tree = sql_parser.Statement();
95       }
96       // Put the statement tree in the cache
97
if (statement_cache != null) {
98         statement_cache.put(query_str, statement_tree);
99       }
100     }
101
102     // Substitute all parameter substitutions in the statement tree.
103
final Object JavaDoc[] vars = query.getVars();
104     ExpressionPreparer preparer = new ExpressionPreparer() {
105       public boolean canPrepare(Object JavaDoc element) {
106         return (element instanceof ParameterSubstitution);
107       }
108       public Object JavaDoc prepare(Object JavaDoc element) {
109         ParameterSubstitution ps = (ParameterSubstitution) element;
110         int param_id = ps.getID();
111         return TObject.objectVal(vars[param_id]);
112       }
113     };
114     statement_tree.prepareAllExpressions(preparer);
115
116     // Convert the StatementTree to a statement object
117
Statement statement;
118     String JavaDoc statement_class = statement_tree.getClassName();
119     try {
120       Class JavaDoc c = Class.forName(statement_class);
121       statement = (Statement) c.newInstance();
122     }
123     catch (ClassNotFoundException JavaDoc e) {
124       throw new SQLException JavaDoc(
125                     "Could not find statement class: " + statement_class);
126     }
127     catch (InstantiationException JavaDoc e) {
128       throw new SQLException JavaDoc(
129                     "Could not instantiate class: " + statement_class);
130     }
131     catch (IllegalAccessException JavaDoc e) {
132       throw new SQLException JavaDoc(
133                     "Could not access class: " + statement_class);
134     }
135
136
137     // Initialize the statement
138
statement.init(connection, statement_tree, query);
139
140     // Automated statement tree preparation
141
statement.resolveTree();
142
143     // Prepare the statement.
144
statement.prepare();
145
146     // Evaluate the SQL statement.
147
Table result = statement.evaluate();
148
149     return result;
150
151   }
152
153 }
154
Popular Tags