KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: InformixDataHandler.java,v 1.5 2005/05/16 06:43:33 otmarhumbel 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.DataHandler;
12 import com.ziclix.python.sql.FilterDataHandler;
13 import org.python.core.Py;
14 import org.python.core.PyFile;
15 import org.python.core.PyObject;
16 import org.python.core.PyString;
17
18 import java.io.InputStream JavaDoc;
19 import java.sql.Blob JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.ResultSet JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Statement JavaDoc;
24 import java.sql.Types JavaDoc;
25
26 /**
27  * Informix specific data handling.
28  *
29  * @author brian zimmer
30  * @author last revised by $Author: otmarhumbel $
31  * @version $Revision: 1.5 $
32  */

33 public class InformixDataHandler extends FilterDataHandler {
34
35     /**
36      * Decorator for handling Informix specific issues.
37      *
38      * @param datahandler the delegate DataHandler
39      */

40     public InformixDataHandler(DataHandler datahandler) {
41         super(datahandler);
42     }
43
44     /**
45      * Returns the serial for the statement.
46      *
47      * @param stmt
48      * @return PyObject
49      * @throws SQLException
50      */

51     public PyObject getRowId(Statement JavaDoc stmt) throws SQLException JavaDoc {
52
53         if (stmt instanceof com.informix.jdbc.IfmxStatement) {
54             return Py.newInteger(((com.informix.jdbc.IfmxStatement) stmt).getSerial());
55         }
56
57         return super.getRowId(stmt);
58     }
59
60     /**
61      * Provide fixes for Ifx driver.
62      *
63      * @param stmt
64      * @param index
65      * @param object
66      * @param type
67      * @throws SQLException
68      */

69     public void setJDBCObject(PreparedStatement JavaDoc stmt, int index, PyObject object, int type) throws SQLException JavaDoc {
70
71         if (DataHandler.checkNull(stmt, index, object, type)) {
72             return;
73         }
74
75         switch (type) {
76
77             case Types.LONGVARCHAR:
78
79                 String JavaDoc varchar;
80                 // Ifx driver can't handle the setCharacterStream() method so use setObject() instead
81
if (object instanceof PyFile) {
82                     varchar = ((PyFile) object).read();
83                 } else {
84                     varchar = (String JavaDoc) object.__tojava__(String JavaDoc.class);
85                 }
86                 stmt.setObject(index, varchar, type);
87                 break;
88
89             case Types.OTHER:
90
91                 // this is most likely an Informix boolean
92
stmt.setBoolean(index, object.__nonzero__());
93                 break;
94
95             default :
96                 super.setJDBCObject(stmt, index, object, type);
97         }
98     }
99
100     /**
101      * Provide fixes for Ifx driver.
102      *
103      * @param stmt
104      * @param index
105      * @param object
106      * @throws SQLException
107      */

108     public void setJDBCObject(PreparedStatement JavaDoc stmt, int index, PyObject object) throws SQLException JavaDoc {
109
110         // there is a bug in the Ifx driver when using setObject() with a String for a prepared statement
111
if (object instanceof PyString) {
112             super.setJDBCObject(stmt, index, object, Types.VARCHAR);
113         } else {
114             super.setJDBCObject(stmt, index, object);
115         }
116     }
117
118     /**
119      * Override to handle Informix related issues.
120      *
121      * @param set the result set
122      * @param col the column number
123      * @param type the SQL type
124      * @return the mapped Python object
125      * @throws SQLException thrown for a sql exception
126      */

127     public PyObject getPyObject(ResultSet JavaDoc set, int col, int type) throws SQLException JavaDoc {
128
129         PyObject obj = Py.None;
130
131         switch (type) {
132
133             case Types.OTHER:
134                 try {
135
136                     // informix returns boolean as OTHERs, so let's give that a try
137
obj = set.getBoolean(col) ? Py.One : Py.Zero;
138                 } catch (SQLException JavaDoc e) {
139                     obj = super.getPyObject(set, col, type);
140                 }
141                 break;
142
143             case Types.BLOB:
144                 int major = set.getStatement().getConnection().getMetaData().getDriverMajorVersion();
145                 int minor = set.getStatement().getConnection().getMetaData().getDriverMinorVersion();
146
147                 if ((major <= 2) && (minor <= 11)) {
148                     Blob JavaDoc blob = set.getBlob(col);
149
150                     if (blob == null) {
151                         obj = Py.None;
152                     } else {
153                         InputStream JavaDoc is = null;
154
155                         try {
156
157                             // the available() bug means we CANNOT buffer this stream
158
is = blob.getBinaryStream();
159                             obj = Py.java2py(DataHandler.read(is));
160                         } finally {
161                             try {
162                                 is.close();
163                             } catch (Exception JavaDoc e) {
164                             }
165                         }
166                     }
167
168                     break;
169                 }
170             default :
171                 obj = super.getPyObject(set, col, type);
172         }
173
174         return (set.wasNull() || (obj == null)) ? Py.None : obj;
175     }
176 }
177
Popular Tags