KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > pipe > db > BaseDB


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

9 package com.ziclix.python.sql.pipe.db;
10
11 import com.ziclix.python.sql.DataHandler;
12 import com.ziclix.python.sql.PyConnection;
13 import com.ziclix.python.sql.PyCursor;
14 import com.ziclix.python.sql.zxJDBC;
15 import org.python.core.Py;
16
17 import java.lang.reflect.Constructor JavaDoc;
18
19 /**
20  * Abstract class to assist in generating cursors.
21  *
22  * @author brian zimmer
23  * @version $Revision: 1.3 $
24  */

25 public abstract class BaseDB {
26
27     /**
28      * Field cursor
29      */

30     protected PyCursor cursor;
31
32     /**
33      * Field dataHandler
34      */

35     protected Class JavaDoc dataHandler;
36
37     /**
38      * Field tableName
39      */

40     protected String JavaDoc tableName;
41
42     /**
43      * Field connection
44      */

45     protected PyConnection connection;
46
47     /**
48      * Construct the helper.
49      */

50     public BaseDB(PyConnection connection, Class JavaDoc dataHandler, String JavaDoc tableName) {
51
52         this.tableName = tableName;
53         this.dataHandler = dataHandler;
54         this.connection = connection;
55         this.cursor = this.cursor();
56     }
57
58     /**
59      * Create a new constructor and optionally bind a new DataHandler. The new DataHandler must act as
60      * a Decorator, having a single argument constructor of another DataHandler. The new DataHandler is
61      * then expected to delegate all calls to the original while enhancing the functionality in any matter
62      * desired. This allows additional functionality without losing any previous work or requiring any
63      * complicated inheritance dependencies.
64      */

65     protected PyCursor cursor() {
66
67         PyCursor cursor = this.connection.cursor(true);
68         DataHandler origDataHandler = cursor.getDataHandler(), newDataHandler = null;
69
70         if ((origDataHandler != null) && (this.dataHandler != null)) {
71             Constructor JavaDoc cons = null;
72
73             try {
74                 Class JavaDoc[] args = new Class JavaDoc[1];
75
76                 args[0] = DataHandler.class;
77                 cons = this.dataHandler.getConstructor(args);
78             } catch (Exception JavaDoc e) {
79                 return cursor;
80             }
81
82             if (cons == null) {
83                 String JavaDoc msg = zxJDBC.getString("invalidCons", new Object JavaDoc[]{this.dataHandler.getName()});
84
85                 throw zxJDBC.makeException(msg);
86             }
87
88             try {
89                 Object JavaDoc[] args = new Object JavaDoc[1];
90
91                 args[0] = origDataHandler;
92                 newDataHandler = (DataHandler) cons.newInstance(args);
93             } catch (Exception JavaDoc e) {
94                 return cursor;
95             }
96
97             if (newDataHandler != null) {
98                 cursor.__setattr__("datahandler", Py.java2py(newDataHandler));
99             }
100         }
101
102         return cursor;
103     }
104 }
105
Popular Tags