1 24 25 package com.mckoi.database.interpret; 26 27 import com.mckoi.database.*; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 31 38 39 abstract class ColumnChecker { 40 41 49 static String stripTableName(String table_domain, String column) { 50 if (column.indexOf('.') != -1) { 51 String st = table_domain + "."; 52 if (!column.startsWith(st)) { 53 throw new StatementException("Column '" + column + 54 "' is not within the expected table domain '" + table_domain + "'"); 55 } 56 column = column.substring(st.length()); 57 } 58 return column; 59 } 60 61 64 static ArrayList stripColumnList(String table_domain, 65 ArrayList column_list) { 66 if (column_list != null) { 67 int size = column_list.size(); 68 for (int i = 0; i < size; ++i) { 69 String res = stripTableName(table_domain, (String ) column_list.get(i)); 70 column_list.set(i, res); 71 } 72 } 73 return column_list; 74 } 75 76 81 abstract String resolveColumnName(String col_name) throws DatabaseException; 82 83 88 void checkExpression(Expression expression) throws DatabaseException { 89 90 if (expression != null) { 91 List list = expression.allVariables(); 92 for (int i = 0; i < list.size(); ++i) { 93 Variable v = (Variable) list.get(i); 94 String orig_col = v.getName(); 95 String resolved_column = resolveColumnName(orig_col); 96 if (resolved_column == null) { 97 throw new DatabaseException("Column '" + orig_col + 98 "' not found in the table."); 99 } 100 if (!orig_col.equals(resolved_column)) { 102 v.setColumnName(resolved_column); 103 } 104 105 } 106 107 if (expression.hasSubQuery()) { 110 throw new DatabaseException("Sub-queries not permitted in " + 111 "the check constraint expression."); 112 } 113 } 114 115 } 116 117 122 void checkColumnList(ArrayList list) throws DatabaseException { 123 if (list != null) { 124 for (int i = 0; i < list.size(); ++i) { 125 String col = (String ) list.get(i); 126 String resolved_col = resolveColumnName(col); 127 if (resolved_col == null) { 128 throw new DatabaseException( 129 "Column '" + col + "' not found the table."); 130 } 131 list.set(i, resolved_col); 132 } 133 } 134 } 135 136 137 139 144 static ColumnChecker standardColumnChecker( 145 DatabaseConnection database, TableName tname) { 146 final DataTableDef table_def = database.getTable(tname).getDataTableDef(); 147 final boolean ignores_case = database.isInCaseInsensitiveMode(); 148 149 return new ColumnChecker() { 151 String resolveColumnName(String col_name) throws DatabaseException { 152 String found_col = null; 154 for (int n = 0; n < table_def.columnCount(); ++n) { 155 DataTableColumnDef col = 156 (DataTableColumnDef) table_def.columnAt(n); 157 if (!ignores_case) { 158 if (col.getName().equals(col_name)) { 159 return col_name; 160 } 161 } 162 else { 163 if (col.getName().equalsIgnoreCase(col_name)) { 164 if (found_col != null) { 165 throw new DatabaseException("Ambiguous column name '" + 166 col_name + "'"); 167 } 168 found_col = col.getName(); 169 } 170 } 171 } 172 return found_col; 173 } 174 }; 175 } 176 177 } 178 | Popular Tags |