1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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
165
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) {
174
175
176
177
178
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 {
192
193
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 }