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