Package com.zfabrik.util.jdbc
Class QueryBuilder
java.lang.Object
com.zfabrik.util.jdbc.QueryBuilder
Some simple utilities that help constructing SQL queries and update statements
from field arrays. Parameter sets and filter expressions may be derived from value Java beans and
filter Java beans respectively.
Naming conventions: A field name ALPHA_BETA (regardless of case) tranlates to the Java Bean property alphaBeta.
Java Bean property conventions: When reading conditions from beans, a property maxAlpha translates into an including upper bound for the field ALPHA, while a property minBeta translates into an excluding lower bound for the field BETA.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String[]
beanFields(Class<?> clz, String... omitted)
compute an array of field names given a bean, following our naming conventions (see above).static Object[]
beanParams(Object bean, String[] fields, String... additionals)
Reads Java bean property values from the given POJO for the given fields.static int
delete(Connection conn, String table, String where, Object... whereParams)
DELETEs the rows specified by the given where statement in the given table using the given connectionstatic int
insert(Connection conn, String table, Object bean)
INSERTs the data defined by the given bean into the given table using the given connection.static int
INSERTs the data defined by the given map into the given table using the given connection.static String
insertClause(String table, String[] fields)
constructs INSERT clause for the given table and fields to be used as a prepared statement stringstatic String
Translate the usual wildcards * and ? into SQL wildcards % and _.static int
rowsCount(Connection conn, String table)
Returns the number of rows of the given tablerun a querystatic String
selectClause(String table, String[] fields)
constructs a select clause for the given table and fieldsstatic int
UPDATEs the data defined by the given bean into the given table using the given connection.static int
update(Connection conn, String table, Map<String,Object> data, String where, Object... whereParams)
UPDATEs the data defined by the given map into the given table using the given connection.static String
updateClause(String table, String[] fields)
constructs UPDATE clause for the given table and fields to be used as a prepared statement string.
-
Constructor Details
-
QueryBuilder
public QueryBuilder()
-
-
Method Details
-
insertClause
constructs INSERT clause for the given table and fields to be used as a prepared statement string- Parameters:
table
-fields
-- Returns:
-
insert
INSERTs the data defined by the given bean into the given table using the given connection. The fields names are derived from the bean properties using the following convention: a) Field names are lower case; b) an underscore is inserted where the property has a case switch to upper case- Parameters:
conn
- the DB connectiontable
- the name of the tablebean
- the bean providing the names and the data- Returns:
- the result of the
PreparedStatement.executeUpdate()
-
insert
INSERTs the data defined by the given map into the given table using the given connection. The fields names are derived from the map keys using the following convention: a) Field names are lower case; b) an underscore is inserted where the property has a case switch to upper case- Parameters:
conn
- the DB connectiontable
- the name of the tabledata
- the data map provides the names and the data- Returns:
- the result of the
PreparedStatement.executeUpdate()
-
updateClause
constructs UPDATE clause for the given table and fields to be used as a prepared statement string. In general it is necessary to append a WHERE clause- Parameters:
table
-fields
-- Returns:
-
update
public static int update(Connection conn, String table, Object bean, String where, Object... whereParams)UPDATEs the data defined by the given bean into the given table using the given connection. The fields names are derived from the bean properties using the following convention: a) Field names are lower case; b) an underscore is inserted where the property has a case switch to upper case- Parameters:
conn
- the DB connectiontable
- the name of the tablebean
- the bean providing the names and the datawhere
- a where clause using '?' as place holder for the datawhereParams
- parameters for the '?'s in the where clause- Returns:
- the result of the
PreparedStatement.executeUpdate()
-
update
public static int update(Connection conn, String table, Map<String,Object> data, String where, Object... whereParams)UPDATEs the data defined by the given map into the given table using the given connection. The fields names are derived from the map keys using the following convention: a) Field names are lower case; b) an underscore is inserted where the property has a case switch to upper case- Parameters:
conn
- the DB connectiontable
- the name of the tabledata
- the data map provides the names and the datawhere
- a where clause using '?' as place holder for the datawhereParams
- parameters for the '?'s in the where clause- Returns:
- the result of the
PreparedStatement.executeUpdate()
-
selectClause
constructs a select clause for the given table and fields -
select
public static List<Map<String,Object>> select(Connection conn, String table, String[] fields, String where, Object... whereParams)run a query -
delete
DELETEs the rows specified by the given where statement in the given table using the given connection- Parameters:
conn
- the DB connectiontable
- the name of the tablewhere
- a where clause using '?' as place holder for the datawhereParams
- parameters for the '?'s in the where clause- Returns:
- the result of the
PreparedStatement.executeUpdate()
-
rowsCount
Returns the number of rows of the given table- Parameters:
conn
-table
-- Returns:
-
beanParams
Reads Java bean property values from the given POJO for the given fields. The set of fields introspected is constructed from the passed array as well as some additional fields that will be appended.- Parameters:
bean
-fields
-additionals
-- Returns:
-
beanFields
compute an array of field names given a bean, following our naming conventions (see above). A list of field names to omit from the resulting array can be specified as convenience.- Parameters:
clz
-- Returns:
-
mask
Translate the usual wildcards * and ? into SQL wildcards % and _.
-