KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: OracleDataHandler.java,v 1.6 2005/02/23 04:26:19 bzimmer 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 com.ziclix.python.sql.zxJDBC;
14 import oracle.jdbc.driver.OracleResultSet;
15 import oracle.jdbc.driver.OracleTypes;
16 import oracle.sql.BLOB;
17 import oracle.sql.ROWID;
18 import org.python.core.Py;
19 import org.python.core.PyObject;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.sql.CallableStatement JavaDoc;
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Types JavaDoc;
28
29 /**
30  * Oracle specific data handling.
31  *
32  * @author brian zimmer
33  * @author last revised by $Author: bzimmer $
34  * @version $Revision: 1.6 $
35  */

36 public class OracleDataHandler extends FilterDataHandler {
37
38     /**
39      * Default constructor for DataHandler filtering.
40      */

41     public OracleDataHandler(DataHandler datahandler) {
42         super(datahandler);
43     }
44
45     /**
46      * Method getMetaDataName
47      *
48      * @param name
49      * @return String
50      */

51     public String JavaDoc getMetaDataName(PyObject name) {
52
53         String JavaDoc metaName = super.getMetaDataName(name);
54
55         return (metaName == null) ? null : metaName.toUpperCase();
56     }
57
58     /**
59      * Provide functionality for Oracle specific types, such as ROWID.
60      */

61     public void setJDBCObject(PreparedStatement JavaDoc stmt, int index, PyObject object, int type) throws SQLException JavaDoc {
62
63         if (DataHandler.checkNull(stmt, index, object, type)) {
64             return;
65         }
66
67         switch (type) {
68
69             case OracleTypes.ROWID:
70                 stmt.setString(index, (String JavaDoc) object.__tojava__(String JavaDoc.class));
71                 break;
72
73             case Types.DECIMAL:
74
75                 // Oracle is annoying
76
Object JavaDoc input = object.__tojava__(Double JavaDoc.class);
77
78                 if (input != Py.NoConversion) {
79                     stmt.setDouble(index, ((Double JavaDoc) input).doubleValue());
80
81                     break;
82                 }
83
84                 super.setJDBCObject(stmt, index, object, type);
85                 break;
86
87             case Types.NUMERIC:
88                 super.setJDBCObject(stmt, index, object, Types.DOUBLE);
89                 break;
90
91             case Types.BLOB:
92             case Types.CLOB:
93                 Integer JavaDoc[] vals = {new Integer JavaDoc(index), new Integer JavaDoc(type)};
94                 String JavaDoc msg = zxJDBC.getString("errorSettingIndex", vals);
95
96                 throw new SQLException JavaDoc(msg);
97             default :
98                 super.setJDBCObject(stmt, index, object, type);
99         }
100     }
101
102     /**
103      * Provide functionality for Oracle specific types, such as ROWID.
104      */

105     public PyObject getPyObject(ResultSet set, int col, int type) throws SQLException JavaDoc {
106
107         PyObject obj = Py.None;
108
109         switch (type) {
110
111             case Types.BLOB:
112                 BLOB blob = ((OracleResultSet) set).getBLOB(col);
113
114                 if (blob == null) {
115                     return Py.None;
116                 }
117
118                 InputStream JavaDoc stream = new BufferedInputStream JavaDoc(blob.getBinaryStream());
119
120                 obj = Py.java2py(DataHandler.read(stream));
121                 break;
122
123             case OracleTypes.ROWID:
124                 ROWID rowid = ((OracleResultSet) set).getROWID(col);
125
126                 if (rowid != null) {
127                     obj = Py.java2py(rowid.stringValue());
128                 }
129                 break;
130
131             default :
132                 obj = super.getPyObject(set, col, type);
133         }
134
135         return (set.wasNull() ? Py.None : obj);
136     }
137
138     /**
139      * Called when a stored procedure or function is executed and OUT parameters
140      * need to be registered with the statement.
141      *
142      * @param statement
143      * @param index the JDBC offset column number
144      * @param colType the column as from DatabaseMetaData (eg, procedureColumnOut)
145      * @param dataType the JDBC datatype from Types
146      * @param dataTypeName the JDBC datatype name
147      * @throws SQLException
148      */

149     public void registerOut(CallableStatement JavaDoc statement, int index, int colType, int dataType, String JavaDoc dataTypeName) throws SQLException JavaDoc {
150
151         if (dataType == Types.OTHER) {
152             if ("REF CURSOR".equals(dataTypeName)) {
153                 statement.registerOutParameter(index, OracleTypes.CURSOR);
154
155                 return;
156             } else if ("PL/SQL RECORD".equals(dataTypeName)) {
157                 statement.registerOutParameter(index, OracleTypes.CURSOR);
158
159                 return;
160             }
161         }
162
163         super.registerOut(statement, index, colType, dataType, dataTypeName);
164     }
165 }
166
Popular Tags