1 24 25 package com.mckoi.database.interpret; 26 27 import com.mckoi.database.*; 28 import java.util.List ; 29 import java.util.Collections ; 30 31 38 39 public class FromTableDirectSource implements FromTableInterface { 40 41 44 private TableQueryDef table_query; 45 46 49 private DataTableDef data_table_def; 50 51 54 private String unique_name; 55 56 59 private TableName table_name; 60 61 65 private TableName root_name; 66 67 70 private boolean case_insensitive = false; 71 72 75 public FromTableDirectSource(DatabaseConnection connection, 76 TableQueryDef table_query, String unique_name, 77 TableName given_name, TableName root_name) { 78 this.unique_name = unique_name; 79 this.data_table_def = table_query.getDataTableDef(); 80 this.root_name = root_name; 81 if (given_name != null) { 82 this.table_name = given_name; 83 } 84 else { 85 this.table_name = root_name; 86 } 87 this.case_insensitive = connection.isInCaseInsensitiveMode(); 89 this.table_query = table_query; 90 } 91 92 97 public TableName getGivenTableName() { 98 return table_name; 99 } 100 101 105 public TableName getRootTableName() { 106 return root_name; 107 } 108 109 113 public QueryPlanNode createFetchQueryPlanNode() { 114 return table_query.getQueryPlanNode(); 115 } 116 117 120 public void setCaseInsensitive(boolean status) { 121 case_insensitive = status; 122 } 123 124 private boolean stringCompare(String str1, String str2) { 125 if (!case_insensitive) { 126 return str1.equals(str2); 127 } 128 return str1.equalsIgnoreCase(str2); 129 } 130 131 132 134 public String getUniqueName() { 135 return unique_name; 136 } 137 138 public boolean matchesReference(String catalog, 139 String schema, String table) { 140 143 if (schema != null && 145 !stringCompare(schema, table_name.getSchema())) { 146 return false; 148 } 149 if (table != null && 150 !stringCompare(table, table_name.getName())) { 151 return false; 154 } 155 return true; 158 } 159 160 public int resolveColumnCount(String catalog, String schema, 161 String table, String column) { 162 165 167 if (schema != null && 169 !stringCompare(schema, table_name.getSchema())) { 170 return 0; 172 } 173 if (table != null && 174 !stringCompare(table, table_name.getName())) { 175 return 0; 178 } 179 180 if (column != null) { 181 if (!case_insensitive) { 182 int i = data_table_def.fastFindColumnName(column); 184 return i == -1 ? 0 : 1; 186 } 187 else { 188 int resolve_count = 0; 190 int col_count = data_table_def.columnCount(); 191 for (int i = 0; i < col_count; ++i) { 192 if (data_table_def.columnAt(i).getName().equalsIgnoreCase(column)) { 193 ++resolve_count; 194 } 195 } 196 return resolve_count; 197 } 198 } 199 else { return data_table_def.columnCount(); 202 } 203 } 204 205 public Variable resolveColumn(String catalog, String schema, 206 String table, String column) { 207 208 if (schema != null && 210 !stringCompare(schema, table_name.getSchema())) { 211 throw new Error ("Incorrect schema."); 213 } 214 if (table != null && 215 !stringCompare(table, table_name.getName())) { 216 throw new Error ("Incorrect table."); 218 } 219 220 if (column != null) { 221 if (!case_insensitive) { 222 int i = data_table_def.fastFindColumnName(column); 224 if (i == -1) { 225 throw new Error ("Could not resolve '" + column + "'"); 226 } 227 return new Variable(table_name, column); 228 } 229 else { 230 int col_count = data_table_def.columnCount(); 232 for (int i = 0; i < col_count; ++i) { 233 String col_name = data_table_def.columnAt(i).getName(); 234 if (col_name.equalsIgnoreCase(column)) { 235 return new Variable(table_name, col_name); 236 } 237 } 238 throw new Error ("Could not resolve '" + column + "'"); 239 } 240 } 241 else { return new Variable(table_name, data_table_def.columnAt(0).getName()); 244 } 245 246 } 247 248 public Variable[] allColumns() { 249 int col_count = data_table_def.columnCount(); 250 Variable[] vars = new Variable[col_count]; 251 for (int i = 0; i < col_count; ++i) { 252 vars[i] = new Variable(table_name, data_table_def.columnAt(i).getName()); 253 } 254 return vars; 255 } 256 257 } 258 | Popular Tags |