haul 02/05/28 06:43:52 Added: src/java/org/apache/cocoon/util JDBCTypeConversions.java Log: Changed modules from using the request object to using objectModel instead. This entails some larger modifications to the modular database actions as they used to inherit the setColumn method which doesn't work anymore. Some code has been put to a utility class and the modular datbase action are now no subclass of the original ones anymore. Revision Changes Path 1.1 xml-cocoon2/src/java/org/apache/cocoon/util/JDBCTypeConversions.java Index: JDBCTypeConversions.java =================================================================== /* ============================================================================ The Apache Software License, Version 1.1 ============================================================================ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved. Redistribution and use in source and binary forms, with or without modifica- tion, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The end-user documentation included with the redistribution, if any, must include the following acknowledgment: "This product includes software developed by the Apache Software Foundation (http://www.apache.org/)." Alternately, this acknowledgment may appear in the software itself, if and wherever such third-party acknowledgments normally appear. 4. The names "Apache Cocoon" and "Apache Software Foundation" must not be used to endorse or promote products derived from this software without prior written permission. For written permission, please contact [EMAIL PROTECTED] 5. Products derived from this software may not be called "Apache", nor may "Apache" appear in their name, without prior written permission of the Apache Software Foundation. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU- DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This software consists of voluntary contributions made by many individuals on behalf of the Apache Software Foundation and was originally created by Stefano Mazzocchi <[EMAIL PROTECTED]>. For more information on the Apache Software Foundation, please see <http://www.apache.org/>. */ package org.apache.cocoon.util; import java.io.*; import java.sql.PreparedStatement; import java.sql.Statement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.sql.Time; import java.sql.Date; import java.sql.Array; import java.sql.Types; import java.sql.Clob; import java.sql.Struct; import java.math.BigDecimal; import java.util.Map; import java.util.HashMap; import java.util.Collections; import java.text.DateFormat; import java.text.SimpleDateFormat; import org.apache.avalon.framework.configuration.Configuration; import org.apache.avalon.framework.configuration.ConfigurationException; /** * Provide some utility methods to read from JDBC result sets or store * them to JDBC statements. Largely copied from * org.apache.cocoon.acting.AbstractDatabaseAction. * */ public class JDBCTypeConversions { public static final Map typeConstants; static { /** Initialize the map of type names to jdbc column types. Note that INTEGER, BLOB, and VARCHAR column types map to more than one type name. **/ Map constants = new HashMap(); constants.put("ascii", new Integer(Types.CLOB)); constants.put("big-decimal", new Integer(Types.BIGINT)); constants.put("binary", new Integer(Types.BLOB)); constants.put("byte", new Integer(Types.TINYINT)); constants.put("string", new Integer(Types.VARCHAR)); constants.put("date", new Integer(Types.DATE)); constants.put("double", new Integer(Types.DOUBLE)); constants.put("float", new Integer(Types.FLOAT)); constants.put("int", new Integer(Types.INTEGER)); constants.put("long", new Integer(Types.NUMERIC)); constants.put("short", new Integer(Types.SMALLINT)); constants.put("time", new Integer(Types.TIME)); constants.put("time-stamp", new Integer(Types.TIMESTAMP)); constants.put("array", new Integer(Types.ARRAY)); constants.put("row", new Integer(Types.STRUCT)); constants.put("object", new Integer(Types.OTHER)); typeConstants = Collections.unmodifiableMap(constants); } /** * Converts an object to a JDBC type. This has just been started * and does not do much at the moment. * */ public static Object convert(Object value, String jType) { Object object=null; if (jType.equalsIgnoreCase("string")) { if (value instanceof String) { object = value; } else { object = value.toString(); } } else if (jType.equalsIgnoreCase("int")) { if (value instanceof String) { object = Integer.decode((String)value); } else if (value instanceof Integer) { object = value; } else { // } } else if (jType.equalsIgnoreCase("long")) { if (value instanceof String) { object = Long.decode((String)value); } else if (value instanceof Long) { object = value; } else { // } } else { // other types need parsing & creation // } return object; } /** * Get the Statement column so that the results are mapped correctly. * (this has been copied from AbstractDatabaseAction and modified slightly) */ public static Object getColumn(ResultSet set, Configuration column ) throws Exception { Integer type = (Integer) JDBCTypeConversions.typeConstants.get(column.getAttribute("type")); String dbcol = column.getAttribute("name"); Object value = null; switch (type.intValue()) { case Types.CLOB: Clob dbClob = set.getClob(dbcol); int length = (int) dbClob.length(); InputStream asciiStream = new BufferedInputStream(dbClob.getAsciiStream()); byte[] buffer = new byte[length]; asciiStream.read(buffer); String str = new String(buffer); asciiStream.close(); value = str; break; case Types.BIGINT: value = set.getBigDecimal(dbcol); break; case Types.TINYINT: value = new Byte(set.getByte(dbcol)); break; case Types.VARCHAR: value = set.getString(dbcol); break; case Types.DATE: value = set.getDate(dbcol); break; case Types.DOUBLE: value = new Double(set.getDouble(dbcol)); break; case Types.FLOAT: value = new Float(set.getFloat(dbcol)); break; case Types.INTEGER: value = new Integer(set.getInt(dbcol)); break; case Types.NUMERIC: value = new Long(set.getLong(dbcol)); break; case Types.SMALLINT: value = new Short(set.getShort(dbcol)); break; case Types.TIME: value = set.getTime(dbcol); break; case Types.TIMESTAMP: value = set.getTimestamp(dbcol); break; case Types.ARRAY: value = set.getArray(dbcol); // new Integer(set.getInt(dbcol)); break; case Types.BIT: value = new Integer(set.getInt(dbcol)); break; case Types.CHAR: value = new Integer(set.getInt(dbcol)); break; case Types.STRUCT: value = (Struct) set.getObject(dbcol); break; case Types.OTHER: value = set.getObject(dbcol); break; default: // The blob types have to be requested separately, via a Reader. value = ""; break; } return value; } /** * Set the Statement column so that the results are mapped correctly. * * @param statement the prepared statement * @param position the position of the column * @param request the request * @param entry the configuration object * @param param the name of the request parameter * @param value the value of the column * @param rowIndex the index of the current row for manyrows inserts */ public static void setColumn(PreparedStatement statement, int position, Object value, Integer typeObject) throws Exception { if (value instanceof String) { value = ((String) value).trim(); } if (typeObject == null) { throw new SQLException("Can't set column because the type is unrecognized"); } if (value == null) { /** If the value is null, set the column value null and return **/ statement.setNull(position, typeObject.intValue()); return; } if ("".equals(value)) { switch (typeObject.intValue()) { case Types.CHAR: case Types.CLOB: case Types.VARCHAR: /** If the value is an empty string and the column is a string type, we can continue **/ break; default: /** If the value is an empty string and the column is something else, we treat it as a null value **/ statement.setNull(position, typeObject.intValue()); return; } } File file = null; switch (typeObject.intValue()) { case Types.CLOB: int length = -1; InputStream asciiStream = null; if (value instanceof File) { File asciiFile = (File) value; asciiStream = new BufferedInputStream(new FileInputStream(asciiFile)); length = (int) asciiFile.length(); } else { String asciiText = (String) value; asciiStream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes())); length = asciiText.length(); } statement.setAsciiStream(position, asciiStream, length); break; case Types.BIGINT: BigDecimal bd = null; if (value instanceof BigDecimal) { bd = (BigDecimal) value; } else { bd = new BigDecimal((String) value); } statement.setBigDecimal(position, bd); break; case Types.TINYINT: Byte b = null; if (value instanceof Byte) { b = (Byte) value; } else { b = new Byte((String) value); } statement.setByte(position, b.byteValue()); break; case Types.DATE: Date d = null; if (value instanceof Date) { d = (Date) value; } else if (value instanceof java.util.Date) { d = new Date(((java.util.Date) value).getTime()); } statement.setDate(position, d); break; case Types.DOUBLE: Double db = null; if (value instanceof Double) { db = (Double) value; } else { db = new Double((String) value); } statement.setDouble(position, db.doubleValue()); break; case Types.FLOAT: Float f = null; if (value instanceof Float) { f = (Float) value; } else { f = new Float((String) value); } statement.setFloat(position, f.floatValue()); break; case Types.NUMERIC: Long l = null; if (value instanceof Long) { l = (Long) value; } else { l = new Long((String) value); } statement.setLong(position, l.longValue()); break; case Types.SMALLINT: Short s = null; if (value instanceof Short) { s = (Short) value; } else { s = new Short((String) value); } statement.setShort(position, s.shortValue()); break; case Types.TIME: Time t = null; if (value instanceof Time) { t = (Time) value; } statement.setTime(position, t); break; case Types.TIMESTAMP: Timestamp ts = null; if (value instanceof Time) { ts = (Timestamp) value; } statement.setTimestamp(position, ts); break; case Types.ARRAY: statement.setArray(position, (Array) value); // no way to convert string to array break; case Types.STRUCT: case Types.OTHER: statement.setObject(position, value); break; case Types.LONGVARBINARY: statement.setTimestamp(position, new Timestamp((new java.util.Date()).getTime())); case Types.VARCHAR: statement.setString(position, (String) value); break; case Types.BLOB: if (value instanceof File) { file = (File)value; } else if (value instanceof String) { file = new File((String)value); } else { throw new SQLException("Invalid type for blob: "+value.getClass().getName()); } //InputStream input = new BufferedInputStream(new FileInputStream(file)); FileInputStream input = new FileInputStream(file); statement.setBinaryStream(position, input, (int)file.length()); break; case Types.INTEGER: Integer i = null; if (value instanceof Integer) { i = (Integer) value; } else { i = new Integer((String) value); } statement.setInt(position, i.intValue()); break; default: throw new SQLException("Impossible exception - invalid type "); } } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]