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