From: <one...@us...> - 2003-03-01 02:45:41
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/hql In directory sc8-pr-cvs1:/tmp/cvs-serv19202/sf/hibernate/hql Modified Files: FromParser.java QueryTranslator.java Log Message: finished implementing new FROM clause style Index: FromParser.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/hql/FromParser.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** FromParser.java 28 Feb 2003 07:01:28 -0000 1.7 --- FromParser.java 1 Mar 2003 02:45:26 -0000 1.8 *************** *** 2,12 **** package net.sf.hibernate.hql; - import java.util.HashSet; - import java.util.Set; - import net.sf.hibernate.QueryException; import net.sf.hibernate.persister.Loadable; - import net.sf.hibernate.sql.JoinFragment; - import net.sf.hibernate.sql.QueryJoinFragment; import net.sf.hibernate.util.StringHelper; --- 2,7 ---- *************** *** 18,96 **** public class FromParser implements Parser { ! //private boolean expectingIn; private boolean expectingJoin; private boolean expectingAs; ! private String entityName; ! private boolean expectingJoinToken; ! private boolean fromClass; ! private String joinType; ! private String name; ! private PathExpressionParser peParser = new FromPathExpressionParser(); ! ! private static final Set joinTokens = new HashSet(); ! static { ! joinTokens.add("left"); ! joinTokens.add("right"); ! joinTokens.add("full"); ! } public void token(String token, QueryTranslator q) throws QueryException { String lcToken = token.toLowerCase(); ! if ( lcToken.equals("class") ) { ! fromClass = true; } ! else //if (expectingIn) { ! if ( lcToken.equals("in") ) { ! //expectingIn = false; ! } ! /*else { ! throw new QueryException("IN expected but found: " + token); ! }*/ ! //} ! else if (lcToken.equals("as") ) { ! if (!expectingAs) throw new QueryException("AS not expected here"); ! expectingAs=false; } ! else //if (expectingJoin) { ! if ( token.equals(StringHelper.COMMA) ) { ! expectingJoin = false; ! fromClass = false; ! joinType = StringHelper.COMMA; ! } ! else if ( lcToken.equals("join") ) { ! joinType += ' ' + lcToken; ! expectingJoin = false; ! fromClass = false; ! expectingJoinToken=false; } ! else if ( joinTokens.contains(lcToken) ) { ! joinType = ' ' + lcToken; ! expectingJoinToken = true; } ! /*else { ! throw new QueryException(", or WHERE expected but found: " + token); ! }*/ ! //} ! else //{ ! if ( name==null && token.indexOf(StringHelper.DOT)<0 ) { ! //expectingIn=true; ! if (entityName==null) { ! name = token; } else { ! q.setAliasName(token, entityName); } } else { ! if (fromClass) { ! Loadable p = q.getPersisterUsingImports(token); ! JoinFragment ojf = new QueryJoinFragment(); ! ojf.addCrossJoin( p.getTableName(), name ); ! q.addFromType( name, p.getMappedClass(), ojf ); ! name = null; ! expectingJoin=true; } else { ParserHelper.parse(peParser, q.unalias(token), ParserHelper.PATH_SEPARATORS, q); if ( peParser.isCollectionValued() ) { --- 13,128 ---- public class FromParser implements Parser { ! private PathExpressionParser peParser = new FromPathExpressionParser(); ! private String entityName; ! private String alias; ! private boolean afterIn; ! private boolean afterAs; ! private boolean afterClass; private boolean expectingJoin; + private boolean expectingIn; private boolean expectingAs; ! private Loadable classPersister; public void token(String token, QueryTranslator q) throws QueryException { + // start by looking for HQL keywords... String lcToken = token.toLowerCase(); ! if ( lcToken.equals(StringHelper.COMMA) ) { ! if (!expectingJoin) throw new QueryException("unexpected join: " + token); ! expectingJoin = false; } ! else if ( lcToken.equals("class") ) { ! if (!afterIn) throw new QueryException("unexpected token: class"); ! afterClass=true; } ! else if ( lcToken.equals("in") ) { ! if (!expectingIn) throw new QueryException("unexpected token: in"); ! afterIn = true; ! expectingIn = false; ! } ! else if ( lcToken.equals("as") ) { ! if (!expectingAs) throw new QueryException("unexpected token: as"); ! afterAs = true; ! expectingAs = false; ! } ! else { ! ! if (expectingIn || expectingJoin) { // AS is optional ! throw new QueryException("in or join expected: " + token); } ! ! // now anything that is not a HQL keyword ! ! if ( afterAs || expectingAs ) { ! ! // (AS is always optional, for consistency with SQL/OQL) ! ! // process the "new" HQL style where aliases are assigned ! // _after_ the class name or path expression ie. using ! // the AS construction ! ! if (classPersister!=null) { ! q.addCrossJoinedFromType(token, classPersister); ! } ! else if (entityName!=null) { ! q.setAliasName(token, entityName); ! } ! else { ! throw new QueryException("unexpected: as " + token); ! } ! afterAs = false; ! expectingJoin = true; ! expectingAs = false; ! entityName = null; ! classPersister = null; ! } ! else if (afterIn) { ! ! // process the "old" HQL style where aliases appear _first_ ! // ie. using the IN or IN CLASS constructions ! ! if (alias==null) throw new QueryException("alias not specified for: " + token); ! ! if (afterClass) { ! // treat it as a classname ! Loadable p = q.getPersisterUsingImports(token); ! if (p==null) throw new QueryException("persister not found: " + token); ! q.addCrossJoinedFromType(alias, p); } else { ! // treat it as a path expression ! ParserHelper.parse(peParser, q.unalias(token), ParserHelper.PATH_SEPARATORS, q); ! if ( !peParser.isCollectionValued() ) throw new QueryException("path expression did not resolve to collection: " + token); ! String nm = peParser.addFromCollection(q); ! q.setAliasName(alias, nm); } + + alias = null; + afterIn = false; + afterClass = false; + expectingJoin = true; } else { ! ! // handle a path expression or class name that ! // appears at the start, in the "new" HQL ! // style or an alias that appears at the start ! // in the "old" HQL style ! ! Loadable p = q.getPersisterUsingImports(token); ! if (p!=null) { ! // starts with the name of a mapped class (new style) ! classPersister = p; ! expectingAs = true; ! } ! else if ( token.indexOf('.') < 0 ) { ! // starts with an alias (old style) ! // semi-bad thing about this: can't re-alias another alias..... ! alias = token; ! expectingIn = true; } else { + // starts with a path expression (new style) ParserHelper.parse(peParser, q.unalias(token), ParserHelper.PATH_SEPARATORS, q); if ( peParser.isCollectionValued() ) { *************** *** 98,126 **** } else { ! entityName = peParser.addFromAssociation(q); ! ! } ! if (name!=null) { ! q.setAliasName(name, entityName); ! name = null; ! expectingJoin=true; ! entityName = null; ! } ! else { ! expectingAs = true; } ! ! //} } } } public void start(QueryTranslator q) { ! //expectingIn = false; expectingJoin = false; ! expectingJoinToken = false; ! joinType = null; ! name = null; ! fromClass = false; } --- 130,152 ---- } else { ! entityName = peParser.addFromAssociation(q); } ! expectingAs = true; ! } } } + } public void start(QueryTranslator q) { ! entityName = null; ! classPersister = null; ! alias = null; ! afterIn = false; ! afterAs = false; ! afterClass = false; expectingJoin = false; ! expectingIn = false; ! expectingAs = false; } Index: QueryTranslator.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/hql/QueryTranslator.java,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** QueryTranslator.java 28 Feb 2003 07:01:28 -0000 1.13 --- QueryTranslator.java 1 Mar 2003 02:45:26 -0000 1.14 *************** *** 323,326 **** --- 323,332 ---- } + void addCrossJoinedFromType(String name, Loadable classPersister) { + JoinFragment ojf = new QueryJoinFragment(); + ojf.addCrossJoin( classPersister.getTableName(), name ); + addFromType( name, classPersister.getMappedClass(), ojf ); + } + void addReturnType(String name) { returnTypes.add(name); |