1 30 31 32 package org.hsqldb; 33 34 import org.hsqldb.HsqlNameManager.HsqlName; 35 import org.hsqldb.lib.HsqlArrayList; 36 import org.hsqldb.lib.Iterator; 37 38 41 49 class View extends Table { 50 51 Select viewSelect; 52 SubQuery viewSubQuery; 53 private String statement; 54 private HsqlName[] colList; 55 56 57 HsqlName compileTimeSchema; 58 59 63 SubQuery[] viewSubqueries; 64 65 74 View(Session session, Database db, HsqlName name, String definition, 75 HsqlName[] columns) throws HsqlException { 76 77 super(db, name, VIEW); 78 79 isReadOnly = true; 80 colList = columns; 81 statement = trimStatement(definition); 82 compileTimeSchema = session.getSchemaHsqlName(null); 83 84 compile(session); 85 86 HsqlName[] schemas = getSchemas(); 87 88 for (int i = 0; i < schemas.length; i++) { 89 if (db.schemaManager.isSystemSchema(schemas[i])) { 90 continue; 91 } 92 93 if (!schemas[i].equals(name.schema)) { 94 throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS); 95 } 96 } 97 } 98 99 103 static String trimStatement(String s) throws HsqlException { 104 105 int position; 106 String str; 107 Tokenizer tokenizer = new Tokenizer(s); 108 109 do { 113 position = tokenizer.getPosition(); 114 str = tokenizer.getString(); 115 } while (str.length() != 0 || tokenizer.wasValue()); 116 117 return s.substring(0, position).trim(); 118 } 119 120 123 void compile(Session session) throws HsqlException { 124 125 Parser p = new Parser(session, this.database, 127 new Tokenizer(statement)); 128 int brackets = p.parseOpenBracketsSelect(); 129 130 viewSubQuery = p.parseSubquery(brackets, colList, true, 131 Expression.VIEW); 132 133 p.setAsView(this); 134 135 viewSubqueries = p.getSortedSubqueries(); 136 viewSelect = viewSubQuery.select; 137 138 viewSelect.prepareResult(session); 139 140 Result.ResultMetaData metadata = viewSelect.resultMetaData; 141 int columns = viewSelect.iResultLen; 142 143 if (super.columnCount == 0) { 144 145 super.addColumns(metadata, columns); 147 } 148 } 149 150 153 String getStatement() { 154 return statement; 155 } 156 157 160 void setDataReadOnly(boolean value) throws HsqlException { 161 throw Trace.error(Trace.NOT_A_TABLE); 162 } 163 164 167 HsqlName[] getSchemas() { 168 169 HsqlArrayList list = new HsqlArrayList(); 170 171 for (int i = 0; i < viewSubqueries.length; i++) { 172 Select select = viewSubqueries[i].select; 173 174 for (; select != null; select = select.unionSelect) { 175 TableFilter[] tfilter = select.tFilter; 176 177 for (int j = 0; j < tfilter.length; j++) { 178 list.add(tfilter[j].filterTable.tableName.schema); 179 } 180 } 181 } 182 183 return (HsqlName[]) list.toArray(new HsqlName[list.size()]); 184 } 185 186 boolean hasView(View view) { 187 188 if (view == this) { 189 return false; 190 } 191 192 for (int i = 0; i < viewSubqueries.length; i++) { 193 if (viewSubqueries[i].view == view) { 194 return true; 195 } 196 } 197 198 return false; 199 } 200 201 204 boolean hasTable(Table table) { 205 206 for (int i = 0; i < viewSubqueries.length; i++) { 207 Select select = viewSubqueries[i].select; 208 209 for (; select != null; select = select.unionSelect) { 210 TableFilter[] tfilter = select.tFilter; 211 212 for (int j = 0; j < tfilter.length; j++) { 213 if (table.equals(tfilter[j].filterTable.tableName)) { 214 return true; 215 } 216 } 217 } 218 } 219 220 return false; 221 } 222 223 227 boolean hasColumn(Table table, String colname) { 228 229 if (hasTable(table)) { 230 Expression.Collector coll = new Expression.Collector(); 231 232 coll.addAll(viewSubqueries[viewSubqueries.length - 1].select, 233 Expression.COLUMN); 234 235 Iterator it = coll.iterator(); 236 237 for (; it.hasNext(); ) { 238 Expression e = (Expression) it.next(); 239 240 if (colname.equals(e.getBaseColumnName()) 241 && table.equals(e.getTableHsqlName())) { 242 return true; 243 } 244 } 245 } 246 247 return false; 248 } 249 250 254 boolean hasSequence(NumberSequence sequence) { 255 256 Expression.Collector coll = new Expression.Collector(); 257 258 coll.addAll(viewSubqueries[viewSubqueries.length - 1].select, 259 Expression.SEQUENCE); 260 261 Iterator it = coll.iterator(); 262 263 for (; it.hasNext(); ) { 264 Expression e = (Expression) it.next(); 265 266 if (e.valueData == sequence) { 267 return true; 268 } 269 } 270 271 return false; 272 } 273 } 274 | Popular Tags |