SqlBuilder

Framework Components [com.onepg.db]

← Back to Class List

Class Information

Package: com.onepg.db

Class Name: SqlBuilder

Extends: SqlBean

Description

SQL builder.
  • A class that encapsulates SQL and parameter list required for database access.
  • Has methods that can build SQL and set parameters simultaneously.
  • add* methods return the instance itself, so they can be used in method chains.
[SQL Addition Example 1] sqlBuilder.addQuery("AND a.user_id IS NOT NULL ");
[SQL Addition Example 2] sqlBuilder.addQuery("AND a.user_id = ? ", userId);
[SQL Addition Example 3] sqlBuilder.addQuery("AND ? <= a.birth_dt AND a.birth_dt <= ?", birthDtFrom, birthDtTo);
[SQL Addition Example 4] sqlBuilder.addQnotB("AND a.user_id = ? ", userId);
[SQL Addition Example 5] sqlBuilder.addQnotB("AND a.user_id = ? ", userId).addQnotB("AND a.user_nm LIKE ? ", '%' + name + '%');
[SQL Execution Example] SqlResultSet rSet = SqlUtil.select(getDbConn(), sqlBuilder);

Method List

addSqlBuilder

public void addSqlBuilder(SqlBuilder sb)
Adds SQL builder.
  • Inherits SQL and parameters.

Parameters

Name Type Description
sb SqlBuilder the SQL builder

addQuery

public SqlBuilder addQuery(String sql, Object... params)
Adds SQL and parameters.
  • Parameter arguments are optional and can be passed as single or multiple.
[Example 1] sqlBuilder.addQuery("AND a.user_id IS NOT NULL ");
[Example 2] sqlBuilder.addQuery("AND a.user_id = ? ", userId);
[Example 3] sqlBuilder.addQuery("AND ? <= a.birth_dt AND a.birth_dt <= ?", birthDtFrom, birthDtTo);

Parameters

Name Type Description
sql String the SQL
params Object... the parameters (multiple allowed) (optional)

Return Value

SqlBuilder - the instance itself

addParams

public SqlBuilder addParams(Object... params)
Adds parameters.
  • Can pass multiple parameters.
[Example 1] sqlBuilder.addParams(userId);
[Example 2] sqlBuilder.addParams(birthDtFrom, birthDtTo);

Parameters

Name Type Description
params Object... the parameters (multiple allowed)

Return Value

SqlBuilder - the instance itself

addListInBind

public SqlBuilder addListInBind(List<Object> params)
Adds comma-separated SQL bind characters.
  • Adds SQL bind characters "?" separated by commas to SQL for the number of elements in the list.
  • If the list contains 3 elements, "?,?,?" is added to SQL.
  • Values in the list are added as SQL bind character parameters.
  • Intended for use in IN clauses with variable number of SQL bind characters.
[Example]sqlBuilder.addQuery("AND type_cs IN (").addListInBind(list).addQuery(")");

Parameters

Name Type Description
params List<Object> the parameter list

Return Value

SqlBuilder - the instance itself

addQueryIfNotBlankParameter

public SqlBuilder addQueryIfNotBlankParameter(String sql, Object param)
Adds SQL and parameter only if the parameter is not null and not blank.
  • Adds SQL and parameter only if the parameter is not null and not blank.
  • Other specifications are the same as #addQuery(String, Object...).
  • Use the shortcut method #addQnotB(String, Object) for this method.
In the example below, SQL is added only if userId is not null and not blank.
[Example] sqlBuilder.addQueryIfNotBlankParameter("AND user_id = ? ", userId);

Parameters

Name Type Description
sql String the SQL
param Object the parameter (single only)

Return Value

SqlBuilder - this instance

addQnotB

public SqlBuilder addQnotB(String sql, Object param)
Adds SQL and parameter only if the parameter is not null and not blank.
  • Shortcut for #addQueryIfNotBlankParameter(String, Object).
  • Adds SQL and parameter only if the parameter is not null and not blank.
  • Other specifications are the same as #addQuery(String, Object...).
In the example below, SQL is added only if userId is not null and not blank.
[Example] sqlBuilder.addQnotB("AND user_id = ? ", userId);

Parameters

Name Type Description
sql String the SQL
param Object the parameter (single only)

Return Value

SqlBuilder - this instance

delLastChar

public SqlBuilder delLastChar()
Deletes the last SQL character.
  • Deletes the last character (one character) of the SQL string.
[Example]
for (final String key : params.keySet()) {
  sb.addQuery(key).addQuery("=?", params.get(key)).addQuery(",");
}
// Delete the last comma
sb.deleteLastChar();

Return Value

SqlBuilder - the instance itself

delLastChar

public SqlBuilder delLastChar(int deleteCharCount)
Deletes the last SQL characters.
  • Deletes the specified number of characters from the end of the SQL string.
[Example]
for (final String key : params.keySet()) {
  sb.addQuery(key).addQuery("=?", params.get(key)).addQuery(" AND ");
}
// Delete the last AND
sb.delLastChar(4);

Parameters

Name Type Description
deleteCharCount int the number of characters to delete

Return Value

SqlBuilder - the instance itself