org.jdbi.v3.sqlobject.SqlObjectPlugin Java Examples

The following examples show how to use org.jdbi.v3.sqlobject.SqlObjectPlugin. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SessionPropertiesDaoProvider.java    From presto with Apache License 2.0 6 votes vote down vote up
@Inject
public SessionPropertiesDaoProvider(DbSessionPropertyManagerConfig config)
{
    requireNonNull(config, "config is null");
    String url = requireNonNull(config.getConfigDbUrl(), "db url is null");

    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setURL(url);

    Optional<String> username = Optional.ofNullable(config.getUsername());
    username.ifPresent(dataSource::setUser);

    Optional<String> password = Optional.ofNullable(config.getPassword());
    password.ifPresent(dataSource::setPassword);

    this.dao = Jdbi.create(dataSource)
            .installPlugin(new SqlObjectPlugin())
            .onDemand(SessionPropertiesDao.class);
}
 
Example #2
Source File: TestDbSessionPropertyManagerIntegration.java    From presto with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setupTest()
{
    queryRunner.getCoordinator().getSessionPropertyDefaults()
            .setConfigurationManager("db-test", ImmutableMap.<String, String>builder()
                    .put("session-property-manager.db.url", mysqlContainer.getJdbcUrl())
                    .put("session-property-manager.db.username", mysqlContainer.getUsername())
                    .put("session-property-manager.db.password", mysqlContainer.getPassword())
                    .build());

    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setURL(mysqlContainer.getJdbcUrl());
    dataSource.setUser(mysqlContainer.getUsername());
    dataSource.setPassword(mysqlContainer.getPassword());
    dao = Jdbi.create(dataSource)
            .installPlugin(new SqlObjectPlugin())
            .onDemand(SessionPropertiesDao.class);
}
 
Example #3
Source File: JdbiDatabase.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new connection to database. This connection should only be used by the TripleA Java
 * Lobby. DropWizard will create a connection from configuration automatically.
 */
public static Jdbi newConnection() {
  final Jdbi jdbi =
      Jdbi.create(
          String.format(
              "jdbc:postgresql://%s:%s/%s",
              DatabaseEnvironmentVariable.POSTGRES_HOST.getValue(),
              DatabaseEnvironmentVariable.POSTGRES_PORT.getValue(),
              DatabaseEnvironmentVariable.POSTGRES_DATABASE.getValue()),
          DatabaseEnvironmentVariable.POSTGRES_USER.getValue(),
          DatabaseEnvironmentVariable.POSTGRES_PASSWORD.getValue());
  jdbi.installPlugin(new SqlObjectPlugin());
  registerRowMappers(jdbi);
  registerSqlLogger(jdbi);
  return jdbi;
}
 
Example #4
Source File: MysqlDaoProvider.java    From presto with Apache License 2.0 5 votes vote down vote up
@Inject
public MysqlDaoProvider(DbResourceGroupConfig config)
{
    requireNonNull(config, "DbResourceGroupConfig is null");
    MysqlDataSource dataSource = new MysqlDataSource();
    dataSource.setURL(requireNonNull(config.getConfigDbUrl(), "resource-groups.config-db-url is null"));
    this.dao = Jdbi.create(dataSource)
            .installPlugin(new SqlObjectPlugin())
            .onDemand(ResourceGroupsDao.class);
}
 
Example #5
Source File: H2DaoProvider.java    From presto with Apache License 2.0 5 votes vote down vote up
@Inject
public H2DaoProvider(DbResourceGroupConfig config)
{
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(requireNonNull(config.getConfigDbUrl(), "resource-groups.config-db-url is null"));
    // TODO: this should use onDemand()
    this.dao = Jdbi.create(ds)
            .installPlugin(new SqlObjectPlugin())
            .open()
            .attach(H2ResourceGroupsDao.class);
}
 
Example #6
Source File: AbstractJdbiTest.java    From Rosetta with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
  jdbi = Jdbi.create("jdbc:h2:mem:rosetta;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE");
  jdbi.installPlugin(new SqlObjectPlugin());
  jdbi.registerRowMapper(new RosettaRowMapperFactory());
  jdbi.useHandle(handle -> {
    handle.execute("CREATE TABLE IF NOT EXISTS test_table (id INT, name VARCHAR(255) NOT NULL, PRIMARY KEY (id))");
    handle.execute("TRUNCATE TABLE test_table");
  });
}
 
Example #7
Source File: Chapter7JdbcApplication.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 4 votes vote down vote up
@Override
public void run(String... args) {

     // Spring JDBC
     log.info("--- Spring JDBC ---------------------------------------------");
     log.info("Book with id 13: {}", bookRepositoryJdbc.findById(13));

	List<Book> booksWithTitle = bookRepositoryJdbc.findByTitle("Blue Mars");
	log.info("Books with title 'Mars': {}", toString(booksWithTitle.stream()));

	List<Book> booksAll = bookRepositoryJdbc.findAll();
	log.info("All books in DB: {}", toString(booksAll.stream()));

	// Spring Data JDBC
     log.info("--- Spring Data JDBC------------------------------------------");
	Iterable<Book> booksFromDataJdbc = bookRepositoryDataJdbc.findAllById(asList(11, 13));
	List<Book> booksFromDataJdbcList = new ArrayList<>();
	booksFromDataJdbc.iterator().forEachRemaining(booksFromDataJdbcList::add);
	log.info("Books (id:11), (id:13) from Spring Data JDBC: {}",
		toString(booksFromDataJdbcList.stream()));

	log.info("Book with the longest title: {}",
		toString(bookRepositoryDataJdbc.findByLongestTitle().stream()));

	log.info("Book with the shortest title: {}",
		toString(bookRepositoryDataJdbc.findByShortestTitle()));

	bookRepositoryDataJdbc.findBookByTitleAsync("The Martian")
		.thenAccept(b -> log.info("Book by title, async: {}", toString(Stream.of(b))));

	bookRepositoryDataJdbc.findBooksByIdBetweenAsync(10, 14)
		.thenAccept(bookStream -> log.info("Book by id (11..13), async: {}", toString(bookStream)));

	// JDBI
     log.info("--- JDBI ----------------------------------------------------");
     Jdbi jdbi = Jdbi
        .create(dataSource)
        .installPlugin(new SqlObjectPlugin());

     List<Book> allBooksFromJdbi = jdbi.withExtension(BookJdbiDao.class, dao -> {
        dao.insertBook(new Book(20, "Moving Mars"));
        return dao.listBooks();
     });
     log.info("All Books according to Jdbi: {}", toString(allBooksFromJdbi.stream()));

     log.info("Application finished successfully!");
  }
 
Example #8
Source File: JdbiConfiguration.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public JdbiPlugin sqlObjectPlugin() {
    return new SqlObjectPlugin();
}