KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > config > JDBCDataHelper


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/JDBCDataHelper.java,v 1.12 2004/10/24 13:47:29 hkollmann Exp $
3  * $Revision: 1.12 $
4  * $Date: 2004/10/24 13:47:29 $
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.util.IEscaper;
30 import org.dbforms.util.FileHolder;
31
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.io.ByteArrayOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.ObjectOutputStream JavaDoc;
36
37 import java.sql.Clob JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.sql.Types JavaDoc;
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 JavaDoc getData(ResultSet JavaDoc rs, IEscaper escaper, int col)
72             throws SQLException JavaDoc {
73         Object JavaDoc res = null;
74
75         switch (rs.getMetaData().getColumnType(col)) {
76         case Types.CLOB: {
77             String JavaDoc s;
78             Clob JavaDoc tmpObj = (Clob JavaDoc) 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 JavaDoc s = rs.getString(col);
96             res = (escaper == null) ? s : escaper.unescapeJDBC(s);
97
98             break;
99
100         default: {
101             Object JavaDoc 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 JavaDoc ps, IEscaper escaper,
134             int col, Object JavaDoc value, int fieldType, int blobStrategy)
135             throws SQLException JavaDoc {
136         logCat.debug("fillPreparedStatement( ps, " + col + ", " + value + ", "
137                 + fieldType + ")...");
138
139         switch (fieldType) {
140         case 0:
141             throw new SQLException JavaDoc("illegal type!");
142
143         case FieldTypes.BLOB:
144
145             if (value == null) {
146                 ps.setNull(col, java.sql.Types.BLOB);
147             } else if (blobStrategy == Table.BLOB_CLASSIC) {
148                 //FileHolder fileHolder = (FileHolder) value;
149
FileHolder fileHolder = (FileHolder) value;
150
151                 try {
152                     ByteArrayOutputStream JavaDoc byteOut = new ByteArrayOutputStream JavaDoc();
153                     ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(byteOut);
154                     out.writeObject(fileHolder);
155                     out.flush();
156
157                     byte[] buf = byteOut.toByteArray();
158                     byteOut.close();
159                     out.close();
160
161                     ByteArrayInputStream JavaDoc bytein = new ByteArrayInputStream JavaDoc(buf);
162                     int byteLength = buf.length;
163                     ps.setBinaryStream(col, bytein, byteLength);
164
165                     // store fileHolder as a whole (this way we don't lose
166
// file meta-info!)
167
} catch (IOException JavaDoc ioe) {
168                     ioe.printStackTrace();
169                     logCat.info(ioe.toString());
170                     throw new SQLException JavaDoc(
171                             "error storing BLOB in database (BLOB_CLASSIC MODE) - "
172                                     + ioe.toString(), null, 2);
173                 }
174             } else if (value instanceof FileHolder) { // if we have a file
175
// case TABLE.BLOB_INTERCEPTOR: direct storage, the rest
176
// is dont
177
// by interceptor
178

179                 // upload
180
try {
181                     FileHolder fileHolder = (FileHolder) value;
182                     ps.setBinaryStream(col, fileHolder
183                             .getInputStreamFromBuffer(), fileHolder
184                             .getFileLength());
185                 } catch (IOException JavaDoc ioe) {
186                     ioe.printStackTrace();
187                     logCat.info(ioe.toString());
188                     throw new SQLException JavaDoc(
189                             "error storing BLOB in database (BLOB_CLASSIC MODE) - "
190                                     + ioe.toString(), null, 2);
191                 }
192             } else { // if the blob field is updated from within
193

194                 // textarea
195
byte[] data = ((String JavaDoc) value).getBytes();
196                 ps.setBinaryStream(col, new ByteArrayInputStream JavaDoc(data),
197                         data.length);
198             }
199
200             break;
201
202         case FieldTypes.DISKBLOB:
203             ps.setObject(col, (escaper == null) ? value : escaper
204                     .escapeJDBC((String JavaDoc) value), FieldTypes.CHAR);
205
206             break;
207
208         case FieldTypes.CHAR:
209             ps.setObject(col, (escaper == null) ? value : escaper
210                     .escapeJDBC((String JavaDoc) value), FieldTypes.CHAR);
211
212             break;
213
214         default:
215             ps.setObject(col, value, fieldType);
216
217             break;
218         }
219     }
220 }
221
Popular Tags