1 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 ; 33 import java.sql.SQLException ; 34 35 44 45 public class SQLQueryExecutor { 46 47 50 private static SQL sql_parser; 51 52 static { 53 sql_parser = new SQL(new StringReader ("")); 55 } 56 57 60 public SQLQueryExecutor() { 61 } 62 63 73 public Table execute(DatabaseConnection connection, SQLQuery query) 74 throws SQLException , DatabaseException, TransactionException, 75 ParseException { 76 77 79 String query_str = query.getQuery(); 81 StatementTree statement_tree = null; 82 StatementCache statement_cache = 83 connection.getSystem().getStatementCache(); 84 85 if (statement_cache != null) { 86 statement_tree = statement_cache.get(query_str); 88 } 89 if (statement_tree == null) { 90 synchronized (sql_parser) { 91 sql_parser.ReInit(new StringReader (query_str)); 92 sql_parser.reset(); 93 statement_tree = sql_parser.Statement(); 95 } 96 if (statement_cache != null) { 98 statement_cache.put(query_str, statement_tree); 99 } 100 } 101 102 final Object [] vars = query.getVars(); 104 ExpressionPreparer preparer = new ExpressionPreparer() { 105 public boolean canPrepare(Object element) { 106 return (element instanceof ParameterSubstitution); 107 } 108 public Object prepare(Object 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 Statement statement; 118 String statement_class = statement_tree.getClassName(); 119 try { 120 Class c = Class.forName(statement_class); 121 statement = (Statement) c.newInstance(); 122 } 123 catch (ClassNotFoundException e) { 124 throw new SQLException ( 125 "Could not find statement class: " + statement_class); 126 } 127 catch (InstantiationException e) { 128 throw new SQLException ( 129 "Could not instantiate class: " + statement_class); 130 } 131 catch (IllegalAccessException e) { 132 throw new SQLException ( 133 "Could not access class: " + statement_class); 134 } 135 136 137 statement.init(connection, statement_tree, query); 139 140 statement.resolveTree(); 142 143 statement.prepare(); 145 146 Table result = statement.evaluate(); 148 149 return result; 150 151 } 152 153 } 154 | Popular Tags |