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
-
ClassDescriptionMain class for performing database access operations.Builder used to construct instances of
Database.Allows specification of alternate column names for resultset mapping.Thrown when an error occurs when interacting with aDatabase.Identifies different types of databases, which allows for special platform-specific handling.Basic implementation ofInstanceProvider.Basic implementation ofPreparedStatementBinder.Basic implementation ofResultSetMapper.Basic implementation ofStatementLoggerwhich logs via java.util.logging.Contract for a factory that creates instances given a type.Contract for binding parameters to SQL prepared statements.Contract for mapping aResultSetrow to a different type.Represents a transactional operation capable of returning a value.Represents a SQL statement and an identifier for it.Data that represents a SQL statement.Builder used to construct instances ofStatementContext.StatementLog<T>A collection of SQL statement execution diagnostics.Builder used to construct instances ofStatementLog.Contract for handling database statement log events.Represents a database transaction.Represents an operation performed with a transactional context.Strategies for database locking during transactional operations.Indicates whether a transaction was committed or rolled back.