use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenIdentityColumn_thenInsertReturnsNewId.
@Test
public void whenIdentityColumn_thenInsertReturnsNewId() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
Update update = handle.createUpdate("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
ResultBearing generatedKeys = update.executeAndReturnGeneratedKeys();
assertEquals(0, generatedKeys.mapToMap().findOnly().get("id"));
update = handle.createUpdate("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
assertEquals(1, generatedKeys.mapToMap().findOnly().get("id"));
});
}
use of org.jdbi.v3.core.Jdbi in project SimpleFlatMapper by arnaudroger.
the class RowMapperFactoryTest method testMapToDbObject.
@Test
public void testMapToDbObject() throws Exception {
Jdbi dbi = Jdbi.create(DbHelper.getHsqlDataSource());
dbi.installPlugins();
Handle handle = dbi.open();
try {
DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).findFirst().get();
DbHelper.assertDbObjectMapping(dbObject);
} finally {
handle.close();
}
}
use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.
the class OnDemandExtensions method create.
static <E> E create(Jdbi db, Class<E> extensionType) {
ThreadLocal<E> threadExtension = new ThreadLocal<>();
InvocationHandler handler = (proxy, method, args) -> {
if (EQUALS_METHOD.equals(method)) {
return proxy == args[0];
}
if (HASHCODE_METHOD.equals(method)) {
return System.identityHashCode(proxy);
}
if (TOSTRING_METHOD.equals(method)) {
return extensionType + "@" + Integer.toHexString(System.identityHashCode(proxy));
}
if (threadExtension.get() != null) {
return invoke(threadExtension.get(), method, args);
}
return db.withExtension(extensionType, extension -> JdbiThreadLocals.invokeInContext(threadExtension, extension, () -> invoke(extension, method, args)));
};
return extensionType.cast(Proxy.newProxyInstance(extensionType.getClassLoader(), new Class[] { extensionType }, handler));
}
use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.
the class Jdbi method open.
/**
* Obtain a Handle to the data source wrapped by this Jdbi instance.
* You own this expensive resource and are required to close it or
* risk leaks. Using a {@code try-with-resources} block is recommended.
*
* @return an open Handle instance
* @see #useHandle(HandleConsumer)
* @see #withHandle(HandleCallback)
*/
public Handle open() {
try {
final long start = System.nanoTime();
Connection conn = connectionFactory.openConnection();
final long stop = System.nanoTime();
for (JdbiPlugin p : plugins) {
conn = p.customizeConnection(conn);
}
StatementBuilder cache = statementBuilderFactory.get().createStatementBuilder(conn);
Handle h = new Handle(config.createCopy(), transactionhandler.get(), cache, conn);
for (JdbiPlugin p : plugins) {
h = p.customizeHandle(h);
}
LOG.trace("Jdbi [{}] obtain handle [{}] in {}ms", this, h, (stop - start) / 1000000L);
return h;
} catch (SQLException e) {
throw new ConnectionException(e);
}
}
use of org.jdbi.v3.core.Jdbi in project jdbi by jdbi.
the class TestVendorArrays method testHsqlDb.
@Test
public void testHsqlDb() {
Jdbi db = Jdbi.create("jdbc:hsqldb:mem:" + UUID.randomUUID());
init(db);
try (Handle handle = db.open()) {
handle.execute("create table player_stats (" + "name varchar(64) primary key, " + "seasons varchar(36) array, " + "points int array)");
handle.createUpdate("insert into player_stats (name,seasons,points) values (?,?,?)").bind(0, "Jack Johnson").bind(1, new String[] { "2013-2014", "2014-2015", "2015-2016" }).bind(2, new Integer[] { 42, 51, 50 }).execute();
String[] seasons = handle.createQuery("select seasons from player_stats where name=:name").bind("name", "Jack Johnson").mapTo(String[].class).findOnly();
assertThat(seasons).containsExactly("2013-2014", "2014-2015", "2015-2016");
}
}
Aggregations