SqlConst

Framework Components [com.onepg.db]

← Back to Class List

Class Information

Package: com.onepg.db

Class Name: SqlConst

Extends: SqlBean

Description

Fixed SQL.
  • Stores the SQL string and bind field definitions (field names and types).
  • Primarily intended for use in batch processing; declare as a class variable (constant).
  • Using this class enables caching of prepared statements, so its use for performance improvement in web service processing is also assumed.
  • When executing SQL, pass parameters with bind values together with this SQL.
  • The same field name can be bound multiple times with the same type.
  • If there are no bind fields, use as-is.
  • To execute using the prepared statement cache, use SqlUtil.executeOneCache or SqlUtil.executeCache.
[SQL declaration example - with bind fields]
SqlConst SQL_INS_PET = SqlConst.begin()
    .addQuery("INSERT INTO t_pet ( ")
    .addQuery("  pet_no ")
    .addQuery(", pet_nm ")
    .addQuery(", birth_dt ")
    .addQuery(", ins_ts ")
    .addQuery(", upd_ts ")
    .addQuery(" ) VALUES ( ")
    .addQuery("  ? ", "pet_no", BindType.BigDecimal)
    .addQuery(", ? ", "pet_nm", BindType.String)
    .addQuery(", ? ", "birth_dt", BindType.Date)
    .addQuery(", ? ", "now_ts", BindType.Timestamp)
    .addQuery(", ? ", "now_ts", BindType.Timestamp)
    .addQuery(" ) ")
    .end();
[SQL execution example - with bind fields]
SqlUtil.executeOne(conn, SQL_INS_PET.bind(io));
[SQL declaration example - without bind fields]
SqlConst SQL_SEL_USER = SqlConst.begin()
    .addQuery("SELECT ")
    .addQuery("  u.user_id ")
    .addQuery(", u.user_nm ")
    .addQuery(", u.email ")
    .addQuery(", u.birth_dt ")
    .addQuery(" FROM t_user AS u ")
    .addQuery(" ORDER BY u.user_id ")
    .end();
[SQL execution example - without bind fields]
SqlResultSet rSet = SqlUtil.select(getDbConn(), SQL_SEL_USER);
[SQL execution example - prepared statement cache execution]
SqlUtil.executeOneCache(conn, SQL_INS_PET.bind(io));

Enumeration List

BindType

public enum BindType
Bind type.
  • Indicates the type used when binding to SQL.
  • Numeric types are unified as BigDecimal.

Constants

  • STRING
  • BIGDECIMAL
  • DATE
  • TIMESTAMP

Method List

begin

public SqlConstBuilder begin()
Creates a fixed SQL builder instance.

Return Value

SqlConstBuilder - the fixed SQL builder instance

bind

public SqlConst bind(AbstractIoTypeMap params)
Sets bind values.
  • Sets the stored SQL string and the parameter value map received as an argument into a SQL Bean and returns it.
  • Creates a bind value list from the bind field name list and bind field definition map using values from the parameter value map, and sets it in the SQL Bean.
  • The elements of the bind value list are of type Object, and each field value is stored with the type specified in the bind field definition.
  • If a bind field name does not exist in the parameter value map, a runtime error occurs.

Parameters

Name Type Description
params AbstractIoTypeMap parameter value map

Return Value

SqlConst - the fixed SQL

end

public SqlConst end()
Returns the fixed SQL.

Return Value

SqlConst - the fixed SQL

addQuery

public SqlConstBuilder addQuery(String sql)
Adds SQL.
  • Replaces two or more consecutive blanks with a single blank before appending.

Parameters

Name Type Description
sql String SQL

Return Value

SqlConstBuilder - the instance itself

addQuery

public SqlConstBuilder addQuery(String sql, String itemName, BindType bindType)
Adds SQL and bind field definition (field name and type).
  • The SQL string must contain exactly one bind placeholder ?.
  • The bind field name must be a valid value as an Io object key (key rules of AbstractIoTypeMap).

Parameters

Name Type Description
sql String SQL
itemName String bind field name
bindType BindType bind type

Return Value

SqlConstBuilder - the instance itself

delLastChar

public SqlConstBuilder delLastChar()
Deletes the last SQL string character.
  • Deletes the last character (one character) of the SQL string.

Return Value

SqlConstBuilder - the instance itself

delLastChar

public SqlConstBuilder delLastChar(int deleteCharCount)
Deletes the last SQL string characters.
  • Deletes the specified number of characters from the end of the SQL string.

Parameters

Name Type Description
deleteCharCount int number of characters to delete

Return Value

SqlConstBuilder - the instance itself