KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Jython Database Specification API 2.0
3 *
4 * $Id: PostgresqlDataHandler.java,v 1.4 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 org.python.core.Py;
13 import org.python.core.PyFile;
14 import org.python.core.PyObject;
15
16 import java.math.BigDecimal JavaDoc;
17 import java.sql.PreparedStatement JavaDoc;
18 import java.sql.ResultSet JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.sql.Types JavaDoc;
21
22 /**
23  * Postgresql specific data handling.
24  *
25  * @author brian zimmer
26  * @author last revised by $Author: otmarhumbel $
27  * @version $Revision: 1.4 $
28  */

29 public class PostgresqlDataHandler extends RowIdHandler {
30
31   /**
32    * Decorator for handling Postgresql specific issues.
33    *
34    * @param datahandler the delegate DataHandler
35    */

36   public PostgresqlDataHandler(DataHandler datahandler) {
37     super(datahandler);
38   }
39
40   protected String JavaDoc getRowIdMethodName() {
41     return "getLastOID";
42   }
43
44   /**
45    * Override to handle Postgresql related issues.
46    *
47    * @param set the result set
48    * @param col the column number
49    * @param type the SQL type
50    * @return the mapped Python object
51    * @throws SQLException thrown for a sql exception
52    */

53   public PyObject getPyObject(ResultSet JavaDoc set, int col, int type) throws SQLException JavaDoc {
54
55     PyObject obj = Py.None;
56
57     switch (type) {
58
59       case Types.NUMERIC:
60       case Types.DECIMAL:
61
62         // in JDBC 2.0, use of a scale is deprecated
63
// The big fix here is a problem with numeric types. It seems the ResultSet
64
// tries to fix a JBuilder bug (as commented in the source) by including a
65
// scale of 0. Well this blows up BigDecimal if the number is, say, 4.22.
66
// It appears the workaround is to call the deprecated method with a scale of
67
// -1 which forces the return of the BD without setting the scale.
68
BigDecimal JavaDoc bd = set.getBigDecimal(col, -1);
69
70         obj = (bd == null) ? Py.None : Py.newFloat(bd.doubleValue());
71         break;
72
73       case Types.OTHER:
74
75         // it seems pg doesn't like to handle OTHER types as anything but strings
76
// but we'll try first anyways just to see what happens
77
try {
78           obj = super.getPyObject(set, col, type);
79         } catch (SQLException JavaDoc e) {
80           obj = super.getPyObject(set, col, Types.VARCHAR);
81         }
82         break;
83
84       default :
85         obj = super.getPyObject(set, col, type);
86     }
87
88     return (set.wasNull() || (obj == null)) ? Py.None : obj;
89   }
90
91   /**
92    * Provide fixes for Postgresql driver.
93    *
94    * @param stmt
95    * @param index
96    * @param object
97    * @param type
98    * @throws SQLException
99    */

100   public void setJDBCObject(PreparedStatement JavaDoc stmt, int index, PyObject object, int type) throws SQLException JavaDoc {
101
102     if (DataHandler.checkNull(stmt, index, object, type)) {
103       return;
104     }
105
106     switch (type) {
107
108       case Types.LONGVARCHAR:
109
110         String JavaDoc varchar;
111         // Postgresql driver can't handle the setCharacterStream() method so use setObject() instead
112
if (object instanceof PyFile) {
113           varchar = ((PyFile) object).read();
114         } else {
115           varchar = (String JavaDoc) object.__tojava__(String JavaDoc.class);
116         }
117         
118         stmt.setObject(index, varchar, type);
119         break;
120
121       default :
122         super.setJDBCObject(stmt, index, object, type);
123     }
124   }
125 }
126
Popular Tags