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