public class SqlUtils extends Object
Modifier and Type | Method and Description |
---|---|
static Expr |
and(Collection<Expr> exprs)
This method returns an expression that combines the sub-expressions with logical AND.
|
static Expr |
and(Expr... exprs)
This method returns an expression that combines the sub-expressions with logical AND.
|
static Expr |
bool(String columnName,
boolean value)
This method returns an expression for the specified column name and boolean value.
|
static Expr[] |
exprWrap(Object... objs)
This method returns the specified objects wrapped as expressions.
|
static Expr |
exprWrapQuoted(String s)
This method returns the specified string wrapped as an expression of that string
in single quotes.
|
static GroupByClause |
groupBy(String columnName)
Convenience method to create a group-by clause for a single column.
|
static GroupByClause.Builder |
groupByBuilder()
Convenience method to create a group-by clause builder.
|
static Expr |
in(String columnName,
Collection<String> values)
This method returns an IN expression for the specified column name and the given
collection of string values, wrapping each value in single quotes.
|
static Expr |
in(String columnName,
Expr... exprs)
This method returns an IN expression for the specified column name and sub expressions.
|
static String |
join(String... s)
This is a convenience method that calls
StringUtils.join() , using ", "
(comma-space) as the delimiter. |
static Expr |
leaf(String s)
This is a temporary method which may go away in the future.
|
static Expr |
like(String columnName,
String string)
This method returns a LIKE expression for the specified column name and substring to match.
|
static LimitClause |
limit()
Convenience method to return a limit clause.
|
static Expr |
notIn(String columnName,
Collection<String> values)
This method returns a NOT IN expression for the specified column name and the
given collection of string values, wrapping each value in single quotes.
|
static Expr |
notIn(String columnName,
Expr... exprs)
This method returns a NOT IN expression for the specified column name and sub expressions.
|
static Expr |
or(Collection<Expr> exprs)
This method returns an expression that combines the sub-expressions with logical OR.
|
static Expr |
or(Expr... exprs)
This method returns an expression that combines the sub-expressions with logical OR.
|
static OrderByClause |
orderByAsc(String columnName)
Convenience method to create an order-by clause (ascending) for a single column.
|
static OrderByClause.Builder |
orderByBuilder()
Convenience method to create an order-by clause builder.
|
static OrderByClause |
orderByDesc(String columnName)
Convenience method to create an order-by clause (descending) for a single column.
|
static SelectClause |
select(Expr e)
Convenience method to create a select clause.
|
static SelectClause |
select(Expr e,
String tableName)
Convenience method to create a select clause.
|
static SqlStatement |
sql(SelectClause sc)
Convenience method to create an SQL statement from the given select clause.
|
static SqlStatement |
sql(SelectClause sc,
WhereClause wc)
Convenience method to create an SQL statement from the given clauses.
|
static SqlStatement |
sql(SelectClause sc,
WhereClause wc,
GroupByClause gbc,
OrderByClause obc)
Convenience method to create an SQL statement from the given clauses.
|
static SqlStatement |
sql(SelectClause sc,
WhereClause wc,
OrderByClause obc,
LimitClause l)
Convenience method to create an SQL statement from the given clauses.
|
static SqlStatement.Builder |
sqlBuilder()
Convenience method to create an SQL statement builder.
|
static WhereClause |
where(Expr e)
Convenience method to create a where clause.
|
public static Expr leaf(String s)
s
- the stringpublic static String join(String... s)
StringUtils.join()
, using ", "
(comma-space) as the delimiter.s
- the strings to be joinedpublic static Expr and(Expr... exprs)
(e1 and e2 and e3 and ...)
exprs
- sub expressions to be ANDedpublic static Expr and(Collection<Expr> exprs)
exprs
- sub expressions to be ANDedpublic static Expr or(Expr... exprs)
(e1 or e2 or e3 or ...)
exprs
- sub expressions to be ORedpublic static Expr or(Collection<Expr> exprs)
exprs
- sub expressions to be ORedpublic static Expr in(String columnName, Collection<String> values)
columnName in ('v1', 'v2', 'v3', ...)
columnName
- the column namevalues
- the string valuespublic static Expr notIn(String columnName, Collection<String> values)
columnName not in ('v1', 'v2', 'v3', ...)
columnName
- the column namevalues
- the string valuespublic static Expr in(String columnName, Expr... exprs)
columnName in (e1, e2, e3, ...)
columnName
- the column nameexprs
- the sub-expressions to matchpublic static Expr notIn(String columnName, Expr... exprs)
columnName not in (e1, e2, e3, ...)
columnName
- the column nameexprs
- the sub-expressions to matchpublic static Expr like(String columnName, String string)
columnName like '%string%'
columnName
- the column namestring
- the string to matchpublic static Expr bool(String columnName, boolean value)
FALSE: columnName = 0 TRUE: columnName = 1
columnName
- the column namevalue
- the value to matchpublic static Expr exprWrapQuoted(String s)
'stringValue'
s
- the stringpublic static Expr[] exprWrap(Object... objs)
objs
- the objects to wrappublic static SelectClause select(Expr e)
"select count(*) from table"
, one could write:
import static com.hp.util.sql.SqlUtils.*; ... SelectClause c = select(leaf("count(*) from table"));
e
- the expression for the select clauseselect(Expr,String)
public static SelectClause select(Expr e, String tableName)
"select count(*) from table"
, one could write:
import static com.hp.util.sql.SqlUtils.*; ... SelectClause c = select(leaf("count(*)"), "table"));
e
- the expression of what should be selectedtableName
- the table from which the data should be selectedselect(Expr)
public static WhereClause where(Expr e)
"where columnA = 0"
, one could write:
import static com.hp.util.sql.SqlUtils.*; ... WhereClause c = where(leaf("columnA = 0"));or, a slightly more complex example to create
"where (columnA = 0 and quantity > 5)"
Expr a = leaf("columnA = 0"); Expr b = leaf("quantity > 5"); WhereClause c = where(and(a, b));
e
- the expression for the where clausepublic static GroupByClause.Builder groupByBuilder()
public static GroupByClause groupBy(String columnName)
columnName
- the column namepublic static OrderByClause.Builder orderByBuilder()
public static OrderByClause orderByAsc(String columnName)
columnName
- the column namepublic static OrderByClause orderByDesc(String columnName)
columnName
- the column namepublic static LimitClause limit()
public static SqlStatement.Builder sqlBuilder()
public static SqlStatement sql(SelectClause sc)
sc
- the select clausepublic static SqlStatement sql(SelectClause sc, WhereClause wc)
sc
- the select clausewc
- the where clausepublic static SqlStatement sql(SelectClause sc, WhereClause wc, GroupByClause gbc, OrderByClause obc)
sc
- the select clausewc
- the where clausegbc
- the group by clauseobc
- the order by clausepublic static SqlStatement sql(SelectClause sc, WhereClause wc, OrderByClause obc, LimitClause l)
sc
- the select clausewc
- the where clauseobc
- the order by clausel
- the limit clauseCopyright © 2016. All Rights Reserved.