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 import java.math.BigDecimal;
23 import java.sql.Connection;
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 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 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 connection, Object keyInfo)
67 throws Exception
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 connection, Object keyInfo)
81 throws Exception
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 getIdAsBigDecimal(Connection connection, Object keyInfo)
96 throws Exception
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 getIdAsString(Connection connection, Object keyInfo)
112 throws Exception
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 connection, Object keyInfo)
158 throws Exception
159 {
160 String tableName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name);
161 String 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 }