aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/trygvis/esper/testing/util/sql
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-12-22 00:31:00 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2012-12-22 00:32:28 +0100
commitc8c863ce36f57954369a0b4a15e6c5e720f03f87 (patch)
tree2e49e11db5be949571642ceca947bb7b2178c777 /src/main/java/io/trygvis/esper/testing/util/sql
parent012b0864e95e120ea57433ab0e719cc6011c7647 (diff)
downloadesper-testing-c8c863ce36f57954369a0b4a15e6c5e720f03f87.tar.gz
esper-testing-c8c863ce36f57954369a0b4a15e6c5e720f03f87.tar.bz2
esper-testing-c8c863ce36f57954369a0b4a15e6c5e720f03f87.tar.xz
esper-testing-c8c863ce36f57954369a0b4a15e6c5e720f03f87.zip
o Moving stuff to utils package.
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing/util/sql')
-rw-r--r--src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java18
-rw-r--r--src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java15
-rw-r--r--src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java109
3 files changed, 142 insertions, 0 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java
new file mode 100644
index 0000000..9e42242
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java
@@ -0,0 +1,18 @@
+package io.trygvis.esper.testing.util.sql;
+
+import java.sql.*;
+
+public class ResultSetF {
+ public static final SqlF<ResultSet, Integer> getInt = new SqlF<ResultSet, Integer>() {
+ public Integer apply(ResultSet rs) throws SQLException {
+ return rs.getInt(1);
+ }
+ };
+
+ public static final SqlF<ResultSet, Integer> getInteger = new SqlF<ResultSet, Integer>() {
+ public Integer apply(ResultSet rs) throws SQLException {
+ int i = rs.getInt(1);
+ return rs.wasNull() ? null : i;
+ }
+ };
+}
diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java
new file mode 100644
index 0000000..e4e8197
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java
@@ -0,0 +1,15 @@
+package io.trygvis.esper.testing.util.sql;
+
+import java.sql.*;
+
+public abstract class SqlF<A, B> {
+ public abstract B apply(A a) throws SQLException;
+
+ public <C> SqlF<A, C> andThen(final SqlF<B, C> f) {
+ return new SqlF<A, C>() {
+ public C apply(A a) throws SQLException {
+ return f.apply(SqlF.this.apply(a));
+ }
+ };
+ }
+}
diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java
new file mode 100644
index 0000000..286a872
--- /dev/null
+++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java
@@ -0,0 +1,109 @@
+package io.trygvis.esper.testing.util.sql;
+
+import java.sql.*;
+
+public abstract class SqlOption<A> {
+ public static <A> SqlOption<A> none() {
+ return new None<>();
+ }
+
+ public static <A> SqlOption<A> some(A a) {
+ return new Some<>(a);
+ }
+
+ public static SqlOption<ResultSet> fromRs(ResultSet rs) throws SQLException {
+ if (!rs.next()) {
+ return none();
+ }
+
+ return some(rs);
+ }
+
+ // -----------------------------------------------------------------------
+ //
+ // -----------------------------------------------------------------------
+
+ public abstract <B> SqlOption<B> map(SqlF<A, B> f) throws SQLException;
+
+ public <B> SqlOption<B> flatMap(SqlF<A, SqlOption<B>> f) throws SQLException {
+ SqlOption<SqlOption<B>> x = map(f);
+
+ if (x.isNone()) {
+ return none();
+ }
+
+ return x.get();
+ }
+
+ public abstract A get() throws SQLException;
+
+ public abstract boolean isSome();
+
+ public boolean isNone() {
+ return !isSome();
+ }
+
+ public abstract A getOrElse(A a);
+
+ public static <A> SqlOption<A> fromNull(A a) {
+ if (a != null) {
+ return some(a);
+ } else {
+ return none();
+ }
+ }
+
+ // -----------------------------------------------------------------------
+ //
+ // -----------------------------------------------------------------------
+
+ private static class None<A> extends SqlOption<A> {
+ public <B> SqlOption<B> map(SqlF<A, B> f) {
+ return none();
+ }
+
+ public A get() throws SQLException {
+ throw new SQLException("get() on None");
+ }
+
+ public boolean isSome() {
+ return false;
+ }
+
+ public A getOrElse(A a) {
+ return a;
+ }
+
+ public String toString() {
+ return "None";
+ }
+ }
+
+ private static class Some<A> extends SqlOption<A> {
+ private final A a;
+
+ private Some(A a) {
+ this.a = a;
+ }
+
+ public <B> SqlOption<B> map(SqlF<A, B> f) throws SQLException {
+ return some(f.apply(a));
+ }
+
+ public A get() {
+ return a;
+ }
+
+ public boolean isSome() {
+ return true;
+ }
+
+ public A getOrElse(A a) {
+ return this.a;
+ }
+
+ public String toString() {
+ return "Some(" + a + ")";
+ }
+ }
+}