01: /*
02: * Jython Database Specification API 2.0
03: *
04: * $Id: RowIdHandler.java 2429 2005-03-02 07:16:17Z bzimmer $
05: *
06: * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
07: *
08: */
09: package com.ziclix.python.sql.handler;
10:
11: import com.ziclix.python.sql.FilterDataHandler;
12: import com.ziclix.python.sql.DataHandler;
13:
14: import java.util.Map;
15: import java.util.HashMap;
16: import java.sql.Statement;
17: import java.sql.SQLException;
18: import java.lang.reflect.Method;
19:
20: import org.python.core.PyObject;
21: import org.python.core.Py;
22:
23: /**
24: * Handle the rowid methods since the API is not available until JDBC 3.0.
25: *
26: * @author brian zimmer
27: * @author last revised by $Author: bzimmer $
28: * @version $Revision: 2429 $
29: */
30: public abstract class RowIdHandler extends FilterDataHandler {
31:
32: private static Map ROWIDS = new HashMap();
33: private static Object CHECKED = new Object();
34:
35: public RowIdHandler(DataHandler handler) {
36: super (handler);
37: }
38:
39: /**
40: * Return the name of the method that returns the last row id. The
41: * method can take no arguments but the return type is flexible and
42: * will be figured out by the Jython runtime system.
43: * @return
44: */
45: protected abstract String getRowIdMethodName();
46:
47: /**
48: * Return the row id of the last insert statement.
49: * @param stmt
50: * @return an object representing the last row id
51: * @throws SQLException
52: */
53: public PyObject getRowId(Statement stmt) throws SQLException {
54:
55: Class c = stmt.getClass();
56: Object o = ROWIDS.get(c);
57:
58: if (o == null) {
59: synchronized (ROWIDS) {
60: try {
61: o = c.getMethod(getRowIdMethodName(),
62: (Class[]) null);
63: ROWIDS.put(c, o);
64: } catch (Throwable t) {
65: ROWIDS.put(c, CHECKED);
66: }
67: }
68: }
69:
70: if (!(o == null || o == CHECKED)) {
71: try {
72: return Py.java2py(((Method) o).invoke(stmt,
73: (Object[]) null));
74: } catch (Throwable t) {
75: }
76: }
77:
78: return super.getRowId(stmt);
79: }
80:
81: }
|