SqlUtil

Framework Components [com.onepg.db]

← Back to Class List

Class Information

Package: com.onepg.db

Class Name: SqlUtil

Description

SQL execution utility class.

Constants and Variables

ORACLE_PROTOCOL_ERR_AVOID_SQL

public String ORACLE_PROTOCOL_ERR_AVOID_SQL
SQL to avoid Oracle protocol violation errors.
support.oracle.com (reference)

Method List

selectOneExists

public IoItems selectOneExists(Connection conn, SqlBean sb)
Retrieves a single record (throws an error if no record exists).
  • Use this method when exactly one matching record is expected to exist.
  • Throws an exception if the result is zero records.
  • Throws an exception if multiple records are returned.

Parameters

Name Type Description
conn Connection the database connection
sb SqlBean the SQL Bean

Return Value

IoItems - the row data map

selectOne

public IoItems selectOne(Connection conn, SqlBean sb)
Retrieves a single record.
  • Use this method when exactly one matching record is expected to exist.
  • Returns null if the result is zero records.
  • Throws an exception if multiple records are returned.
  • Physical field names are in lowercase. (key rule of AbstractIoTypeMap)

Parameters

Name Type Description
conn Connection the database connection
sb SqlBean the SQL Bean

Return Value

IoItems - the row data map

selectOneMultiIgnore

public IoItems selectOneMultiIgnore(Connection conn, SqlBean sb)
Retrieves a single record (multiple records allowed).
  • Returns null if the result is zero records.
  • Returns the first record without error even if multiple records are retrieved.
  • Physical field names are in lowercase. (key rule of AbstractIoTypeMap)

Parameters

Name Type Description
conn Connection the database connection
sb SqlBean the SQL Bean

Return Value

IoItems - the row data map

select

public SqlResultSet select(Connection conn, SqlBean sb)
Retrieves multiple records.
  • Returns a SqlResultSet.
  • Physical field names of row maps obtained from the SqlResultSet iterator are in lowercase. (key rule of AbstractIoTypeMap)
  • Use in a try statement (try-with-resources).
  • The default fetch size in this class is 500. Use SqlUtil#selectFetchAll(Connection, SqlBean) to fetch all records.
  • Notes on fetch size per DBMS:
    • Oracle defaults to 10, which is small, so specify the fetch size.
    • PostgreSQL defaults to fetching all records, which may cause OutOfMemory, so specify the fetch size.
    • With PostgreSQL, specifying a fetch size (without fetching all), updating retrieved data, and performing intermediate commits causes a cursor invalidation error (SQLSTATE 34000). In that case, stop intermediate commits or fetch all records.
      Alternatively, though more complex, splitting data retrieval using a SQL LIMIT clause also resolves the issue.
    • With MS SQL Server, specifying a fetch size may not work as expected, so use a SQL LIMIT clause to split data retrieval if OutOfMemory is a concern.
[Example]
try (final SqlResultSet rSet = SqlUtil.select(getDbConn(), sqlBuilder);) {
  for (final IoItems row : rSet) {
    : (omitted)
  }
  if (rSet.getReadedCount() <= 0) {
    // Zero records.
  }
}

Parameters

Name Type Description
conn Connection the database connection
sb SqlBean the SQL Bean

Return Value

SqlResultSet - the SQL result set

selectFetchAll

public SqlResultSet selectFetchAll(Connection conn, SqlBean sb)
Retrieves multiple records (fetches all records).
  • Do not use this method in general.
  • Use this method only when issues occur without fetching all records.
  • Retrieving a large number of records with this method may cause an OutOfMemory error.

Parameters

Name Type Description
conn Connection the database connection
sb SqlBean the SQL Bean

Return Value

SqlResultSet - the SQL result set

selectBulk

public IoRows selectBulk(Connection conn, SqlBean sb, int limitCount)
Retrieves multiple records in bulk.
  • Returns a list of multiple rows.
  • Returns an empty list if the result is zero records.
  • Physical field names of each row map are in lowercase. (key rule of AbstractIoTypeMap)
  • Use #select(Connection, SqlBean) for loop processing since this method consumes memory.
  • Retrieving a large number of records with this method may cause an OutOfMemory error.

Parameters

Name Type Description
conn Connection the database connection
sb SqlBean the SQL Bean
limitCount int the maximum number of records to retrieve

Return Value

IoRows - the list of multiple rows

selectBulkAll

public IoRows selectBulkAll(Connection conn, SqlBean sb)
Retrieves all records in bulk.
  • Returns a list of multiple rows.
  • Returns an empty list if the result is zero records.
  • Physical field names of each row map are in lowercase. (key rule of AbstractIoTypeMap)
  • Use #select(Connection, SqlBean) for loop processing since this method consumes memory.
  • Retrieving a large number of records with this method may cause an OutOfMemory error.

Parameters

Name Type Description
conn Connection the database connection
sb SqlBean the SQL Bean

Return Value

IoRows - the list of multiple rows

insertOne

public boolean insertOne(Connection conn, String tableName, AbstractIoTypeMap params)
Inserts a single record into the specified table.
  • Inserts one record into the specified table.
  • Parameters not present in the table are ignored.
  • Note: if a column is added to the table after implementation and that column name already exists in the parameters, values will be inserted into the new column without modifying the implementation.
  • Throws an exception if the number of affected rows is zero for reasons other than a unique constraint violation.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values

Return Value

boolean - false if a unique constraint violation occurs; true if the record was successfully inserted

insertOne

public boolean insertOne(Connection conn, String tableName, AbstractIoTypeMap params, String tsItem)
Inserts a single record into the specified table (with automatic timestamp setting).
  • Inserts one record into the specified table.
  • Sets the timestamp for optimistic locking.
  • Parameters not present in the table are ignored.
  • Note: if a column is added to the table after implementation and that column name already exists in the parameters, values will be inserted into the new column without modifying the implementation.
  • Throws an exception if the number of affected rows is zero for reasons other than a unique constraint violation.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values
tsItem String the timestamp column name (for optimistic locking)

Return Value

boolean - false if a unique constraint violation occurs; true if the record was successfully inserted

updateOne

public boolean updateOne(Connection conn, String tableName, AbstractIoTypeMap params, String[] keyItems)
Updates a single record in the specified table.
  • Updates one record in the specified table.
  • Throws an exception if multiple records are updated.
  • Parameters not present in the table are ignored.
  • Note: if a column is added to the table after implementation and that column name already exists in the parameters, values will be updated in the new column without modifying the implementation.
  • A WHERE clause is built using the key columns.
  • Key columns MUST be included in the parameter values.
  • Key column names MUST be specified in lowercase. (key rule of AbstractIoTypeMap)

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including key columns)
keyItems String[] the key column names

Return Value

boolean - true if one record was updated; false if zero records were updated

updateOne

public boolean updateOne(Connection conn, String tableName, AbstractIoTypeMap params, String[] keyItems, String tsItem)
Updates a single record in the specified table (with timestamp optimistic locking).
  • Updates one record in the specified table.
  • Throws an exception if multiple records are updated.
  • Performs optimistic locking using a timestamp.
  • Parameters not present in the table are ignored.
  • Note: if a column is added to the table after implementation and that column name already exists in the parameters, values will be updated in the new column without modifying the implementation.
  • A WHERE clause is built using the key columns and the timestamp column (for locking).
  • Key columns and the timestamp column MUST be included in the parameter values.
  • Key column names and the timestamp column name MUST be specified in lowercase. (key rule of AbstractIoTypeMap)
  • The timestamp column is updated to the current date and time.
  • Use #updateOne(Connection, String, AbstractIoTypeMap, String[]) if timestamp optimistic locking is not required.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including key columns and the timestamp column)
keyItems String[] the key column names
tsItem String the timestamp column name (for optimistic locking)

Return Value

boolean - true if one record was updated; false if zero records were updated

updateOneByPkey

public boolean updateOneByPkey(Connection conn, String tableName, AbstractIoTypeMap params)
Updates a single record in the specified table by primary key.
  • Updates one record in the specified table.
  • Throws an exception if multiple records are updated.
  • Parameters not present in the table are ignored.
  • Note: if a column is added to the table after implementation and that column name already exists in the parameters, values will be updated in the new column without modifying the implementation.
  • A WHERE clause is built using the primary key columns of the table.
  • Throws an exception if the table has no primary key.
  • Primary key columns MUST be included in the parameter values.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including primary key columns)

Return Value

boolean - true if one record was updated; false if zero records were updated

updateOneByPkey

public boolean updateOneByPkey(Connection conn, String tableName, AbstractIoTypeMap params, String tsItem)
Updates a single record in the specified table by primary key (with timestamp optimistic locking).
  • Updates one record in the specified table.
  • Throws an exception if multiple records are updated.
  • Performs optimistic locking using a timestamp.
  • Parameters not present in the table are ignored.
  • Note: if a column is added to the table after implementation and that column name already exists in the parameters, values will be updated in the new column without modifying the implementation.
  • A WHERE clause is built using the primary key columns and the timestamp column (for locking).
  • Throws an exception if the table has no primary key.
  • Primary key columns and the timestamp column MUST be included in the parameter values.
  • The timestamp column name MUST be specified in lowercase. (key rule of AbstractIoTypeMap)
  • The timestamp column is updated to the current date and time.
  • Use #updateOneByPkey(Connection, String, AbstractIoTypeMap) if timestamp optimistic locking is not required.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including primary key columns and the timestamp column)
tsItem String the timestamp column name (for optimistic locking)

Return Value

boolean - true if one record was updated; false if zero records were updated

update

public int update(Connection conn, String tableName, AbstractIoTypeMap params, String[] whereItems)
Updates multiple records in the specified table.
  • Updates multiple records in the specified table.
  • Parameters not present in the table are ignored.
  • Note: if a column is added to the table after implementation and that column name already exists in the parameters, values will be updated in the new column without modifying the implementation.
  • A WHERE clause is built using the search condition columns.
  • Search condition columns MUST be included in the parameter values.
  • Search condition column names MUST be specified in lowercase. (key rule of AbstractIoTypeMap)

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including search condition columns)
whereItems String[] the search condition column names (optional; pass null if omitted)

Return Value

int - the number of updated records

updateAll

public int updateAll(Connection conn, String tableName, AbstractIoTypeMap params)
Updates all records in the specified table.
  • Updates all records in the specified table.
  • Parameters not present in the table are ignored.
  • Note: if a column is added to the table after implementation and that column name already exists in the parameters, values will be updated in the new column without modifying the implementation.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values

Return Value

int - the number of updated records

deleteOne

public boolean deleteOne(Connection conn, String tableName, AbstractIoTypeMap params, String[] keyItems)
Deletes a single record from the specified table.
  • Deletes one record from the specified table.
  • Throws an exception if multiple records are deleted.
  • A WHERE clause is built using the key columns.
  • Key columns MUST be included in the parameter values.
  • Parameters that are not key columns are ignored.
  • Key column names MUST be specified in lowercase. (key rule of AbstractIoTypeMap)

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including key columns)
keyItems String[] the key column names

Return Value

boolean - true if one record was deleted; false if zero records were deleted

deleteOne

public boolean deleteOne(Connection conn, String tableName, AbstractIoTypeMap params, String[] keyItems, String tsItem)
Deletes a single record from the specified table (with timestamp optimistic locking).
  • Deletes one record from the specified table.
  • Throws an exception if multiple records are deleted.
  • Performs optimistic locking using a timestamp.
  • A WHERE clause is built using the key columns and the timestamp column (for locking).
  • Key columns and the timestamp column MUST be included in the parameter values.
  • Parameters that are neither key columns nor the timestamp column are ignored.
  • Key column names and the timestamp column name MUST be specified in lowercase. (key rule of AbstractIoTypeMap)
  • Use #deleteOne(Connection, String, AbstractIoTypeMap, String[]) if timestamp optimistic locking is not required.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including key columns and the timestamp column)
keyItems String[] the key column names
tsItem String the timestamp column name (for optimistic locking)

Return Value

boolean - true if one record was deleted; false if zero records were deleted

deleteOneByPkey

public boolean deleteOneByPkey(Connection conn, String tableName, AbstractIoTypeMap params)
Deletes a single record from the specified table by primary key.
  • Deletes one record from the specified table.
  • Throws an exception if multiple records are deleted.
  • A WHERE clause is built using the primary key columns of the table.
  • Throws an exception if the table has no primary key.
  • Primary key columns MUST be included in the parameter values.
  • Parameters that are not primary key columns are ignored.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including primary key columns)

Return Value

boolean - true if one record was deleted; false if zero records were deleted

deleteOneByPkey

public boolean deleteOneByPkey(Connection conn, String tableName, AbstractIoTypeMap params, String tsItem)
Deletes a single record from the specified table by primary key (with timestamp optimistic locking).
  • Deletes one record from the specified table.
  • Throws an exception if multiple records are deleted.
  • Performs optimistic locking using a timestamp.
  • A WHERE clause is built using the primary key columns and the timestamp column (for locking).
  • Throws an exception if the table has no primary key.
  • Primary key columns and the timestamp column MUST be included in the parameter values.
  • Parameters that are neither primary key columns nor the timestamp column are ignored.
  • The timestamp column name MUST be specified in lowercase. (key rule of AbstractIoTypeMap)
  • Use #deleteOneByPkey(Connection, String, AbstractIoTypeMap) if timestamp optimistic locking is not required.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including primary key columns and the timestamp column)
tsItem String the timestamp column name (for optimistic locking)

Return Value

boolean - true if one record was deleted; false if zero records were deleted

delete

public int delete(Connection conn, String tableName, AbstractIoTypeMap params, String[] whereItems)
Deletes multiple records from the specified table.
  • Deletes multiple records from the specified table.
  • A WHERE clause is built using the search condition columns.
  • Search condition columns MUST be included in the parameter values.
  • Search condition column names MUST be specified in lowercase. (key rule of AbstractIoTypeMap)

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name
params AbstractIoTypeMap the parameter values (including search condition columns)
whereItems String[] the search condition column names

Return Value

int - the number of deleted records

deleteAll

public int deleteAll(Connection conn, String tableName)
Deletes all records from the specified table.
  • Deletes all records from the specified table.

Parameters

Name Type Description
conn Connection the database connection
tableName String the table name

Return Value

int - the number of deleted records

executeOne

public boolean executeOne(Connection conn, SqlBean sb)
SQL 1件登録・更新・削除.
  • 反映件数が複数件の場合は例外エラーとする。

Parameters

Name Type Description
conn Connection DB接続
sb SqlBean SQL Bean

Return Value

boolean - 反映件数が1件の場合は true、0件の場合は false

execute

public int execute(Connection conn, SqlBean sb)
SQL 登録・更新・削除.

Parameters

Name Type Description
conn Connection DB接続
sb SqlBean SQL Bean

Return Value

int - 反映件数

getToday

public String getToday(Connection conn)
DBMS別 現在日付取得.

Parameters

Name Type Description
conn Connection DB接続

Return Value

String - 現在日付(YYYYMMDD形式)