-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathRowIdHandler.java
More file actions
76 lines (63 loc) · 1.83 KB
/
RowIdHandler.java
File metadata and controls
76 lines (63 loc) · 1.83 KB
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
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql.handler;
import com.ziclix.python.sql.FilterDataHandler;
import com.ziclix.python.sql.DataHandler;
import java.util.Map;
import java.sql.Statement;
import java.sql.SQLException;
import java.lang.reflect.Method;
import org.python.core.PyObject;
import org.python.core.Py;
import org.python.util.Generic;
/**
* Handle the rowid methods since the API is not available until JDBC 3.0.
*
* @author brian zimmer
*/
public abstract class RowIdHandler extends FilterDataHandler {
private static Map<Class<?>, Object> ROWIDS = Generic.map();
private static Object CHECKED = new Object();
public RowIdHandler(DataHandler handler) {
super(handler);
}
/**
* Return the name of the method that returns the last row id. The
* method can take no arguments but the return type is flexible and
* will be figured out by the Jython runtime system.
* @return name of the method that returns the last row id
*/
protected abstract String getRowIdMethodName();
/**
* Return the row id of the last insert statement.
* @param stmt
* @return an object representing the last row id
* @throws SQLException
*/
@Override
public PyObject getRowId(Statement stmt) throws SQLException {
Class<?> c = stmt.getClass();
Object o = ROWIDS.get(c);
if (o == null) {
synchronized (ROWIDS) {
try {
o = c.getMethod(getRowIdMethodName(), (Class[])null);
ROWIDS.put(c, o);
} catch (Throwable t) {
ROWIDS.put(c, CHECKED);
}
}
}
if (!(o == null || o == CHECKED)) {
try {
return Py.java2py(((Method) o).invoke(stmt, (Object[])null));
} catch (Throwable t) {}
}
return super.getRowId(stmt);
}
}