Package org.sql2o
Class Query
java.lang.Object
org.sql2o.Query
- All Implemented Interfaces:
AutoCloseable
Represents a sql2o statement. With sql2o, all statements are instances of the Query class.
-
Constructor Summary
ConstructorDescriptionQuery
(Connection connection, String queryText, boolean returnGeneratedKeys) Query
(Connection connection, String queryText, String[] columnNames) -
Method Summary
Modifier and TypeMethodDescriptionaddColumnMapping
(String columnName, String propertyName) addParameter
(String name, boolean value) addParameter
(String name, int value) addParameter
(String name, long value) addParameter
(String name, InputStream value) addParameter
(String name, Boolean value) <T> Query
addParameter
(String name, Class<T> parameterClass, T value) addParameter
(String name, Integer value) addParameter
(String name, Long value) addParameter
(String name, Object value) addParameter
(String name, Object... values) Set an array parameter.
For example:addParameter
(String name, String value) addParameter
(String name, Time value) addParameter
(String name, Timestamp value) addParameter
(String name, Collection<?> values) Set an array parameter.
SeeaddParameter(String, Object...)
for detailsaddParameter
(String name, UUID value) Adds a set of parameters to thisQuery
object's batch of commands.<A> List
<A> addToBatchGetKeys
(Class<A> klass) Adds a set of parameters to thisQuery
object's batch of commands and returns any generated keys.void
close()
<T> List
<T> executeAndFetch
(Class<T> returnType) <T> List
<T> executeAndFetch
(ResultSetHandler<T> resultSetHandler) <T> List
<T> executeAndFetch
(ResultSetHandlerFactory<T> factory) <T> T
executeAndFetchFirst
(Class<T> returnType) <T> T
executeAndFetchFirst
(ResultSetHandler<T> resultSetHandler) <T> T
executeAndFetchFirst
(ResultSetHandlerFactory<T> resultSetHandlerFactory) <T> ResultSetIterable
<T> executeAndFetchLazy
(Class<T> returnType) Read a collection lazily.<T> ResultSetIterable
<T> executeAndFetchLazy
(ResultSetHandler<T> resultSetHandler) Read a collection lazily.<T> ResultSetIterable
<T> executeAndFetchLazy
(ResultSetHandlerFactory<T> resultSetHandlerFactory) Read a collection lazily.<T> T
executeAndFetchUnique
(Class<T> returnType) <T> T
executeAndFetchUnique
(ResultSetHandler<T> resultSetHandler) <T> T
executeAndFetchUnique
(ResultSetHandlerFactory<T> factory) <V> V
executeScalar
(Class<V> returnType) <V> V
executeScalar
(Converter<V> converter) <T> List
<T> executeScalarList
(Class<T> returnType) column mappingint
int
getName()
boolean
boolean
boolean
boolean
setAutoDeriveColumnNames
(boolean autoDeriveColumnNames) setCaseSensitive
(boolean caseSensitive) setColumnMappings
(Map<String, String> mappings) setMaxBatchRecords
(int maxBatchRecords) Sets the number of batched commands this Query allows to be added before implicitly callingexecuteBatch()
fromaddToBatch()
.void
setResultSetHandlerFactoryBuilder
(ResultSetHandlerFactoryBuilder resultSetHandlerFactoryBuilder) throwOnMappingFailure
(boolean throwOnMappingFailure) toString()
withParams
(Object... paramValues)
-
Constructor Details
-
Query
-
Query
-
-
Method Details
-
toString
-
isCaseSensitive
public boolean isCaseSensitive() -
setCaseSensitive
-
isAutoDeriveColumnNames
public boolean isAutoDeriveColumnNames() -
setAutoDeriveColumnNames
-
throwOnMappingFailure
-
isThrowOnMappingFailure
public boolean isThrowOnMappingFailure() -
getConnection
-
getName
-
setName
-
getResultSetHandlerFactoryBuilder
-
setResultSetHandlerFactoryBuilder
public void setResultSetHandlerFactoryBuilder(ResultSetHandlerFactoryBuilder resultSetHandlerFactoryBuilder) -
getParamNameToIdxMap
-
addParameter
-
withParams
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
-
addParameter
Set an array parameter.
For example:createQuery("SELECT * FROM user WHERE id IN(:ids)") .addParameter("ids", 4, 5, 6) .executeAndFetch(...)
will generate the query :SELECT * FROM user WHERE id IN(4,5,6)
It is not possible to use array parameters with a batchPreparedStatement
: since the text query passed to thePreparedStatement
depends on the number of parameters in the array, array parameters are incompatible with batch mode.
If the values array is empty,null
will be set to the array parameter:SELECT * FROM user WHERE id IN(NULL)
- Throws:
NullPointerException
- if values parameter is null
-
addParameter
Set an array parameter.
SeeaddParameter(String, Object...)
for details -
bind
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
executeAndFetchLazy
Read a collection lazily. Generally speaking, this should only be used if you are reading MANY results and keeping them all in a Collection would cause memory issues. You MUST callResultSetIterable.close()
when you are done iterating.- Parameters:
returnType
- type of each row- Returns:
- iterable results
-
executeAndFetchLazy
public <T> ResultSetIterable<T> executeAndFetchLazy(ResultSetHandlerFactory<T> resultSetHandlerFactory) Read a collection lazily. Generally speaking, this should only be used if you are reading MANY results and keeping them all in a Collection would cause memory issues. You MUST callResultSetIterable.close()
when you are done iterating.- Parameters:
resultSetHandlerFactory
- factory to provide ResultSetHandler- Returns:
- iterable results
-
executeAndFetchLazy
Read a collection lazily. Generally speaking, this should only be used if you are reading MANY results and keeping them all in a Collection would cause memory issues. You MUST callResultSetIterable.close()
when you are done iterating.- Parameters:
resultSetHandler
- ResultSetHandler- Returns:
- iterable results
-
executeAndFetchUnique
-
executeAndFetchUnique
-
executeAndFetchUnique
-
executeAndFetch
-
executeAndFetch
-
executeAndFetch
-
executeAndFetchFirst
-
executeAndFetchFirst
-
executeAndFetchFirst
-
executeAndFetchTableLazy
-
executeAndFetchTable
-
executeUpdate
-
executeScalar
-
executeScalar
-
executeScalar
-
executeScalarList
-
setMaxBatchRecords
Sets the number of batched commands this Query allows to be added before implicitly callingexecuteBatch()
fromaddToBatch()
.
When set to 0, executeBatch is not called implicitly. This is the default behaviour.
When using this, please take care about callingexecuteBatch()
after finished adding all commands to the batch because commands may remain unexecuted after the lastaddToBatch()
call. Additionally, if fetchGeneratedKeys is set, then previously generated keys will be lost after a batch is executed.- Throws:
IllegalArgumentException
- Thrown if the value is negative.
-
getMaxBatchRecords
public int getMaxBatchRecords() -
getCurrentBatchRecords
public int getCurrentBatchRecords()- Returns:
- The current number of unexecuted batched statements
-
isExplicitExecuteBatchRequired
public boolean isExplicitExecuteBatchRequired()- Returns:
- True if maxBatchRecords is set and there are unexecuted batched commands or maxBatchRecords is not set
-
addToBatch
Adds a set of parameters to thisQuery
object's batch of commands.
If maxBatchRecords is more than 0, executeBatch is called upon adding that many commands to the batch.
The current number of batched commands is accessible via thegetCurrentBatchRecords()
method. -
addToBatchGetKeys
Adds a set of parameters to thisQuery
object's batch of commands and returns any generated keys.
If maxBatchRecords is more than 0, executeBatch is called upon adding that many commands to the batch. This method will return any generated keys iffetchGeneratedKeys
is set.
The current number of batched commands is accessible via thegetCurrentBatchRecords()
method. -
executeBatch
- Throws:
Sql2oException
-
getColumnMappings
column mapping -
setColumnMappings
-
addColumnMapping
-