1 24 25 package com.mckoi.database.interpret; 26 27 import com.mckoi.database.jdbc.SQLQuery; 28 import com.mckoi.database.*; 29 import com.mckoi.debug.DebugLogger; 30 import java.util.*; 31 32 38 39 public abstract class Statement { 40 41 44 protected DatabaseConnection database; 45 46 49 protected User user; 50 51 54 protected StatementTree cmd; 55 56 59 protected SQLQuery query; 60 61 65 protected Vector table_list = new Vector(); 66 67 70 public final DebugLogger Debug() { 71 return database.Debug(); 72 } 73 74 78 void reset() { 79 database = null; 80 user = null; 81 table_list = new Vector(); 82 } 83 84 97 public final void resolveTree() throws DatabaseException { 98 99 ExpressionPreparer preparer = new ExpressionPreparer() { 104 public boolean canPrepare(Object element) { 105 return element instanceof StatementTree; 106 } 107 public Object prepare(Object element) throws DatabaseException { 108 StatementTree stmt_tree = (StatementTree) element; 109 Select stmt = new Select(); 110 stmt.init(database, stmt_tree, null); 111 stmt.resolveTree(); 112 stmt.prepare(); 113 return stmt; 114 } 115 }; 116 cmd.prepareAllExpressions(preparer); 117 118 } 119 120 124 FromTableInterface findTableWithColumn(Variable column_name) { 125 for (int i = 0; i < table_list.size(); ++i) { 126 FromTableInterface table = (FromTableInterface) table_list.elementAt(i); 127 TableName tname = column_name.getTableName(); 128 String sch_name = null; 129 String tab_name = null; 130 String col_name = column_name.getName(); 131 if (tname != null) { 132 sch_name = tname.getSchema(); 133 tab_name = tname.getName(); 134 } 135 int rcc = table.resolveColumnCount(null, sch_name, tab_name, col_name); 136 if (rcc > 0) { 137 return table; 138 } 139 } 140 return null; 141 } 142 143 149 boolean existsTableWithColumn(Variable column_name) { 150 return findTableWithColumn(column_name) != null; 151 } 152 153 161 ArrayList resolveAgainstAliases(Variable alias_name) { 162 return new ArrayList(0); 163 } 164 165 172 TableName resolveTableName(String name, DatabaseConnection db) { 173 return db.resolveTableName(name); 174 } 175 176 181 FromTableInterface findTableInQuery(String schema, String name) { 182 for (int p = 0; p < table_list.size(); ++p) { 183 FromTableInterface table = (FromTableInterface) table_list.get(p); 184 if (table.matchesReference(null, schema, name)) { 185 return table; 186 } 187 } 188 return null; 189 } 190 191 195 Variable resolveColumn(Variable v) { 196 ArrayList list = new ArrayList(); 198 list.addAll(resolveAgainstAliases(v)); 199 200 TableName tname = v.getTableName(); 201 String sch_name = null; 202 String tab_name = null; 203 String col_name = v.getName(); 204 if (tname != null) { 205 sch_name = tname.getSchema(); 206 tab_name = tname.getName(); 207 } 208 209 int matches_found = 0; 210 for (int i = 0; i < table_list.size(); ++i) { 212 FromTableInterface table = (FromTableInterface) table_list.elementAt(i); 213 int rcc = table.resolveColumnCount(null, sch_name, tab_name, col_name); 214 if (rcc == 1) { 215 Variable matched = 216 table.resolveColumn(null, sch_name, tab_name, col_name); 217 list.add(matched); 218 } 219 else if (rcc > 1) { 220 throw new StatementException("Ambiguous column name (" + v + ")"); 221 } 222 } 223 224 int total_matches = list.size(); 225 if (total_matches == 0) { 226 throw new StatementException("Can't find column: " + v); 227 } 228 else if (total_matches == 1) { 229 return (Variable) list.get(0); 230 } 231 else if (total_matches > 1) { 232 throw new StatementException("Ambiguous column name (" + v + ")"); 235 } 236 else { 237 throw new Error ("Negative total matches?"); 240 } 241 242 } 243 244 248 public Variable resolveVariableName(Variable v) { 249 return resolveColumn(v); 250 } 251 252 256 void resolveExpression(Expression exp) { 257 List vars = exp.allVariables(); 259 for (int i = 0; i < vars.size(); ++i) { 260 Variable v = (Variable) vars.get(i); 261 Variable to_set = resolveVariableName(v); 262 v.set(to_set); 263 } 264 } 265 266 270 protected void addTable(FromTableInterface table) { 271 table_list.addElement(table); 272 } 273 274 285 public final void init(DatabaseConnection db, StatementTree stree, 286 SQLQuery query) { 287 this.database = db; 288 this.user = db.getUser(); 289 this.cmd = stree; 290 this.query = query; 291 } 292 293 313 public abstract void prepare() throws DatabaseException; 314 315 319 public abstract Table evaluate() 320 throws DatabaseException, TransactionException; 321 322 } 323 | Popular Tags |