001: /*
002: * Jython Database Specification API 2.0
003: *
004: * $Id: InformixDataHandler.java 2469 2005-05-16 06:43:33Z otmarhumbel $
005: *
006: * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
007: *
008: */
009: package com.ziclix.python.sql.handler;
010:
011: import com.ziclix.python.sql.DataHandler;
012: import com.ziclix.python.sql.FilterDataHandler;
013: import org.python.core.Py;
014: import org.python.core.PyFile;
015: import org.python.core.PyObject;
016: import org.python.core.PyString;
017:
018: import java.io.InputStream;
019: import java.sql.Blob;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024: import java.sql.Types;
025:
026: /**
027: * Informix specific data handling.
028: *
029: * @author brian zimmer
030: * @author last revised by $Author: otmarhumbel $
031: * @version $Revision: 2469 $
032: */
033: public class InformixDataHandler extends FilterDataHandler {
034:
035: /**
036: * Decorator for handling Informix specific issues.
037: *
038: * @param datahandler the delegate DataHandler
039: */
040: public InformixDataHandler(DataHandler datahandler) {
041: super (datahandler);
042: }
043:
044: /**
045: * Returns the serial for the statement.
046: *
047: * @param stmt
048: * @return PyObject
049: * @throws SQLException
050: */
051: public PyObject getRowId(Statement stmt) throws SQLException {
052:
053: if (stmt instanceof com.informix.jdbc.IfmxStatement) {
054: return Py
055: .newInteger(((com.informix.jdbc.IfmxStatement) stmt)
056: .getSerial());
057: }
058:
059: return super .getRowId(stmt);
060: }
061:
062: /**
063: * Provide fixes for Ifx driver.
064: *
065: * @param stmt
066: * @param index
067: * @param object
068: * @param type
069: * @throws SQLException
070: */
071: public void setJDBCObject(PreparedStatement stmt, int index,
072: PyObject object, int type) throws SQLException {
073:
074: if (DataHandler.checkNull(stmt, index, object, type)) {
075: return;
076: }
077:
078: switch (type) {
079:
080: case Types.LONGVARCHAR:
081:
082: String varchar;
083: // Ifx driver can't handle the setCharacterStream() method so use setObject() instead
084: if (object instanceof PyFile) {
085: varchar = ((PyFile) object).read();
086: } else {
087: varchar = (String) object.__tojava__(String.class);
088: }
089: stmt.setObject(index, varchar, type);
090: break;
091:
092: case Types.OTHER:
093:
094: // this is most likely an Informix boolean
095: stmt.setBoolean(index, object.__nonzero__());
096: break;
097:
098: default:
099: super .setJDBCObject(stmt, index, object, type);
100: }
101: }
102:
103: /**
104: * Provide fixes for Ifx driver.
105: *
106: * @param stmt
107: * @param index
108: * @param object
109: * @throws SQLException
110: */
111: public void setJDBCObject(PreparedStatement stmt, int index,
112: PyObject object) throws SQLException {
113:
114: // there is a bug in the Ifx driver when using setObject() with a String for a prepared statement
115: if (object instanceof PyString) {
116: super .setJDBCObject(stmt, index, object, Types.VARCHAR);
117: } else {
118: super .setJDBCObject(stmt, index, object);
119: }
120: }
121:
122: /**
123: * Override to handle Informix related issues.
124: *
125: * @param set the result set
126: * @param col the column number
127: * @param type the SQL type
128: * @return the mapped Python object
129: * @throws SQLException thrown for a sql exception
130: */
131: public PyObject getPyObject(ResultSet set, int col, int type)
132: throws SQLException {
133:
134: PyObject obj = Py.None;
135:
136: switch (type) {
137:
138: case Types.OTHER:
139: try {
140:
141: // informix returns boolean as OTHERs, so let's give that a try
142: obj = set.getBoolean(col) ? Py.One : Py.Zero;
143: } catch (SQLException e) {
144: obj = super .getPyObject(set, col, type);
145: }
146: break;
147:
148: case Types.BLOB:
149: int major = set.getStatement().getConnection()
150: .getMetaData().getDriverMajorVersion();
151: int minor = set.getStatement().getConnection()
152: .getMetaData().getDriverMinorVersion();
153:
154: if ((major <= 2) && (minor <= 11)) {
155: Blob blob = set.getBlob(col);
156:
157: if (blob == null) {
158: obj = Py.None;
159: } else {
160: InputStream is = null;
161:
162: try {
163:
164: // the available() bug means we CANNOT buffer this stream
165: is = blob.getBinaryStream();
166: obj = Py.java2py(DataHandler.read(is));
167: } finally {
168: try {
169: is.close();
170: } catch (Exception e) {
171: }
172: }
173: }
174:
175: break;
176: }
177: default:
178: obj = super.getPyObject(set, col, type);
179: }
180:
181: return (set.wasNull() || (obj == null)) ? Py.None : obj;
182: }
183: }
|