Class Query

  • All Implemented Interfaces:
    SQLStringable

    public class Query
    extends java.lang.Object
    implements SQLStringable
    Represents an SQL query in parsed form.
    Author:
    Matthew Wakeling, Andrew Varley
    • Constructor Summary

      Constructors 
      Constructor Description
      Query()
      Construct a new Query.
      Query​(java.lang.String sql)
      Construct a new parsed Query from a String.
      Query​(java.lang.String sql, boolean treeParse)
      Construct a new parsed Query from a String.
      Query​(java.lang.String sql, boolean treeParse, java.lang.Long timeOut)
      Construct a new parsed Query from a String.
      Query​(java.lang.String sql, java.lang.Long timeOut)
      Construct a new parsed Query from a String.
      Query​(java.util.Map<java.lang.String,​AbstractTable> aliasToTable)
      Construct a new Query.
      Query​(java.util.Map<java.lang.String,​AbstractTable> aliasToTable, java.util.List<Query> queriesInUnion)
      Construct a new Query.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFrom​(AbstractTable obj)
      Adds a table to the from list of this query.
      void addGroupBy​(AbstractValue obj)
      Adds a field to the GROUP BY clause of this query.
      void addHaving​(AbstractConstraint obj)
      Adds a constraint to the HAVING clause of this query.
      void addOrderBy​(AbstractValue obj)
      Adds a field to the ORDER BY clause of this query.
      void addSelect​(SelectValue obj)
      Adds a field to the select list of this query.
      void addToUnion​(Query query)
      Adds another Query to the UNION set of this query.
      void addWhere​(AbstractConstraint obj)
      Adds a constraint to the where clause for this query.
      protected static java.lang.String collectionToSQLString​(java.util.Collection<? extends SQLStringable> c, java.lang.String comma)
      Converts a collection of objects that implement the getSQLString method into a String, with the given comma string between each element.
      protected static java.lang.String collectionToSQLString​(java.util.Collection<? extends SQLStringable> c, java.util.Collection<java.lang.String> extraValues, java.lang.String comma)
      Converts a collection of objects that implement the getSQLString method into a String, with the given comma string between each element.
      boolean equals​(java.lang.Object obj)
      Overrides Object.equals().
      boolean equalsNoUnion​(java.lang.Object obj)
      Returns true if this Query is equivalent to obj, disregarding other queries in the UNION.
      java.util.Set<AbstractTable> getFrom()
      Gets the Set of from tables for this query.
      java.util.Set<AbstractValue> getGroupBy()
      Gets the Set of fields in the GROUP BY clause of this query.
      java.util.Set<AbstractConstraint> getHaving()
      Gets the set of constraints forming the HAVING clause of this query.
      int getLimit()
      Gets the LIMIT number for this query.
      int getOffset()
      Gets the OFFSET number for this query.
      java.util.List<AbstractValue> getOrderBy()
      Gets the list of fields forming the ORDER BY clause of this query.
      java.util.List<SelectValue> getSelect()
      Gets the list of select fields for this query.
      java.lang.String getSQLString()
      Convert this Query into a SQL String query.
      java.lang.String getSQLStringForPrecomputedTable​(java.lang.String extraSelect)
      Convert this Query into a SQL String query, without regard to the other members of the the UNION, with an extra field in the SELECT list.
      java.lang.String getSQLStringNoUnion()
      Convert this Query into a SQL String query, without regard to the other members of the UNION.
      java.util.List<Query> getUnion()
      Returns the List of queries in the UNION set of this query.
      java.util.Set<AbstractConstraint> getWhere()
      Gets the Set of constraints in the where clause of this query.
      int hashCode()
      Overrides Object.hashCode().
      int hashCodeNoUnion()
      Returns a partial hashcode, ignoring other queries in the union.
      boolean isDistinct()
      Gets the current distinct status of this query.
      boolean isExplain()
      Gets the current explain status of this query.
      static void main​(java.lang.String[] args)
      A testing method - converts the argument into a Query object, and then converts it back to a String again.
      void processGroupClause​(antlr.collections.AST node)
      Processes an AST node that describes a GROUP clause.
      void processLimitClause​(antlr.collections.AST ast)
      Processes an AST node that describes a Limit clause.
      AbstractConstraint processNewAbstractConstraint​(antlr.collections.AST ast)
      Processes an AST node that describes an AbstractConstraint.
      AbstractValue processNewAbstractValue​(antlr.collections.AST ast)
      Processes a single AST node that describes an AbstractValue.
      AbstractValue processNewField​(antlr.collections.AST node)
      Processes an AST node that describes a Field.
      Function processNewSafeFunction​(antlr.collections.AST node)
      Processes an AST node that describes a safe function.
      void processNewSelect​(antlr.collections.AST node)
      Processes an AST node that describes a SelectValue.
      Function processNewTypecast​(antlr.collections.AST node)
      Processes an AST node that describes a typecast.
      Function processNewUnsafeFunction​(antlr.collections.AST ast)
      Processes an AST node that describes an unsafe function.
      void processOrderClause​(antlr.collections.AST node)
      Processes an AST node that describes a ORDER clause.
      void processSelectList​(antlr.collections.AST node)
      Processes an AST node that describes a SELECT list.
      void setDistinct​(boolean distinct)
      Sets the distinct status of this query.
      void setExplain​(boolean explain)
      Sets the explain status of this query.
      void setLimitOffset​(int limit, int offset)
      Sets the LIMIT and OFFSET numbers for this query.
      java.lang.String toString()
      Overrides Object.toString().
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • limit

        protected int limit
      • offset

        protected int offset
      • explain

        protected boolean explain
      • distinct

        protected boolean distinct
      • queriesInUnion

        protected java.util.List<Query> queriesInUnion
    • Constructor Detail

      • Query

        public Query()
        Construct a new Query.
      • Query

        public Query​(java.util.Map<java.lang.String,​AbstractTable> aliasToTable)
        Construct a new Query.
        Parameters:
        aliasToTable - a map of tables in a surrounding query, which are in the scope of this query.
      • Query

        public Query​(java.util.Map<java.lang.String,​AbstractTable> aliasToTable,
                     java.util.List<Query> queriesInUnion)
        Construct a new Query.
        Parameters:
        aliasToTable - a Map of tables in a surrounding query, which are in the scope of this query
        queriesInUnion - a List of Queries which are in the currently-being-created UNION of queries, to which this constructor should add this
      • Query

        public Query​(java.lang.String sql)
        Construct a new parsed Query from a String.
        Parameters:
        sql - a SQL SELECT String to parse
        Throws:
        java.lang.IllegalArgumentException - if the SQL String is invalid
      • Query

        public Query​(java.lang.String sql,
                     boolean treeParse)
        Construct a new parsed Query from a String.
        Parameters:
        sql - a SQL SELECT String to parse
        treeParse - true if a tree-parse step is required (usually so)
        Throws:
        java.lang.IllegalArgumentException - if the SQL String is invalid
      • Query

        public Query​(java.lang.String sql,
                     java.lang.Long timeOut)
        Construct a new parsed Query from a String.
        Parameters:
        sql - a SQL SELECT String to parse
        timeOut - maximum time in milliseconds to spend parsing, can be null for no timeout
        Throws:
        java.lang.IllegalArgumentException - if the SQL String is invalid
      • Query

        public Query​(java.lang.String sql,
                     boolean treeParse,
                     java.lang.Long timeOut)
        Construct a new parsed Query from a String.
        Parameters:
        sql - a SQL SELECT String to parse
        treeParse - true if a tree-parse step is required (usually so)
        timeOut - maximum time in milliseconds to spend parsing, can be null for no timeout
        Throws:
        java.lang.IllegalArgumentException - if the SQL String is invalid
    • Method Detail

      • isDistinct

        public boolean isDistinct()
        Gets the current distinct status of this query.
        Returns:
        true if this query is distinct
      • setDistinct

        public void setDistinct​(boolean distinct)
        Sets the distinct status of this query.
        Parameters:
        distinct - the new distinct status
      • isExplain

        public boolean isExplain()
        Gets the current explain status of this query.
        Returns:
        true if this query is an explain
      • setExplain

        public void setExplain​(boolean explain)
        Sets the explain status of this query.
        Parameters:
        explain - the new explain status
      • getSelect

        public java.util.List<SelectValue> getSelect()
        Gets the list of select fields for this query.
        Returns:
        a List of SelectValue objects representing the select list of the query
      • addSelect

        public void addSelect​(SelectValue obj)
        Adds a field to the select list of this query. Fields are stored in a List in the order they are added.
        Parameters:
        obj - a SelectValue to add to the list
      • getFrom

        public java.util.Set<AbstractTable> getFrom()
        Gets the Set of from tables for this query.
        Returns:
        a Set of AbstractTable objects representing the from list of the query
      • addFrom

        public void addFrom​(AbstractTable obj)
        Adds a table to the from list of this query. The order is not important.
        Parameters:
        obj - an AbstractTable to add to the set
      • getWhere

        public java.util.Set<AbstractConstraint> getWhere()
        Gets the Set of constraints in the where clause of this query.
        Returns:
        a Set of AbstractConstraint objects which, ANDed together form the where clause
      • addWhere

        public void addWhere​(AbstractConstraint obj)
        Adds a constraint to the where clause for this query. The order is not important. The constraints in the Set formed are ANDed together to form the where clause. If you wish to OR constraints together, use a ConstraintSet.
        Parameters:
        obj - an AbstractConstraint to add to the where clause
      • getGroupBy

        public java.util.Set<AbstractValue> getGroupBy()
        Gets the Set of fields in the GROUP BY clause of this query.
        Returns:
        a Set of AbstractValue objects representing the GROUP BY clause
      • addGroupBy

        public void addGroupBy​(AbstractValue obj)
        Adds a field to the GROUP BY clause of this query. The order is not important.
        Parameters:
        obj - an AbstractValue to add to the GROUP BY clause
      • getHaving

        public java.util.Set<AbstractConstraint> getHaving()
        Gets the set of constraints forming the HAVING clause of this query.
        Returns:
        a Set of AbstractConstraints representing the HAVING clause
      • addHaving

        public void addHaving​(AbstractConstraint obj)
        Adds a constraint to the HAVING clause of this query. The order is not important.
        Parameters:
        obj - an AbstractConstraint to add to the HAVING clause
      • getOrderBy

        public java.util.List<AbstractValue> getOrderBy()
        Gets the list of fields forming the ORDER BY clause of this query.
        Returns:
        a List of AbstractValues representing the ORDER BY clause
      • addOrderBy

        public void addOrderBy​(AbstractValue obj)
        Adds a field to the ORDER BY clause of this query. The fields are repesented in the clause in the order they were added.
        Parameters:
        obj - an AbstractValue to add to the ORDER BY clause
      • getLimit

        public int getLimit()
        Gets the LIMIT number for this query.
        Returns:
        the maximum number of rows that this query is allowed to return
      • getOffset

        public int getOffset()
        Gets the OFFSET number for this query.
        Returns:
        the number of rows in the query to discard before returning the first result
      • setLimitOffset

        public void setLimitOffset​(int limit,
                                   int offset)
        Sets the LIMIT and OFFSET numbers for this query.
        Parameters:
        limit - the LIMIT number
        offset - the OFFSET number
      • addToUnion

        public void addToUnion​(Query query)
        Adds another Query to the UNION set of this query.
        Parameters:
        query - the Query to UNION with this Query
      • getUnion

        public java.util.List<Query> getUnion()
        Returns the List of queries in the UNION set of this query.
        Returns:
        a List of Query objects
      • getSQLString

        public java.lang.String getSQLString()
        Convert this Query into a SQL String query.
        Specified by:
        getSQLString in interface SQLStringable
        Returns:
        this Query in String form
      • getSQLStringNoUnion

        public java.lang.String getSQLStringNoUnion()
        Convert this Query into a SQL String query, without regard to the other members of the UNION.
        Returns:
        this Query in String form
      • getSQLStringForPrecomputedTable

        public java.lang.String getSQLStringForPrecomputedTable​(java.lang.String extraSelect)
        Convert this Query into a SQL String query, without regard to the other members of the the UNION, with an extra field in the SELECT list.
        Parameters:
        extraSelect - an extra String to put into the select list
        Returns:
        this Query in String form
      • collectionToSQLString

        protected static java.lang.String collectionToSQLString​(java.util.Collection<? extends SQLStringable> c,
                                                                java.lang.String comma)
        Converts a collection of objects that implement the getSQLString method into a String, with the given comma string between each element.
        Parameters:
        c - the Collection of SQLStringable objects
        comma - the String to use as a separator between elements
        Returns:
        a String representation
      • collectionToSQLString

        protected static java.lang.String collectionToSQLString​(java.util.Collection<? extends SQLStringable> c,
                                                                java.util.Collection<java.lang.String> extraValues,
                                                                java.lang.String comma)
        Converts a collection of objects that implement the getSQLString method into a String, with the given comma string between each element.
        Parameters:
        c - the Collection of SQLStringable objects
        extraValues - a Collection of extra values which are Strings to add
        comma - the String to use as a separator between elements
        Returns:
        a String representation
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides Object.equals().
        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - an Object to compare to
        Returns:
        true if the object is equivalent
      • equalsNoUnion

        public boolean equalsNoUnion​(java.lang.Object obj)
        Returns true if this Query is equivalent to obj, disregarding other queries in the UNION.
        Parameters:
        obj - the object to compare to
        Returns:
        true if equal
      • hashCode

        public int hashCode()
        Overrides Object.hashCode().
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        an arbitrary integer created from the contents of the Query
      • hashCodeNoUnion

        public int hashCodeNoUnion()
        Returns a partial hashcode, ignoring other queries in the union.
        Returns:
        an integer
      • toString

        public java.lang.String toString()
        Overrides Object.toString().
        Overrides:
        toString in class java.lang.Object
        Returns:
        a String representation of this Query
      • processSelectList

        public void processSelectList​(antlr.collections.AST node)
        Processes an AST node that describes a SELECT list.
        Parameters:
        node - an AST node to process
      • processNewSelect

        public void processNewSelect​(antlr.collections.AST node)
        Processes an AST node that describes a SelectValue.
        Parameters:
        node - an AST node to process
      • processGroupClause

        public void processGroupClause​(antlr.collections.AST node)
        Processes an AST node that describes a GROUP clause.
        Parameters:
        node - an AST node to process
      • processOrderClause

        public void processOrderClause​(antlr.collections.AST node)
        Processes an AST node that describes a ORDER clause.
        Parameters:
        node - an AST node to process
      • processNewAbstractValue

        public AbstractValue processNewAbstractValue​(antlr.collections.AST ast)
        Processes a single AST node that describes an AbstractValue.
        Parameters:
        ast - as AST node to process
        Returns:
        an AbstractValue object corresponding to the input
      • processNewField

        public AbstractValue processNewField​(antlr.collections.AST node)
        Processes an AST node that describes a Field. If no table alias is found for a field the field may be an alias, in which case find the actual field name/function/etc that was defined in the select list.
        Parameters:
        node - an AST node to process
        Returns:
        a Field object corresponding to the input
      • processNewTypecast

        public Function processNewTypecast​(antlr.collections.AST node)
        Processes an AST node that describes a typecast.
        Parameters:
        node - an AST node to process
        Returns:
        a Function object corresponding to the input
      • processNewUnsafeFunction

        public Function processNewUnsafeFunction​(antlr.collections.AST ast)
        Processes an AST node that describes an unsafe function.
        Parameters:
        ast - an AST node to process
        Returns:
        a Function object corresponding to the input
      • processNewSafeFunction

        public Function processNewSafeFunction​(antlr.collections.AST node)
        Processes an AST node that describes a safe function.
        Parameters:
        node - an AST node to process
        Returns:
        a Function object corresponding to the input
      • processNewAbstractConstraint

        public AbstractConstraint processNewAbstractConstraint​(antlr.collections.AST ast)
        Processes an AST node that describes an AbstractConstraint.
        Parameters:
        ast - an AST node to process
        Returns:
        an AbstractConstraint object corresponding to the input
      • processLimitClause

        public void processLimitClause​(antlr.collections.AST ast)
        Processes an AST node that describes a Limit clause.
        Parameters:
        ast - an AST node to process
      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.Exception
        A testing method - converts the argument into a Query object, and then converts it back to a String again.
        Parameters:
        args - command-line arguments
        Throws:
        java.lang.Exception - anytime