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 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 }