View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/JDBCDataHelper.java,v 1.16 2006/01/13 21:22:56 hkollmann Exp $
3    * $Revision: 1.16 $
4    * $Date: 2006/01/13 21:22:56 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.config;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.dbforms.interfaces.IEscaper;
30  import org.dbforms.util.FileHolder;
31  
32  import java.io.ByteArrayInputStream;
33  import java.io.ByteArrayOutputStream;
34  import java.io.IOException;
35  import java.io.ObjectOutputStream;
36  
37  import java.sql.Clob;
38  import java.sql.PreparedStatement;
39  import java.sql.ResultSet;
40  import java.sql.SQLException;
41  import java.sql.Types;
42  
43  /***
44   * <p>
45   * this utility-class provides convenience methods for SQL related tasks
46   * </p>
47   * 
48   * @author Joe Peer
49   * @author Eric Pugh
50   */
51  public class JDBCDataHelper {
52      // logging category for this class
53      private static Log logCat = LogFactory.getLog(JDBCDataHelper.class
54              .getName());
55  
56      /***
57       * DOCUMENT ME!
58       * 
59       * @param rs
60       *            DOCUMENT ME!
61       * @param escaper
62       *            DOCUMENT ME!
63       * @param col
64       *            DOCUMENT ME!
65       * 
66       * @return DOCUMENT ME!
67       * 
68       * @throws SQLException
69       *             DOCUMENT ME!
70       */
71      public static Object getData(ResultSet rs, IEscaper escaper, int col)
72              throws SQLException {
73          Object res = null;
74  
75          switch (rs.getMetaData().getColumnType(col)) {
76          case Types.CLOB: {
77              String s;
78              Clob tmpObj = (Clob) rs.getObject(col);
79  
80              if (tmpObj != null) {
81                  s = tmpObj.getSubString(1, (int) tmpObj.length());
82              } else {
83                  s = null;
84              }
85  
86              res = (escaper == null) ? s : escaper.unescapeJDBC(s);
87  
88              break;
89          }
90  
91          case Types.LONGVARCHAR:
92          case Types.CHAR:
93          case Types.VARCHAR:
94  
95              String s = rs.getString(col);
96              res = (escaper == null) ? s : escaper.unescapeJDBC(s);
97  
98              break;
99  
100         default: {
101             Object tmpObj = rs.getObject(col);
102             res = tmpObj;
103 
104             break;
105         }
106         }
107 
108         return res;
109     }
110 
111     /***
112      * this utility-method assigns a particular value to a place holder of a
113      * PreparedStatement. it tries to find the correct setXxx() value, accoring
114      * to the field-type information represented by "fieldType". quality: this
115      * method is bloody alpha (as you might see :=)
116      * 
117      * @param ps
118      *            DOCUMENT ME!
119      * @param escaper
120      *            DOCUMENT ME!
121      * @param col
122      *            DOCUMENT ME!
123      * @param value
124      *            DOCUMENT ME!
125      * @param fieldType
126      *            DOCUMENT ME!
127      * @param blobStrategy
128      *            DOCUMENT ME!
129      * 
130      * @throws SQLException
131      *             DOCUMENT ME!
132      */
133     public static void fillWithData(PreparedStatement ps, IEscaper escaper,
134             int col, Object value, int fieldType, int blobStrategy)
135             throws SQLException {
136         logCat.debug("fillPreparedStatement( ps, " + col + ", " + value + ", "
137                 + fieldType + ")...");
138 
139         switch (fieldType) {
140         case 0:
141             throw new SQLException("illegal type!");
142 
143         case FieldTypes.BLOB:
144             if (value == null) {
145                 ps.setNull(col, java.sql.Types.BLOB);
146             } else if (blobStrategy == Table.BLOB_CLASSIC) {
147                 //FileHolder fileHolder = (FileHolder) value;
148                 FileHolder fileHolder = (FileHolder) value;
149 
150                 try {
151                     ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
152                     ObjectOutputStream out = new ObjectOutputStream(byteOut);
153                     out.writeObject(fileHolder);
154                     out.flush();
155 
156                     byte[] buf = byteOut.toByteArray();
157                     byteOut.close();
158                     out.close();
159 
160                     ByteArrayInputStream bytein = new ByteArrayInputStream(buf);
161                     int byteLength = buf.length;
162                     ps.setBinaryStream(col, bytein, byteLength);
163 
164                     // store fileHolder as a whole (this way we don't lose
165                     // file meta-info!)
166                 } catch (IOException ioe) {
167                     ioe.printStackTrace();
168                     logCat.info(ioe.toString());
169                     throw new SQLException(
170                             "error storing BLOB in database (BLOB_CLASSIC MODE) - "
171                                     + ioe.toString(), null, 2);
172                 }
173             } else if (value instanceof FileHolder) { // if we have a file
174                 // case TABLE.BLOB_INTERCEPTOR: direct storage, the rest
175                 // is dont
176                 // by interceptor
177 
178                 // upload
179                 try {
180                     FileHolder fileHolder = (FileHolder) value;
181                     ps.setBinaryStream(col, fileHolder
182                             .getInputStreamFromBuffer(), fileHolder
183                             .getFileLength());
184                 } catch (IOException ioe) {
185                     ioe.printStackTrace();
186                     logCat.info(ioe.toString());
187                     throw new SQLException(
188                             "error storing BLOB in database (BLOB_CLASSIC MODE) - "
189                                     + ioe.toString(), null, 2);
190                 }
191             } else { // if the blob field is updated from within
192 
193                 // textarea
194                 byte[] data = ((String) value).getBytes();
195                 ps.setBinaryStream(col, new ByteArrayInputStream(data),
196                         data.length);
197             }
198 
199             break;
200 
201         case FieldTypes.DISKBLOB:
202             if (value == null) {
203                 ps.setNull(col, FieldTypes.CHAR);
204             } else {
205             ps.setObject(col, (escaper == null) ? value : escaper
206                     .escapeJDBC((String) value), FieldTypes.CHAR);
207             }
208             break;
209 
210         case FieldTypes.LONGVARCHAR:
211         case FieldTypes.VARCHAR:
212         case FieldTypes.CHAR:
213             if (value == null) {
214                 ps.setNull(col, fieldType);
215             } else {
216             ps.setObject(col, (escaper == null) ? value : escaper
217                     .escapeJDBC((String) value), fieldType);
218 
219             }
220             break;
221 
222         default:
223             if (value == null) {
224                ps.setNull(col, fieldType);
225             } else {
226                ps.setObject(col, value, fieldType);
227             }
228             break;
229         }
230     }
231 }