1 package org.apache.torque.oid;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 import java.math.BigDecimal;
58 import java.sql.Connection;
59
60 import org.apache.commons.logging.Log;
61 import org.apache.commons.logging.LogFactory;
62
63 import org.apache.torque.adapter.DB;
64
65 import com.workingdogs.village.QueryDataSet;
66 import com.workingdogs.village.Record;
67 import com.workingdogs.village.Value;
68
69 /***
70 * This generator works with databases that have an sql syntax for
71 * getting an id prior to inserting a row into the database.
72 *
73 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
74 * @version $Id: SequenceIdGenerator.java,v 1.10 2003/08/25 16:33:22 henning Exp $
75 */
76 public class SequenceIdGenerator implements IdGenerator
77 {
78 /*** The log. */
79 private static Log log = LogFactory.getLog(SequenceIdGenerator.class);
80
81 /*** the adapter that knows the correct sql syntax */
82 private DB dbAdapter;
83
84 /***
85 * Creates an IdGenerator which will work with the specified database.
86 *
87 * @param adapter the adapter that knows the correct sql syntax.
88 */
89 public SequenceIdGenerator(DB adapter)
90 {
91 dbAdapter = adapter;
92 }
93
94 /***
95 * Retrieves an id as an int.
96 *
97 * @param connection A Connection.
98 * @param keyInfo an Object that contains additional info.
99 * @return An int with the value for the id.
100 * @exception Exception Database error.
101 */
102 public int getIdAsInt(Connection connection, Object keyInfo)
103 throws Exception
104 {
105 return getIdAsVillageValue(connection, keyInfo).asInt();
106 }
107
108 /***
109 * Retrieves an id as an long.
110 *
111 * @param connection A Connection.
112 * @param keyInfo an Object that contains additional info.
113 * @return A long with the value for the id.
114 * @exception Exception Database error.
115 */
116 public long getIdAsLong(Connection connection, Object keyInfo)
117 throws Exception
118 {
119 return getIdAsVillageValue(connection, keyInfo).asLong();
120 }
121
122 /***
123 * Retrieves an id as a BigDecimal.
124 *
125 * @param connection A Connection.
126 * @param keyInfo an Object that contains additional info.
127 * @return A BigDecimal id
128 * @exception Exception Database error.
129 */
130 public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo)
131 throws Exception
132 {
133 return getIdAsVillageValue(connection, keyInfo).asBigDecimal();
134 }
135
136 /***
137 * Retrieves an id as an String.
138 *
139 * @param connection A Connection.
140 * @param keyInfo an Object that contains additional info.
141 * @return A String id
142 * @exception Exception Database error.
143 */
144 public String getIdAsString(Connection connection, Object keyInfo)
145 throws Exception
146 {
147 return getIdAsVillageValue(connection, keyInfo).asString();
148 }
149
150 /***
151 * A flag to determine the timing of the id generation
152 *
153 * @return a <code>boolean</code> value
154 */
155 public boolean isPriorToInsert()
156 {
157 return true;
158 }
159
160 /***
161 * A flag to determine the timing of the id generation
162 *
163 * @return a <code>boolean</code> value
164 */
165 public boolean isPostInsert()
166 {
167 return false;
168 }
169
170 /***
171 * A flag to determine whether a Connection is required to
172 * generate an id.
173 *
174 * @return a <code>boolean</code> value
175 */
176 public boolean isConnectionRequired()
177 {
178 return true;
179 }
180
181 /***
182 * Retrieves an id as a Village Value.
183 *
184 * @param connection A Connection.
185 * @param keyInfo an Object that contains additional info.
186 * @return A Village Value id.
187 * @exception Exception Database error.
188 */
189 private Value getIdAsVillageValue(Connection connection, Object keyInfo)
190 throws Exception
191 {
192 String idSql = dbAdapter.getIDMethodSQL(keyInfo);
193 if (log.isDebugEnabled())
194 {
195 log.debug(idSql);
196 }
197
198
199 QueryDataSet qds = new QueryDataSet(connection, idSql);
200 Record rec;
201 try
202 {
203 qds.fetchRecords(1);
204 rec = qds.getRecord(0);
205 }
206 finally
207 {
208 if (qds != null)
209 {
210 qds.close();
211 }
212 }
213 return rec.getValue(1);
214 }
215 }