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
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.apache.torque.adapter.DB;
26 import org.apache.torque.util.SQLBuilder;
27
28 import com.workingdogs.village.QueryDataSet;
29 import com.workingdogs.village.Record;
30 import com.workingdogs.village.Value;
31
32 /***
33 * This generator works with databases that have an sql syntax for
34 * getting an id prior to inserting a row into the database.
35 *
36 * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a>
37 * @version $Id: SequenceIdGenerator.java 239630 2005-08-24 12:25:32Z henning $
38 */
39 public class SequenceIdGenerator implements IdGenerator
40 {
41 /*** The log. */
42 private static Log log = LogFactory.getLog(SequenceIdGenerator.class);
43
44 /*** the adapter that knows the correct sql syntax */
45 private DB dbAdapter;
46
47 /*** The internal name of the Database that this Generator is connected to */
48 private String name = null;
49
50 /***
51 * Creates an IdGenerator which will work with the specified database.
52 *
53 * @param adapter the adapter that knows the correct sql syntax.
54 * @param name The name of the datasource to find the correct schema
55 */
56 public SequenceIdGenerator(final DB dbAdapter, final String name)
57 {
58 this.dbAdapter = dbAdapter;
59 this.name = name;
60 }
61
62 /***
63 * Retrieves an id as an int.
64 *
65 * @param connection A Connection.
66 * @param keyInfo an Object that contains additional info.
67 * @return An int with the value for the id.
68 * @exception Exception Database error.
69 */
70 public int getIdAsInt(Connection connection, Object keyInfo)
71 throws Exception
72 {
73 return getIdAsVillageValue(connection, keyInfo).asInt();
74 }
75
76 /***
77 * Retrieves an id as an long.
78 *
79 * @param connection A Connection.
80 * @param keyInfo an Object that contains additional info.
81 * @return A long with the value for the id.
82 * @exception Exception Database error.
83 */
84 public long getIdAsLong(Connection connection, Object keyInfo)
85 throws Exception
86 {
87 return getIdAsVillageValue(connection, keyInfo).asLong();
88 }
89
90 /***
91 * Retrieves an id as a BigDecimal.
92 *
93 * @param connection A Connection.
94 * @param keyInfo an Object that contains additional info.
95 * @return A BigDecimal id
96 * @exception Exception Database error.
97 */
98 public BigDecimal getIdAsBigDecimal(Connection connection, Object keyInfo)
99 throws Exception
100 {
101 return getIdAsVillageValue(connection, keyInfo).asBigDecimal();
102 }
103
104 /***
105 * Retrieves an id as an String.
106 *
107 * @param connection A Connection.
108 * @param keyInfo an Object that contains additional info.
109 * @return A String id
110 * @exception Exception Database error.
111 */
112 public String getIdAsString(Connection connection, Object keyInfo)
113 throws Exception
114 {
115 return getIdAsVillageValue(connection, keyInfo).asString();
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 isPriorToInsert()
124 {
125 return true;
126 }
127
128 /***
129 * A flag to determine the timing of the id generation
130 *
131 * @return a <code>boolean</code> value
132 */
133 public boolean isPostInsert()
134 {
135 return false;
136 }
137
138 /***
139 * A flag to determine whether a Connection is required to
140 * generate an id.
141 *
142 * @return a <code>boolean</code> value
143 */
144 public boolean isConnectionRequired()
145 {
146 return true;
147 }
148
149 /***
150 * Retrieves an id as a Village Value.
151 *
152 * @param connection A Connection.
153 * @param keyInfo an Object that contains additional info.
154 * @return A Village Value id.
155 * @exception Exception Database error.
156 */
157 private Value getIdAsVillageValue(Connection connection, Object keyInfo)
158 throws Exception
159 {
160 String sequenceName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name);
161 String idSql = dbAdapter.getIDMethodSQL(sequenceName);
162 if (log.isDebugEnabled())
163 {
164 log.debug(idSql);
165 }
166
167
168 QueryDataSet qds = new QueryDataSet(connection, idSql);
169 Record rec;
170 try
171 {
172 qds.fetchRecords(1);
173 rec = qds.getRecord(0);
174 }
175 finally
176 {
177 if (qds != null)
178 {
179 qds.close();
180 }
181 }
182 return rec.getValue(1);
183 }
184 }