class DBHelper
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private boolean |
connected
Flag indicating the database is connected.
|
private java.sql.Connection |
connection
Database connection
|
private static java.lang.String |
dbPass
Default database password.
|
private java.lang.String |
dbURL
Database URL
|
private static java.lang.String |
dbUser
Default database user.
|
private boolean |
inPersist
Flag indicating if we are persisting the collections.
|
private java.util.Map<java.lang.String,java.sql.PreparedStatement> |
statements
A cache of prepared statements.
|
private java.util.Map<java.lang.String,StoredCollection> |
stores
Cache of db-backed collections.
|
Constructor and Description |
---|
DBHelper() |
Modifier and Type | Method and Description |
---|---|
void |
connect()
Connect to the database.
|
boolean |
connected()
Check if the database is connected.
|
java.lang.String |
convertToSql(java.lang.Class<?> clazz)
Convert the specified type to SQL.
|
private java.sql.PreparedStatement |
createStatement(java.lang.String sql)
Prepare the SQL statement and cache it.
|
void |
disconnect()
Disconnect the database.
|
int |
executeCount(java.lang.String sql,
java.lang.Object... args)
Execute an SQL which selects a single integer result.
|
void |
executeQuery(java.lang.String sql,
java.util.function.Consumer<java.sql.ResultSet> c,
java.lang.Object... args)
Execute a SQL query statement, which will use the specified consumer to process the results.
|
java.util.List<java.lang.Object[]> |
executeQuery(java.lang.String sql,
java.lang.Object... args)
Execute a SQL query statement.
|
void |
executeSQL(java.lang.String sql,
java.lang.Object... args)
Execute a generic SQL.
|
int |
executeUpdate(java.lang.String sql,
java.lang.Object... args)
Execute an update SQL.
|
StoredCollection |
getCollection(java.lang.String name)
Get the stored collection with the specified name.
|
boolean |
inPersist()
Get the state of the
inPersist flag. |
void |
persistCollection(java.util.Collection c,
java.lang.String sql,
java.util.function.BiConsumer<java.sql.PreparedStatement,java.lang.Object> ps)
Persist this collection, using the specified SQL and consumer to set the query batch
arguments.
|
void |
register(java.lang.String name,
StoredCollection c)
Register a db-backed collection with the given name.
|
private static java.lang.String dbUser
private static java.lang.String dbPass
private boolean connected
private java.sql.Connection connection
private java.lang.String dbURL
private java.util.Map<java.lang.String,java.sql.PreparedStatement> statements
private java.util.Map<java.lang.String,StoredCollection> stores
private boolean inPersist
public boolean inPersist()
inPersist
flag.public boolean connected()
connected
state.public void disconnect()
This will persist all db-backed collection.
public void connect()
public java.lang.String convertToSql(java.lang.Class<?> clazz)
clazz
- The Java type.public int executeUpdate(java.lang.String sql, java.lang.Object... args)
sql
- The SQL update statement.args
- The SQL arguments.public void executeSQL(java.lang.String sql, java.lang.Object... args)
sql
- The SQL statement.args
- The SQL arguments.public int executeCount(java.lang.String sql, java.lang.Object... args)
sql
- The SQL statement.args
- The SQL arguments.public java.util.List<java.lang.Object[]> executeQuery(java.lang.String sql, java.lang.Object... args)
sql
- The SQL statement.args
- The SQL arguments.public void executeQuery(java.lang.String sql, java.util.function.Consumer<java.sql.ResultSet> c, java.lang.Object... args)
sql
- The SQL statement.c
- The consumer to process the result set.args
- The SQL arguments.public StoredCollection getCollection(java.lang.String name)
name
- The stored collection name.null
if it was not yet added to
stores
.public void register(java.lang.String name, StoredCollection c)
name
- The collection name.c
- The collection.java.lang.IllegalStateException
- If there is already a collection with this name in stores
.public void persistCollection(java.util.Collection c, java.lang.String sql, java.util.function.BiConsumer<java.sql.PreparedStatement,java.lang.Object> ps)
c
- The collection to persist.sql
- The insert SQL.ps
- The consumer to add the records as a batch insert.private java.sql.PreparedStatement createStatement(java.lang.String sql) throws java.sql.SQLException
sql
- The SQL statement to prepare.statements
cache).java.sql.SQLException
- If the SQL statement can't be created.