KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > oid > AutoIncrementIdGenerator


1 package org.apache.torque.oid;
2
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements. See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership. The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied. See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */

21
22 import java.math.BigDecimal JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import org.apache.torque.adapter.DB;
25 import org.apache.torque.util.SQLBuilder;
26 import com.workingdogs.village.QueryDataSet;
27 import com.workingdogs.village.Record;
28 import com.workingdogs.village.Value;
29
30 /**
31  * This generator works with databases that have an sql syntax that
32  * allows the retrieval of the last id used to insert a row for a
33  * Connection.
34  *
35  * @author <a HREF="mailto:jmcnally@collab.net">John D. McNally</a>
36  * @version $Id: AutoIncrementIdGenerator.java 473821 2006-11-11 22:37:25Z tv $
37  */

38 public class AutoIncrementIdGenerator implements IdGenerator
39 {
40     /** the adapter that knows the correct sql syntax */
41     private DB dbAdapter;
42
43     /** The internal name of the Database that this Generator is connected to */
44     private String JavaDoc name = null;
45
46     /**
47      * Creates an IdGenerator which will work with the specified database.
48      *
49      * @param dbAdapter the adapter that knows the correct sql syntax.
50      * @param name The name of the datasource to find the correct schema
51      */

52     public AutoIncrementIdGenerator(final DB dbAdapter, final String JavaDoc name)
53     {
54         this.dbAdapter = dbAdapter;
55         this.name = name;
56     }
57
58     /**
59      * Returns the last ID used by this connection.
60      *
61      * @param connection A Connection.
62      * @param keyInfo an Object that contains additional info.
63      * @return An int with the value for the id.
64      * @exception Exception Database error.
65      */

66     public int getIdAsInt(Connection JavaDoc connection, Object JavaDoc keyInfo)
67         throws Exception JavaDoc
68     {
69         return getIdAsVillageValue(connection, keyInfo).asInt();
70     }
71
72     /**
73      * Returns the last ID used by this connection.
74      *
75      * @param connection A Connection.
76      * @param keyInfo an Object that contains additional info.
77      * @return A long with the value for the id.
78      * @exception Exception Database error.
79      */

80     public long getIdAsLong(Connection JavaDoc connection, Object JavaDoc keyInfo)
81         throws Exception JavaDoc
82     {
83         return getIdAsVillageValue(connection, keyInfo).asLong();
84     }
85
86     /**
87      * Returns the last ID used by this connection.
88      *
89      * @param connection A Connection.
90      * @param keyInfo an Object that contains additional info.
91      * @return A BigDecimal with the last value auto-incremented as a
92      * result of an insert.
93      * @exception Exception Database error.
94      */

95     public BigDecimal JavaDoc getIdAsBigDecimal(Connection JavaDoc connection, Object JavaDoc keyInfo)
96         throws Exception JavaDoc
97     {
98         return getIdAsVillageValue(connection, keyInfo).asBigDecimal();
99     }
100
101
102     /**
103      * Returns the last ID used by this connection.
104      *
105      * @param connection A Connection.
106      * @param keyInfo an Object that contains additional info.
107      * @return A String with the last value auto-incremented as a
108      * result of an insert.
109      * @exception Exception Database error.
110      */

111     public String JavaDoc getIdAsString(Connection JavaDoc connection, Object JavaDoc keyInfo)
112         throws Exception JavaDoc
113     {
114         return getIdAsVillageValue(connection, keyInfo).asString();
115     }
116
117     /**
118      * A flag to determine the timing of the id generation
119      *
120      * @return a <code>boolean</code> value
121      */

122     public boolean isPriorToInsert()
123     {
124         return false;
125     }
126
127     /**
128      * A flag to determine the timing of the id generation
129      *
130      * @return a <code>boolean</code> value
131      */

132     public boolean isPostInsert()
133     {
134         return true;
135     }
136
137     /**
138      * A flag to determine whether a Connection is required to
139      * generate an id.
140      *
141      * @return a <code>boolean</code> value
142      */

143     public final boolean isConnectionRequired()
144     {
145         return true;
146     }
147
148     /**
149      * Returns the last ID used by this connection.
150      *
151      * @param connection A Connection.
152      * @param keyInfo an Object that contains additional info.
153      * @return A Village Value with the last value auto-incremented as a
154      * result of an insert.
155      * @exception Exception Database error.
156      */

157     private Value getIdAsVillageValue(Connection JavaDoc connection, Object JavaDoc keyInfo)
158         throws Exception JavaDoc
159     {
160         String JavaDoc tableName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name);
161         String JavaDoc idSQL = dbAdapter.getIDMethodSQL(tableName);
162         Value id = null;
163         QueryDataSet qds = null;
164         try
165         {
166             qds = new QueryDataSet(connection, idSQL);
167             qds.fetchRecords(1);
168             Record rec = qds.getRecord(0);
169             id = rec.getValue(1);
170         }
171         finally
172         {
173             if (qds != null)
174             {
175                 qds.close();
176             }
177         }
178         return id;
179     }
180 }
181
Popular Tags