1 package org.apache.torque.oid;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.math.BigDecimal;
58 import java.sql.Connection;
59 import org.apache.torque.adapter.DB;
60 import com.workingdogs.village.QueryDataSet;
61 import com.workingdogs.village.Record;
62 import com.workingdogs.village.Value;
63
64 /***
65 * This generator works with databases that have an sql syntax that
66 * allows the retrieval of the last id used to insert a row for a
67 * Connection.
68 *
69 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
70 * @version $Id: AutoIncrementIdGenerator.java,v 1.5 2002/11/26 23:46:56 mpoeschl Exp $
71 */
72 public class AutoIncrementIdGenerator implements IdGenerator
73 {
74 /*** the adapter that knows the correct sql syntax */
75 private DB dbAdapter;
76
77 /***
78 * Creates an IdGenerator which will work with the specified database.
79 *
80 * @param adapter the adapter that knows the correct sql syntax.
81 */
82 public AutoIncrementIdGenerator(DB adapter)
83 {
84 dbAdapter = adapter;
85 }
86
87 /***
88 * Returns the last ID used by this connection.
89 *
90 * @param connection A Connection.
91 * @param keyInfo an Object that contains additional info.
92 * @return An int with the value for the id.
93 * @exception Exception Database error.
94 */
95 public int getIdAsInt(Connection connection, Object keyInfo)
96 throws Exception
97 {
98 return getIdAsVillageValue(connection, keyInfo).asInt();
99 }
100
101 /***
102 * Returns the last ID used by this connection.
103 *
104 * @param connection A Connection.
105 * @param keyInfo an Object that contains additional info.
106 * @return A long with the value for the id.
107 * @exception Exception Database error.
108 */
109 public long getIdAsLong(Connection connection, Object keyInfo)
110 throws Exception
111 {
112 return getIdAsVillageValue(connection, keyInfo).asLong();
113 }
114
115 /***
116 * Returns the last ID used by this connection.
117 *
118 * @param connection A Connection.
119 * @param keyInfo an Object that contains additional info.
120 * @return A BigDecimal with the last value auto-incremented as a
121 * result of an insert.
122 * @exception Exception Database error.
123 */
124 public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo)
125 throws Exception
126 {
127 return getIdAsVillageValue(connection, keyInfo).asBigDecimal();
128 }
129
130
131 /***
132 * Returns the last ID used by this connection.
133 *
134 * @param connection A Connection.
135 * @param keyInfo an Object that contains additional info.
136 * @return A String with the last value auto-incremented as a
137 * result of an insert.
138 * @exception Exception Database error.
139 */
140 public String getIdAsString(Connection connection, Object keyInfo)
141 throws Exception
142 {
143 return getIdAsVillageValue(connection, keyInfo).asString();
144 }
145
146 /***
147 * A flag to determine the timing of the id generation
148 *
149 * @return a <code>boolean</code> value
150 */
151 public boolean isPriorToInsert()
152 {
153 return false;
154 }
155
156 /***
157 * A flag to determine the timing of the id generation
158 *
159 * @return a <code>boolean</code> value
160 */
161 public boolean isPostInsert()
162 {
163 return true;
164 }
165
166 /***
167 * A flag to determine whether a Connection is required to
168 * generate an id.
169 *
170 * @return a <code>boolean</code> value
171 */
172 public final boolean isConnectionRequired()
173 {
174 return true;
175 }
176
177 /***
178 * Returns the last ID used by this connection.
179 *
180 * @param connection A Connection.
181 * @param keyInfo an Object that contains additional info.
182 * @return A Village Value with the last value auto-incremented as a
183 * result of an insert.
184 * @exception Exception Database error.
185 */
186 private Value getIdAsVillageValue(Connection connection, Object keyInfo)
187 throws Exception
188 {
189 String idSQL = dbAdapter.getIDMethodSQL(keyInfo);
190 Value id = null;
191 QueryDataSet qds = null;
192 try
193 {
194 qds = new QueryDataSet(connection, idSQL);
195 qds.fetchRecords(1);
196 Record rec = qds.getRecord(0);
197 id = rec.getValue(1);
198 }
199 finally
200 {
201 if (qds != null)
202 {
203 qds.close();
204 }
205 }
206 return id;
207 }
208 }
This page was automatically generated by Maven