java.lang.Object
org.seasar.doma.jdbc.builder.SelectBuilder
A builder for an SQL SELECT statement.
This is not thread safe.
Java
SelectBuilder builder = SelectBuilder.newInstance(config);
builder.sql("select");
builder.sql("id").sql(",");
builder.sql("name").sql(",");
builder.sql("salary");
builder.sql("from Emp");
builder.sql("where");
builder.sql("name like ").param(String.class, "S%");
builder.sql("and");
builder.sql("age > ").param(int.class, 20);
Emp emp = builder.getEntitySingleResult(Emp.class);
built SQL
select id, name, salary from Emp where name like 'S%' and age > 20
-
Method Summary
Modifier and TypeMethodDescriptionvoidcallerClassName(String className) Sets the caller class name.voidcallerMethodName(String methodName) Sets the caller method name.voidensureResult(boolean ensureResult) Whether to ensure that one or more rows are found in a result set.voidensureResultMapping(boolean ensureResultMapping) Whether to ensure that all entity properties are mapped to columns of a result set.voidSet the fetch type.voidfetchSize(int fetchSize) Sets the fetch size.<ELEMENT> List<ELEMENT>getEntityResultList(Class<ELEMENT> elementClass) Executes an SQL SELECT statement and returns the entity results.<RESULT> RESULTgetEntitySingleResult(Class<RESULT> resultClass) Executes an SQL SELECT statement and returns a single entity result.getMapResultList(MapKeyNamingType mapKeyNamingType) Executes an SQL SELECT statement and returns the map results.getMapSingleResult(MapKeyNamingType mapKeyNamingType) Executes an SQL SELECT statement and returns a single map result.<RESULT> Optional<RESULT>getOptionalEntitySingleResult(Class<RESULT> resultClass) Executes an SQL SELECT statement and returns a single entity result asOptional.getOptionalMapSingleResult(MapKeyNamingType mapKeyNamingType) Executes an SQL SELECT statement and returns a single map result asOptional.getOptionalScalarResultList(Class<ELEMENT> elementClass) Executes an SQL SELECT statement and returns the scalar results that are wrapped withOptional.<RESULT> Optional<RESULT>getOptionalScalarSingleResult(Class<RESULT> resultClass) Executes an SQL SELECT statement and returns a single scalar result asOptional.<ELEMENT> List<ELEMENT>getScalarResultList(Class<ELEMENT> elementClass) Executes an SQL SELECT statement and returns the scalar results.<RESULT> RESULTgetScalarSingleResult(Class<RESULT> resultClass) Executes an SQL SELECT statement and returns a single scalar result.Sql<?>getSql()Returns the built SQL.<P> SelectBuilderAppends a parameter as literal.<E> SelectBuilderAppends a parameter list as literal.voidmaxRows(int maxRows) Sets the maximum number of rows for aResultSetobject.static SelectBuildernewInstance(Config config) Creates a new instance.voidoptions(SelectOptions options) Sets the options about the SQL SELECT execution.<P> SelectBuilderAppends a parameter.<E> SelectBuilderAppends a parameter list.voidqueryTimeout(int queryTimeout) Sets the query timeout limit in seconds.Removes the last SQL fragment or parameter.Appends an SQL fragment.voidsqlLogType(SqlLogType sqlLogType) Sets the SQL log format.<TARGET> Stream<TARGET>streamEntity(Class<TARGET> targetClass) Executes an SQL SELECT statement and returns the stream of entity results.<TARGET,RESULT>
RESULTstreamEntity(Class<TARGET> targetClass, Function<Stream<TARGET>, RESULT> mapper) Executes an SQL SELECT statement, handles the stream of entity values and returns the result.protected <TARGET,RESULT>
RESULTstreamEntityInternal(Class<TARGET> targetClass, Function<Stream<TARGET>, RESULT> mapper) streamMap(MapKeyNamingType mapKeyNamingType) Executes an SQL SELECT statement and returns the stream of map results.<RESULT> RESULTExecutes an SQL SELECT statement, handles the stream of map values and returns the result.protected <RESULT> RESULTstreamMapInternal(MapKeyNamingType mapKeyNamingType, Function<Stream<Map<String, Object>>, RESULT> mapper) streamOptionalScalar(Class<TARGET> targetClass) Executes an SQL SELECT statement and returns the stream ofOptionalscalar results.<RESULT,TARGET>
RESULTstreamOptionalScalar(Class<TARGET> targetClass, Function<Stream<Optional<TARGET>>, RESULT> mapper) Executes an SQL SELECT statement, handles the stream ofOptionalscalar values and returns the result.protected <RESULT,TARGET>
RESULTstreamOptionalScalarInternal(Class<TARGET> targetClass, Function<Stream<Optional<TARGET>>, RESULT> mapper) <TARGET> Stream<TARGET>streamScalar(Class<TARGET> targetClass) Executes an SQL SELECT statement and returns the stream of scalar results.<RESULT,TARGET>
RESULTstreamScalar(Class<TARGET> targetClass, Function<Stream<TARGET>, RESULT> mapper) Executes an SQL SELECT statement, handles the stream of scalar values and returns the result.protected <RESULT,TARGET>
RESULTstreamScalarInternal(Class<TARGET> targetClass, Function<Stream<TARGET>, RESULT> mapper)
-
Method Details
-
newInstance
Creates a new instance.- Parameters:
config- the configuration- Returns:
- a builder
- Throws:
DomaNullPointerException- ifconfigisnull
-
sql
Appends an SQL fragment.- Parameters:
sql- the SQL fragment- Returns:
- a builder
- Throws:
DomaNullPointerException- ifsqlisnull
-
removeLast
Removes the last SQL fragment or parameter.- Returns:
- a builder
-
param
Appends a parameter.The parameter type must be one of basic types or holder types.
- Type Parameters:
P- the parameter type- Parameters:
paramClass- the parameter classparam- the parameter- Returns:
- a builder
- Throws:
DomaNullPointerException- ifparamClassisnull
-
params
Appends a parameter list.The element type of the list must be one of basic types or holder types.
- Type Parameters:
E- the element type of the list- Parameters:
elementClass- the element class of the listparams- the parameter list- Returns:
- a builder
- Throws:
DomaNullPointerException- ifelementClassorparamsisnull
-
literal
Appends a parameter as literal.The parameter type must be one of basic types or holder types.
- Type Parameters:
P- the parameter type- Parameters:
paramClass- the parameter classparam- the parameter- Returns:
- a builder
- Throws:
DomaNullPointerException- ifparamClassisnull
-
literals
Appends a parameter list as literal.The element type of the list must be one of basic types or holder types.
- Type Parameters:
E- the element type of the list- Parameters:
elementClass- the element class of the listparams- the parameter list- Returns:
- a builder
- Throws:
DomaNullPointerException- ifelementClassorparamsisnull
-
getEntitySingleResult
Executes an SQL SELECT statement and returns a single entity result.- Type Parameters:
RESULT- the entity type- Parameters:
resultClass- the entity class- Returns:
- a single entity or
nullif the result is none - Throws:
DomaNullPointerException- ifresultClassisnullDomaIllegalArgumentException- ifresultClassis not entity classUnknownColumnException- if there is the column that is unknown to the entityNoResultException- if you settruetoensureResult(boolean)and the result is noneResultMappingException- if you settruetoensureResultMapping(boolean)and all properties in the entity are not mapped to columns in a result setNonUniqueResultException- if the number of fetched rows is greater than or equal to 2JdbcException- if a JDBC related error occurs
-
getOptionalEntitySingleResult
Executes an SQL SELECT statement and returns a single entity result asOptional.- Type Parameters:
RESULT- the entity type- Parameters:
resultClass- the entity class- Returns:
- a single entity
- Throws:
DomaNullPointerException- ifresultClassisnullDomaIllegalArgumentException- ifresultClassis not entity classUnknownColumnException- if there is the column that is unknown to the entityNoResultException- if you settruetoensureResult(boolean)and the result is noneResultMappingException- if you settruetoensureResultMapping(boolean)and all properties in the entity are not mapped to columns in a result setNonUniqueResultException- if the number of fetched rows is greater than or equal to 2JdbcException- if a JDBC related error occurs
-
getScalarSingleResult
Executes an SQL SELECT statement and returns a single scalar result.- Type Parameters:
RESULT- the basic type or the holder type- Parameters:
resultClass- the basic class or the holder class- Returns:
- a single scalar or
nullif the result is none - Throws:
DomaNullPointerException- ifresultClassisnullDomaIllegalArgumentException- ifresultClassis not entity classNonSingleColumnException- if there are multiple columns in a result setNoResultException- if you settruetoensureResult(boolean)and the result is noneNonUniqueResultException- if the number of fetched rows is greater than or equal to 2JdbcException- if a JDBC related error occurs
-
getOptionalScalarSingleResult
Executes an SQL SELECT statement and returns a single scalar result asOptional.- Type Parameters:
RESULT- the basic type or the holder type- Parameters:
resultClass- the basic class or the holder class- Returns:
- a single scalar
- Throws:
DomaNullPointerException- ifresultClassisnullDomaIllegalArgumentException- ifresultClassis neither the basic class nor the holder classNonSingleColumnException- if there are multiple columns in a result setNoResultException- if you settruetoensureResult(boolean)and the result is noneNonUniqueResultException- if the number of fetched rows is greater than or equal to 2JdbcException- if a JDBC related error occurs
-
getMapSingleResult
Executes an SQL SELECT statement and returns a single map result.- Parameters:
mapKeyNamingType- a naming convention for the keys of the map- Returns:
- a single map or
nullif the result is none - Throws:
DomaNullPointerException- ifmapKeyNamingTypeisnullNoResultException- if you settruetoensureResult(boolean)and the result is noneNonUniqueResultException- if the number of fetched rows is greater than or equal to 2JdbcException- if a JDBC related error occurs
-
getOptionalMapSingleResult
Executes an SQL SELECT statement and returns a single map result asOptional.- Parameters:
mapKeyNamingType- a naming convention for the keys of the map- Returns:
- a single map
- Throws:
DomaNullPointerException- ifmapKeyNamingTypeisnullNoResultException- if you settruetoensureResult(boolean)and the result is noneNonUniqueResultException- if the number of fetched rows is greater than or equal to 2JdbcException- if a JDBC related error occurs
-
getEntityResultList
Executes an SQL SELECT statement and returns the entity results.- Type Parameters:
ELEMENT- the entity type- Parameters:
elementClass- the entity class- Returns:
- the entity results
- Throws:
DomaNullPointerException- ifelementClassisnullDomaIllegalArgumentException- ifelementClassis not entity classUnknownColumnException- if there is the column that is unknown to the entityNoResultException- if you settruetoensureResult(boolean)and the result is noneResultMappingException- if you settruetoensureResultMapping(boolean)and all properties in the entity are not mapped to columns in a result setJdbcException- if a JDBC related error occurs
-
getScalarResultList
Executes an SQL SELECT statement and returns the scalar results.- Type Parameters:
ELEMENT- the basic type or the holder type- Parameters:
elementClass- the basic class or the holder class- Returns:
- the scalar results
- Throws:
DomaNullPointerException- ifelementClassisnullDomaIllegalArgumentException- ifelementClassis neither the basic class nor the holder classUnknownColumnException- if there is the column that is unknown to the entityNoResultException- if you settruetoensureResult(boolean)and the result is noneResultMappingException- if you settruetoensureResultMapping(boolean)and all properties in the entity are not mapped to columns in a result setJdbcException- if a JDBC related error occurs
-
getOptionalScalarResultList
Executes an SQL SELECT statement and returns the scalar results that are wrapped withOptional.- Type Parameters:
ELEMENT- the basic type or the holder type- Parameters:
elementClass- the basic class or the holder class- Returns:
- the scalar results
- Throws:
DomaNullPointerException- ifelementClassisnullDomaIllegalArgumentException- ifelementClassis neither the basic class nor the holder classUnknownColumnException- if there is the column that is unknown to the entityNoResultException- if you settruetoensureResult(boolean)and the result is noneResultMappingException- if you settruetoensureResultMapping(boolean)and all properties in the entity are not mapped to columns in a result setJdbcException- if a JDBC related error occurs
-
getMapResultList
Executes an SQL SELECT statement and returns the map results.- Parameters:
mapKeyNamingType- a naming convention for the keys of the map- Returns:
- the map results
- Throws:
DomaNullPointerException- ifmapKeyNamingTypeisnullJdbcException- if a JDBC related error occurs
-
streamEntity
Executes an SQL SELECT statement and returns the stream of entity results.The caller must close the stream.
- Type Parameters:
TARGET- the entity type- Parameters:
targetClass- the entity class- Returns:
- the stream of the entity results
- Throws:
DomaNullPointerException- iftargetClassisnullDomaIllegalArgumentException- iftargetClassis not entity classJdbcException- if a JDBC related error occurs
-
streamEntity
public <TARGET,RESULT> RESULT streamEntity(Class<TARGET> targetClass, Function<Stream<TARGET>, RESULT> mapper) Executes an SQL SELECT statement, handles the stream of entity values and returns the result.- Type Parameters:
RESULT- the result typeTARGET- the entity type- Parameters:
targetClass- the entity classmapper- the mapper function- Returns:
- the result
- Throws:
DomaNullPointerException- iftargetClassormapperisnullDomaIllegalArgumentException- iftargetClassis not entity classUnknownColumnException- if there is the column that is unknown to the entityNoResultException- if you settruetoensureResult(boolean)and the result is noneResultMappingException- if you settruetoensureResultMapping(boolean)and all properties in the entity are not mapped to columns in a result setJdbcException- if a JDBC related error occurs
-
streamEntityInternal
-
streamScalar
Executes an SQL SELECT statement and returns the stream of scalar results.The caller must close the stream.
- Type Parameters:
TARGET- the basic type or the holder type- Parameters:
targetClass- the basic class or the holder class- Returns:
- the stream of scalar results
- Throws:
DomaNullPointerException- iftargetClassisnullDomaIllegalArgumentException- iftargetClassis neither the basic class nor the holder classJdbcException- if a JDBC related error occurs
-
streamScalar
public <RESULT,TARGET> RESULT streamScalar(Class<TARGET> targetClass, Function<Stream<TARGET>, RESULT> mapper) Executes an SQL SELECT statement, handles the stream of scalar values and returns the result.- Type Parameters:
RESULT- the result typeTARGET- the basic type or the holder type- Parameters:
targetClass- the basic class or the holder classmapper- the mapper function- Returns:
- the result
- Throws:
DomaNullPointerException- iftargetClassormapperisnullDomaIllegalArgumentException- iftargetClassis neither the basic class nor the holder classNonSingleColumnException- if there are multiple columns in a result setNoResultException- if you settruetoensureResult(boolean)and the result is noneJdbcException- if a JDBC related error occurs
-
streamScalarInternal
-
streamOptionalScalar
Executes an SQL SELECT statement and returns the stream ofOptionalscalar results.The caller must close the stream.
- Type Parameters:
TARGET- the basic type or the holder type- Parameters:
targetClass- the basic class or the holder class- Returns:
- the stream of
Optionalscalar results - Throws:
DomaNullPointerException- iftargetClassisnullDomaIllegalArgumentException- iftargetClassis neither the basic class nor the holder classJdbcException- if a JDBC related error occurs
-
streamOptionalScalar
public <RESULT,TARGET> RESULT streamOptionalScalar(Class<TARGET> targetClass, Function<Stream<Optional<TARGET>>, RESULT> mapper) Executes an SQL SELECT statement, handles the stream ofOptionalscalar values and returns the result.- Type Parameters:
RESULT- the result typeTARGET- the basic type or the holder type- Parameters:
targetClass- the basic class or the holder classmapper- the mapper function- Returns:
- the result
- Throws:
DomaNullPointerException- iftargetClassormapperisnullDomaIllegalArgumentException- iftargetClassis neither the basic class nor the holder classNonSingleColumnException- if there are multiple columns in a result setNoResultException- if you settruetoensureResult(boolean)and the result is noneJdbcException- if a JDBC related error occurs
-
streamOptionalScalarInternal
-
streamMap
Executes an SQL SELECT statement and returns the stream of map results.The caller must close the stream.
- Parameters:
mapKeyNamingType- a naming convention for the keys of the map- Returns:
- the map results
- Throws:
DomaNullPointerException- ifmapKeyNamingTypeisnullJdbcException- if a JDBC related error occurs
-
streamMap
public <RESULT> RESULT streamMap(MapKeyNamingType mapKeyNamingType, Function<Stream<Map<String, Object>>, RESULT> mapper) Executes an SQL SELECT statement, handles the stream of map values and returns the result.- Type Parameters:
RESULT- the result type- Parameters:
mapKeyNamingType- a naming convention for the keys of the mapmapper- the mapper function- Returns:
- the result
- Throws:
DomaNullPointerException- ifmapKeyNamingTypeormapperisnullJdbcException- if a JDBC related error occurs
-
streamMapInternal
-
ensureResult
public void ensureResult(boolean ensureResult) Whether to ensure that one or more rows are found in a result set.- Parameters:
ensureResult- whether to ensure
-
ensureResultMapping
public void ensureResultMapping(boolean ensureResultMapping) Whether to ensure that all entity properties are mapped to columns of a result set.- Parameters:
ensureResultMapping- whether to ensure
-
fetch
Set the fetch type.- Parameters:
fetchType- the fetch type
-
fetchSize
public void fetchSize(int fetchSize) Sets the fetch size.If not specified, the value of
Config.getFetchSize()is used.- Parameters:
fetchSize- the fetch size- See Also:
-
maxRows
public void maxRows(int maxRows) Sets the maximum number of rows for aResultSetobject.If not specified, the value of
Config.getMaxRows()is used.- Parameters:
maxRows- the maximum number of rows- See Also:
-
queryTimeout
public void queryTimeout(int queryTimeout) Sets the query timeout limit in seconds.If not specified, the value of
Config.getQueryTimeout()is used.- Parameters:
queryTimeout- the query timeout limit in seconds- See Also:
-
sqlLogType
Sets the SQL log format.- Parameters:
sqlLogType- the SQL log format type
-
callerClassName
Sets the caller class name.If not specified, the class name of this instance is used.
- Parameters:
className- the caller class name- Throws:
DomaNullPointerException- ifclassNameisnull
-
callerMethodName
Sets the caller method name.- Parameters:
methodName- the caller method name- Throws:
DomaNullPointerException- ifmethodNameisnull
-
options
Sets the options about the SQL SELECT execution.- Parameters:
options- the options about the SQL SELECT execution- Throws:
DomaNullPointerException- ifoptionsisnull
-
getSql
Returns the built SQL.- Returns:
- the built SQL
-