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