001: /*
002: * Jython Database Specification API 2.0
003: *
004: * $Id: OracleDataHandler.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.handler;
010:
011: import com.ziclix.python.sql.DataHandler;
012: import com.ziclix.python.sql.FilterDataHandler;
013: import com.ziclix.python.sql.zxJDBC;
014: import oracle.jdbc.driver.OracleResultSet;
015: import oracle.jdbc.driver.OracleTypes;
016: import oracle.sql.BLOB;
017: import oracle.sql.ROWID;
018: import org.python.core.Py;
019: import org.python.core.PyObject;
020:
021: import java.io.BufferedInputStream;
022: import java.io.InputStream;
023: import java.sql.CallableStatement;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.sql.Types;
028:
029: /**
030: * Oracle specific data handling.
031: *
032: * @author brian zimmer
033: * @author last revised by $Author: bzimmer $
034: * @version $Revision: 2414 $
035: */
036: public class OracleDataHandler extends FilterDataHandler {
037:
038: /**
039: * Default constructor for DataHandler filtering.
040: */
041: public OracleDataHandler(DataHandler datahandler) {
042: super (datahandler);
043: }
044:
045: /**
046: * Method getMetaDataName
047: *
048: * @param name
049: * @return String
050: */
051: public String getMetaDataName(PyObject name) {
052:
053: String metaName = super .getMetaDataName(name);
054:
055: return (metaName == null) ? null : metaName.toUpperCase();
056: }
057:
058: /**
059: * Provide functionality for Oracle specific types, such as ROWID.
060: */
061: public void setJDBCObject(PreparedStatement stmt, int index,
062: PyObject object, int type) throws SQLException {
063:
064: if (DataHandler.checkNull(stmt, index, object, type)) {
065: return;
066: }
067:
068: switch (type) {
069:
070: case OracleTypes.ROWID:
071: stmt.setString(index, (String) object
072: .__tojava__(String.class));
073: break;
074:
075: case Types.DECIMAL:
076:
077: // Oracle is annoying
078: Object input = object.__tojava__(Double.class);
079:
080: if (input != Py.NoConversion) {
081: stmt.setDouble(index, ((Double) input).doubleValue());
082:
083: break;
084: }
085:
086: super .setJDBCObject(stmt, index, object, type);
087: break;
088:
089: case Types.NUMERIC:
090: super .setJDBCObject(stmt, index, object, Types.DOUBLE);
091: break;
092:
093: case Types.BLOB:
094: case Types.CLOB:
095: Integer[] vals = { new Integer(index), new Integer(type) };
096: String msg = zxJDBC.getString("errorSettingIndex", vals);
097:
098: throw new SQLException(msg);
099: default:
100: super .setJDBCObject(stmt, index, object, type);
101: }
102: }
103:
104: /**
105: * Provide functionality for Oracle specific types, such as ROWID.
106: */
107: public PyObject getPyObject(ResultSet set, int col, int type)
108: throws SQLException {
109:
110: PyObject obj = Py.None;
111:
112: switch (type) {
113:
114: case Types.BLOB:
115: BLOB blob = ((OracleResultSet) set).getBLOB(col);
116:
117: if (blob == null) {
118: return Py.None;
119: }
120:
121: InputStream stream = new BufferedInputStream(blob
122: .getBinaryStream());
123:
124: obj = Py.java2py(DataHandler.read(stream));
125: break;
126:
127: case OracleTypes.ROWID:
128: ROWID rowid = ((OracleResultSet) set).getROWID(col);
129:
130: if (rowid != null) {
131: obj = Py.java2py(rowid.stringValue());
132: }
133: break;
134:
135: default:
136: obj = super .getPyObject(set, col, type);
137: }
138:
139: return (set.wasNull() ? Py.None : obj);
140: }
141:
142: /**
143: * Called when a stored procedure or function is executed and OUT parameters
144: * need to be registered with the statement.
145: *
146: * @param statement
147: * @param index the JDBC offset column number
148: * @param colType the column as from DatabaseMetaData (eg, procedureColumnOut)
149: * @param dataType the JDBC datatype from Types
150: * @param dataTypeName the JDBC datatype name
151: * @throws SQLException
152: */
153: public void registerOut(CallableStatement statement, int index,
154: int colType, int dataType, String dataTypeName)
155: throws SQLException {
156:
157: if (dataType == Types.OTHER) {
158: if ("REF CURSOR".equals(dataTypeName)) {
159: statement.registerOutParameter(index,
160: OracleTypes.CURSOR);
161:
162: return;
163: } else if ("PL/SQL RECORD".equals(dataTypeName)) {
164: statement.registerOutParameter(index,
165: OracleTypes.CURSOR);
166:
167: return;
168: }
169: }
170:
171: super.registerOut(statement, index, colType, dataType,
172: dataTypeName);
173: }
174: }
|