KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > AutoIncJdbcKeyGenerator


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql;
13
14 import com.versant.core.jdbc.JdbcKeyGenerator;
15 import com.versant.core.jdbc.JdbcKeyGeneratorFactory;
16 import com.versant.core.jdbc.JdbcMetaDataBuilder;
17 import com.versant.core.jdbc.metadata.JdbcTable;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.SQLException JavaDoc;
21 import java.sql.Statement JavaDoc;
22 import java.util.HashSet JavaDoc;
23
24 import com.versant.core.common.BindingSupportImpl;
25
26 /**
27  * Key generator for classes using a database autoincrement or serial column
28  * for the primary key.
29  */

30 public class AutoIncJdbcKeyGenerator implements JdbcKeyGenerator {
31
32     protected SqlDriver sqlDriver;
33
34     /**
35      * Our factory.
36      */

37     public static class Factory implements JdbcKeyGeneratorFactory {
38
39         /**
40          * Create a javabean to hold args for a createJdbcKeyGenerator call or null
41          * if the key generator does not accept any arguments.
42          */

43         public Object JavaDoc createArgsBean() {
44             return null;
45         }
46
47         /**
48          * Create a JdbcKeyGenerator for class using props as parameters. The
49          * instance returned may be new or may be a shared instance.
50          */

51         public JdbcKeyGenerator createJdbcKeyGenerator(String JavaDoc className,
52                 JdbcTable classTable, Object JavaDoc args) {
53             return new AutoIncJdbcKeyGenerator(className, classTable);
54         }
55     }
56
57     public AutoIncJdbcKeyGenerator(String JavaDoc className, JdbcTable classTable) {
58         if (classTable.pk.length > 1) {
59             throw BindingSupportImpl.getInstance().illegalArgument("Cannot use AUTOINC key generator on a table with multiple " +
60                     "primary key columns: " + classTable.name + " (" +
61                     className + ")");
62         }
63         sqlDriver = classTable.sqlDriver;
64     }
65
66     /**
67      * Add any JdbcTable instances that this key generator requires to the
68      * supplied set. This method is called once per key generator during meta
69      * data generation. Any tables returned will be added to the meta data and
70      * will get into SQL scripts and so on. If the same key generator
71      * instance is returned more than once by a factory then this method
72      * will still only be called once on the instance.
73      */

74     public void addKeyGenTables(HashSet JavaDoc set, JdbcMetaDataBuilder mdb) {
75         // we do not use any extra tables
76
}
77
78     /**
79      * If the new key can only be detirmined after the new row has been
80      * inserted (e.g. if using a database autoincrement column) then this
81      * should return true.
82      */

83     public boolean isPostInsertGenerator() {
84         return true;
85     }
86
87     /**
88      * Get extra SQL to be appended to the insert statement. This is only
89      * called for post insert key generators. Return null if no extra SQL
90      * is required. Key generators can use this as an alternative to running
91      * a separate query to get the primary key for the just inserted row.
92      */

93     public String JavaDoc getPostInsertSQLSuffix(JdbcTable classTable) {
94         return sqlDriver.getAutoIncPostInsertSQLSuffix(classTable);
95     }
96
97     /**
98      * Generate a new primary key value for a new instance of the supplied
99      * class after the row has been inserted. The values generated will be used
100      * to populate a new OID and then set on a PreparedStatement for the
101      * insert. This is called if isPostInsertGenerator returns true.
102      *
103      * @param className The name of the class
104      * @param classTable The table for the class
105      * @param data The array to store the key values in.
106      * @param con Connection to the DataSource for the class.
107      * @param stat Statement created from con. Do not close it. This will have
108      * just been used to insert the new row.
109      * @throws SQLException on errors
110      */

111     public void generatePrimaryKeyPost(String JavaDoc className,
112             JdbcTable classTable, Object JavaDoc[] data,
113             Connection JavaDoc con, Statement JavaDoc stat) throws SQLException JavaDoc {
114         data[0] = sqlDriver.getAutoIncColumnValue(classTable, con, stat);
115     }
116
117     /**
118      * Generate a new primary key value for a new instance of the supplied
119      * class prior to the row being inserted. The values generated will be used
120      * to populate a new OID and then set on a PreparedStatement for the
121      * insert. This is called if isPostInsertGenerator returns false.<p>
122      * <p/>
123      * The newObjectCount parameter indicates the number of new objects that
124      * will be inserted (including this one) in the same transaction using
125      * this key generator. This may be used to optimize the behavior of the
126      * key generator or be ignored. The highlow key generator uses this value
127      * instead of its grabSize to avoid executing redundant updates and
128      * selects.<p>
129      *
130      * @param className The name of the class
131      * @param classTable The table for the class
132      * @param newObjectCount The number of new objects being created
133      * @param data The array to store the key values in.
134      * @param con Connection to the DataSource for the class.
135      * @throws SQLException on errors
136      */

137     public void generatePrimaryKeyPre(String JavaDoc className,
138             JdbcTable classTable, int newObjectCount, Object JavaDoc[] data,
139             Connection JavaDoc con) throws SQLException JavaDoc {
140         throw BindingSupportImpl.getInstance().internal("generatePrimaryKeyPre called for " +
141                 "post insert key generator");
142     }
143
144     /**
145      * Initialize this key generator. This is called when the JDO
146      * implementation initializes before any keys are generated. Key
147      * generators should use this to avoid popular race conditions and
148      * deadlock opportunities (e.g. multiple 'select max(id) from table'
149      * statements executing at the same time). If the same key generator
150      * instance is used on more than one class this will be called once
151      * for each class.
152      *
153      * @param className The name of the class
154      * @param classTable The table for the class
155      * @param con Connection to the DataSource for the class
156      */

157     public void init(String JavaDoc className, JdbcTable classTable,
158             Connection JavaDoc con) throws SQLException JavaDoc {
159         // nothing to do
160
}
161
162     /**
163      * Does this key generator require its own connection? If it does then
164      * one will be obtained to generate the key and committed after the
165      * key has been generated. This is called prior to every key generation
166      * call.
167      */

168     public boolean isRequiresOwnConnection() {
169         return false;
170     }
171
172 }
173
174
Popular Tags