Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions core/src/main/java/fj/control/db/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fj.F;
import fj.Function;
import fj.function.Try1;

import java.sql.Connection;
import java.sql.SQLException;
Expand Down Expand Up @@ -35,6 +36,21 @@ public A run(final Connection c) {
};
}

/**
* Constructs a database action as a function from a database connection to a value.
*
* @param t A function from a database connection to a value allowed to throw
* SQLException
* @return A database action representing the given function.
*/
public static <A> DB<A> db(final Try1<Connection, A, SQLException> t){
return new DB<A>() {
public A run(final Connection c) throws SQLException {
return t.f(c);
}
};
}

/**
* Returns the callable-valued function projection of this database action.
*
Expand Down
25 changes: 12 additions & 13 deletions core/src/test/java/fj/control/db/TestDbState.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fj.Unit;
import fj.data.Option;
import fj.function.Try1;
import org.apache.commons.dbutils.DbUtils;
import org.junit.Test;

Expand All @@ -16,20 +17,18 @@ public void testWriter() throws SQLException {
final int TEN = 10;
DbState writer = DbState.writer(DbState.driverManager("jdbc:h2:mem:"));

DB<Unit> setup = new DB<Unit>() {
@Override
public Unit run(Connection c) throws SQLException {
Statement s = null;
try {
s = c.createStatement();
assertThat(s.executeUpdate("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))"), is(0));
assertThat(s.executeUpdate("INSERT INTO TEST (ID, NAME) VALUES (" + TEN + ", 'FOO')"), is(1));
} finally {
DbUtils.closeQuietly(s);
}
return Unit.unit();
DB<Unit> setup = DB.db((Try1<Connection, Unit, SQLException>) c -> {
Statement s = null;
try {
s = c.createStatement();
assertThat(s.executeUpdate("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))"), is(0));
assertThat(s.executeUpdate("INSERT INTO TEST (ID, NAME) VALUES (" + TEN + ", 'FOO')"), is(1));
} finally {
DbUtils.closeQuietly(s);
}
};
return Unit.unit();
});

DB<Option<Integer>> query = new DB<Option<Integer>>() {
@Override
public Option<Integer> run(Connection c) throws SQLException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gliptak You might want to do this method here while you are at it. I will merge and you can raise another PR if you make the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mperry I specifically left this unchanged, as it takes a different path in the codebase (as additional coverage) ...

Expand Down