Package com.pyranid


package com.pyranid
Pyranid is a zero-dependency JDBC interface for modern Java applications.

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

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

 // Customized setup

 // Override JVM default timezone
 ZoneId timeZone = ZoneId.of("UTC");

 // Controls how Pyranid creates instances of objects that represent ResultSet rows
 InstanceProvider instanceProvider = new DefaultInstanceProvider() {
   @Override
   @Nonnull
   public <T> T provide(@Nonnull StatementContext<T> statementContext,
                        @Nonnull Class<T> instanceType) {
     // You might have your DI framework vend regular object instances
     return guiceInjector.getInstance(instanceType);
   }

   @Override
   @Nonnull
   public <T extends Record> T provideRecord(@Nonnull StatementContext<T> statementContext,
                                             @Nonnull Class<T> recordType,
                                             @Nullable Object... initargs) {
     // If you use Record types, customize their instantiation here.
     // Default implementation will use the canonical constructor
     return super.provideRecord(statementContext, recordType, initargs);
   }
 };

 // Copies data from a ResultSet row to an instance of the specified type
 ResultSetMapper resultSetMapper = new DefaultResultSetMapper(timeZone) {
   @Nonnull
   @Override
   public <T> Optional<T> map(@Nonnull StatementContext<T> statementContext,
                              @Nonnull ResultSet resultSet,
                              @Nonnull Class<T> resultSetRowType,
                              @Nonnull InstanceProvider instanceProvider) {
     return super.map(statementContext, resultSet, resultSetRowType, instanceProvider);
   }
 };

 // Binds parameters to a SQL PreparedStatement
 PreparedStatementBinder preparedStatementBinder = new DefaultPreparedStatementBinder(timeZone) {
   @Override
   public <T> void bind(@Nonnull StatementContext<T> statementContext,
                        @Nonnull PreparedStatement preparedStatement,
                        @Nonnull List<Object> parameters) {
     super.bind(statementContext, preparedStatement, parameters);
   }
 };

 // Optionally logs SQL statements
 StatementLogger statementLogger = new StatementLogger() {
   @Override
   public void log(@Nonnull StatementLog statementLog) {
     // Send to whatever output sink you'd like
     System.out.println(statementLog);
   }
 };

 Database customDatabase = Database.forDataSource(dataSource)
   .timeZone(timeZone)
   .instanceProvider(instanceProvider)
   .resultSetMapper(resultSetMapper)
   .preparedStatementBinder(preparedStatementBinder)
   .statementLogger(statementLogger)
   .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
 long updateCount = database.execute("UPDATE car SET color = ?", Color.RED);
 Optional<UUID> id = database.executeForObject("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