View Javadoc

1   /***
2    * License Agreement.
3    * 
4    * JSPA (POJO-SP)
5    * 
6    * Copyright (C) 2009 HRX Pty Ltd
7    * 
8    * This file is part of JSPA.
9    * 
10   * JSPA is free software: you can redistribute it and/or modify it under the
11   * terms of the GNU Lesser General Public License version 3 as published by the
12   * Free Software Foundation.
13   * 
14   * JSPA is distributed in the hope that it will be useful, but WITHOUT ANY
15   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16   * A PARTICULAR PURPOSE. See the Lesser General Public License for more details.
17   * 
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with JSPA. If not, see <http://www.gnu.org/licenses/>.
20   */
21  package com.hrx.rasp.util.dao;
22  
23  import java.lang.reflect.Constructor;
24  import java.lang.reflect.Method;
25  import java.sql.Array;
26  import java.sql.Connection;
27  import java.util.Collection;
28  
29  import com.hrx.rasp.util.dao.exception.StoredProcedurePrepareException;
30  import com.hrx.rasp.util.dao.metadata.MetadataHelper;
31  
32  /***
33   * @author dan.stoica <dan.stoica@acslink.net.au>
34   *
35   */
36  public class OracleHelper
37  {
38  	public static final int ORACLE_CURSOR = -10;
39  
40  	public static final String ORACLE_SQL_ARRAY_DESCRIPTOR = "oracle.sql.ArrayDescriptor";
41  	public static final String ORACLE_SQL_ARRAY = "oracle.sql.ARRAY";
42  
43  	public final static Array createOracleARRAY(String arrayType, Collection<?> values, Connection conn) throws StoredProcedurePrepareException
44  	{
45  		Object arrayDescr = createOracleArrayDescriptor(arrayType, conn);
46  		Array array = createOracleARRAY(arrayDescr, conn, values.toArray());
47  		return array;
48  	}
49  
50  	public final static Array createOracleARRAY(Object arrayDescr, java.sql.Connection conn, Object[] values) throws StoredProcedurePrepareException
51  	{
52  		try
53  		{
54  			Class<?> oracleArrayClass = Class.forName(ORACLE_SQL_ARRAY);
55  
56  			Object[] args =
57  			{
58  					arrayDescr, conn, values
59  			};
60  
61  			Class<?>[] argsClass = new Class[]
62  			{
63  					arrayDescr.getClass(), java.sql.Connection.class, Object.class
64  			};
65  
66  			Constructor<?> constructor = oracleArrayClass.getConstructor(argsClass);
67  			return (Array) constructor.newInstance(args);
68  
69  		}
70  		catch (Throwable e)
71  		{
72  			throw new StoredProcedurePrepareException("Prepare Oracle array exception", e);
73  		}
74  	}
75  
76  	@SuppressWarnings("unchecked")
77  	public final static Object createOracleArrayDescriptor(String arrayType, java.sql.Connection conn) throws StoredProcedurePrepareException
78  	{
79  		try
80  		{
81  			Class descClass = Class.forName(ORACLE_SQL_ARRAY_DESCRIPTOR);
82  			Class<?>[] argsClass = new Class<?>[]
83  			{
84  					String.class, Connection.class
85  			};
86  			Object[] args = new Object[]
87  			{
88  					arrayType, conn
89  			};
90  			Method descMethod = MetadataHelper.getMethod(descClass, "createDescriptor", argsClass);
91  			return descMethod.invoke(null, args);
92  		}
93  		catch (Throwable e)
94  		{
95  			throw new StoredProcedurePrepareException("Prepare OracleArrayDescriptor exception", e);
96  		}
97  
98  	}
99  }