001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.c3p0.dbms;
024:
025: import java.lang.reflect.*;
026: import java.sql.*;
027: import com.mchange.v2.c3p0.*;
028: import com.mchange.v2.sql.SqlUtils;
029: import oracle.sql.BLOB;
030: import oracle.sql.CLOB;
031: import oracle.jdbc.driver.OracleConnection;
032:
033: /**
034: * A convenience class for OracleUsers who wish to use Oracle-specific Connection API
035: * without working directly with c3p0 raw connection operations.
036: */
037: public final class OracleUtils {
038: final static Class[] CREATE_TEMP_ARGS = new Class[] {
039: Connection.class, boolean.class, int.class };
040:
041: /**
042: * Uses Oracle-specific API on the raw, underlying Connection to create a temporary BLOB.
043: * <b>Users are responsible for calling freeTemporary on the returned BLOB prior to Connection close() / check-in!
044: * c3p0 will <i>not</i> automatically clean up temporary BLOBs.</b>
045: *
046: * @param c3p0ProxyCon may be a c3p0 proxy for an <tt>oracle.jdbc.driver.OracleConnection</tt>, or an
047: * <tt>oracle.jdbc.driver.OracleConnection</tt> directly.
048: */
049: public static BLOB createTemporaryBLOB(Connection c3p0ProxyCon,
050: boolean cache, int duration) throws SQLException {
051: if (c3p0ProxyCon instanceof C3P0ProxyConnection) {
052: try {
053: C3P0ProxyConnection castCon = (C3P0ProxyConnection) c3p0ProxyCon;
054: Method m = BLOB.class.getMethod("createTemporary",
055: CREATE_TEMP_ARGS);
056: Object[] args = new Object[] {
057: C3P0ProxyConnection.RAW_CONNECTION,
058: Boolean.valueOf(cache), new Integer(duration) };
059: return (BLOB) castCon.rawConnectionOperation(m, null,
060: args);
061: } catch (InvocationTargetException e) {
062: if (Debug.DEBUG)
063: e.printStackTrace();
064: throw SqlUtils.toSQLException(e.getTargetException());
065: } catch (Exception e) {
066: if (Debug.DEBUG)
067: e.printStackTrace();
068: throw SqlUtils.toSQLException(e);
069: }
070: } else if (c3p0ProxyCon instanceof OracleConnection)
071: return BLOB.createTemporary(c3p0ProxyCon, cache, duration);
072: else
073: throw new SQLException(
074: "Cannot create an oracle BLOB from a Connection that is neither an oracle.jdbc.driver.Connection, "
075: + "nor a C3P0ProxyConnection wrapped around an oracle.jdbc.driver.Connection.");
076: }
077:
078: /**
079: * Uses Oracle-specific API on the raw, underlying Connection to create a temporary CLOB.
080: * <b>Users are responsible for calling freeTemporary on the returned BLOB prior to Connection close() / check-in!
081: * c3p0 will <i>not</i> automatically clean up temporary CLOBs.</b>
082: *
083: * @param c3p0ProxyCon may be a c3p0 proxy for an <tt>oracle.jdbc.driver.OracleConnection</tt>, or an
084: * <tt>oracle.jdbc.driver.OracleConnection</tt> directly.
085: */
086: public static CLOB createTemporaryCLOB(Connection c3p0ProxyCon,
087: boolean cache, int duration) throws SQLException {
088: if (c3p0ProxyCon instanceof C3P0ProxyConnection) {
089: try {
090: C3P0ProxyConnection castCon = (C3P0ProxyConnection) c3p0ProxyCon;
091: Method m = CLOB.class.getMethod("createTemporary",
092: CREATE_TEMP_ARGS);
093: Object[] args = new Object[] {
094: C3P0ProxyConnection.RAW_CONNECTION,
095: Boolean.valueOf(cache), new Integer(duration) };
096: return (CLOB) castCon.rawConnectionOperation(m, null,
097: args);
098: } catch (InvocationTargetException e) {
099: if (Debug.DEBUG)
100: e.printStackTrace();
101: throw SqlUtils.toSQLException(e.getTargetException());
102: } catch (Exception e) {
103: if (Debug.DEBUG)
104: e.printStackTrace();
105: throw SqlUtils.toSQLException(e);
106: }
107: } else if (c3p0ProxyCon instanceof OracleConnection)
108: return CLOB.createTemporary(c3p0ProxyCon, cache, duration);
109: else
110: throw new SQLException(
111: "Cannot create an oracle CLOB from a Connection that is neither an oracle.jdbc.driver.Connection, "
112: + "nor a C3P0ProxyConnection wrapped around an oracle.jdbc.driver.Connection.");
113: }
114:
115: private OracleUtils() {
116: }
117: }
|