From: <one...@us...> - 2002-11-20 07:06:47
|
Update of /cvsroot/hibernate/Hibernate/cirrus/hibernate/query In directory sc8-pr-cvs1:/tmp/cvs-serv2204/hibernate/query Modified Files: ParserHelper.java PreprocessingParser.java QueryTranslator.java WhereParser.java Log Message: query expressions like not (foo.bar.baz=1) now translated to (bar.baz!=1 and foo.bar=bar.id) support for postgres ~ operator Index: ParserHelper.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/query/ParserHelper.java,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** ParserHelper.java 19 Nov 2002 15:28:07 -0000 1.10 --- ParserHelper.java 20 Nov 2002 07:06:44 -0000 1.11 *************** *** 10,14 **** public static final String HQL_VARIABLE_PREFIX = ":"; ! public static final String HQL_SEPERATORS = " \n\r\f\t,()=<>&|+-=/*'^![]#"; //NOTICE: no " or . since they are part of (compound) identifiers public static final String PATH_SEPERATORS = "."; --- 10,14 ---- public static final String HQL_VARIABLE_PREFIX = ":"; ! public static final String HQL_SEPERATORS = " \n\r\f\t,()=<>&|+-=/*'^![]#~"; //NOTICE: no " or . since they are part of (compound) identifiers public static final String PATH_SEPERATORS = "."; Index: PreprocessingParser.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/query/PreprocessingParser.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PreprocessingParser.java 19 Nov 2002 15:28:07 -0000 1.1 --- PreprocessingParser.java 20 Nov 2002 07:06:44 -0000 1.2 *************** *** 23,26 **** --- 23,27 ---- operators.add("<>"); operators.add("!#"); + operators.add("!~"); operators.add("!<"); operators.add("!>"); Index: QueryTranslator.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/query/QueryTranslator.java,v retrieving revision 1.44 retrieving revision 1.45 diff -C2 -d -r1.44 -r1.45 *** QueryTranslator.java 19 Nov 2002 15:28:07 -0000 1.44 --- QueryTranslator.java 20 Nov 2002 07:06:44 -0000 1.45 *************** *** 47,56 **** --- 47,71 ---- //dontSpace.add("'"); dontSpace.add("."); + dontSpace.add("+"); + dontSpace.add("-"); + dontSpace.add("/"); + dontSpace.add("*"); dontSpace.add("<"); dontSpace.add(">"); dontSpace.add("="); dontSpace.add("#"); + dontSpace.add("~"); dontSpace.add("|"); dontSpace.add("&"); + dontSpace.add("<="); + dontSpace.add(">="); + dontSpace.add("=>"); + dontSpace.add("=<"); + dontSpace.add("!="); + dontSpace.add("<>"); + dontSpace.add("!#"); + dontSpace.add("!~"); + dontSpace.add("!<"); + dontSpace.add("!>"); dontSpace.add("("); //for MySQL dontSpace.add(")"); Index: WhereParser.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate/cirrus/hibernate/query/WhereParser.java,v retrieving revision 1.38 retrieving revision 1.39 diff -C2 -d -r1.38 -r1.39 *** WhereParser.java 19 Nov 2002 15:28:07 -0000 1.38 --- WhereParser.java 20 Nov 2002 07:06:44 -0000 1.39 *************** *** 2,7 **** --- 2,9 ---- package cirrus.hibernate.query; + import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; + import java.util.Map; import java.util.Set; import java.util.StringTokenizer; *************** *** 39,42 **** --- 41,45 ---- private static final Set expressionOpeners = new HashSet(); //tokens that open a sub expression private static final Set booleanOperators = new HashSet(); //tokens that would indicate a sub expression is a boolean expression + private static final Map negations = new HashMap(); static { *************** *** 55,58 **** --- 58,62 ---- booleanOperators.add(">"); booleanOperators.add("#"); + booleanOperators.add("~"); booleanOperators.add("like"); booleanOperators.add("is"); *************** *** 70,73 **** --- 74,78 ---- booleanOperators.add("<>"); booleanOperators.add("!#"); + booleanOperators.add("!~"); booleanOperators.add("!<"); booleanOperators.add("!>"); *************** *** 78,81 **** --- 83,114 ---- booleanOperators.add("not exists"); + negations.put("and", "or"); + negations.put("or", "and"); + negations.put("<", ">="); + negations.put("=", "<>"); + negations.put(">", "<="); + negations.put("#", "!#"); + negations.put("~", "!~"); + negations.put("like", "not like"); + negations.put("is", "is not"); + negations.put("in", "not in"); + negations.put("exists", "not exists"); + negations.put("between", "not between"); + negations.put("<=", ">"); + negations.put(">=", "<"); + negations.put("=>", "<"); + negations.put("=<", ">"); + negations.put("!=", "="); + negations.put("<>", "="); + negations.put("!#", "#"); + negations.put("!~", "~"); + negations.put("!<", ">="); + negations.put("!>", "<="); + negations.put("is not", "is"); + negations.put("not like", "like"); + negations.put("not in", "in"); + negations.put("not between", "between"); + negations.put("not exists", "exists"); + } // Handles things like: *************** *** 100,104 **** private boolean quoted = false; //Inside a quoted string private boolean betweenSpecialCase = false; //Inside a BETWEEN ... AND ... expression ! private int bracketsSinceFunction = 0; //How deep inside in IN are we? private boolean inSubselect = false; --- 133,138 ---- private boolean quoted = false; //Inside a quoted string private boolean betweenSpecialCase = false; //Inside a BETWEEN ... AND ... expression ! private int bracketsSinceFunction = 0; //How deep inside in IN are we? ! private boolean negated = false; private boolean inSubselect = false; *************** *** 112,119 **** // in the list of nested subexpressions we are currently processing. ! private LinkedList unaryCounts = new LinkedList(); //how many NOTs were encountered private LinkedList joins = new LinkedList(); //the join string built up by compound paths inside this expression private LinkedList booleanTests = new LinkedList(); //a flag indicating if the subexpression is known to be boolean public void token(String token, QueryTranslator q) throws QueryException { String lcToken = token.toLowerCase(); --- 146,155 ---- // in the list of nested subexpressions we are currently processing. ! private LinkedList nots = new LinkedList(); //were an odd or even number of NOTs encountered private LinkedList joins = new LinkedList(); //the join string built up by compound paths inside this expression private LinkedList booleanTests = new LinkedList(); //a flag indicating if the subexpression is known to be boolean + public void token(String token, QueryTranslator q) throws QueryException { + String lcToken = token.toLowerCase(); *************** *** 222,225 **** --- 258,269 ---- } + if ( lcToken.equals("not") ) { + nots.addLast( + new Boolean( !( (Boolean) nots.removeLast() ).booleanValue() ) + ); + negated = !negated; + return; //NOTE: early return + } + //process a token, mapping OO path expressions to SQL expressions *************** *** 231,237 **** openExpression(q, lcToken); } ! else if ( lcToken.equals("not") ) { startNot(q); ! } //Cope with special cases of AND, NOT, ) --- 275,281 ---- openExpression(q, lcToken); } ! /*else if ( lcToken.equals("not") ) { startNot(q); ! }*/ //Cope with special cases of AND, NOT, ) *************** *** 269,288 **** // finish off any unary operations ! int count = ( (Integer) unaryCounts.removeLast() ).intValue(); for ( int i=0; i<count; i++ ) { //to allow not not, not not not, etc... appendToken(q, ")"); ! } } else { ! unaryCounts.removeLast(); //check that its zero? (As an assertion) StringBuffer join = (StringBuffer) joins.removeLast(); ( (StringBuffer) joins.getLast() ).append( join.toString() ); } if ( !")".equals(lcToken) ) appendToken(q, ")"); } ! private void openExpression(QueryTranslator q, String lcToken) { unaryCounts.addLast( new Integer(0) ); booleanTests.addLast(Boolean.FALSE); joins.addLast( new StringBuffer() ); --- 313,335 ---- // finish off any unary operations ! /*int count = ( (Integer) unaryCounts.removeLast() ).intValue(); for ( int i=0; i<count; i++ ) { //to allow not not, not not not, etc... appendToken(q, ")"); ! }*/ } else { ! //unaryCounts.removeLast(); //check that its zero? (As an assertion) StringBuffer join = (StringBuffer) joins.removeLast(); ( (StringBuffer) joins.getLast() ).append( join.toString() ); } + if ( ( (Boolean) nots.removeLast() ).booleanValue() ) negated = !negated; + if ( !")".equals(lcToken) ) appendToken(q, ")"); } ! private void openExpression(QueryTranslator q, String lcToken) { //unaryCounts.addLast( new Integer(0) ); ! nots.addLast(Boolean.FALSE); booleanTests.addLast(Boolean.FALSE); joins.addLast( new StringBuffer() ); *************** *** 290,299 **** } ! private void startNot(QueryTranslator q) { // increment the count Integer count = new Integer( ( (Integer) unaryCounts.removeLast() ).intValue() + 1 ); unaryCounts.addLast(count); appendToken(q, "("); ! } private void doToken(String token, QueryTranslator q) throws QueryException { --- 337,346 ---- } ! /*private void startNot(QueryTranslator q) { // increment the count Integer count = new Integer( ( (Integer) unaryCounts.removeLast() ).intValue() + 1 ); unaryCounts.addLast(count); appendToken(q, "("); ! }*/ private void doToken(String token, QueryTranslator q) throws QueryException { *************** *** 352,356 **** } else { //anything else ! appendToken(q, token); } } --- 399,410 ---- } else { //anything else ! ! String negatedToken = negated ? (String) negations.get( token.toLowerCase() ) : null; ! if ( negatedToken!=null && ( !betweenSpecialCase || !"or".equals(negatedToken) ) ) { ! appendToken(q, negatedToken); ! } ! else { ! appendToken(q, token); ! } } } |