Package org.fluentjdbc
Class DbContext
- java.lang.Object
-
- org.fluentjdbc.DbContext
-
- Direct Known Subclasses:
DbContextRule
public class DbContext extends Object
Provides a starting point for for context oriented database operation. Create one DbContext for your application and use
table(String)
to createDbContextTable
object for each table you manipulate. All database operations must be nested inside a call tostartConnection(DataSource)
.Example
DbContext context = new DbContext();
DbContextTable
table = context.table("database_test_table"); DataSource dataSource = createDataSource(); try (DbContextConnection ignored = context.startConnection(dataSource)) { Object id = table.insert() .setPrimaryKey("id", null) .setField("code", 1002) .setField("name", "insertTest") .execute(); assertThat(table.where("name", "insertTest").orderBy("code").listLongs("code")) .contains(1002L); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DbContext.ConnectionSupplier
ASupplier
forConnection
objects.static interface
DbContext.RetrieveMethod<KEY,ENTITY>
Functional interface used to populate the query.
-
Constructor Summary
Constructors Constructor Description DbContext()
DbContext(DatabaseReporter reporter)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <ENTITY,KEY>
Optional<ENTITY>cache(String tableName, KEY key, DbContext.RetrieveMethod<KEY,ENTITY> retriever)
Retrieves the underlying or cached value of the retriever argument.DbTransaction
ensureTransaction()
Turns off auto-commit for the current thread until theDbTransaction
is closed.Connection
getThreadConnection()
Returns the connection associated with the current thread or throws exception ifstartConnection(DataSource)
has not been called yetDbContextSqlBuilder
select(String... columns)
Build an arbitrary select statement, e.g.DbContextConnection
startConnection(DataSource dataSource)
Binds a database connection to thisDbContext
for the current thread.DbContextConnection
startConnection(DbContext.ConnectionSupplier connectionSupplier)
Gets a connection fromDbContext.ConnectionSupplier
and assigns it to the the current thread.DbContextStatement
statement(String statement, List<Object> parameters)
Execute an arbitrary SQL statement, e.g.DbContextTable
table(String tableName)
Creates aDbContextTable
associated with this DbContext.DbContextTable
table(DatabaseTable table)
Associate a custom implementation ofDatabaseTable
with thisDbContext
DatabaseTableReporter
tableReporter(String tableName)
DatabaseTableReporter
tableReporter(DatabaseTable table)
DbContextTable
tableWithTimestamps(String tableName)
Creates atable(String)
which automatically updatescreated_at
andupdated_at
columns in the underlying database.
-
-
-
Constructor Detail
-
DbContext
public DbContext()
-
DbContext
public DbContext(DatabaseReporter reporter)
-
-
Method Detail
-
tableReporter
public DatabaseTableReporter tableReporter(DatabaseTable table)
-
tableReporter
public DatabaseTableReporter tableReporter(String tableName)
-
select
@CheckReturnValue public DbContextSqlBuilder select(String... columns)
Build an arbitrary select statement, e.g.dbContext.select("max(age) as max_age").from("persons").where("name", "Johannes").singleLong("max_age")
-
statement
@CheckReturnValue public DbContextStatement statement(String statement, List<Object> parameters)
Execute an arbitrary SQL statement, e.g.dbContext.select("select 1 as number from dual").singleLong("max_age")
ordbContext.select("update persons set full_name = first_name || ' ' || last_name").executeUpdate()
-
table
@CheckReturnValue public DbContextTable table(@Nonnull String tableName)
Creates aDbContextTable
associated with this DbContext. All operations will be executed with the connection from thisDbContext
-
tableWithTimestamps
@CheckReturnValue public DbContextTable tableWithTimestamps(String tableName)
Creates atable(String)
which automatically updatescreated_at
andupdated_at
columns in the underlying database.- See Also:
DatabaseTableWithTimestamps
-
table
@CheckReturnValue public DbContextTable table(DatabaseTable table)
Associate a custom implementation ofDatabaseTable
with thisDbContext
-
startConnection
@CheckReturnValue public DbContextConnection startConnection(DataSource dataSource)
Binds a database connection to thisDbContext
for the current thread. All database operations onDbContextTable
objects created from thisDbContext
in the current thread will be executed using this connection. Calls to startConnection can be nested.Example:
try (DbContextConnection ignored = context.startConnection(dataSource)) { return table.where("id", id).orderBy("data").listLongs("data"); }
-
startConnection
@CheckReturnValue public DbContextConnection startConnection(DbContext.ConnectionSupplier connectionSupplier)
Gets a connection fromDbContext.ConnectionSupplier
and assigns it to the the current thread. Associates the cache with the current thread as well- See Also:
startConnection(DataSource)
-
getThreadConnection
@CheckReturnValue public Connection getThreadConnection()
Returns the connection associated with the current thread or throws exception ifstartConnection(DataSource)
has not been called yet
-
cache
@CheckReturnValue public <ENTITY,KEY> Optional<ENTITY> cache(String tableName, KEY key, DbContext.RetrieveMethod<KEY,ENTITY> retriever)
Retrieves the underlying or cached value of the retriever argument. This cache is perDbContextConnection
and is evicted when the connection is closed
-
ensureTransaction
@CheckReturnValue public DbTransaction ensureTransaction()
Turns off auto-commit for the current thread until theDbTransaction
is closed. Returns aDbTransaction
object which can be used to control commit and rollback. Can be nested
-
-