public interface SQLDataType extends DataType, AutoCloseable
Modifier and Type | Method and Description |
---|---|
default <T> void |
batchUpdate(String query,
int batchSize,
Collection<T> params,
Function<T,?>... paramMappers)
Executes a batch update on a database, and maps passed parameters based
upon 0-indexed
paramMappers for each parameter. |
boolean |
checkColumn(String tableName,
String columnName)
Checks if a table has the following column
|
boolean |
checkTable(String tableName)
Checks if a table exists within the set database
|
default void |
close() |
default void |
commit()
Pushes any queued transactions to the database
|
Connection |
getConnection()
Returns the
Connection object for ease of use in exposing more
internal API |
default boolean |
isAutoCommit()
Returns whether or not this connection automatically commits changes
to the database.
|
boolean |
isSendingErrorOutput()
Determines whether or not to automatically print errors to the console
|
default <R> SQLResponse<R> |
operate(SQLFunction<? super PreparedStatement,R> oper,
String sql,
Object... params)
Runs a
PreparedStatement using the provided sql parameter. |
default PreparedStatement |
prepare(String stmt)
Returns a
PreparedStatement in which you can easily protect
against SQL injection attacks. |
default SQLResponse |
query(SQLConsumer<? super ResultSet> oper,
String sql,
Object... params)
Executes a query, and applies the resulting
ResultSet to the
passed SQLConsumer |
default <R> SQLResponse<R> |
query(SQLFunction<? super ResultSet,R> oper,
String sql,
Object... params)
Executes a query, and applies the resulting
ResultSet to the
passed SQLFunction . |
default void |
rollback()
Cancel any queued transactions
|
default void |
setAutoCommit(boolean set)
Sets whether or not to automatically commit changes to the database.
|
void |
setErrorOutput(boolean errors)
Sets whether or not to print errors to the console automatically.
|
default SQLResponse |
update(String query,
Object... params)
Executes a query that can change values
|
boolean checkTable(String tableName)
tableName
- Name of the table to check fortrue
if the table existsboolean checkColumn(String tableName, String columnName)
tableName
- The table to look incolumnName
- The column to search fortrue
if the column existsdefault <R> SQLResponse<R> query(SQLFunction<? super ResultSet,R> oper, String sql, Object... params)
ResultSet
to the
passed SQLFunction
. This method will return anything returned
from the lambda bodyR
- The return type from the lambda bodyoper
- The operation to apply to the ResultSet
sql
- The SQL statement to executeparams
- Any PreparedStatement
parametersdefault SQLResponse query(SQLConsumer<? super ResultSet> oper, String sql, Object... params)
ResultSet
to the
passed SQLConsumer
oper
- The operation to apply to the ResultSet
sql
- The SQL statement to executeparams
- Any PreparedStatement
parametersSQLResponse
containing any required informationdefault SQLResponse update(String query, Object... params)
query
- The string query to executeparams
- Any PreparedStatement
parametersdefault <T> void batchUpdate(String query, int batchSize, Collection<T> params, Function<T,?>... paramMappers)
paramMappers
for each parameter. An example of
a usage would be:
//A collection of objects to apply to each batch
Collection<SomeObject> yourCollection;
SQLDataType#batchUpdate("<SQL Query>", 500, yourCollection,
(s) -> s.getName(),
(s) -> s.getID(),
(s) -> s.getLastName());
);
PreparedStatement
parameters would
map each object so that "Parameter 1" would be the result of
SomeObject#getName
, and parameter 2 would be the result of
SomeObject#getID
, and so onT
- The type of the objects being batch committedquery
- The SQL query to executebatchSize
- The size of each batchparams
- The objects to use in each batchparamMappers
- A series of functions for mapping objects to paramsdefault PreparedStatement prepare(String stmt) throws SQLException
PreparedStatement
in which you can easily protect
against SQL injection attacks.stmt
- The string to preparePreparedStatement
from the passed stringSQLException
- The connection cannot be establisheddefault boolean isAutoCommit() throws SQLException
SQLException
- database access error or closed connectiondefault void setAutoCommit(boolean set)
commit()
or
rollback()
.set
- true
to enable, false
to disabledefault void commit() throws SQLException
SQLException
- The connection cannot be established, an access
error occurred, or auto-commit is enableddefault void rollback() throws SQLException
SQLException
- The connection cannot be established, an access
error occurred, or auto-commit is enableddefault <R> SQLResponse<R> operate(SQLFunction<? super PreparedStatement,R> oper, String sql, Object... params)
PreparedStatement
using the provided sql
parameter.
The following SQLFunction
will then be run using this constructed
statement. This is typically more-so for use in one-time executed
statementsR
- The type of the return valueoper
- The SQLFunction
operation to usesql
- The SQL statement to executeparams
- Parameters to pass to the PreparedStatement
SQLFunction
Connection getConnection()
Connection
object for ease of use in exposing more
internal APIConnection
object in use by this SQLDataType
default void close()
close
in interface AutoCloseable
void setErrorOutput(boolean errors)
true
errors
- false
to disable error outputboolean isSendingErrorOutput()
true
if content is printed to the console.Copyright © 2015. All rights reserved.