Package com.github.princesslana.somedb
Class TheDB
- java.lang.Object
-
- com.github.princesslana.somedb.TheDB
-
public class TheDB extends java.lang.Object
Provides a static/singleton interface to a single database.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
execute(java.lang.String sql, java.lang.Object... params)
Execute an SQL statement.static void
initialize(Config config)
Initializes for use with a db with the provided Config.static void
initialize(java.lang.String name)
Initializes for use with a db with the provided name.static org.jdbi.v3.core.Jdbi
jdbi()
Provides access to the Jdbi instance.static org.jooq.DSLContext
jooq()
Entrypoint for using the Jooq DSL.static org.jdbi.v3.core.Handle
open()
Open a Handle to this database.static <T> java.util.stream.Stream<T>
select(OneDB.RowMapper<T> mapper, java.lang.String sql, java.lang.Object... params)
Run an SQL query.static <X extends java.lang.Exception>
voiduseHandle(org.jdbi.v3.core.HandleConsumer<X> consumer)
Use a Jdbi Handle.static <R,X extends java.lang.Exception>
RwithHandle(org.jdbi.v3.core.HandleCallback<R,X> callback)
Execute a callback with a Jdbi Handle.
-
-
-
Method Detail
-
initialize
public static void initialize(java.lang.String name)
Initializes for use with a db with the provided name.- Parameters:
name
- the db name- Throws:
java.lang.IllegalStateException
- if the db is already initalized
-
initialize
public static void initialize(Config config)
Initializes for use with a db with the provided Config.- Parameters:
config
- the db config- Throws:
java.lang.IllegalStateException
- if the db is already initialized
-
jdbi
public static org.jdbi.v3.core.Jdbi jdbi()
Provides access to the Jdbi instance. The methods on OneDB are designed to cover the most common use cases. The Jdbi instance is available if they do not fit your needs.- Returns:
- the Jdbi instance
-
jooq
public static org.jooq.DSLContext jooq()
Entrypoint for using the Jooq DSL.- Returns:
- the Jooq DSLContext
-
open
public static org.jdbi.v3.core.Handle open()
Open a Handle to this database.- Returns:
- an open Handle instance
-
useHandle
public static <X extends java.lang.Exception> void useHandle(org.jdbi.v3.core.HandleConsumer<X> consumer) throws X extends java.lang.Exception
Use a Jdbi Handle.- Type Parameters:
X
- exeception that may be thrown by the consumer- Parameters:
consumer
- the consumer that uses the Handle- Throws:
X
- if consumer doesX extends java.lang.Exception
-
withHandle
public static <R,X extends java.lang.Exception> R withHandle(org.jdbi.v3.core.HandleCallback<R,X> callback) throws X extends java.lang.Exception
Execute a callback with a Jdbi Handle.- Type Parameters:
R
- return type of callbackX
- exception that may be thrown by the callback- Parameters:
callback
- the code to run with the Handle- Returns:
- the result of the callback
- Throws:
X
- if callback doesX extends java.lang.Exception
-
execute
public static void execute(java.lang.String sql, java.lang.Object... params)
Execute an SQL statement.- Parameters:
sql
- the SQL to executeparams
- the parameters to include in the sql
-
select
public static <T> java.util.stream.Stream<T> select(OneDB.RowMapper<T> mapper, java.lang.String sql, java.lang.Object... params)
Run an SQL query.- Type Parameters:
T
- the return type of the mapper- Parameters:
mapper
- Function to convert from ResultSet to the result we wantsql
- the SQL to executeparams
- the parameters to include in the sql- Returns:
- a Stream of mapped results
-
-