public BatchUpdateOp(String sql, F<A, TryEffect1<PreparedStatement, SQLException>> binder, Iterable<A> as) { this.binder = batchBinder(binder, as); this.as = as; this.sql = sql; }
public EffectOp(String sql) { this.op = new UpdateOp(sql, NO_BINDER); }
public AggregateOp(String sql, TryEffect1<PreparedStatement, SQLException> binder) { this.selectOp = new SelectOp.List<>(sql, binder, x -> x.getLong(1)); }
private static DB<Option<String>> selectByUniqueDescOp(String desc) { return DbOps.unique(new SelectOp.FjList<>("SELECT * FROM MySqlTest_DATA WHERE DESCRIPTION=?", (preparedStatement) -> preparedStatement.setString(1, desc), resultSet -> resultSet.getString(2))); }
@Test public void nextValFromSequence() { DB.submit(new EffectOp("CREATE SEQUENCE MY_SEQ START WITH 1 MINVALUE 1")); final Option<Long> firstValFromSeqOp = DB.submit(unique(new SelectOp.FjList<>("call NEXT VALUE FOR MY_SEQ", NO_BINDER, rs -> rs.getLong(1)))); assertThat(firstValFromSeqOp.some(), is(1L)); final Option<Long> secondsValFromSeqOp = DB.submit(unique(new SelectOp.FjList<>("call NEXT VALUE FOR MY_SEQ", NO_BINDER, rs -> rs.getLong(1)))); assertThat(secondsValFromSeqOp.some(), is(2L)); }
/** * Shorthand of sequence() that returns an fj.data.List */ public static <A> DB<fj.data.List<A>> sequence(Iterable<DB<A>> xs) { return sequence(xs, fjListCanBuildFrom()); }
private DB<List<String>> selectAll() { return new SelectOp.FjList<>("SELECT * FROM BAR", NO_BINDER, rs -> rs.getString(1)); }
public static DB<Unit> createStuffTable() { //this specific table has auto-generated ID return new EffectOp("CREATE TABLE STUFF (ID INTEGER PRIMARY KEY IDENTITY, DESCRIPTION NVARCHAR(200) NOT NULL)"); }
@Override public Unit run(Connection c) throws SQLException { op.run(c); return Unit.unit(); } }
public static DB<Option<Integer>> insertMany(Iterable<String> descriptions) { return new BatchUpdateOp<>( //SQL "INSERT INTO STUFF(DESCRIPTION) VALUES(?)", //given the current element of the iterable, return a binder which sets the parameters for that element // if you extracted the binder from the previous operations in a field named SET_DESCRIPTION, code becomes // description -> SET_DESCRIPTION description -> ps -> ps.setString(1, description), //the iterables to insert descriptions ); }
public static DB<Integer> insertStuffGetKey(String description) { /* An InsertGenKeysOp is the same as an UpdateOp, only that it expects an auto-generated key to be present after executing the update, and returns it as the result. InsertGenKeysOp.Int is a specialization which expects an Integer key */ return new InsertGenKeysOp.Int( "INSERT INTO STUFF(DESCRIPTION) VALUES(?)", // you can see that the binder is the same as in the previous operation. // in that case it is good practice to extract it as a static final field of your module ps -> ps.setString(1, description) ); }
public static DB<Option<Stuff>> selectByKey(int id) { //given a regular operation which returns an iterable: DB<List<Stuff>> temp = new SelectOp.FjList<>( "SELECT ID, DESCRIPTION FROM STUFF WHERE ID=?", ps -> ps.setInt(1, id), rs -> new Stuff(rs.getInt(1), rs.getString(2))); // using the unique() function, // we can convert it to an operation which expects at most one result, and returns that optional result: return unique(temp); }
public static DB<Integer> insertStuff(String description) { // an UpdateOp takes SQL, and a binder. It returns an update count // a binder is a piece of code which takes a prepared statement, and returns nothing. // it is so called because it binds the prepared statement parameters return new UpdateOp( "INSERT INTO STUFF(DESCRIPTION) VALUES(?)", // bind the single parameter of the statement ps -> ps.setString(1, description) ); }
private DB<List<String>> selectAll() { return new SelectOp.FjList<>("SELECT * FROM BAR", NO_BINDER, rs -> rs.getString(1)); }
public BatchInsertGenKeysOp(String sql, F<A, TryEffect1<PreparedStatement, SQLException>> binder, Iterable<A> as, Try1<ResultSet, B, SQLException> getKey, CanBuildFrom<B, C1, C2> cbf) { this.sql = sql; this.binder = batchBinder(binder, as); this.getKey = getKey; this.cbf = cbf; this.as = as; }
private static DB<Option<User>> selectUser(String email) { return unique(new SelectOp.FjList<>( "SELECT * FROM USERS WHERE EMAIL=?", ps -> ps.setString(1, email), rs -> new User(rs.getString(1), rs.getString(2)) )); }
private static UpdateOp insertSingleChar(String dummy) { return new UpdateOp("INSERT INTO MySqlTest_IDS(DUMMY) VALUES (?)", (stmt) -> stmt.setString(1, dummy)); }
private DB<Integer> insert(String x) { return new UpdateOp("INSERT INTO BAR VALUES(?)", ps -> ps.setString(1, x)); }
private DB<Integer> insert(String x) { return new UpdateOp("INSERT INTO BAR VALUES(?)", ps -> ps.setString(1, x)); }