Class SqlBuilder


  • public final class SqlBuilder
    extends Object
    The SQL Builder that allows representing SQL queries in a similar way they're written.

    It is intended to be used like the following.

     
          import static com.feedzai.commons.sql.abstraction.dml.dialect.SqlBuilder.*;
    
          (...)
    
          select(all()).from(table("TABLE_NAME"))
     
     
    Since:
    2.0.0
    Author:
    Rui Vilao (rui.vilao@feedzai.com)
    • Constructor Detail

      • SqlBuilder

        public SqlBuilder()
    • Method Detail

      • select

        public static Query select​(Expression... select)
        Starts a new query.
        Parameters:
        select - The objects.
        Returns:
        A new query.
      • select

        public static Query select​(Collection<? extends Expression> select)
        Starts a new query.
        Parameters:
        select - The collection of select columns.
        Returns:
        A new query.
      • all

        public static Expression all()
        Selects all columns in a row.
        Returns:
        The expression.
      • all

        public static Expression all​(String tableName)
        Selects all columns in a table.
        Parameters:
        tableName - The table.
        Returns:
        The expression.
      • column

        public static Name column​(String name)
        A column.
        Parameters:
        name - The column name.
        Returns:
        The expression.
      • column

        public static Name column​(String tableName,
                                  String name)
        A column from a table.
        Parameters:
        tableName - The table name.
        name - The column name.
        Returns:
        The expression.
      • table

        public static Name table​(String name)
        A table.
        Parameters:
        name - The table name.
        Returns:
        The expression.
      • eq

        public static Expression eq​(Expression... e1)
        The equals expression.
        Parameters:
        e1 - The expressions.
        Returns:
        The expression.
      • eq

        public static Expression eq​(Collection<? extends Expression> e1)
        The equals expression.
        Parameters:
        e1 - The expressions.
        Returns:
        The expression.
      • k

        public static Expression k​(Object o)
        A constant.
        Parameters:
        o - The constant.
        Returns:
        The expression.
      • and

        public static Expression and​(Expression... exps)
        The AND operator.
        Parameters:
        exps - The expressions.
        Returns:
        The AND representation.
      • and

        public static Expression and​(Collection<? extends Expression> exps)
        The AND operator.
        Parameters:
        exps - The expressions.
        Returns:
        The AND representation.
      • or

        public static Expression or​(Expression... exps)
        The OR operator.
        Parameters:
        exps - The expressions.
        Returns:
        The OR representation.
      • or

        public static Expression or​(Collection<? extends Expression> exps)
        The OR operator.
        Parameters:
        exps - The expressions.
        Returns:
        The OR representation.
      • div

        public static Expression div​(Expression... exps)
        The DIV operator.
        Parameters:
        exps - The expressions.
        Returns:
        The DIV representation.
      • div

        public static Expression div​(Collection<? extends Expression> exps)
        The DIV operator.
        Parameters:
        exps - The expressions.
        Returns:
        The DIV representation.
      • mod

        public static Expression mod​(Expression exp1,
                                     Expression exp2)
        The MOD operator.
        Parameters:
        exp1 - The dividend.
        exp2 - The divisor.
        Returns:
        The DIV representation.
      • gt

        public static Expression gt​(Expression... exps)
        The GT operator.
        Parameters:
        exps - The expressions.
        Returns:
        The GT representation.
      • gt

        public static Expression gt​(Collection<? extends Expression> exps)
        The GT operator.
        Parameters:
        exps - The expressions.
        Returns:
        The GT representation.
      • gteq

        public static Expression gteq​(Expression... exps)
        The GTEQ operator.
        Parameters:
        exps - The expressions.
        Returns:
        The GTEQ representation.
      • gteq

        public static Expression gteq​(Collection<? extends Expression> exps)
        The GTEQ operator.
        Parameters:
        exps - The expressions.
        Returns:
        The GTEQ representation.
      • like

        public static Expression like​(Expression... exps)
        The LIKE operator.
        Parameters:
        exps - The expressions.
        Returns:
        The LIKE representation.
      • like

        public static Expression like​(Collection<? extends Expression> exps)
        The LIKE operator.
        Parameters:
        exps - The expressions.
        Returns:
        The LIKE representation.
      • lt

        public static Expression lt​(Expression... exps)
        The LT operator.
        Parameters:
        exps - The expressions.
        Returns:
        The LT representation.
      • lt

        public static Expression lt​(Collection<? extends Expression> exps)
        The LT operator.
        Parameters:
        exps - The expressions.
        Returns:
        The LT representation.
      • lteq

        public static Expression lteq​(Expression... exps)
        The LTEQ operator.
        Parameters:
        exps - The expressions.
        Returns:
        The LTEQ representation.
      • lteq

        public static Expression lteq​(Collection<? extends Expression> exps)
        The LTEQ operator.
        Parameters:
        exps - The expressions.
        Returns:
        The LTEQ representation.
      • minus

        public static Expression minus​(Expression... exps)
        The MINUS operator.
        Parameters:
        exps - The expressions.
        Returns:
        The MINUS representation.
      • minus

        public static Expression minus​(Collection<? extends Expression> exps)
        The MINUS operator.
        Parameters:
        exps - The expressions.
        Returns:
        The MINUS representation.
      • mult

        public static Expression mult​(Expression... exps)
        The MULT operator.
        Parameters:
        exps - The expressions.
        Returns:
        The MULT representation.
      • mult

        public static Expression mult​(Collection<? extends Expression> exps)
        The MULT operator.
        Parameters:
        exps - The expressions.
        Returns:
        The MULT representation.
      • plus

        public static Expression plus​(Expression... exps)
        The PLUS operator.
        Parameters:
        exps - The expressions.
        Returns:
        The PLUS representation.
      • plus

        public static Expression plus​(Collection<? extends Expression> exps)
        The PLUS operator.
        Parameters:
        exps - The expressions.
        Returns:
        The PLUS representation.
      • max

        public static Expression max​(Expression exp)
        The MAX operator.
        Parameters:
        exp - The expression.
        Returns:
        The MAX representation.
      • min

        public static Expression min​(Expression exp)
        The MIN operator.
        Parameters:
        exp - The expression.
        Returns:
        The MIN representation.
      • stddev

        public static Expression stddev​(Expression exp)
        The STDDEV operator.
        Parameters:
        exp - The expression.
        Returns:
        The STDDEV representation.
      • avg

        public static Expression avg​(Expression exp)
        The AVG operator.
        Parameters:
        exp - The expression.
        Returns:
        The AVG representation.
      • count

        public static Expression count​(Expression exp)
        The COUNT operator.
        Parameters:
        exp - The expression.
        Returns:
        The COUNT representation.
      • sum

        public static Expression sum​(Expression exp)
        The SUM operator.
        Parameters:
        exp - The expression.
        Returns:
        The SUM representation.
      • floor

        public static Expression floor​(Expression exp)
        The FLOOR operator.
        Parameters:
        exp - The expression.
        Returns:
        The FLOOR representation.
      • ceiling

        public static Expression ceiling​(Expression exp)
        The CEILING operator.
        Parameters:
        exp - The expression.
        Returns:
        The CEILING representation.
      • udf

        public static Expression udf​(String udf)
        The Used Defined Function operator.
        Parameters:
        udf - The UDF name.
        Returns:
        The UDF representation.
      • udf

        public static Expression udf​(String udf,
                                     Expression exp)
        The Used Defined Function operator.
        Parameters:
        udf - The UDF name.
        exp - The expression.
        Returns:
        The UDF representation.
      • upper

        public static Expression upper​(Expression exp)
        The UPPER operator.
        Parameters:
        exp - The expression inside the operator.
        Returns:
        The UPPER representation.
      • stringAgg

        public static StringAgg stringAgg​(Expression column)
        The StringAgg function.
        Parameters:
        column - The expression inside the operator.
        Returns:
        The StringAgg function.
      • with

        public static With with​(String alias,
                                Expression expression)
        The With clause.
        Parameters:
        alias - The name to be assigned to the expression.
        expression - The expression.
        Returns:
        The With clause.
      • lower

        public static Expression lower​(Expression exp)
        The LOWER operator.
        Parameters:
        exp - The expression inside the operator.
        Returns:
        The LOWER representation.
      • f

        public static Expression f​(String function)
        An internal function (provided by the engine in place).
        Parameters:
        function - The function.
        Returns:
        The function representation.
      • f

        public static final Expression f​(String function,
                                         Expression exp)
        An internal function (provided by the engine in place).
        Parameters:
        function - The function.
        exp - The expression.
        Returns:
        The function representation.
      • join

        public static Expression join​(Expression e1,
                                      Expression e2)
        The same of making eq(e1, e2).
        Parameters:
        e1 - The first expression.
        e2 - The second expression.
        Returns:
        A join representation using the equal operator.
      • createView

        public static View createView​(String name)
        Creates a view.
        Parameters:
        name - The name of the view.
        Returns:
        The view representation.
      • cast

        public static Cast cast​(Expression expression,
                                DbColumnType type)
        Creates a cast expression. PDB does not support cast to JSON, CLOB and BLOB. An exception will be thrown when trying to translate the data type.
        Parameters:
        expression - The expression.
        type - the type to be converted.
        Returns:
        The cast representation.
      • caseWhen

        public static Case caseWhen()
        Returns a new "case when".
        Returns:
        a new "case when".
      • caseWhen

        public static Case caseWhen​(Expression condition,
                                    Expression trueAction)
        Creates a case expression.
        Parameters:
        condition - The name of the view.
        trueAction - The name of the view.
        Returns:
        The case when representation.
      • union

        public static Union union​(Expression... expressions)
        Creates a union expression.
        Parameters:
        expressions - expressions to union.
        Returns:
        The union representation.
      • union

        public static Union union​(Collection<Expression> expressions)
        Creates a union expression.
        Parameters:
        expressions - expressions to union.
        Returns:
        The union representation.
      • values

        public static Values values​(String... aliases)
        Creates a values expression.
        Parameters:
        aliases - the columns' aliases.
        Returns:
        The values representation.
      • values

        public static Values values​(Collection<String> aliases)
        Creates a values expression.
        Parameters:
        aliases - the columns' aliases.
        Returns:
        The values representation.
      • neq

        public static Expression neq​(Expression... exps)
        The not equal expression.
        Parameters:
        exps - The expressions.
        Returns:
        The expression.
      • neq

        public static Expression neq​(Collection<? extends Expression> exps)
        The not equal expression.
        Parameters:
        exps - The expressions.
        Returns:
        The expression.
      • L

        public static Expression L​(Expression... exps)
        A list of expressions enclosed.
        Parameters:
        exps - The expressions.
        Returns:
        The expression.
      • L

        public static Expression L​(Collection<? extends Expression> exps)
        A list of expression enclosed.
        Parameters:
        exps - The expressions.
        Returns:
        The expression.
      • in

        public static Expression in​(Expression e1,
                                    Expression e2)
        The IN expression.
        Parameters:
        e1 - The first expression.
        e2 - The second expression.
        Returns:
        The expression.
      • notIn

        public static Expression notIn​(Expression e1,
                                       Expression e2)
        The NOT IN expression.
        Parameters:
        e1 - The first expression.
        e2 - The second expression.
        Returns:
        The expression.
      • coalesce

        public static Coalesce coalesce​(Expression exp,
                                        Expression... alternative)
        The coalesce operator.
        Parameters:
        exp - The expression to test if is NULL.
        alternative - The alternative expressions to use.
        Returns:
        The coalesce operator.
      • between

        public static Between between​(Expression exp1,
                                      Expression exp2,
                                      Expression exp3)
        The BETWEEN operator.
        Parameters:
        exp1 - The column.
        exp2 - The first bound.
        exp3 - The second bound.
        Returns:
        The between expression.
      • notBetween

        public static Between notBetween​(Expression exp1,
                                         Expression exp2,
                                         Expression exp3)
        The NOT BETWEEN operator.
        Parameters:
        exp1 - The column.
        exp2 - The first bound.
        exp3 - The second bound.
        Returns:
        The between expression.
      • update

        public static Update update​(Expression table)
        The UPDATE operator.
        Parameters:
        table - The table.
        Returns:
        The UPDATE expression.
      • delete

        public static Delete delete​(Expression table)
        The DELETE keyword.
        Parameters:
        table - The table.
        Returns:
        The DELETE expression.
      • lit

        public static Literal lit​(Object o)
        Use this to add literals (strings, etc) when building the SQL statement.
        Parameters:
        o - The literal.
        Returns:
        The literal object.
      • truncate

        public static Truncate truncate​(Expression table)
        The TRUNCATE keyword.
        Parameters:
        table - The table.
        Returns:
        The TRUNCATE object.
      • concat

        public static Concat concat​(Expression delimiter,
                                    Collection<Expression> expressions)
        The CONCAT keyword.
        Parameters:
        delimiter - The concatenation delimiter.
        expressions - The expressions to concatenate.
        Returns:
        The CONCAT object.
      • concat

        public static Concat concat​(Expression delimiter,
                                    Expression... expressions)
        The CONCAT keyword.
        Parameters:
        delimiter - The concatenation delimiter.
        expressions - The expressions to concatenate.
        Returns:
        The CONCAT object.
      • rename

        public static Rename rename​(Expression oldName,
                                    Expression newName)
        Rename table operator.
        Parameters:
        oldName - The table.
        Returns:
        The rename instance.
      • dropPK

        public static DropPrimaryKey dropPK​(Expression table)
        Drop primary key table operator.
        Parameters:
        table - The table.
        Returns:
        The drop primary key instance.
      • alterColumn

        public static AlterColumn alterColumn​(Expression table,
                                              Name column,
                                              DbColumnType dbColumnType,
                                              DbColumnConstraint... constraints)
        Alter column operator.
        Parameters:
        table - The table containing the column.
        column - The column of the table.
        dbColumnType - The db column type.
        constraints - The constraints of the column.
        Returns:
        The alter column operator.
      • alterColumn

        public static AlterColumn alterColumn​(Expression table,
                                              DbColumn dbColumn)
        Alter column operator.
        Parameters:
        table - The table containing the column.
        dbColumn - The database column definition.
        Returns:
        The alter column operator.
      • dbFk

        public static DbFk.Builder dbFk()
        Creates a Database Foreign Key builder.
        Returns:
        A Database Foreign Key builder.
      • dbEntity

        public static DbEntity.Builder dbEntity()
        Creates a Database Entity builder.
        Returns:
        A Database Entity Key builder.
      • dbIndex

        public static DbIndex.Builder dbIndex()
        Creates a Database Index builder.
        Returns:
        A Database Index Key builder.
      • dbColumn

        public static DbColumn.Builder dbColumn()
        Creates a Database Column builder.
        Returns:
        A Database Column Key builder.
      • dbColumn

        public static DbColumn.Builder dbColumn​(String name,
                                                DbColumnType type,
                                                boolean autoInc)
        Creates a Database Column builder.
        Returns:
        A Database Column Key builder.
      • dbColumn

        public static DbColumn.Builder dbColumn​(String name,
                                                DbColumnType type,
                                                int size)
        Creates a Database Column builder.
        Returns:
        A Database Column Key builder.
      • entry

        public static EntityEntry.Builder entry()
        Creates a Database Entry builder.
        Returns:
        A Database Entry Key builder.