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: com_mckoi_JDBCDriver.java 3695 2007-03-16 09:26:50Z gbevin $
007: */
008: package com.uwyn.rife.database.types.databasedrivers;
009:
010: import java.sql.*;
011:
012: import com.uwyn.rife.database.types.SqlArrays;
013: import com.uwyn.rife.database.types.SqlConversion;
014: import com.uwyn.rife.database.types.SqlNull;
015: import com.uwyn.rife.tools.ClassUtils;
016: import com.uwyn.rife.tools.StringUtils;
017: import java.math.BigDecimal;
018: import java.util.Calendar;
019: import java.util.Date;
020:
021: public class com_mckoi_JDBCDriver extends Common implements
022: SqlConversion {
023: public String getSqlValue(Object value) {
024: // handle the null value
025: if (null == value || SqlNull.NULL == value) {
026: return SqlNull.NULL.toString();
027: }
028: // make sure that strings are escaped correctly
029: else if (value instanceof CharSequence) {
030: if (0 == ((CharSequence) value).length()) {
031: return "''";
032: } else {
033: return "'" + StringUtils.encodeSql(value.toString())
034: + "'";
035: }
036: } else if (value instanceof Character) {
037: if (((Character) value).charValue() == 0) {
038: return SqlNull.NULL.toString();
039: } else {
040: return "'" + StringUtils.encodeSql(value.toString())
041: + "'";
042: }
043: }
044: // handle the numbers
045: else if (ClassUtils.isNumeric(value.getClass())) {
046: return value.toString();
047: }
048: // handle the time / date types
049: else if (value instanceof Time) {
050: return "TIME '" + StringUtils.encodeSql(value.toString())
051: + "'";
052: } else if (value instanceof Timestamp) {
053: return "TIMESTAMP '"
054: + StringUtils.encodeSql(value.toString()) + "'";
055: } else if (value instanceof java.sql.Date) {
056: return "DATE '" + StringUtils.encodeSql(value.toString())
057: + "'";
058: } else if (value instanceof Date) {
059: return "TIMESTAMP '"
060: + StringUtils.encodeSql(new Timestamp(
061: ((Date) value).getTime()).toString()) + "'";
062: } else if (value instanceof Calendar) {
063: return "TIMESTAMP '"
064: + StringUtils.encodeSql(new Timestamp(
065: ((Calendar) value).getTime().getTime())
066: .toString()) + "'";
067: }
068: // make sure that the Boolean type is correctly caught
069: else if (value instanceof Boolean) {
070: if (((Boolean) value).booleanValue()) {
071: return "true";
072: } else {
073: return "false";
074: }
075: }
076: // make sure that the object arrays are correctly caught
077: else if (value instanceof Object[]) {
078: return SqlArrays.convertArray((Object[]) value);
079: }
080: // just return the other types through their toString() method
081: else {
082: return "'" + StringUtils.encodeSql(value.toString()) + "'";
083: }
084: }
085:
086: protected Object retrieveFieldObject(ResultSet resultSet,
087: int columnNumber, int type) throws SQLException {
088: assert resultSet != null;
089: assert columnNumber > 0;
090:
091: Object result = null;
092:
093: if (type == Types.BIT || type == Types.BOOLEAN) {
094: boolean value = resultSet.getBoolean(columnNumber);
095: if (!resultSet.wasNull()) {
096: result = Boolean.valueOf(value);
097: }
098: } else if (type == Types.TINYINT) {
099: byte value = resultSet.getByte(columnNumber);
100: if (!resultSet.wasNull()) {
101: result = new Byte(value);
102: }
103: } else if (type == Types.SMALLINT || type == Types.INTEGER) {
104: int value = resultSet.getInt(columnNumber);
105: if (!resultSet.wasNull()) {
106: result = new Integer(value);
107: }
108: } else if (type == Types.BIGINT) {
109: long value = resultSet.getLong(columnNumber);
110: if (!resultSet.wasNull()) {
111: result = new Long(value);
112: }
113: } else if (type == Types.CHAR || type == Types.VARCHAR
114: || type == Types.LONGVARCHAR) {
115: result = resultSet.getString(columnNumber);
116: } else if (type == Types.DATE) {
117: result = resultSet.getDate(columnNumber);
118: } else if (type == Types.TIME) {
119: result = resultSet.getTime(columnNumber);
120: } else if (type == Types.TIMESTAMP) {
121: result = resultSet.getTimestamp(columnNumber);
122: } else if (type == Types.NUMERIC || type == Types.DECIMAL) {
123: result = resultSet.getBigDecimal(columnNumber);
124: } else if (type == Types.DOUBLE || type == Types.FLOAT
125: || type == Types.REAL) {
126: double value = resultSet.getDouble(columnNumber);
127: if (!resultSet.wasNull()) {
128: result = new Double(value);
129: }
130: } else if (type == Types.BLOB) {
131: result = resultSet.getBlob(columnNumber);
132: } else if (type == Types.CLOB) {
133: result = resultSet.getClob(columnNumber);
134: } else if (type == Types.REF) {
135: result = resultSet.getRef(columnNumber);
136: } else if (type == Types.JAVA_OBJECT || type == Types.OTHER) {
137: result = resultSet.getObject(columnNumber);
138: } else if (type == Types.ARRAY) {
139: result = resultSet.getArray(columnNumber);
140: } else if (type == Types.BINARY || type == Types.LONGVARBINARY
141: || type == Types.VARBINARY) {
142: result = resultSet.getBinaryStream(columnNumber);
143: }
144:
145: return result;
146: }
147:
148: public String getSqlType(Class type, int precision, int scale) {
149: // handle character types
150: if (type == String.class || type == StringBuilder.class
151: || type == StringBuffer.class) {
152: if (precision < 0) {
153: return "TEXT";
154: } else {
155: return "VARCHAR(" + precision + ")";
156: }
157: } else if (type == Character.class || type == char.class) {
158: if (precision < 0) {
159: return "CHAR";
160: } else {
161: return "CHAR(" + precision + ")";
162: }
163: }
164: // handle the time / date types
165: else if (type == Time.class) {
166: return "TIME";
167: } else if (type == java.sql.Date.class) {
168: return "DATE";
169: } else if (type == Timestamp.class || type == Date.class
170: || type == Calendar.class) {
171: return "TIMESTAMP";
172: }
173: // make sure that the Boolean type is correctly caught
174: else if (type == Boolean.class || type == boolean.class) {
175: return "BOOLEAN";
176: }
177: // make sure that the Integer types are correctly caught
178: else if (type == Byte.class || type == byte.class
179: || type == Short.class || type == short.class) {
180: return "SMALLINT";
181: } else if (type == Integer.class || type == int.class) {
182: return "INTEGER";
183: } else if (type == Long.class || type == long.class) {
184: return "BIGINT";
185: }
186: // make sure that the Float types are correctly caught
187: else if (type == Double.class || type == double.class
188: || type == Float.class || type == float.class) {
189: return "FLOAT";
190: }
191: // make sure that the BigDecimal type is correctly caught
192: else if (type == BigDecimal.class) {
193: if (precision < 0) {
194: return "NUMERIC";
195: } else if (scale < 0) {
196: return "NUMERIC(" + precision + ")";
197: } else {
198: return "NUMERIC(" + precision + "," + scale + ")";
199: }
200: }
201: // make sure that the Blob type is correctly caught
202: else if (type == Blob.class || type == byte[].class) {
203: return "BLOB";
204: }
205: // make sure that the Clob type is correctly caught
206: else if (type == Clob.class) {
207: return "CLOB";
208: } else {
209: String result = handleCommonSqlType(type, precision, scale);
210: if (result != null) {
211: return result;
212: }
213:
214: // just return a TEXT type in which the object's toString value will be stored
215: return "TEXT";
216: }
217: }
218: }
|