Skip navigation links

Package be.bendem.sqlstreams

This package contains the public API for the sql-streams package.

See: Description

Package be.bendem.sqlstreams Description

This package contains the public API for the sql-streams package.

sql-streams abstracts the JDBC API by providing tools to easily handle set parameters of prepared statements and map database result sets to classes.

The heart of this library is the SqlStream class which provides static factories as well as the methods used to execute DML and DDL sql queries. These methods generally come in two flavors: prepared and non prepared.

The prepared methods return a Prepared_____ instance implementing the ParameterProvider interface so as to allow you to set your query parameters before executing your query. You'll most likely want to use the ParameterProvider.with(java.lang.Object...) method to set parameters of the most common types.

The non prepared methods are generally shortcuts for the prepared methods.

You should make sure to always close instances you get from the library that implement AutoCloseable (applies to Streams as well). This is generally best done using the try-with-resources construct as such:

 try (PreparedUpdate update = sql.update("update salaries set amount = amount * 2")) {
     // use update here
 }

Transactions

Transactions are handled using SqlStream.transaction(). Unlike the JDBC API, closing a transaction (and thus the underlying connection) is guaranteed to rollback your current transaction. As such, transactional code is best written using the try-with-resources construct as such:
 try(Transaction transaction = sql.transaction()) {
     transaction.update("update salaries set amount = amount * 2");
     transaction.commit();
 } catch (UncheckedSqlException e) {
     // Handle the exception
 } // automatic rollback of uncommitted data

Mapping

This library offers a few facilities to map each row of a result set to a class.

Auto-magic mapping

Magic mapping provides automatic mapping between sql types and java types for java primitives (byte, short, int, long, boolean), their boxed equivalent, String, Date, Time, Timestamp, LocalDate, LocalTime and LocalDateTime.

Manual mapping

If you need a more complex mapping method, you can use Query.map(be.bendem.sqlstreams.util.SqlFunction) to map each row of the result set using your own code.
Skip navigation links

Copyright © 2019. All rights reserved.