1 package org.apache.torque.oid;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.math.BigDecimal;
20 import java.sql.Connection;
21 import org.apache.torque.adapter.DB;
22 import com.workingdogs.village.QueryDataSet;
23 import com.workingdogs.village.Record;
24 import com.workingdogs.village.Value;
25
26 /***
27 * This generator works with databases that have an sql syntax that
28 * allows the retrieval of the last id used to insert a row for a
29 * Connection.
30 *
31 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
32 * @version $Id: AutoIncrementIdGenerator.java,v 1.5.4.2 2004/05/20 04:36:07 seade Exp $
33 */
34 public class AutoIncrementIdGenerator implements IdGenerator
35 {
36 /*** the adapter that knows the correct sql syntax */
37 private DB dbAdapter;
38
39 /***
40 * Creates an IdGenerator which will work with the specified database.
41 *
42 * @param adapter the adapter that knows the correct sql syntax.
43 */
44 public AutoIncrementIdGenerator(DB adapter)
45 {
46 dbAdapter = adapter;
47 }
48
49 /***
50 * Returns the last ID used by this connection.
51 *
52 * @param connection A Connection.
53 * @param keyInfo an Object that contains additional info.
54 * @return An int with the value for the id.
55 * @exception Exception Database error.
56 */
57 public int getIdAsInt(Connection connection, Object keyInfo)
58 throws Exception
59 {
60 return getIdAsVillageValue(connection, keyInfo).asInt();
61 }
62
63 /***
64 * Returns the last ID used by this connection.
65 *
66 * @param connection A Connection.
67 * @param keyInfo an Object that contains additional info.
68 * @return A long with the value for the id.
69 * @exception Exception Database error.
70 */
71 public long getIdAsLong(Connection connection, Object keyInfo)
72 throws Exception
73 {
74 return getIdAsVillageValue(connection, keyInfo).asLong();
75 }
76
77 /***
78 * Returns the last ID used by this connection.
79 *
80 * @param connection A Connection.
81 * @param keyInfo an Object that contains additional info.
82 * @return A BigDecimal with the last value auto-incremented as a
83 * result of an insert.
84 * @exception Exception Database error.
85 */
86 public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo)
87 throws Exception
88 {
89 return getIdAsVillageValue(connection, keyInfo).asBigDecimal();
90 }
91
92
93 /***
94 * Returns the last ID used by this connection.
95 *
96 * @param connection A Connection.
97 * @param keyInfo an Object that contains additional info.
98 * @return A String with the last value auto-incremented as a
99 * result of an insert.
100 * @exception Exception Database error.
101 */
102 public String getIdAsString(Connection connection, Object keyInfo)
103 throws Exception
104 {
105 return getIdAsVillageValue(connection, keyInfo).asString();
106 }
107
108 /***
109 * A flag to determine the timing of the id generation
110 *
111 * @return a <code>boolean</code> value
112 */
113 public boolean isPriorToInsert()
114 {
115 return false;
116 }
117
118 /***
119 * A flag to determine the timing of the id generation
120 *
121 * @return a <code>boolean</code> value
122 */
123 public boolean isPostInsert()
124 {
125 return true;
126 }
127
128 /***
129 * A flag to determine whether a Connection is required to
130 * generate an id.
131 *
132 * @return a <code>boolean</code> value
133 */
134 public final boolean isConnectionRequired()
135 {
136 return true;
137 }
138
139 /***
140 * Returns the last ID used by this connection.
141 *
142 * @param connection A Connection.
143 * @param keyInfo an Object that contains additional info.
144 * @return A Village Value with the last value auto-incremented as a
145 * result of an insert.
146 * @exception Exception Database error.
147 */
148 private Value getIdAsVillageValue(Connection connection, Object keyInfo)
149 throws Exception
150 {
151 String idSQL = dbAdapter.getIDMethodSQL(keyInfo);
152 Value id = null;
153 QueryDataSet qds = null;
154 try
155 {
156 qds = new QueryDataSet(connection, idSQL);
157 qds.fetchRecords(1);
158 Record rec = qds.getRecord(0);
159 id = rec.getValue(1);
160 }
161 finally
162 {
163 if (qds != null)
164 {
165 qds.close();
166 }
167 }
168 return id;
169 }
170 }