Skip navigation links

Package com.pyranid

Pyranid is a minimalist JDBC interface for modern Java applications.

See: Description

Package com.pyranid Description

Pyranid is a minimalist JDBC interface for modern Java applications.

See http://pyranid.com for more detailed documentation and code samples.

// Minimal setup, uses defaults
Database database = Database.forDataSource(dataSource).build();

// Customized setup
Database customDatabase = Database.forDataSource(dataSource).instanceProvider(new InstanceProvider() {
  @Override
  public <T> T provide(Class<T> instanceClass) {
    // You might have your DI framework vend resultset row instances
    return guiceInjector.getInstance(instanceClass);
  }
}).resultSetMapper(new ResultSetMapper() {
  @Override
  public <T> T map(ResultSet rs, Class<T> resultClass) {
    // Do some custom mapping here
  }
}).preparedStatementBinder(new PreparedStatementBinder() {
  @Override
  public void bind(PreparedStatement ps, List<Object> parameters) {
    // Do some custom binding here
  }
}).statementLogger(new StatementLogger() {
  @Override
  public void log(StatementLog statementLog) {
    // Send log to whatever output sink you'd like
    out.println(statementLog);
  }
}).build();

// Queries
Optional<Car> specificCar = database.queryForObject("SELECT * FROM car WHERE id = ?", Car.class, 123);
List<Car> blueCars = database.queryForList("SELECT * FROM car WHERE color = ?", Car.class, Color.BLUE);
Optional<UUID> id = database.queryForObject("SELECT id FROM widget LIMIT 1", UUID.class);
List<BigDecimal> balances = database.queryForList("SELECT balance FROM account", BigDecimal.class);

// Statements
int updateCount = database.execute("UPDATE car SET color = ?", Color.RED);
Optional<UUID> id = database.executeReturning("INSERT INTO book VALUES (?) RETURNING id", UUID.class, "The Stranger");

// Transactions
database.transaction(() -> {
  BigDecimal balance1 = database.queryForObject("SELECT balance FROM account WHERE id = 1", BigDecimal.class).get();
  BigDecimal balance2 = database.queryForObject("SELECT balance FROM account WHERE id = 2", BigDecimal.class).get();
 
  balance1 = balance1.subtract(amount);
  balance2 = balance2.add(amount);

  database.execute("UPDATE account SET balance = ? WHERE id = 1", balance1);
  database.execute("UPDATE account SET balance = ? WHERE id = 2", balance2);
});
Since:
1.0.0
Author:
Mark Allen
Skip navigation links

Copyright © 2018. All rights reserved.