View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006 Imola Informatica.
3    *  All rights reserved. This program and the accompanying materials
4    *  are made available under the terms of the LGPL License v2.1
5    *  which accompanies this distribution, and is available at
6    *  http://www.gnu.org/licenses/lgpl.html
7    *******************************************************************************/
8   /**
9    * 
10   */
11  package it.imolinfo.jbi4cics.connection.jdbc.util;
12  
13  import java.sql.SQLException;
14  import java.sql.Statement;
15  
16  /**
17   * @author raffaele
18   *
19   */
20  class PreparedStatementInvocationHandler extends StatementInvocationHandler {
21    
22    protected String sql;
23    protected int autoGeneratedKeys=-1;
24    protected int[] columnIndexes;
25    protected String[] columnNames;
26  
27    /**
28     * @param sql    The sql
29     */
30    public PreparedStatementInvocationHandler(String sql) {
31      super();
32      this.sql=sql;
33    }
34    
35    /**
36     * @param sql     The sql
37     * @param autoGeneratedKeys    Theauto-generated key
38     */
39    public PreparedStatementInvocationHandler(String sql, int autoGeneratedKeys) {
40      super();
41      this.sql=sql;
42      this.autoGeneratedKeys=autoGeneratedKeys;
43    }
44   
45    /**
46     * @param sql     The sql
47     * @param columnIndexes    The column index
48     */
49    public PreparedStatementInvocationHandler(String sql, int[] columnIndexes) {
50      super();
51      this.sql=sql;    
52      this.columnIndexes=columnIndexes;
53    }
54  
55    /**
56     * @param sql    The SQL
57     * @param resultSetType    The result set type
58     * @param resultSetConcurrency    The result set concurrency
59     */
60    public PreparedStatementInvocationHandler(String sql, int resultSetType, int resultSetConcurrency) {
61      super(resultSetType, resultSetConcurrency);
62      this.sql=sql;
63    }
64  
65    /** 
66     * @param sql    The SQL
67     * @param resultSetType    The result set type
68     * @param resultSetConcurrency    The result set concurrency
69     * @param resultSetHoldability    The result set Hodability
70     */
71    public PreparedStatementInvocationHandler(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) {
72      super(resultSetType, resultSetConcurrency, resultSetHoldability);
73      this.sql=sql;
74    }
75    
76    /**
77     * @param sql    The sql
78     * @param columnNames The colum names
79     */
80    public PreparedStatementInvocationHandler(String sql, String[] columnNames) {
81      super();
82      this.sql=sql;
83      this.columnNames=columnNames;
84    }
85    
86    /**
87     * Metodo un po' complicato per capire quale era il costruttore e riprodure esattamente il comportamento desiderato.
88     * @return    The created statement
89     * @throws SQLException    The SQL exceptio
90     */
91    protected Statement createStatement() throws SQLException {
92      if (columnNames!=null){
93        return getConnection().prepareStatement(sql,columnNames);
94      }
95      if (columnIndexes!=null){
96        return getConnection().prepareStatement(sql,columnIndexes);
97      }
98      if (autoGeneratedKeys!=-1){
99        return getConnection().prepareStatement(sql,autoGeneratedKeys);
100     }    
101     return getConnection().prepareStatement(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
102   }
103 
104 }