~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/util/NamedGeneratedKeysHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hibernate.util;
 
2
 
 
3
import org.hibernate.AssertionFailure;
 
4
 
 
5
import java.lang.reflect.Method;
 
6
import java.lang.reflect.InvocationTargetException;
 
7
import java.sql.Connection;
 
8
import java.sql.Statement;
 
9
import java.sql.PreparedStatement;
 
10
import java.sql.SQLException;
 
11
import java.sql.ResultSet;
 
12
 
 
13
/**
 
14
 * @author Steve Ebersole
 
15
 */
 
16
public class NamedGeneratedKeysHelper {
 
17
        private NamedGeneratedKeysHelper() {
 
18
        }
 
19
 
 
20
        private static final Method PREPARE_STATEMENT_METHOD;
 
21
        private static final Method GET_GENERATED_KEYS_METHOD;
 
22
 
 
23
        static {
 
24
                try {
 
25
                        PREPARE_STATEMENT_METHOD = Connection.class.getMethod(
 
26
                                        "prepareStatement",
 
27
                                new Class[] { String.class, String[].class }
 
28
                        );
 
29
                        GET_GENERATED_KEYS_METHOD = Statement.class.getDeclaredMethod(
 
30
                                        "getGeneratedKeys",
 
31
                                null
 
32
                        );
 
33
                }
 
34
                catch ( Exception e ) {
 
35
                        throw new AssertionFailure( "could not initialize getGeneratedKeys() support", e );
 
36
                }
 
37
        }
 
38
 
 
39
        public static PreparedStatement prepareStatement(Connection conn, String sql, String[] columnNames) throws SQLException {
 
40
                Object[] args = new Object[] { sql, columnNames } ;
 
41
                try {
 
42
                        return ( PreparedStatement ) PREPARE_STATEMENT_METHOD.invoke( conn, args );
 
43
                }
 
44
                catch ( InvocationTargetException ite ) {
 
45
                        if ( ite.getTargetException() instanceof SQLException ) {
 
46
                                throw ( SQLException ) ite.getTargetException();
 
47
                        }
 
48
                        else if ( ite.getTargetException() instanceof RuntimeException ) {
 
49
                                throw ( RuntimeException ) ite.getTargetException();
 
50
                        }
 
51
                        else {
 
52
                                throw new AssertionFailure( "InvocationTargetException preparing statement capable of returning generated keys (JDBC3)", ite );
 
53
                        }
 
54
                }
 
55
                catch ( IllegalAccessException iae ) {
 
56
                        throw new AssertionFailure( "IllegalAccessException preparing statement capable of returning generated keys (JDBC3)", iae );
 
57
                }
 
58
        }
 
59
 
 
60
        public static ResultSet getGeneratedKey(PreparedStatement ps) throws SQLException {
 
61
                try {
 
62
                        return ( ResultSet ) GET_GENERATED_KEYS_METHOD.invoke( ps, null );
 
63
                }
 
64
                catch ( InvocationTargetException ite ) {
 
65
                        if ( ite.getTargetException() instanceof SQLException ) {
 
66
                                throw ( SQLException ) ite.getTargetException();
 
67
                        }
 
68
                        else if ( ite.getTargetException() instanceof RuntimeException ) {
 
69
                                throw ( RuntimeException ) ite.getTargetException();
 
70
                        }
 
71
                        else {
 
72
                                throw new AssertionFailure( "InvocationTargetException extracting generated keys (JDBC3)", ite );
 
73
                        }
 
74
                }
 
75
                catch ( IllegalAccessException iae ) {
 
76
                        throw new AssertionFailure( "IllegalAccessException extracting generated keys (JDBC3)", iae );
 
77
                }
 
78
        }
 
79
}