Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cchantep
committed
Apr 25, 2015
1 parent
679f282
commit a8ed90b
Showing
12 changed files
with
848 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
} | |||
} |
91 changes: 91 additions & 0 deletions
91
jdbc-java8/src/main/java/acolyte/jdbc/Java8CompositeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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.