database-2.0
Java 8 features
- Java 7 and earlier do not work with this release.
- Deprecated
DatabaseProvider.Builder.transact(DbRun)
as it is not lambda-friendly and provides undesirable default transaction behavior of always commit (even on error). - Use
transact(dbs -> ...)
for commit with rollback on error, or usetransact((dbs, tx) -> ...)
if you want to customize rollback behavior. - Use
Supplier<Database>
rather thatProvider<Database>
. It works either way currently, butProvider
support will be remove in a future release.
Support for Vert.x
- Add
DatabaseProviderVertx
with support for the Vert.x event/worker thread model and asynchronous callbacks after the database operation. - The SLF4J
MDC
will be correctly preserved for these calls (viaVertxUtil.executeBlocking()
wrappers). - See
demo/src/main/java/VertxServer.java
for an example.
Batch inserts
Insert multiple rows in one statement:
db.toInsert(...).argString("row1").batch().argString("row2").insertBatch();
The batch()
call is forgiving in that you can call it before the first row, after the last row, etc. to make looping easier.
Built in database connection pooling
See DatabaseProvider*.pooledBuilder()
to get it for free, or fromPool()
if you want to roll your own.
Support for SQL Server
Support for HyperSQL (HSQLDB)
Configuration
There is now a Config
class for managing properties from different sources. There are lots of neat little tricks you can play with filtering and mapping prefixes, but here is a very simple use case:
String propertiesFile = System.getProperty("properties", "app.properties");
Config config = Config.from().systemProperties().propertyFile(propertiesFile).get();
DatabaseProvider.fromDriverManager(config).transact(...);
Logging
Generally made the logs a bit more useful and machine parseable.
- Include the database flavor when getting the connection ("Get oracle database: ..." rather than "Get database: ...").
- Debug log SQL respects the flavor for generating date functions, and preserves millisecond precision.
- Named parameters with prefix "secret" are now omitted from logging. In debug SQL the value will appear as "<secret>".
- Improve documentation for Metric and ensure the message will not contain spaces or tabs.
- Change the log format to make it easier to parse. It now uses tabs to delimit SQL (<tab>select a from b where c=?<tab>ParamSql:<tab>select a from b where c=1) and ensures the SQL and other portions of the log message do not contain tabs.
- Avoid printing really long values into debug SQL. String values are truncated to 4000 length by default, which can be controlled by
Options.maxStringLengthParam()
.
Miscellaneous
- Avoid the need for iterating the result set in most cases for queries. Introduce
Row
andRowHandler
, andSqlSelect.query*()
methods to callback per row. - Improve support for dynamic SQL generation. Add convenience methods
Database.to*(Sql)
. AddSql.argCount()
andSql.appendQuestionMarks(int)
to make generating insert statements easier. - Add ability to access the JDBC result set metadata via
Row.getMetadata()
. - Add ability to read
SqlArgs
andSchema
table from a query result (Row
). This makes is easier to implement things like saving a result to another table that is created on the fly. - Add support for create/update tracking, history tables, etc. to
Schema
. - Make connection access configurable on the database provider builder.
- Now
Row.getX()
will pick up the next column following an explicitRow.getX(int)
orRow.getX(String)
. Previously it always started at column 1. - Switch from IntelliJ annotations to jsr305 to remove a dependency.
Bugs
- Fix time zone issues with date functions.
- Fix
DatabaseProvider.fakeBuilder()
so it actually works and provide a sample (demo/src/main/java/FakeBuilder.java
). - Make database providers stricter about prohibiting use after close().
- Use "if exists" when dropping tables in postgres.
- Fix return types and annotations for
query*OrZero()
. - Automatically stop the
Metric
when you get/print the message. - Allow custom table clause per-flavor in
Schema.Table.customTableClause()
. - Prevent defining primary key multiple times in
Schema
.