001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: in_co_daffodil_db_jdbc_DaffodilDBDriver.java 3714 2007-04-08 02:57:38Z gbevin $
007: */
008: package com.uwyn.rife.database.types.databasedrivers;
009:
010: import java.sql.*;
011:
012: import com.uwyn.rife.config.RifeConfig;
013: import com.uwyn.rife.database.DbPreparedStatement;
014: import com.uwyn.rife.database.exceptions.DatabaseException;
015: import com.uwyn.rife.database.types.SqlArrays;
016: import com.uwyn.rife.database.types.SqlConversion;
017: import com.uwyn.rife.database.types.SqlNull;
018: import com.uwyn.rife.database.types.databasedrivers.Common;
019: import com.uwyn.rife.site.Constrained;
020: import com.uwyn.rife.tools.ClassUtils;
021: import com.uwyn.rife.tools.StringUtils;
022: import java.math.BigDecimal;
023: import java.text.SimpleDateFormat;
024: import java.util.Calendar;
025: import java.util.Date;
026:
027: public class in_co_daffodil_db_jdbc_DaffodilDBDriver extends Common
028: implements SqlConversion {
029: public String getSqlValue(Object value) {
030: // handle the null value
031: if (null == value || SqlNull.NULL == value) {
032: return SqlNull.NULL.toString();
033: }
034: // make sure that strings are escaped correctly
035: else if (value instanceof CharSequence) {
036: if (0 == ((CharSequence) value).length()) {
037: return "''";
038: } else {
039: return "'" + StringUtils.encodeSql(value.toString())
040: + "'";
041: }
042: } else if (value instanceof Character) {
043: if (((Character) value).charValue() == 0) {
044: return SqlNull.NULL.toString();
045: } else {
046: return "'" + StringUtils.encodeSql(value.toString())
047: + "'";
048: }
049: }
050: // handle the numbers
051: else if (ClassUtils.isNumeric(value.getClass())) {
052: return value.toString();
053: }
054: // handle the time / date types
055: else if (value instanceof Time) {
056: return "TIME '" + StringUtils.encodeSql(value.toString())
057: + "'";
058: } else if (value instanceof Timestamp) {
059: return "TIMESTAMP '"
060: + StringUtils.encodeSql(value.toString()) + "'";
061: } else if (value instanceof java.sql.Date) {
062: SimpleDateFormat dateformat = new SimpleDateFormat(
063: "yyyy-MM-dd");
064: dateformat.setTimeZone(RifeConfig.Tools
065: .getDefaultTimeZone());
066: return "DATE '"
067: + StringUtils.encodeSql(dateformat.format(value))
068: + "'";
069: } else if (value instanceof Date) {
070: return "TIMESTAMP '"
071: + StringUtils.encodeSql(new Timestamp(
072: ((Date) value).getTime()).toString()) + "'";
073: } else if (value instanceof Calendar) {
074: return "TIMESTAMP '"
075: + StringUtils.encodeSql(new Timestamp(
076: ((Calendar) value).getTime().getTime())
077: .toString()) + "'";
078: }
079: // make sure that the Boolean type is correctly caught
080: else if (value instanceof Boolean) {
081: if (((Boolean) value).booleanValue()) {
082: return "true";
083: } else {
084: return "false";
085: }
086: }
087: // make sure that the object arrays are correctly caught
088: else if (value instanceof Object[]) {
089: return SqlArrays.convertArray((Object[]) value);
090: }
091: // just return the other types through their toString() method
092: else {
093: return "'" + StringUtils.encodeSql(value.toString()) + "'";
094: }
095: }
096:
097: protected Object retrieveFieldObject(ResultSet resultSet,
098: int columnNumber, int type) throws SQLException {
099: assert resultSet != null;
100: assert columnNumber > 0;
101:
102: Object result = null;
103:
104: if (type == Types.BIT || type == Types.BOOLEAN) {
105: boolean value = resultSet.getBoolean(columnNumber);
106: if (!resultSet.wasNull()) {
107: result = Boolean.valueOf(value);
108: }
109: } else if (type == Types.TINYINT) {
110: byte value = resultSet.getByte(columnNumber);
111: if (!resultSet.wasNull()) {
112: result = new Byte(value);
113: }
114: } else if (type == Types.SMALLINT || type == Types.INTEGER) {
115: int value = resultSet.getInt(columnNumber);
116: if (!resultSet.wasNull()) {
117: result = new Integer(value);
118: }
119: } else if (type == Types.BIGINT) {
120: long value = resultSet.getLong(columnNumber);
121: if (!resultSet.wasNull()) {
122: result = new Long(value);
123: }
124: } else if (type == Types.CHAR || type == Types.VARCHAR
125: || type == Types.LONGVARCHAR) {
126: result = resultSet.getString(columnNumber);
127: } else if (type == Types.DATE) {
128: result = resultSet.getDate(columnNumber);
129: } else if (type == Types.TIME) {
130: result = resultSet.getTime(columnNumber);
131: } else if (type == Types.TIMESTAMP) {
132: result = resultSet.getTimestamp(columnNumber);
133: } else if (type == Types.NUMERIC || type == Types.DECIMAL) {
134: result = resultSet.getBigDecimal(columnNumber);
135: } else if (type == Types.DOUBLE || type == Types.FLOAT
136: || type == Types.REAL) {
137: double value = resultSet.getDouble(columnNumber);
138: if (!resultSet.wasNull()) {
139: result = new Double(value);
140: }
141: } else if (type == Types.BLOB) {
142: result = resultSet.getBlob(columnNumber);
143: } else if (type == Types.CLOB) {
144: result = resultSet.getClob(columnNumber);
145: } else if (type == Types.REF) {
146: result = resultSet.getRef(columnNumber);
147: } else if (type == Types.JAVA_OBJECT || type == Types.OTHER) {
148: result = resultSet.getObject(columnNumber);
149: } else if (type == Types.ARRAY) {
150: result = resultSet.getArray(columnNumber);
151: } else if (type == Types.BINARY || type == Types.LONGVARBINARY
152: || type == Types.VARBINARY) {
153: result = resultSet.getBinaryStream(columnNumber);
154: }
155:
156: return result;
157: }
158:
159: public String getSqlType(Class type, int precision, int scale) {
160: // handle character types
161: if (type == String.class || type == StringBuilder.class
162: || type == StringBuffer.class) {
163: if (precision < 0) {
164: return "VARCHAR(4192)";
165: } else {
166: return "VARCHAR(" + precision + ")";
167: }
168: } else if (type == Character.class || type == char.class) {
169: if (precision < 0) {
170: return "CHAR";
171: } else {
172: return "CHAR(" + precision + ")";
173: }
174: }
175: // handle the time / date types
176: else if (type == Time.class) {
177: return "TIME";
178: } else if (type == java.sql.Date.class) {
179: return "DATE";
180: } else if (type == Timestamp.class || type == Date.class
181: || type == Calendar.class) {
182: return "TIMESTAMP";
183: }
184: // make sure that the Boolean type is correctly caught
185: else if (type == Boolean.class || type == boolean.class) {
186: return "BOOLEAN";
187: }
188: // make sure that the Integer types are correctly caught
189: else if (type == Byte.class || type == byte.class
190: || type == Short.class || type == short.class) {
191: return "SMALLINT";
192: } else if (type == Integer.class || type == int.class) {
193: return "INTEGER";
194: } else if (type == Long.class || type == long.class) {
195: return "BIGINT";
196: }
197: // make sure that the Float types are correctly caught
198: else if (type == Double.class || type == double.class
199: || type == Float.class || type == float.class) {
200: return "FLOAT";
201: }
202: // make sure that the BigDecimal type is correctly caught
203: else if (type == BigDecimal.class) {
204: if (precision < 0) {
205: return "NUMERIC";
206: } else if (scale < 0) {
207: return "NUMERIC(" + precision + ")";
208: } else {
209: return "NUMERIC(" + precision + "," + scale + ")";
210: }
211: }
212: // make sure that the Blob type is correctly caught
213: else if (type == Blob.class || type == byte[].class) {
214: return "LONG VARBINARY";
215: }
216: // make sure that the Clob type is correctly caught
217: else if (type == Clob.class) {
218: return "LONG VARCHAR";
219: } else {
220: String result = handleCommonSqlType(type, precision, scale);
221: if (result != null) {
222: return result;
223: }
224:
225: // just return a TEXT type in which the object's toString value will be stored
226: return "LONG VARCHAR";
227: }
228: }
229:
230: public void setTypedParameter(DbPreparedStatement statement,
231: int parameterIndex, Class targetType, String name,
232: Object value, Constrained constrained)
233: throws DatabaseException {
234: if (null == statement)
235: throw new IllegalArgumentException(
236: "statement can't be null.");
237: if (parameterIndex < 1)
238: throw new IllegalArgumentException(
239: "parameterIndex must be at least 1.");
240: if (null == targetType)
241: throw new IllegalArgumentException(
242: "targetType can't be null.");
243:
244: if (targetType == String.class
245: || targetType == StringBuilder.class
246: || targetType == StringBuffer.class) {
247: if (null == value) {
248: statement.setNull(parameterIndex, Types.VARCHAR);
249: } else {
250: statement.setString(parameterIndex, value.toString());
251: }
252: } else {
253: super.setTypedParameter(statement, parameterIndex,
254: targetType, name, value, constrained);
255: }
256: }
257: }
|