%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.oid.SequenceIdGenerator |
|
|
1 | package org.apache.torque.oid; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation. |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
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 | 0 | 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 | 0 | 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, class="keyword">final String name) |
|
57 | 0 | { |
58 | 0 | this.dbAdapter = dbAdapter; |
59 | 0 | this.name = name; |
60 | 0 | } |
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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | String sequenceName = SQLBuilder.getFullTableName(String.valueOf(keyInfo), name); |
161 | 0 | String idSql = dbAdapter.getIDMethodSQL(sequenceName); |
162 | 0 | if (log.isDebugEnabled()) |
163 | { |
|
164 | 0 | log.debug(idSql); |
165 | } |
|
166 | ||
167 | // Execute the query. |
|
168 | 0 | QueryDataSet qds = new QueryDataSet(connection, idSql); |
169 | Record rec; |
|
170 | try |
|
171 | { |
|
172 | 0 | qds.fetchRecords(1); |
173 | 0 | rec = qds.getRecord(0); // Records are 0 based. |
174 | } |
|
175 | finally |
|
176 | { |
|
177 | 0 | if (qds != null) |
178 | { |
|
179 | 0 | qds.close(); |
180 | } |
|
181 | } |
|
182 | 0 | return rec.getValue(1); // Values are 1 based. |
183 | } |
|
184 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |