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: org_apache_derby_jdbc_EmbeddedDriver.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 org_apache_derby_jdbc_EmbeddedDriver extends Common
022: implements 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 "'" + StringUtils.encodeSql(value.toString()) + "'";
051: } else if (value instanceof Timestamp) {
052: return "'" + StringUtils.encodeSql(value.toString()) + "'";
053: } else if (value instanceof java.sql.Date) {
054: return "'" + StringUtils.encodeSql(value.toString()) + "'";
055: } else if (value instanceof Date) {
056: return "'"
057: + StringUtils.encodeSql(new Timestamp(
058: ((Date) value).getTime()).toString()) + "'";
059: } else if (value instanceof Calendar) {
060: return "'"
061: + StringUtils.encodeSql(new Timestamp(
062: ((Calendar) value).getTime().getTime())
063: .toString()) + "'";
064: }
065: // make sure that the Boolean type is correctly caught
066: else if (value instanceof Boolean) {
067: if (((Boolean) value).booleanValue()) {
068: return "1";
069: } else {
070: return "0";
071: }
072: }
073: // make sure that the object arrays are correctly caught
074: else if (value instanceof Object[]) {
075: return SqlArrays.convertArray((Object[]) value);
076: }
077: // just return the other types through their toString() method
078: else {
079: return "'" + StringUtils.encodeSql(value.toString()) + "'";
080: }
081: }
082:
083: protected Object retrieveFieldObject(ResultSet resultSet,
084: int columnNumber, int type) throws SQLException {
085: assert resultSet != null;
086: assert columnNumber > 0;
087:
088: Object result = null;
089:
090: if (type == Types.BIT || type == Types.BOOLEAN) {
091: boolean value = resultSet.getBoolean(columnNumber);
092: if (!resultSet.wasNull()) {
093: result = Boolean.valueOf(value);
094: }
095: } else if (type == Types.TINYINT) {
096: byte value = resultSet.getByte(columnNumber);
097: if (!resultSet.wasNull()) {
098: result = new Byte(value);
099: }
100: } else if (type == Types.SMALLINT || type == Types.INTEGER) {
101: int value = resultSet.getInt(columnNumber);
102: if (!resultSet.wasNull()) {
103: result = new Integer(value);
104: }
105: } else if (type == Types.BIGINT) {
106: long value = resultSet.getLong(columnNumber);
107: if (!resultSet.wasNull()) {
108: result = new Long(value);
109: }
110: } else if (type == Types.CHAR || type == Types.VARCHAR
111: || type == Types.LONGVARCHAR) {
112: result = resultSet.getString(columnNumber);
113: } else if (type == Types.DATE) {
114: result = resultSet.getDate(columnNumber);
115: } else if (type == Types.TIME) {
116: result = resultSet.getTime(columnNumber);
117: } else if (type == Types.TIMESTAMP) {
118: result = resultSet.getTimestamp(columnNumber);
119: } else if (type == Types.NUMERIC || type == Types.DECIMAL) {
120: result = resultSet.getBigDecimal(columnNumber);
121: } else if (type == Types.DOUBLE || type == Types.FLOAT
122: || type == Types.REAL) {
123: double value = resultSet.getDouble(columnNumber);
124: if (!resultSet.wasNull()) {
125: result = new Double(value);
126: }
127: } else if (type == Types.BLOB) {
128: result = resultSet.getBlob(columnNumber);
129: } else if (type == Types.CLOB) {
130: result = resultSet.getClob(columnNumber);
131: } else if (type == Types.REF) {
132: result = resultSet.getRef(columnNumber);
133: } else if (type == Types.JAVA_OBJECT || type == Types.OTHER) {
134: result = resultSet.getObject(columnNumber);
135: } else if (type == Types.ARRAY) {
136: result = resultSet.getArray(columnNumber);
137: } else if (type == Types.BINARY || type == Types.LONGVARBINARY
138: || type == Types.VARBINARY) {
139: result = resultSet.getBinaryStream(columnNumber);
140: }
141:
142: return result;
143: }
144:
145: public String getSqlType(Class type, int precision, int scale) {
146: // handle character types
147: if (type == String.class || type == StringBuilder.class
148: || type == StringBuffer.class) {
149: if (precision < 0) {
150: return "VARCHAR(32672)";
151: } else {
152: return "VARCHAR(" + precision + ")";
153: }
154: } else if (type == Character.class || type == char.class) {
155: if (precision < 0) {
156: return "CHAR";
157: } else {
158: return "CHAR(" + precision + ")";
159: }
160: }
161: // handle the time / date types
162: else if (type == Time.class) {
163: return "TIME";
164: } else if (type == java.sql.Date.class) {
165: return "DATE";
166: } else if (type == Timestamp.class || type == Date.class
167: || type == Calendar.class) {
168: return "TIMESTAMP";
169: }
170: // make sure that the Boolean type is correctly caught
171: else if (type == Boolean.class || type == boolean.class) {
172: return "NUMERIC(1)";
173: }
174: // make sure that the Integer types are correctly caught
175: else if (type == Byte.class || type == byte.class
176: || type == Short.class || type == short.class) {
177: return "SMALLINT";
178: } else if (type == Integer.class || type == int.class) {
179: return "INTEGER";
180: } else if (type == Long.class || type == long.class) {
181: return "BIGINT";
182: }
183: // make sure that the Float types are correctly caught
184: else if (type == Double.class || type == double.class
185: || type == Float.class || type == float.class) {
186: return "FLOAT";
187: }
188: // make sure that the BigDecimal type is correctly caught
189: else if (type == BigDecimal.class) {
190: if (precision < 0) {
191: return "NUMERIC";
192: } else if (scale < 0) {
193: return "NUMERIC(" + precision + ")";
194: } else {
195: return "NUMERIC(" + precision + "," + scale + ")";
196: }
197: }
198: // make sure that the Blob type is correctly caught
199: else if (type == Blob.class || type == byte[].class) {
200: return "BLOB";
201: }
202: // make sure that the Clob type is correctly caught
203: else if (type == Clob.class) {
204: return "CLOB";
205: } else {
206: String result = handleCommonSqlType(type, precision, scale);
207: if (result != null) {
208: return result;
209: }
210:
211: // just return a TEXT type in which the object's toString value will be stored
212: return "LONG VARCHAR";
213: }
214: }
215: }
|