Permalink
Browse files

[jdbc-java8] Acolyte DSL for JDBC.

  • Loading branch information...
cchantep
cchantep committed Apr 9, 2015
1 parent 05d7fba commit 56ca676e19164a251801579397c2411a9a503505
@@ -91,6 +91,8 @@
if (t == Types.BIT
|| t == Types.REAL
|| t == Types.DECIMAL
|| t == Types.VARBINARY
|| t == Types.LONGVARBINARY
|| t == Types.LONGVARCHAR) continue; // Skip
classes.put(mappings.get(t), t);
@@ -3,12 +3,26 @@
import java.util.Properties;
import java.util.function.Function;
import static acolyte.jdbc.Driver.Property;
import static acolyte.jdbc.CompositeHandler.QueryHandler;
import acolyte.jdbc.Java8CompositeHandler.ScalarQueryHandler;
/**
* Acolyte DSL for JDBC.
*/
public final class AcolyteDSL {
/**
* Creates a configuration property for the Acolyte driver.
*
* @param name the name of the configuration property
* @param value the configuration value
* @return the created property
*/
public static Property prop(String name, String value) {
return new Property(name, value);
}
/**
* Creates a connection, whose statement will be passed to given handler.
*
@@ -17,6 +31,8 @@
*
* <pre>
* {@code
* import static acolyte.jdbc.AcolyteDSL.connection;
*
* connection(handler); // without connection properties
* }}}
* }
@@ -29,18 +45,22 @@ public static Connection connection(AbstractCompositeHandler<?> h) {
/**
* Creates a connection, whose statement will be passed to given handler.
*
* @param h statement handler
* @param p connection properties
* @param h the statement handler
* @param ps the configuration properties for the new connection
* @return a new Acolyte connection
*
* <pre>
* {@code
* import acolyte.jdbc.AcolyteDSL;
* import static acolyte.jdbc.AcolyteDSL.connection;
* import static acolyte.jdbc.AcolyteDSL.prop;
*
* // With connection property to fallback untyped null
* connection(handler, properties); //e.g. "acolyte.parameter.untypedNull"
* connection(handler, prop("acolyte.parameter.untypedNull", "true"));
* }
* </pre>
*/
public static Connection connection(AbstractCompositeHandler<?> h, Properties p) { return Driver.connection(h, p); }
public static Connection connection(AbstractCompositeHandler<?> h, Property... ps) { return Driver.connection(h, ps); }
/**
* Creates a connection, managed with given handler.
@@ -67,13 +87,17 @@ public static Connection connection(ConnectionHandler h) {
*
* <pre>
* {@code
* import acolyte.jdbc.AcolyteDSL;
* import static acolyte.jdbc.AcolyteDSL.connection;
* import static acolyte.jdbc.AcolyteDSL.prop;
*
* // With connection property to fallback untyped null
* connection(handler, properties); //e.g. "acolyte.parameter.untypedNull
* connection(handler, prop("acolyte.parameter.untypedNull", "true"));
* }
* </pre>
*/
public static Connection connection(ConnectionHandler h, Properties p) {
return Driver.connection(h, p);
public static Connection connection(ConnectionHandler h, Property... ps) {
return Driver.connection(h, ps);
}
/**
@@ -105,7 +129,7 @@ public static Connection connection(ConnectionHandler h, Properties p) {
*
* connection(handleQuery((String sql, List<Parameter> ps) -> {
* if (sql == "SELECT * FROM Test WHERE id = ?") return aQueryResult;
* else otherResult
* else return otherResult;
* }));
* }
* </pre>
@@ -115,15 +139,39 @@ public static Java8CompositeHandler handleQuery(QueryHandler h) {
withQueryDetection(".*").withQueryHandler(h);
}
/**
* 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 "Foo";
* else return "Bar";
* }));
* }
* </pre>
*/
public static Java8CompositeHandler handleQuery2(ScalarQueryHandler h) {
return AcolyteDSL.handleStatement.
withQueryDetection(".*").withQueryHandler2(h);
}
/**
* Executes |f| using a connection accepting only queries,
* and answering with |result| to any query.
*
* <pre>
* {@code
* import acolyte.jdbc.AcolyteDSL.withQueryResult
* import static acolyte.jdbc.AcolyteDSL.withQueryResult;
*
* val str: String = withQueryResult(queryRes) { con => "str" }
* String str = withQueryResult(queryRes, con -> "str");
* }
* </pre>
*/
@@ -135,15 +183,15 @@ public static Java8CompositeHandler handleQuery(QueryHandler h) {
/**
* Returns an update result with row |count| and generated |keys|.
* @param count Updated (row) count
* @param keys Generated keys
* @param count the updated (row) count
* @param keys the generated keys
*
* <pre>
* {@code
* import acolyte.jdbc.AcolyteDSL.updateResult
* import acolyte.jdbc.RowLists
* import acolyte.jdbc.AcolyteDSL.updateResult;
* import acolyte.jdbc.RowLists;
*
* updateResult(2, RowLists.stringList("a", "b"))
* updateResult(2, RowLists.stringList("a", "b"));
* }
* </pre>
*/
@@ -1,7 +1,14 @@
package acolyte.jdbc;
import java.util.List;
import java.util.regex.Pattern;
import java.util.function.Function;
import java.sql.SQLException;
import acolyte.jdbc.StatementHandler.Parameter;
public final class Java8CompositeHandler extends AbstractCompositeHandler {
Java8CompositeHandler(Pattern[] qd, QueryHandler qh, UpdateHandler uh) {
super(qd, qh, uh);
@@ -54,7 +61,7 @@ public Java8CompositeHandler withQueryDetection(Pattern... pattern) {
*
* handleStatement.withQueryHandler((String sql, List<Parameter> ps) -> {
* if (sql == "SELECT * FROM Test WHERE id = ?") return aQueryResult;
* else otherResult
* else return otherResult;
* });
* }
* </pre>
@@ -63,6 +70,79 @@ public Java8CompositeHandler withQueryHandler(QueryHandler h) {
return new Java8CompositeHandler(this.queryDetection, h, this.updateHandler);
}
/** A query handler whose result is a list of row. */
public static interface RowListQueryHandler {
public RowList apply(String sql, List<Parameter> parameters)
throws SQLException;
}
/**
* 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.withQueryHandler1((String sql, List<Parameter> ps) -> {
* if (sql == "SELECT * FROM Test WHERE id = ?") return aRowList;
* else return anotherRowList;
* });
* }
* </pre>
*/
public Java8CompositeHandler withQueryHandler1(RowListQueryHandler h) {
final QueryHandler qh = new QueryHandler() {
public QueryResult apply(String sql, List<Parameter> ps)
throws SQLException {
return h.apply(sql, ps).asResult();
}
};
return withQueryHandler(qh);
}
/** A query handler whose result is a single row with a single value. */
public static interface ScalarQueryHandler<T> {
public <T> T apply(String sql, List<Parameter> parameters)
throws SQLException;
}
/**
* 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.withQueryHandler2((String sql, List<Parameter> ps) -> {
* if (sql == "SELECT * FROM Test WHERE id = ?") return "Foo";
* else return "Bar";
* });
* }
* </pre>
*/
public <T> Java8CompositeHandler withQueryHandler2(ScalarQueryHandler<T> h) {
final QueryHandler qh = new QueryHandler() {
public QueryResult apply(String sql, List<Parameter> ps)
throws SQLException {
return RowLists.scalar(h.apply(sql, ps)).asResult();
}
};
return withQueryHandler(qh);
}
/**
* Returns handler that delegates update execution to |h| function.
* Given function will be used only if executed statement is not detected
@@ -78,8 +158,8 @@ public Java8CompositeHandler withQueryHandler(QueryHandler h) {
*
* handleStatement.withUpdateHandler((String sql, List<Parameter> ps) -> {
* if (sql == "INSERT INTO Country (code, name) VALUES (?, ?)") {
* return new UpdateResult(1) // update count
* } else return otherResult
* return new UpdateResult(1); // update count
* } else return otherResult;
* });
* }
* </pre>
@@ -88,4 +168,41 @@ public Java8CompositeHandler withUpdateHandler(UpdateHandler h) {
return new Java8CompositeHandler(this.queryDetection, this.queryHandler, h);
}
/**
* An update handler which return the count of successfully updated rows.
*/
static interface CountUpdateHandler {
public int apply(String sql, List<Parameter> parameters);
}
/**
* 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.withUpdateHandler1((String sql, List<Parameter> ps) -> {
* if (sql == "INSERT INTO Country (code, name) VALUES (?, ?)") {
* return 1; // update count
* } else return 0;
* });
* }
* </pre>
*/
public Java8CompositeHandler withUpdateHandler1(CountUpdateHandler h) {
final UpdateHandler uh = new UpdateHandler() {
public UpdateResult apply(String sql, List<Parameter> ps) {
return new UpdateResult(h.apply(sql, ps));
}
};
return withUpdateHandler(uh);
}
}
Oops, something went wrong.

0 comments on commit 56ca676

Please sign in to comment.