KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > JdbcKeyGenerator


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;
13
14 import com.versant.core.jdbc.metadata.JdbcTable;
15
16 import java.sql.Connection JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import java.sql.Statement JavaDoc;
19 import java.util.HashSet JavaDoc;
20
21 /**
22  * These generate primary keys for new rows in tables. Instances must be
23  * thread safe.
24  */

25 public interface JdbcKeyGenerator {
26
27     /**
28      * Add any JdbcTable instances that this key generator requires to the
29      * supplied set. This method is called once per key generator during meta
30      * data generation. Any tables returned will be added to the meta data and
31      * will get into SQL scripts and so on. If the same key generator
32      * instance is returned more than once by a factory then this method
33      * will still only be called once on the instance.
34      */

35     public void addKeyGenTables(HashSet JavaDoc set, JdbcMetaDataBuilder mdb);
36
37     /**
38      * If the new key can only be detirmined after the new row has been
39      * inserted (e.g. if using a database autoincrement column) then this
40      * should return true.
41      */

42     public boolean isPostInsertGenerator();
43
44     /**
45      * Initialize this key generator. This is called when the JDO Genie
46      * server initializes before any keys are generated. Key
47      * generators should use this to avoid popular race conditions and
48      * deadlock opportunities (e.g. multiple 'select max(id) from table'
49      * statements executing at the same time). If the same key generator
50      * instance is used on more than one class this will be called once
51      * for each class.
52      *
53      * @param className The name of the class
54      * @param classTable The table for the class
55      * @param con Connection to the DataSource for the class
56      */

57     public void init(String JavaDoc className, JdbcTable classTable,
58             Connection JavaDoc con) throws SQLException JavaDoc;
59
60     /**
61      * Does this key generator require its own connection? If it does then
62      * one will be obtained to generate the key and committed after the
63      * key has been generated. This is called prior to every key generation
64      * call.
65      */

66     public boolean isRequiresOwnConnection();
67
68     /**
69      * Generate a new primary key value for a new instance of the supplied
70      * class prior to the row being inserted. The values generated will be used
71      * to populate a new OID and then set on a PreparedStatement for the
72      * insert. This is called if isPostInsertGenerator returns false.<p>
73      * <p/>
74      * The newObjectCount parameter indicates the number of new objects that
75      * will be inserted (including this one) in the same transaction using
76      * this key generator. This may be used to optimize the behavior of the
77      * key generator or be ignored. The highlow key generator uses this value
78      * instead of its grabSize to avoid executing redundant updates and
79      * selects.<p>
80      *
81      * @param className The name of the class
82      * @param classTable The table for the class
83      * @param newObjectCount The number of new objects being created
84      * @param data The array to store the key values in.
85      * @param con Connection to the DataSource for the class.
86      * @throws SQLException on errors
87      */

88     public void generatePrimaryKeyPre(String JavaDoc className,
89             JdbcTable classTable, int newObjectCount, Object JavaDoc[] data,
90             Connection JavaDoc con) throws SQLException JavaDoc;
91
92     /**
93      * Get extra SQL to be appended to the insert statement. This is only
94      * called for post insert key generators. Return null if no extra SQL
95      * is required. Key generators can use this as an alternative to running
96      * a separate query to get the primary key for the just inserted row.
97      */

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

114     public void generatePrimaryKeyPost(String JavaDoc className,
115             JdbcTable classTable, Object JavaDoc[] data,
116             Connection JavaDoc con, Statement JavaDoc stat) throws SQLException JavaDoc;
117
118 }
119
Popular Tags