use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenMultipleParameters_thenReplacement.
@Test
public void whenMultipleParameters_thenReplacement() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
Update update = handle.createUpdate("INSERT INTO PROJECT_11 (NAME, URL) VALUES (:name, :url)");
Map<String, String> params = new HashMap<>();
params.put("name", "REST with Spring");
params.put("url", "github.com/eugenp/REST-With-Spring");
update.bindMap(params);
int rows = update.execute();
assertEquals(1, rows);
List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_11").mapToMap().list();
assertEquals(1, list.size());
class Params {
private String name;
private String url;
public Params(String name, String url) {
this.name = name;
this.url = url;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
update.bindBean(new Params("tutorials", "github.com/eugenp/tutorials"));
rows = update.execute();
assertEquals(1, rows);
list = handle.select("SELECT * FROM PROJECT_11").mapToMap().list();
assertEquals(2, list.size());
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenSelectMapToString_thenResultIsString.
@Test
public void whenSelectMapToString_thenResultIsString() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
Query query = handle.createQuery("select name, url from PROJECT_4 order by id");
List<String> list = query.mapTo(String.class).list();
assertEquals("tutorials", list.get(0));
assertEquals("REST with Spring", list.get(1));
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenTransactionRollbackThenCommit_thenOnlyLastInserted.
@Test
public void whenTransactionRollbackThenCommit_thenOnlyLastInserted() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.useTransaction((Handle h) -> {
assertEquals(handle, h);
handle.execute("create table PROJECT_13 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
handle.rollback();
handle.begin();
handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')");
handle.rollback();
handle.begin();
handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')");
handle.commit();
});
List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_13").mapToMap().list();
assertEquals(2, list.size());
});
}
use of org.jdbi.v3.core.Jdbi in project tutorials by eugenp.
the class JdbiTest method whenMultipleResults_thenFindFirstReturnsFirst.
@Test
public void whenMultipleResults_thenFindFirstReturnsFirst() {
Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
jdbi.useHandle(handle -> {
handle.execute("create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
Query query = handle.createQuery("select * from project_7 order by id");
Optional<Map<String, Object>> first = query.mapToMap().findFirst();
assertTrue(first.isPresent());
assertEquals("tutorials", first.get().get("name"));
});
}
use of org.jdbi.v3.core.Jdbi in project presto by prestodb.
the class MySqlConnectionModule method setup.
@Override
protected void setup(Binder binder) {
configBinder(binder).bindConfig(MySqlConnectionConfig.class);
String databaseUrl = buildConfigObject(MySqlConnectionConfig.class).getDatabaseUrl();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
Jdbi jdbi = createJdbi(() -> DriverManager.getConnection(databaseUrl), buildConfigObject(MySqlFunctionNamespaceManagerConfig.class));
binder.bind(Jdbi.class).toInstance(jdbi);
binder.bind(FunctionNamespaceDao.class).toProvider(FunctionNamespaceDaoProvider.class);
binder.bind(new TypeLiteral<Class<? extends FunctionNamespaceDao>>() {
}).toInstance(FunctionNamespaceDao.class);
}
Aggregations