001: /*
002: * Jython Database Specification API 2.0
003: *
004: * $Id: BaseDB.java 2414 2005-02-23 04:26:23Z bzimmer $
005: *
006: * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
007: *
008: */
009: package com.ziclix.python.sql.pipe.db;
010:
011: import com.ziclix.python.sql.DataHandler;
012: import com.ziclix.python.sql.PyConnection;
013: import com.ziclix.python.sql.PyCursor;
014: import com.ziclix.python.sql.zxJDBC;
015: import org.python.core.Py;
016:
017: import java.lang.reflect.Constructor;
018:
019: /**
020: * Abstract class to assist in generating cursors.
021: *
022: * @author brian zimmer
023: * @version $Revision: 2414 $
024: */
025: public abstract class BaseDB {
026:
027: /**
028: * Field cursor
029: */
030: protected PyCursor cursor;
031:
032: /**
033: * Field dataHandler
034: */
035: protected Class dataHandler;
036:
037: /**
038: * Field tableName
039: */
040: protected String tableName;
041:
042: /**
043: * Field connection
044: */
045: protected PyConnection connection;
046:
047: /**
048: * Construct the helper.
049: */
050: public BaseDB(PyConnection connection, Class dataHandler,
051: String tableName) {
052:
053: this .tableName = tableName;
054: this .dataHandler = dataHandler;
055: this .connection = connection;
056: this .cursor = this .cursor();
057: }
058:
059: /**
060: * Create a new constructor and optionally bind a new DataHandler. The new DataHandler must act as
061: * a Decorator, having a single argument constructor of another DataHandler. The new DataHandler is
062: * then expected to delegate all calls to the original while enhancing the functionality in any matter
063: * desired. This allows additional functionality without losing any previous work or requiring any
064: * complicated inheritance dependencies.
065: */
066: protected PyCursor cursor() {
067:
068: PyCursor cursor = this .connection.cursor(true);
069: DataHandler origDataHandler = cursor.getDataHandler(), newDataHandler = null;
070:
071: if ((origDataHandler != null) && (this .dataHandler != null)) {
072: Constructor cons = null;
073:
074: try {
075: Class[] args = new Class[1];
076:
077: args[0] = DataHandler.class;
078: cons = this .dataHandler.getConstructor(args);
079: } catch (Exception e) {
080: return cursor;
081: }
082:
083: if (cons == null) {
084: String msg = zxJDBC.getString("invalidCons",
085: new Object[] { this .dataHandler.getName() });
086:
087: throw zxJDBC.makeException(msg);
088: }
089:
090: try {
091: Object[] args = new Object[1];
092:
093: args[0] = origDataHandler;
094: newDataHandler = (DataHandler) cons.newInstance(args);
095: } catch (Exception e) {
096: return cursor;
097: }
098:
099: if (newDataHandler != null) {
100: cursor.__setattr__("datahandler", Py
101: .java2py(newDataHandler));
102: }
103: }
104:
105: return cursor;
106: }
107: }
|