-
Notifications
You must be signed in to change notification settings - Fork 1
/
BatchInsertGenKeysOp.java
98 lines (79 loc) · 3.25 KB
/
BatchInsertGenKeysOp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.novarto.sanedbc.core.ops;
import com.novarto.lang.CanBuildFrom;
import com.novarto.lang.Collections;
import fj.F;
import fj.control.db.DB;
import fj.data.Option;
import fj.function.Try1;
import fj.function.TryEffect1;
import java.sql.*;
import static com.novarto.sanedbc.core.ops.Binders.batchBinder;
/**
* Batch insert operation which returns the auto-generated keys created for the inserted entries. Please consider that the used
* JDBC driver may not support the retrieval of auto-generated key and this operation may fail with {@link
* SQLFeatureNotSupportedException}
*
* @param <A> Type of single entry to be inserted
* @param <B> Type of the result which must extend {@link Number}
* @param <C1> The type of the optional (mutable) buffer which is intermediately used while constructing the result.
* @param <C2> Type of the result
* @see Statement#getGeneratedKeys()
*/
public abstract class BatchInsertGenKeysOp<A, B extends Number, C1 extends Iterable<B>, C2 extends Iterable<B>> extends DB<C2>
{
private final String sql;
private final Try1<PreparedStatement, Option<Integer>, SQLException> binder;
private final Try1<ResultSet, B, SQLException> getKey;
private final CanBuildFrom<B, C1, C2> cbf;
private final Iterable<A> as;
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;
}
@Override
public C2 run(Connection c) throws SQLException
{
if (Collections.isEmpty(as))
{
return cbf.build(cbf.createBuffer());
}
try (PreparedStatement preparedStatement = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS))
{
binder.f(preparedStatement);
C1 buf = cbf.createBuffer();
try (final ResultSet generatedKeys = preparedStatement.getGeneratedKeys())
{
while (generatedKeys.next())
{
buf = cbf.add(getKey.f(generatedKeys), buf);
}
if (Collections.isEmpty(buf))
{
throw new SQLException("No Auto Generated Keys in Result Set!");
}
return cbf.build(buf);
}
}
}
public static class List<A, B extends Number> extends BatchInsertGenKeysOp<A, B, java.util.List<B>, java.util.List<B>>
{
public List(String sql, F<A, TryEffect1<PreparedStatement, SQLException>> binder, Iterable<A> as,
Try1<ResultSet, B, SQLException> getKey)
{
super(sql, binder, as, getKey, CanBuildFrom.listCanBuildFrom());
}
}
public static class FjList<A, B extends Number> extends BatchInsertGenKeysOp<A, B, fj.data.List.Buffer<B>, fj.data.List<B>>
{
public FjList(String sql, F<A, TryEffect1<PreparedStatement, SQLException>> binder, Iterable<A> as,
Try1<ResultSet, B, SQLException> getKey)
{
super(sql, binder, as, getKey, CanBuildFrom.fjListCanBuildFrom());
}
}
}