Package org.fluentjdbc
Class DatabaseRow
- java.lang.Object
-
- org.fluentjdbc.DatabaseRow
-
@CheckReturnValue public class DatabaseRow extends Object
Retrieves column values for a single row returned by a query, if necessary, calculating the column position and doing necessary conversion.DatabaseRow
serves as an encapsulation of aResultSet
for a single row, with the additional support for convertingInstant
s,ZonedDateTime
sOffsetDateTime
s,LocalDate
s,UUID
s, andEnum
s.getColumnIndex(String)
supports joins for most database drivers by reading column names fromResultSetMetaData
.table(String)
andtable(DatabaseTableAlias)
returns a DatabaseRow where all offsets are relative to the specified table. Example usage:DatabaseTable organizations = new DatabaseTableImpl("organizations"); DatabaseTable persons = new DatabaseTableImpl("persons"); DatabaseTable memberships = new DatabaseTableImpl("memberships"); DatabaseTableAlias m = memberships.alias("m"); DatabaseTableAlias p = persons.alias("p"); DatabaseTableAlias o = organizations.alias("o"); List<Member< result = m.join(m.column("person_id"), p.column("id")) .join(m.column("organization_id"), o.column("id")) .list(connection, row ->s { Member member = new Member(); member.setName(row.table(p).getString("name")); member.setBirthDate(row.table(p).getLocalDate("birthdate")); member.setOrganizationName(row.table(o).getString("name")); member.setStatus(row.table(m).getEnum(MembershipStatus.class, "name")); return person; }
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BigDecimal
getBigDecimal(String column)
Returns the value of the specified column on this row as a BigDecimalboolean
getBoolean(String column)
Returns the value of the specified column on this row as a booleanprotected Integer
getColumnIndex(String fieldName)
Returns the numeric index of the specified column in the current context.Double
getDouble(String column)
Returns the Double value of the specified column on this row.<T extends Enum<T>>
TgetEnum(Class<T> enumClass, String fieldName)
Returns the value of the specified column on this row as a Enum of the specified type.InputStream
getInputStream(String fieldName)
Returns the value of the specified column on this row as a binary stream.Instant
getInstant(String column)
Returns the value of the specified column on this row as an InstantInteger
getInt(String column)
Returns the Integer value of the specified column on this row.List<Integer>
getIntList(String columnName)
Returns the value of the specified column on this row as a List of Integers, if the underlying type is ArrayLocalDate
getLocalDate(String column)
Returns the value of the specified column on this row as a LocalDateLong
getLong(String column)
Returns the long value of the specified column on this row.Object
getObject(String column)
Returns the underlying database-representation for the specified columnOffsetDateTime
getOffsetDateTime(String fieldName)
Returns the value of the specified column on this row as a OffsetDateTimeReader
getReader(String fieldName)
Returns the value of the specified column on this row as a reader.String
getString(String column)
Returns the value of the specified column on this row as a stringList<String>
getStringList(String columnName)
Returns the value of the specified column on this row as a List of String, if the underlying type is ArrayTimestamp
getTimestamp(String column)
Returns the value of the specified column on this row as a timestampUUID
getUUID(String fieldName)
Returns the value of the specified column on this row as a String converted toUUID
ZonedDateTime
getZonedDateTime(String fieldName)
Returns the value of the specified column on this row as a ZonedDateTimeDatabaseRow
table(String table)
Extracts aDatabaseRow
for the specifiedDatabaseTableAlias
belonging to the specified alias.DatabaseRow
table(DatabaseTableAlias alias)
Extracts aDatabaseRow
for the specifiedDatabaseTableAlias
belonging to the specified alias.<T> Optional<T>
table(DatabaseTableAlias alias, DatabaseResult.RowMapper<T> function)
Extracts aDatabaseRow
for the specifiedDatabaseTableAlias
and maps it over theDatabaseResult.RowMapper
function to return an object mapped from the part of theDatabaseRow
belonging to the specified alias.DatabaseRow
table(DbContextTableAlias alias)
Extracts aDatabaseRow
for the specifiedDatabaseTableAlias
belonging to the specified alias.
-
-
-
Field Detail
-
rs
protected final ResultSet rs
-
-
Method Detail
-
getObject
public Object getObject(String column) throws SQLException
Returns the underlying database-representation for the specified column- Throws:
SQLException
-
getString
public String getString(String column) throws SQLException
Returns the value of the specified column on this row as a string- Throws:
SQLException
-
getLong
public Long getLong(String column) throws SQLException
Returns the long value of the specified column on this row. If the column value is null, returns null (unlikeResultSet.getLong(int)
- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getInt
public Integer getInt(String column) throws SQLException
Returns the Integer value of the specified column on this row. If the column value is null, returns null (unlikeResultSet.getInt(int)
- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getDouble
public Double getDouble(String column) throws SQLException
Returns the Double value of the specified column on this row. If the column value is null, returns null (unlikeResultSet.getDouble(int)
- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getBoolean
public boolean getBoolean(String column) throws SQLException
Returns the value of the specified column on this row as a boolean- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getTimestamp
public Timestamp getTimestamp(String column) throws SQLException
Returns the value of the specified column on this row as a timestamp- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getInstant
public Instant getInstant(String column) throws SQLException
Returns the value of the specified column on this row as an Instant- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getZonedDateTime
public ZonedDateTime getZonedDateTime(String fieldName) throws SQLException
Returns the value of the specified column on this row as a ZonedDateTime- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getOffsetDateTime
public OffsetDateTime getOffsetDateTime(String fieldName) throws SQLException
Returns the value of the specified column on this row as a OffsetDateTime- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getLocalDate
public LocalDate getLocalDate(String column) throws SQLException
Returns the value of the specified column on this row as a LocalDate- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getUUID
public UUID getUUID(String fieldName) throws SQLException
Returns the value of the specified column on this row as a String converted toUUID
- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getInputStream
public InputStream getInputStream(String fieldName) throws SQLException
Returns the value of the specified column on this row as a binary stream. Used with BLOB (Binary Large Objects) and bytea (PostgreSQL) data types- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getReader
public Reader getReader(String fieldName) throws SQLException
Returns the value of the specified column on this row as a reader. Used with CLOB (Character Large Objects) and text (PostgreSQL) data types- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getBigDecimal
public BigDecimal getBigDecimal(String column) throws SQLException
Returns the value of the specified column on this row as a BigDecimal- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getIntList
public List<Integer> getIntList(String columnName) throws SQLException
Returns the value of the specified column on this row as a List of Integers, if the underlying type is Array- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getStringList
public List<String> getStringList(String columnName) throws SQLException
Returns the value of the specified column on this row as a List of String, if the underlying type is Array- Throws:
SQLException
- See Also:
getColumnIndex(java.lang.String)
-
getEnum
public <T extends Enum<T>> T getEnum(Class<T> enumClass, String fieldName) throws SQLException
Returns the value of the specified column on this row as a Enum of the specified type. Retrieves the column value as String and converts it to the specified enum- Throws:
IllegalArgumentException
- if the specified enum type has no constant with the specified name, or the specified class object does not represent an enum typeSQLException
- See Also:
getColumnIndex(java.lang.String)
-
getColumnIndex
protected Integer getColumnIndex(String fieldName)
Returns the numeric index of the specified column in the current context. Iftable(org.fluentjdbc.DatabaseTableAlias, org.fluentjdbc.DatabaseResult.RowMapper<T>)
has been called to specify a table or table alias in a join statement, this method can resolve ambiguous column names- Returns:
- the index to be used with
ResultSet.getObject(int)
etc - Throws:
IllegalArgumentException
- if the fieldName was not present in the ResultSet
-
table
public <T> Optional<T> table(DatabaseTableAlias alias, DatabaseResult.RowMapper<T> function) throws SQLException
Extracts aDatabaseRow
for the specifiedDatabaseTableAlias
and maps it over theDatabaseResult.RowMapper
function to return an object mapped from the part of theDatabaseRow
belonging to the specified alias. If the alias was the result of an outer join that didn't return data, this method will instead returnOptional.empty()
- Throws:
SQLException
- See Also:
DatabaseJoinedQueryBuilder
-
table
public DatabaseRow table(DatabaseTableAlias alias) throws SQLException
Extracts aDatabaseRow
for the specifiedDatabaseTableAlias
belonging to the specified alias. If the alias was the result of an outer join that didn't return data, this method will instead returnnull
- Returns:
- A
DatabaseRow
associated with the specified alias, or null if the alias was part of an outer join that didn't return data - Throws:
SQLException
- See Also:
DatabaseJoinedQueryBuilder
-
table
public DatabaseRow table(DbContextTableAlias alias) throws SQLException
Extracts aDatabaseRow
for the specifiedDatabaseTableAlias
belonging to the specified alias. If the alias was the result of an outer join that didn't return data, this method will instead returnnull
- Returns:
- A
DatabaseRow
associated with the specified alias, or null if the alias was part of an outer join that didn't return data - Throws:
SQLException
- See Also:
DatabaseJoinedQueryBuilder
-
table
public DatabaseRow table(String table)
Extracts aDatabaseRow
for the specifiedDatabaseTableAlias
belonging to the specified alias.- Throws:
IllegalArgumentException
- if the specified table wasn't part of theSELECT ... FROM ...
clause- See Also:
DatabaseJoinedQueryBuilder
-
-