KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > handler > RowIdHandler


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: RowIdHandler.java,v 1.3 2005/03/02 07:16:16 bzimmer Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 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 JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.sql.Statement JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
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: 1.3 $
29  */

30 public abstract class RowIdHandler extends FilterDataHandler {
31
32   private static Map JavaDoc ROWIDS = new HashMap JavaDoc();
33   private static Object JavaDoc CHECKED = new Object JavaDoc();
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 JavaDoc 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 JavaDoc stmt) throws SQLException JavaDoc {
54
55     Class JavaDoc c = stmt.getClass();
56     Object JavaDoc o = ROWIDS.get(c);
57
58     if (o == null) {
59       synchronized (ROWIDS) {
60         try {
61           o = c.getMethod(getRowIdMethodName(), (Class JavaDoc[])null);
62           ROWIDS.put(c, o);
63         } catch (Throwable JavaDoc t) {
64           ROWIDS.put(c, CHECKED);
65         }
66       }
67     }
68
69     if (!(o == null || o == CHECKED)) {
70       try {
71         return Py.java2py(((Method JavaDoc) o).invoke(stmt, (Object JavaDoc[])null));
72       } catch (Throwable JavaDoc t) {}
73     }
74
75     return super.getRowId(stmt);
76   }
77
78 }
79
Popular Tags