Class QueryBuilder<T>
- Type Parameters:
T- type of query return type
- All Implemented Interfaces:
DataSourceProvider,ConfigurationStage<T>,InsertStage,QueryStage<T>,ResultStage<T>,RetrievalStage<T>,StatementStage<T>,UpdateStage
The query builder is a stage organized object. Every call will be invoked on a stage of the same query builder.
You may execute as many updates as you want. You may only get the returned results of one query, which must be the last.
All methods which are not labeled with a Sync suffix will be executed in an async context. Async method will
provide a callback via a CompletableFuture.
All queries will be executed in an atomic transaction. This means that data will be only written if all queries are executed successfully.
This behaviour can be changed by calling QueryBuilderConfig.Builder.notAtomic() ()} on config creation.
Any exception thrown while executing queries will be wrapped into an QueryExecutionException. This exception
was created on query submission to the query builder. Not that this is not the execution, which may be called on
another thread. This exception will help you to trace back async calls.
Any SQLException thrown inside the query builder will not be thrown but logged by default.
-
Method Summary
Modifier and TypeMethodDescriptionall()Retrieve all results async as a listRetrieve all results async as a listallSync()Retrieve all results synced as a listappend()Append another query to the query builder.static ConfigurationStage<?>builder(DataSource source) Create a new Query builder without a defined return type.static <T> ConfigurationStage<T>builder(DataSource source, Class<T> clazz) Create a new query builder with a defined return type.configure(AtomicReference<QueryBuilderConfig> config) Configure the query builder.Set the settings to default values.execute()Execute the update async.Execute the update async.intExecute the update synced.first()Retrieve the first result from the results set asyncRetrieve the first result from the results set asyncRetrieve the first result from the results set syncedinsert()Insert data into a table.key()Retrieve the first created key asyncRetrieve the first created key asynckeys()Retrieve all created keys async as a listRetrieve all created keys async as a listkeysSync()Retrieve all created keys as a listkeySync()Retrieve the first created key syncedvoidLogs the exception with a default message.Set the parameter of thePreparedStatementof the query.Set the parameter of thePreparedStatementof the query.Set the query to execute.queryWithoutParams(String query) Set the query to execute.readRow(ThrowingFunction<T, ResultSet, SQLException> mapper) Extract results from a results set.send()Executes the update async.Executes the update async.sendSync()Executes the update.update()Mark this query as update query.Methods inherited from class de.chojo.sqlutil.base.DataHolder
getConnection, logDbError, queryBuilder, setupLogger, sourceMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface de.chojo.sqlutil.wrapper.stage.ConfigurationStage
configureMethods inherited from interface de.chojo.sqlutil.wrapper.stage.QueryStage
query, queryWithoutParamsMethods inherited from interface de.chojo.sqlutil.wrapper.stage.ResultStage
deleteMethods inherited from interface de.chojo.sqlutil.wrapper.stage.StatementStage
emptyParams, paramsBuilder
-
Method Details
-
builder
Create a new query builder with a defined return type. Use it for selects.- Type Parameters:
T- type of return type- Parameters:
source- datasource for connectionclazz- class of required return type. Doesnt matter if you want a list or single result.- Returns:
- a new query builder in a
QueryStage
-
builder
Create a new Query builder without a defined return type. Use it for updates.- Parameters:
source- datasource for connection- Returns:
- a new query builder in a
QueryStage
-
configure
Description copied from interface:ConfigurationStageConfigure the query builder.A configured
QueryBuilderFactorycan be used to skip this step.- Specified by:
configurein interfaceConfigurationStage<T>- Parameters:
config- The config of theQueryBuilder- Returns:
- The
QueryBuilderinQueryStagewith the config set.
-
defaultConfig
Description copied from interface:ConfigurationStageSet the settings to default values.Default will be atomic transactions and not throwing any exception.
A configured
QueryBuilderFactorycan be used to skip this step.- Specified by:
defaultConfigin interfaceConfigurationStage<T>- Returns:
- The
QueryBuilderin
-
query
Description copied from interface:QueryStageSet the query to execute.- Specified by:
queryin interfaceQueryStage<T>- Parameters:
query- query to set.- Returns:
- The
QueryBuilderin aStatementStagewith the query defined.
-
queryWithoutParams
Description copied from interface:QueryStageSet the query to execute.This will also skip the statement stage.
- Specified by:
queryWithoutParamsin interfaceQueryStage<T>- Parameters:
query- query to set.- Returns:
- The
QueryBuilderin aResultStagewith the query defined and no parameter set.
-
params
Description copied from interface:StatementStageSet the parameter of thePreparedStatementof the query.- Specified by:
paramsin interfaceStatementStage<T>- Parameters:
stmt- statement to change- Returns:
- The
QueryBuilderin aResultStagewith the parameters applied to the query.
-
parameter
Description copied from interface:StatementStageSet the parameter of thePreparedStatementof the query.Use the query builder so set the parameters in the defined order.
stmt -> stmt.setString("value").setInt(1)- Specified by:
parameterin interfaceStatementStage<T>- Parameters:
params- a consumer of a param builder used for simple setting of params.- Returns:
- The
QueryBuilderin aResultStagewith the parameters applied to the query.
-
readRow
Description copied from interface:ResultStageExtract results from a results set.This function should not loop through the results set.
It should only transform the current row to the requested object.
- Specified by:
readRowin interfaceResultStage<T>- Parameters:
mapper- mapper to map the current row.- Returns:
- The
QueryBuilderin aRetrievalStageto retrieve the row/s.
-
update
Description copied from interface:ResultStageMark this query as update query.- Specified by:
updatein interfaceResultStage<T>- Returns:
- The
QueryBuilderin aUpdateStageto update the data.
-
insert
Description copied from interface:ResultStageInsert data into a table. Alias forResultStage.update()- Specified by:
insertin interfaceResultStage<T>- Returns:
- The
QueryBuilderin aInsertStageto update the data.
-
append
Description copied from interface:ResultStageAppend another query to the query builder.- Specified by:
appendin interfaceResultStage<T>- Returns:
- The
QueryBuilderin aQueryStage
-
all
Description copied from interface:RetrievalStageRetrieve all results async as a list- Specified by:
allin interfaceRetrievalStage<T>- Returns:
- A
CompletableFutureto retrieve the data.
-
all
Description copied from interface:RetrievalStageRetrieve all results async as a list- Specified by:
allin interfaceRetrievalStage<T>- Parameters:
executor- the executor used for async call- Returns:
- A
CompletableFutureto retrieve the data.
-
allSync
Description copied from interface:RetrievalStageRetrieve all results synced as a list- Specified by:
allSyncin interfaceRetrievalStage<T>- Returns:
- results as list
-
keys
Description copied from interface:InsertStageRetrieve all created keys async as a list- Specified by:
keysin interfaceInsertStage- Returns:
- A
CompletableFutureto retrieve the data.
-
keys
Description copied from interface:InsertStageRetrieve all created keys async as a list- Specified by:
keysin interfaceInsertStage- Parameters:
executor- the executor used for async call- Returns:
- A
CompletableFutureto retrieve the data.
-
keysSync
Description copied from interface:RetrievalStageRetrieve all created keys as a list- Specified by:
keysSyncin interfaceInsertStage- Specified by:
keysSyncin interfaceRetrievalStage<T>- Returns:
- results as list
-
logDbError
Description copied from class:DataHolderLogs the exception with a default message.- Overrides:
logDbErrorin classDataHolder- Parameters:
e- exception
-
first
Description copied from interface:RetrievalStageRetrieve the first result from the results set async- Specified by:
firstin interfaceRetrievalStage<T>- Returns:
- A
CompletableFutureto retrieve the data.
-
first
Description copied from interface:RetrievalStageRetrieve the first result from the results set async- Specified by:
firstin interfaceRetrievalStage<T>- Parameters:
executor- the executor used for async call- Returns:
- A
CompletableFutureto retrieve the data.
-
firstSync
Description copied from interface:RetrievalStageRetrieve the first result from the results set synced- Specified by:
firstSyncin interfaceRetrievalStage<T>- Returns:
- result wrapped into an optional
-
key
Description copied from interface:InsertStageRetrieve the first created key async- Specified by:
keyin interfaceInsertStage- Returns:
- A
CompletableFutureto retrieve the data.
-
key
Description copied from interface:InsertStageRetrieve the first created key async- Specified by:
keyin interfaceInsertStage- Parameters:
executor- the executor used for async call- Returns:
- A
CompletableFutureto retrieve the data.
-
keySync
Description copied from interface:InsertStageRetrieve the first created key synced- Specified by:
keySyncin interfaceInsertStage- Returns:
- result wrapped into an optional
-
sendSync
Description copied from interface:UpdateStageExecutes the update.- Specified by:
sendSyncin interfaceUpdateStage- Returns:
- A
CompletableFuturewhich returns the number of changed rows.
-
send
Description copied from interface:UpdateStageExecutes the update async.- Specified by:
sendin interfaceUpdateStage- Returns:
- A
CompletableFuturewhich returns the number of changed rows.
-
send
Description copied from interface:UpdateStageExecutes the update async.- Specified by:
sendin interfaceUpdateStage- Parameters:
executor- executor used for async call- Returns:
- A
CompletableFuturewhich returns the number of changed rows.
-
execute
Description copied from interface:UpdateStageExecute the update async.- Specified by:
executein interfaceUpdateStage- Returns:
- A
CompletableFuturewhich returns the number of changed rows.
-
execute
Description copied from interface:UpdateStageExecute the update async.- Specified by:
executein interfaceUpdateStage- Parameters:
executor- executor used for async call- Returns:
- A
CompletableFuturewhich returns the number of changed rows.
-
executeSync
public int executeSync()Description copied from interface:UpdateStageExecute the update synced.- Specified by:
executeSyncin interfaceUpdateStage- Returns:
- Number of changed rows
-