1 24 25 package com.mckoi.database.interpret; 26 27 import com.mckoi.database.*; 28 import java.util.Set ; 29 import java.util.HashMap ; 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 33 39 40 public final class FromClause 41 implements java.io.Serializable , StatementTreeObject, Cloneable { 42 43 static final long serialVersionUID = 565726601314503609L; 44 45 49 private JoiningSet join_set = new JoiningSet(); 50 51 55 private ArrayList def_list = new ArrayList (); 56 57 60 private ArrayList all_table_names = new ArrayList (); 61 62 65 private int table_key = 0; 66 67 68 71 private String createNewKey() { 72 ++table_key; 73 return Integer.toString(table_key); 74 } 75 76 77 private void addTableDef(String table_name, FromTableDef def) { 78 if (table_name != null) { 79 if (all_table_names.contains(table_name)) { 80 throw new Error ("Duplicate table name in FROM clause: " + table_name); 81 } 82 all_table_names.add(table_name); 83 } 84 String key = createNewKey(); 86 def.setUniqueKey(key); 87 join_set.addTable(new TableName(key)); 89 def_list.add(def); 91 } 92 93 97 public void addTable(String table_name) { 98 addTableDef(table_name, new FromTableDef(table_name)); 99 } 100 101 104 public void addTable(String table_name, String table_alias) { 105 addTableDef(table_alias, new FromTableDef(table_name, table_alias)); 106 } 107 108 112 public void addTableDeclaration(String table_name, 113 TableSelectExpression select, 114 String table_alias) { 115 if (table_name == null && select != null) { 117 if (table_alias == null) { 118 addTableDef(null, new FromTableDef(select)); 119 } 120 else { 121 addTableDef(table_alias, new FromTableDef(select, table_alias)); 122 } 123 } 124 else if (table_name != null && select == null) { 126 if (table_alias == null) { 127 addTable(table_name); 128 } 129 else { 130 addTable(table_name, table_alias); 131 } 132 } 133 else { 135 throw new Error ("Unvalid declaration parameters."); 136 } 137 138 } 139 140 144 public void addJoin(int type) { 145 join_set.addJoin(type); 147 } 148 149 153 public void addPreviousJoin(int type, Expression on_expression) { 154 join_set.addPreviousJoin(type, on_expression); 155 } 156 157 161 public void addJoin(int type, Expression on_expression) { 162 join_set.addJoin(type, on_expression); 163 } 164 165 168 public JoiningSet getJoinSet() { 169 return join_set; 170 } 171 172 177 public int getJoinType(int n) { 178 return getJoinSet().getJoinType(n); 179 } 180 181 185 public Expression getOnExpression(int n) { 186 return getJoinSet().getOnExpression(n); 187 } 188 189 193 public Collection allTables() { 194 return def_list; 195 } 196 197 public void prepareExpressions(ExpressionPreparer preparer) 199 throws DatabaseException { 200 int size = join_set.getTableCount() - 1; 202 for (int i = 0; i < size; ++i) { 203 Expression exp = join_set.getOnExpression(i); 204 if (exp != null) { 205 exp.prepare(preparer); 206 } 207 } 208 for (int i = 0; i < def_list.size(); ++i) { 210 FromTableDef table_def = (FromTableDef) def_list.get(i); 211 table_def.prepareExpressions(preparer); 212 } 213 214 } 215 216 public Object clone() throws CloneNotSupportedException { 217 FromClause v = (FromClause) super.clone(); 218 v.join_set = (JoiningSet) join_set.clone(); 219 ArrayList cloned_def_list = new ArrayList (def_list.size()); 220 v.def_list = cloned_def_list; 221 v.all_table_names = (ArrayList ) all_table_names.clone(); 222 223 for (int i = 0; i < def_list.size(); ++i) { 224 FromTableDef table_def = (FromTableDef) def_list.get(i); 225 cloned_def_list.add(table_def.clone()); 226 } 227 228 return v; 229 } 230 231 } 232 | Popular Tags |