Permalink
Browse files

[jdbc-driver] Java 1.7+ support

  • Loading branch information...
cchantep
cchantep committed Apr 12, 2015
1 parent 679f282 commit a8ed90b4c44a38626bf7e1935bca692fdc152012
@@ -1,3 +1,4 @@
// -*- mode: java -*-
package acolyte.jdbc;
import java.io.InputStream;
@@ -784,36 +785,6 @@ public Object getObject(final String parameterName,
return this.result.getObject(parameterName, map);
} // end of getObject
/* Java 1.7
* {@inheritDoc}
public <T extends Object> T getObject(final int parameterIndex,
final Class<T> type)
throws SQLException {
checkClosed();
if (this.result == null) {
throw new SQLException("No result");
} // end of if
return this.result.getObject(parameterIndex, type);
} // end of getObject
// {@inheritDoc}
public <T extends Object> T getObject(final String parameterName,
final Class<T> type)
throws SQLException {
checkClosed();
if (this.result == null) {
throw new SQLException("No result");
} // end of if
return this.result.getObject(parameterName, type);
} // end of getObject
*/
/**
* {@inheritDoc}
*/
@@ -1421,4 +1392,6 @@ public SQLXML getSQLXML(final String parameterName) throws SQLException {
return this.result.getSQLXML(parameterName);
} // end of getSQLXML
#JAVA_1_7#
} // end of class CallableStatement
@@ -0,0 +1,153 @@
package acolyte.jdbc;
import java.util.Properties;
import java.util.function.Function;
import static acolyte.jdbc.CompositeHandler.QueryHandler;
/**
* Acolyte DSL for JDBC.
*/
public final class AcolyteDSL {
/**
* Creates a connection, whose statement will be passed to given handler.
*
* @param h statement handler
* @return a new Acolyte connection
*
* <pre>
* {@code
* connection(handler); // without connection properties
* }}}
* }
* </pre>
*/
public static Connection connection(AbstractCompositeHandler<?> h) {
return Driver.connection(h);
}
/**
* Creates a connection, whose statement will be passed to given handler.
*
* @param h statement handler
* @param p connection properties
* @return a new Acolyte connection
*
* <pre>
* {@code
* // With connection property to fallback untyped null
* connection(handler, properties); //e.g. "acolyte.parameter.untypedNull"
* }
* </pre>
*/
public static Connection connection(AbstractCompositeHandler<?> h, Properties p) { return Driver.connection(h, p); }
/**
* Creates a connection, managed with given handler.
*
* @param h connection handler
* @return a new Acolyte connection
*
* <pre>
* {@code
* connection(handler); // without connection properties
* }
* </pre>
*/
public static Connection connection(ConnectionHandler h) {
return Driver.connection(h);
}
/**
* Creates a connection, managed with given handler.
*
* @param h connection handler
* @param p connection properties
* @return a new Acolyte connection
*
* <pre>
* {@code
* // With connection property to fallback untyped null
* connection(handler, properties); //e.g. "acolyte.parameter.untypedNull
* }
* </pre>
*/
public static Connection connection(ConnectionHandler h, Properties p) {
return Driver.connection(h, p);
}
/**
* Creates an empty handler.
*
* <pre>
* {@code
* import static acolyte.jdbc.AcolyteDSL.connection;
* import static acolyte.jdbc.AcolyteDSL.handleStatement;
*
* connection(handleStatement);
* }
* </pre>
*/
public static final Java8CompositeHandler handleStatement =
new Java8CompositeHandler(null, null, null);
/**
* Creates a new handler detecting all statements as queries
* (like `handleStatement.withQueryDetection(".*").withQueryHandler(h)`).
*
* @param h the new query handler
*
* <pre>
* {@code
* import acolyte.jdbc.StatementHandler.Parameter;
* import static acolyte.jdbc.AcolyteDSL.connection;
* import static acolyte.jdbc.AcolyteDSL.handleQuery;
*
* connection(handleQuery((String sql, List<Parameter> ps) -> {
* if (sql == "SELECT * FROM Test WHERE id = ?") return aQueryResult;
* else otherResult
* }));
* }
* </pre>
*/
public static Java8CompositeHandler handleQuery(QueryHandler h) {
return AcolyteDSL.handleStatement.
withQueryDetection(".*").withQueryHandler(h);
}
/**
* Executes |f| using a connection accepting only queries,
* and answering with |result| to any query.
*
* <pre>
* {@code
* import acolyte.jdbc.AcolyteDSL.withQueryResult
*
* val str: String = withQueryResult(queryRes) { con => "str" }
* }
* </pre>
*/
public static <A> A withQueryResult(QueryResult res,
Function<java.sql.Connection, A> f) {
return f.apply(connection(handleQuery((x, y) -> res)));
}
/**
* Returns an update result with row |count| and generated |keys|.
* @param count Updated (row) count
* @param keys Generated keys
*
* <pre>
* {@code
* import acolyte.jdbc.AcolyteDSL.updateResult
* import acolyte.jdbc.RowLists
*
* updateResult(2, RowLists.stringList("a", "b"))
* }
* </pre>
*/
public static UpdateResult updateResult(int count, RowList<?> keys) {
return new UpdateResult(count).withGeneratedKeys(keys);
}
}
@@ -0,0 +1,91 @@
package acolyte.jdbc;
import java.util.regex.Pattern;
public final class Java8CompositeHandler extends AbstractCompositeHandler {
Java8CompositeHandler(Pattern[] qd, QueryHandler qh, UpdateHandler uh) {
super(qd, qh, uh);
}
/**
* Returns handler that detects statement matching given pattern(s)
* as query.
*
* @param pattern the new pattern for query detection
*
* <pre>
* {@code
* import static acolyte.jdbc.AcolyteDSL.handleStatement;
*
* // Created handle will detect as query statements
* // either starting with 'SELECT ' or containing 'EXEC proc'.
* handleStatement.withQueryDetection("^SELECT ", "EXEC proc");
* }
* </pre>
*/
public Java8CompositeHandler withQueryDetection(String... pattern) {
final Pattern[] ps = new Pattern[pattern.length];
for (int i = 0; i < ps.length; i++) {
ps[i] = Pattern.compile(pattern[i]);
}
return withQueryDetection(ps);
}
/**
* {@inheritDoc}
*/
public Java8CompositeHandler withQueryDetection(Pattern... pattern) {
return new Java8CompositeHandler(pattern, this.queryHandler, this.updateHandler);
}
/**
* Returns handler that delegates query execution to |h| function.
* Given function will be used only if executed statement is detected
* as a query by withQueryDetection.
*
* @param h the new query handler
*
* <pre>
* {@code
* import acolyte.jdbc.StatementHandler.Parameter;
* import static acolyte.jdbc.AcolyteDSL.handleStatement;
*
* handleStatement.withQueryHandler((String sql, List<Parameter> ps) -> {
* if (sql == "SELECT * FROM Test WHERE id = ?") return aQueryResult;
* else otherResult
* });
* }
* </pre>
*/
public Java8CompositeHandler withQueryHandler(QueryHandler h) {
return new Java8CompositeHandler(this.queryDetection, h, this.updateHandler);
}
/**
* Returns handler that delegates update execution to |h| function.
* Given function will be used only if executed statement is not detected
* as a query by withQueryDetection.
*
* @param h the new update handler
*
* <pre>
* {@code
* import acolyte.jdbc.UpdateResult;
* import acolyte.jdbc.StatementHandler.Parameter;
* import static acolyte.jdbc.AcolyteDSL.handleStatement;
*
* handleStatement.withUpdateHandler((String sql, List<Parameter> ps) -> {
* if (sql == "INSERT INTO Country (code, name) VALUES (?, ?)") {
* return new UpdateResult(1) // update count
* } else return otherResult
* });
* }
* </pre>
*/
public Java8CompositeHandler withUpdateHandler(UpdateHandler h) {
return new Java8CompositeHandler(this.queryDetection, this.queryHandler, h);
}
}
Oops, something went wrong.

0 comments on commit a8ed90b

Please sign in to comment.