~raginggoblin/infolog/infolog

« back to all changes in this revision

Viewing changes to InfologServer/lib/hibernate-distribution-3.3.2.GA/project/core/src/main/java/org/hibernate/id/SequenceGenerator.java

  • Committer: Raging Goblin
  • Date: 2013-11-16 16:51:32 UTC
  • Revision ID: raging_goblin-20131116165132-weujnptzc88uy4ah
Mavenized the project, now using shared project InfologSync

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Hibernate, Relational Persistence for Idiomatic Java
3
 
 *
4
 
 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5
 
 * indicated by the @author tags or express copyright attribution
6
 
 * statements applied by the authors.  All third-party contributions are
7
 
 * distributed under license by Red Hat Middleware LLC.
8
 
 *
9
 
 * This copyrighted material is made available to anyone wishing to use, modify,
10
 
 * copy, or redistribute it subject to the terms and conditions of the GNU
11
 
 * Lesser General Public License, as published by the Free Software Foundation.
12
 
 *
13
 
 * This program is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
16
 
 * for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public License
19
 
 * along with this distribution; if not, write to:
20
 
 * Free Software Foundation, Inc.
21
 
 * 51 Franklin Street, Fifth Floor
22
 
 * Boston, MA  02110-1301  USA
23
 
 *
24
 
 */
25
 
package org.hibernate.id;
26
 
 
27
 
import java.io.Serializable;
28
 
import java.sql.PreparedStatement;
29
 
import java.sql.ResultSet;
30
 
import java.sql.SQLException;
31
 
import java.util.Properties;
32
 
 
33
 
import org.slf4j.Logger;
34
 
import org.slf4j.LoggerFactory;
35
 
import org.hibernate.HibernateException;
36
 
import org.hibernate.MappingException;
37
 
import org.hibernate.exception.JDBCExceptionHelper;
38
 
import org.hibernate.dialect.Dialect;
39
 
import org.hibernate.engine.SessionImplementor;
40
 
import org.hibernate.mapping.Table;
41
 
import org.hibernate.type.Type;
42
 
import org.hibernate.util.PropertiesHelper;
43
 
 
44
 
/**
45
 
 * <b>sequence</b><br>
46
 
 * <br>
47
 
 * Generates <tt>long</tt> values using an oracle-style sequence. A higher
48
 
 * performance algorithm is <tt>SequenceHiLoGenerator</tt>.<br>
49
 
 * <br>
50
 
 * Mapping parameters supported: sequence, parameters.
51
 
 *
52
 
 * @see SequenceHiLoGenerator
53
 
 * @see TableHiLoGenerator
54
 
 * @author Gavin King
55
 
 */
56
 
 
57
 
public class SequenceGenerator implements PersistentIdentifierGenerator, Configurable {
58
 
 
59
 
        /**
60
 
         * The sequence parameter
61
 
         */
62
 
        public static final String SEQUENCE = "sequence";
63
 
 
64
 
        /**
65
 
         * The parameters parameter, appended to the create sequence DDL.
66
 
         * For example (Oracle): <tt>INCREMENT BY 1 START WITH 1 MAXVALUE 100 NOCACHE</tt>.
67
 
         */
68
 
        public static final String PARAMETERS = "parameters";
69
 
 
70
 
        private String sequenceName;
71
 
        private String parameters;
72
 
        private Type identifierType;
73
 
        private String sql;
74
 
 
75
 
        private static final Logger log = LoggerFactory.getLogger(SequenceGenerator.class);
76
 
 
77
 
        public void configure(Type type, Properties params, Dialect dialect) throws MappingException {
78
 
                sequenceName = PropertiesHelper.getString(SEQUENCE, params, "hibernate_sequence");
79
 
                parameters = params.getProperty(PARAMETERS);
80
 
                String schemaName = params.getProperty(SCHEMA);
81
 
                String catalogName = params.getProperty(CATALOG);
82
 
 
83
 
                if (sequenceName.indexOf( '.' ) < 0) {
84
 
                        sequenceName = Table.qualify( catalogName, schemaName, sequenceName );
85
 
                }
86
 
 
87
 
                this.identifierType = type;
88
 
                sql = dialect.getSequenceNextValString(sequenceName);
89
 
        }
90
 
 
91
 
        public Serializable generate(SessionImplementor session, Object obj) 
92
 
        throws HibernateException {
93
 
                
94
 
                try {
95
 
 
96
 
                        PreparedStatement st = session.getBatcher().prepareSelectStatement(sql);
97
 
                        try {
98
 
                                ResultSet rs = st.executeQuery();
99
 
                                try {
100
 
                                        rs.next();
101
 
                                        Serializable result = IdentifierGeneratorFactory.get(
102
 
                                                        rs, identifierType
103
 
                                                );
104
 
                                        if ( log.isDebugEnabled() ) {
105
 
                                                log.debug("Sequence identifier generated: " + result);
106
 
                                        }
107
 
                                        return result;
108
 
                                }
109
 
                                finally {
110
 
                                        rs.close();
111
 
                                }
112
 
                        }
113
 
                        finally {
114
 
                                session.getBatcher().closeStatement(st);
115
 
                        }
116
 
                        
117
 
                }
118
 
                catch (SQLException sqle) {
119
 
                        throw JDBCExceptionHelper.convert(
120
 
                                        session.getFactory().getSQLExceptionConverter(),
121
 
                                        sqle,
122
 
                                        "could not get next sequence value",
123
 
                                        sql
124
 
                                );
125
 
                }
126
 
 
127
 
        }
128
 
 
129
 
        public String[] sqlCreateStrings(Dialect dialect) throws HibernateException {
130
 
                String[] ddl = dialect.getCreateSequenceStrings(sequenceName);
131
 
                if ( parameters != null ) {
132
 
                        ddl[ddl.length - 1] += ' ' + parameters;
133
 
                }
134
 
                return ddl;
135
 
        }
136
 
 
137
 
        public String[] sqlDropStrings(Dialect dialect) throws HibernateException {
138
 
                return dialect.getDropSequenceStrings(sequenceName);
139
 
        }
140
 
 
141
 
        public Object generatorKey() {
142
 
                return sequenceName;
143
 
        }
144
 
 
145
 
        public String getSequenceName() {
146
 
                return sequenceName;
147
 
        }
148
 
 
149
 
}