There is no common interface with java.sql.ResultSet, CallableStatement, SQLInput

This is the situation

There is a great need for abstraction over JDBC in jOOQ . I want the jOOQ client code not to know that some data is retrieved from a simple ResultSet, some data is retrieved from SQLInput (for UDT) or from CallableStatements (for stored procedures / functions). Therefore, I want to add an abstraction over these JDBC types:

java.sql.ResultSet
java.sql.CallableStatement
java.sql.SQLInput
java.sql.SQLOutput

Now they all work pretty much the same. Usually they have a method getand setfor each data type in java.sql.Types. For example, they come with methods such as

BigDecimal getBigDecimal(int index);
int getInt(int index);

And everyone has methods like

boolean wasNull();

Problem

, JDBC , , JDBC-, , ( ):

// As JDBC has no support for BigInteger types directly,
// read a BigDecimal and transform it to a BigInteger
BigDecimal result = null;

if (source instanceof ResultSet) {
    result = ((ResultSet) source).getBigDecimal(index);
}
else if (source instanceof CallableStatement) {
    result = ((CallableStatement) source).getBigDecimal(index);
}
else if (source instanceof SQLInput) {
    result = ((SQLInput) source).readBigDecimal();
}

return result == null ? null : result.toBigInteger();

, ResultSet, CallableStatement, SQLInput.

  • - JDBC, ?
  • - ( ) ?
  • ?

?

+3
3
  • - JDBC, ?

, - .


  • - ( ) ?

. .

public interface DataProvider {
    public BigInteger getBigInteger(int columnIndex);
    // ...
}

.

public class ResultSetDataProvider implements DataProvider {
    private ResultSet resultSet;

    public ResultSetDataProvider(ResultSet resultSet) {
        this.resultSet = resultSet;
    }

    public BigInteger getBigInteger(int columnIndex) {
        BigDecimal bigDecimal = resultSet.getBigDecimal(columnIndex);
        return bigDecimal != null ? bigDecimal.toBigInteger() : null;
    }

    // ...
}

.

try {
    // Acquire ResultSet.
    DataProvider dataProvider = new ResultSetDataProvider(resultSet);
    // Process DataProvider.
} finally {
    // Close ResultSet.
}

  • ?

, . DRY.

+3

​​. , .

, , SQL . , SQL. , , , .

, Spring JDBC , . , , .

javadocs SQLInput, :

, , SQLInput.

, .

ResultSet CallableStatement ( , ), ResultSets . . , , . .

, , , , . , .

+1
  • Not sure about the extension for JDBC.
  • A wrapper class or visitor template may be useful depending on the rest of your design.
  • How about expanding each of them and providing them with the implementation of the same interface, which has the same method signatures as the required data.
0
source

Source: https://habr.com/ru/post/1781104/


All Articles