Class QueryBuilder


  • public class QueryBuilder
    extends Object
    Provides a fluent API for programmatically constructing and executing SQL queries.
    • Constructor Detail

      • QueryBuilder

        public QueryBuilder​(ByteArrayOutputStream o)
        Constructs a query builder from an existing SQL query.
        Parameters:
        o - The existing SQL query.
      • QueryBuilder

        public QueryBuilder()
    • Method Detail

      • from

        public QueryBuilder from​(String... tables)
        Appends a "from" clause to a query.
        Parameters:
        tables - The table names.
        Returns:
        The QueryBuilder instance.
      • from

        public QueryBuilder from​(Function<PrettyPrinter,​QueryBuilder> queryBuilderFun,
                                 String alias)
        Appends a "from" clause to a query.
        Parameters:
        queryBuilderFun - a builder for a subquery
        alias - the alias for the subquery
        Returns:
        the QueryBuilder instance. public QueryBuilder from(QueryBuilder queryBuilder, String alias) { if (queryBuilder == null || alias == null) { throw new IllegalArgumentException(); } sqlBuilder.append(" FROM ("); sqlBuilder.append(queryBuilder.getSQL()); sqlBuilder.append(") AS "); sqlBuilder.append(alias); sqlBuilder.append(" "); parameters.addAll(queryBuilder.parameters); return this; }
      • join

        public QueryBuilder join​(String table)
        Appends a "join" clause to a query.
        Parameters:
        table - The table name.
        Returns:
        The QueryBuilder instance.
      • leftJoin

        public QueryBuilder leftJoin​(String table)
        Appends a "left join" clause to a query.
        Parameters:
        table - The table name.
        Returns:
        The QueryBuilder instance.
      • rightJoin

        public QueryBuilder rightJoin​(String table)
        Appends a "right join" clause to a query.
        Parameters:
        table - The table name.
        Returns:
        The QueryBuilder instance.
      • on

        public QueryBuilder on​(String... predicates)
        Appends an "on" clause to a query.
        Parameters:
        predicates - The clause predicates.
        Returns:
        The QueryBuilder instance.
      • where

        public QueryBuilder where​(String... predicates)
        Appends a "where" clause to a query.
        Parameters:
        predicates - The clause predicates.
        Returns:
        The QueryBuilder instance.
      • and

        public QueryBuilder and​(String... predicates)
        Creates an "and" conditional.
        Parameters:
        predicates - The conditional's predicates.
        Returns:
        The conditional text.
      • or

        public QueryBuilder or​(String... predicates)
        Creates an "or" conditional.
        Parameters:
        predicates - The conditional's predicates.
        Returns:
        The conditional text.
      • allOf

        public static String allOf​(String... predicates)
        Creates an "and" conditional group.
        Parameters:
        predicates - The group's predicates.
        Returns:
        The conditional text.
      • anyOf

        public static String anyOf​(String... predicates)
        Creates an "or" conditional group.
        Parameters:
        predicates - The group's predicates.
        Returns:
        The conditional text.
      • equalTo

        public static String equalTo​(QueryBuilder queryBuilder)
        Creates an "equal to" conditional.
        Parameters:
        queryBuilder - The conditional's subquery.
        Returns:
        The conditional text.
      • notEqualTo

        public static String notEqualTo​(QueryBuilder queryBuilder)
        Creates a "not equal to" conditional.
        Parameters:
        queryBuilder - The conditional's subquery.
        Returns:
        The conditional text.
      • in

        public static String in​(QueryBuilder queryBuilder)
        Creates an "in" conditional.
        Parameters:
        queryBuilder - The conditional's subquery.
        Returns:
        The conditional text.
      • notIn

        public static String notIn​(QueryBuilder queryBuilder)
        Creates a "not in" conditional.
        Parameters:
        queryBuilder - The conditional's subquery.
        Returns:
        The conditional text.
      • exists

        public static String exists​(QueryBuilder queryBuilder)
        Creates an "exists" conditional.
        Parameters:
        queryBuilder - The conditional's subquery.
        Returns:
        The conditional text.
      • notExists

        public static String notExists​(QueryBuilder queryBuilder)
        Creates a "not exists" conditional.
        Parameters:
        queryBuilder - The conditional's subquery.
        Returns:
        The conditional text.
      • orderBy

        public QueryBuilder orderBy​(String... columns)
        Appends an "order by" clause to a query.
        Parameters:
        columns - The column names.
        Returns:
        The QueryBuilder instance.
      • alias

        public QueryBuilder alias​(String alias)
        Appends alias
        Parameters:
        alias - the alias for the subquery
        Returns:
        The QueryBuilder instance.
      • limit

        public QueryBuilder limit​(int count)
        Appends a "limit" clause to a query.
        Parameters:
        count - The limit count.
        Returns:
        The QueryBuilder instance.
      • forUpdate

        public QueryBuilder forUpdate()
        Appends a "for update" clause to a query.
        Returns:
        The QueryBuilder instance.
      • insertInto

        public QueryBuilder insertInto​(String table)
        Creates an "insert into" query.
        Parameters:
        table - The table name.
        Returns:
        The new QueryBuilder instance.
      • values

        public QueryBuilder values​(Map<String,​?> values)
        Appends column values to an "insert into" query.
        Parameters:
        values - The values to insert.
        Returns:
        The QueryBuilder instance.
      • args

        public QueryBuilder args​(List<?> values)
        Appends arguments to a function call
        Parameters:
        values - The values to insert.
        Returns:
        The QueryBuilder instance.
      • update

        public static QueryBuilder update​(String table)
        Creates an "update" query.
        Parameters:
        table - The table name.
        Returns:
        The new QueryBuilder instance.
      • set

        public QueryBuilder set​(Map<String,​?> values)
        Appends column values to an "update" query.
        Parameters:
        values - The values to update.
        Returns:
        The QueryBuilder instance.
      • bodyEnd

        public QueryBuilder bodyEnd​(String key)
        Appends body end
        Parameters:
        key - The params of the function.
        Returns:
        The QueryBuilder instance.
      • bodyStart

        public QueryBuilder bodyStart​(String key)
        Appends body start
        Parameters:
        key - The params of the function.
        Returns:
        The QueryBuilder instance.
      • returns

        public QueryBuilder returns​(String relation,
                                    Map<String,​?> params)
        Appends return clause to an "create function" statement.
        Parameters:
        relation - the relation name
        params - The params of the function.
        Returns:
        The QueryBuilder instance.
      • params

        public QueryBuilder params​(Map<String,​?> params)
        Appends column params to an "create function" statement.
        Parameters:
        params - The params of the function.
        Returns:
        The QueryBuilder instance.
      • flush

        public void flush()
      • createFunction

        public static Function<PrettyPrinter,​org.openprovenance.prov.template.compiler.sql.QueryBuilder.FunctionQueryBuilder> createFunction​(String fun)
        Creates a "create function" statement.
        Parameters:
        fun - The function name.
        Returns:
        The new QueryBuilder instance.
      • deleteFrom

        public static QueryBuilder deleteFrom​(String table)
        Creates a "delete from" query.
        Parameters:
        table - The table name.
        Returns:
        The new QueryBuilder instance.
      • execute

        public QueryBuilder execute​(Connection connection,
                                    Map<String,​?> arguments)
                             throws SQLException
        Executes a query.
        Parameters:
        connection - The connection on which the query will be executed.
        arguments - The query arguments.
        Returns:
        The QueryBuilder instance.
        Throws:
        SQLException - If an error occurs while executing the query.
      • getResult

        public Map<String,​Object> getResult()
        Returns the result of executing a query that is expected to return at most a single row.
        Returns:
        The query result, or null if the query either did not produce a result set or did not return any rows.
      • getResults

        public List<Map<String,​Object>> getResults()
        Returns the results of executing a query.
        Returns:
        The query results, or null if the query did not produce a result set.
      • getUpdateCount

        public int getUpdateCount()
        Returns the number of rows that were affected by the query.
        Returns:
        The number of rows that were affected by the query, or -1 if the query did not produce an update count.
      • getGeneratedKeys

        public List<Object> getGeneratedKeys()
        Returns the keys that were generated by the query.
        Returns:
        The keys that were generated by the query, or null if the query did not produce any generated keys.
      • prepare

        public PreparedStatement prepare​(Connection connection)
                                  throws SQLException
        Prepares a query for execution.
        Parameters:
        connection - The connection on which the query will be executed.
        Returns:
        A prepared statement that can be used to execute the query.
        Throws:
        SQLException - If an error occurs while preparing the query.
      • executeQuery

        public ResultSet executeQuery​(PreparedStatement statement,
                                      Map<String,​?> arguments)
                               throws SQLException
        Executes a query.
        Parameters:
        statement - The statement that will be used to execute the query.
        arguments - The query arguments.
        Returns:
        The query results.
        Throws:
        SQLException - If an error occurs while executing the query.
      • executeUpdate

        public int executeUpdate​(PreparedStatement statement,
                                 Map<String,​?> arguments)
                          throws SQLException
        Executes a query.
        Parameters:
        statement - The statement that will be used to execute the query.
        arguments - The query arguments.
        Returns:
        The number of rows that were affected by the query.
        Throws:
        SQLException - If an error occurs while executing the query.
      • getParameters

        public Collection<String> getParameters()
        Returns the parameters parsed by the query builder.
        Returns:
        The parameters parsed by the query builder.
      • getSQL

        public String getSQL()
        Returns the generated SQL.
        Returns:
        The generated SQL.
      • toString

        public String toString()
        Returns the query as a string.
        Overrides:
        toString in class Object