1 24 25 package com.mckoi.database.interpret; 26 27 import com.mckoi.database.*; 28 29 35 36 public class FromTableSubQuerySource implements FromTableInterface { 37 38 41 private TableSelectExpression table_expression; 42 43 47 private TableExpressionFromSet from_set; 48 49 53 private TableName end_table_name; 54 55 59 private String unique_key; 60 61 64 private Variable[] vars; 65 66 69 private boolean case_insensitive = false; 70 71 74 public FromTableSubQuerySource(DatabaseConnection connection, 75 String unique_key, 76 TableSelectExpression table_expression, 77 TableExpressionFromSet from_set, 78 TableName aliased_table_name) { 79 this.unique_key = unique_key; 80 this.table_expression = table_expression; 81 this.from_set = from_set; 82 this.end_table_name = aliased_table_name; 83 this.case_insensitive = connection.isInCaseInsensitiveMode(); 85 } 86 87 90 TableSelectExpression getTableExpression() { 91 return table_expression; 92 } 93 94 97 TableExpressionFromSet getFromSet() { 98 return from_set; 99 } 100 101 105 TableName getAliasedName() { 106 return end_table_name; 107 } 108 109 110 113 private void ensureVarList() { 114 if (vars == null) { 115 vars = from_set.generateResolvedVariableList(); 116 if (end_table_name != null) { 122 for (int i = 0; i < vars.length; ++i) { 123 vars[i].setTableName(end_table_name); 124 } 125 } 126 } 127 } 128 129 132 public String getUniqueKey() { 133 return unique_key; 134 } 135 136 139 public void setCaseInsensitive(boolean status) { 140 case_insensitive = status; 141 } 142 143 private boolean stringCompare(String str1, String str2) { 144 if (!case_insensitive) { 145 return str1.equals(str2); 146 } 147 return str1.equalsIgnoreCase(str2); 148 } 149 150 154 private boolean matchesVar(Variable v, String catalog, String schema, 155 String table, String column) { 156 TableName tn = v.getTableName(); 157 String cn = v.getName(); 158 159 if (column == null) { 160 return true; 161 } 162 if (!stringCompare(cn, column)) { 163 return false; 164 } 165 166 if (table == null) { 167 return true; 168 } 169 if (tn == null) { 170 return false; 171 } 172 String tname = tn.getName(); 173 if (tname != null && !stringCompare(tname, table)) { 174 return false; 175 } 176 177 if (schema == null) { 178 return true; 179 } 180 String sname = tn.getSchema(); 181 if (sname != null && !stringCompare(sname, schema)) { 182 return false; 183 } 184 185 return true; 187 188 } 189 190 192 public String getUniqueName() { 193 return getUniqueKey(); 194 } 195 196 public boolean matchesReference(String catalog, 197 String schema, String table) { 198 if (schema == null && table == null) { 199 return true; 200 } 201 if (end_table_name != null) { 202 String ts = end_table_name.getSchema(); 203 String tt = end_table_name.getName(); 204 if (schema == null) { 205 if (stringCompare(tt, table)) { 206 return true; 207 } 208 } 209 else { 210 if (stringCompare(tt, table) && stringCompare(ts, schema)) { 211 return true; 212 } 213 } 214 } 215 return false; 217 } 218 219 public int resolveColumnCount(String catalog, String schema, 220 String table, String column) { 221 ensureVarList(); 222 223 if (catalog == null && schema == null && table == null && column == null) { 224 return vars.length; 226 } 227 228 int matched_count = 0; 229 for (int i = 0; i < vars.length; ++i) { 230 Variable v = vars[i]; 231 if (matchesVar(v, catalog, schema, table, column)) { 232 ++matched_count; 233 } 234 } 235 236 return matched_count; 237 238 } 239 240 public Variable resolveColumn(String catalog, String schema, 241 String table, String column) { 242 ensureVarList(); 243 244 247 for (int i = 0; i < vars.length; ++i) { 248 Variable v = vars[i]; 249 if (matchesVar(v, catalog, schema, table, column)) { 250 return v; 252 } 253 } 254 255 throw new Error ("Couldn't resolve to a column."); 256 } 257 258 public Variable[] allColumns() { 259 ensureVarList(); 260 return vars; 261 } 262 263 } 264 | Popular Tags |