/** * Check if a tuple attribute reference is valid. Disambiguate if needed. Also set the * RelationSchema for the TupleAttribute. * */ boolean analyzeTupleAttribute(TupleAttribute ta) { if (ta.tableName == null) { BaseRelationSchema found_in = null; for (BaseRelationSchema rs : query_relations) { // System.out.println("searching for " + ta.attributeName + " in " + rs); if (rs.hasAttribute(ta.attributeName)) { if (found_in == null) { found_in = rs; } else { System.out.println("=========> Attribute " + ta.attributeName + " ambiguous"); return false; } } } if (found_in != null) { ta.tableName = found_in.getName(); ta.setRelationSchema(found_in); return true; } else { System.out.println( "=========> Attribute " + ta.attributeName + " not found in any of the tables in the FROM clause"); return false; } } else { if (!checkRelationContainedInFromClause(ta.tableName)) { System.out.println("=========> Relation " + ta.tableName + " not in the From Clause"); return false; } ta.setRelationSchema(Globals.getRelationSchema(ta.tableName)); if (!ta.rs.hasAttribute(ta.attributeName)) { System.out.println( "=========> Attribute " + ta.attributeName + " not present in the relation " + ta.tableName); return false; } return true; } }
/* Construct the query object. Analyze, check for errors etc. */ boolean analyze() { /* Get the relation schemas, and make sure the relations exist. */ query_relations = new Vector<BaseRelationSchema>(); for (String name : query_relation_names) { BaseRelationSchema rs; if ((rs = Globals.getRelationSchema(name)) == null) { System.out.println("=========> Relation " + name + " does not exist"); return false; } query_relations.add(rs); } /* Now check all the predicates. */ for (Predicate p : query_predicates) { /* Check the validity of the attributes. */ if ((p.lhs() instanceof TupleAttribute) && (!analyzeTupleAttribute((TupleAttribute) p.lhs()))) return false; if ((p.rhs() instanceof TupleAttribute) && (!analyzeTupleAttribute((TupleAttribute) p.rhs()))) return false; } /* Finally check the select list. */ if (select_attributes.size() != 0) { for (TupleAttribute ta : select_attributes) { /* Check the validity of the attribute. */ if (!analyzeTupleAttribute(ta)) return false; } } else { for (BaseRelationSchema rs : query_relations) { for (String attrName : rs.attributeNames) { TupleAttribute ta = new TupleAttribute(rs.getName(), attrName); ta.setRelationSchema(rs); select_attributes.add(ta); } } } return true; }