Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PlatformDb2Impl |
|
| 3.0;3 |
1 | package org.apache.ojb.broker.platforms; | |
2 | ||
3 | /* Copyright 2002-2005 The Apache Software Foundation | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | import java.sql.PreparedStatement; | |
19 | import java.sql.SQLException; | |
20 | import java.sql.Types; | |
21 | import java.util.Properties; | |
22 | ||
23 | import org.apache.ojb.broker.util.sequence.SequenceManagerHelper; | |
24 | ||
25 | /** | |
26 | * This class extends <code>PlatformDefaultImpl</code> and defines specific | |
27 | * behavior for the DB2 platform. | |
28 | * | |
29 | * <p/> | |
30 | * Many of the database sequence specific properties can be specified using | |
31 | * <em>custom attributes</em> within the <em>sequence-manager</em> element. | |
32 | * <br/> | |
33 | * The database sequence specific properties are generally speaking, see database user guide | |
34 | * for detailed description. | |
35 | * | |
36 | * <p> | |
37 | * Supported properties on sequence creation: | |
38 | * </p> | |
39 | * | |
40 | * <table cellspacing="2" cellpadding="2" border="3" frame="box"> | |
41 | * <tr> | |
42 | * <td><strong>Property Key</strong></td> | |
43 | * <td><strong>Property Values</strong></td> | |
44 | * </tr> | |
45 | * <tr> | |
46 | * <td>seq.as</td> | |
47 | * <td> | |
48 | * Database sequence specific property.<br/> | |
49 | * Specifies the datatype used for the sequence. | |
50 | * Allowed: all numeric datatypes? e.g. <em>INTEGER</em> | |
51 | * </td> | |
52 | * </tr> | |
53 | * <tr> | |
54 | * <td>sequenceStart</td> | |
55 | * <td> | |
56 | * DEPRECATED. Database sequence specific property.<br/> | |
57 | * Specifies the first sequence number to be | |
58 | * generated. Allowed: <em>1</em> or greater. | |
59 | * </td> | |
60 | * </tr> | |
61 | * <tr> | |
62 | * <td>seq.start</td> | |
63 | * <td> | |
64 | * Database sequence specific property.<br/> | |
65 | * Specifies the first sequence number to be | |
66 | * generated. Allowed: <em>1</em> or greater. | |
67 | * </td> | |
68 | * </tr> | |
69 | * <tr> | |
70 | * <td>seq.incrementBy</td> | |
71 | * <td> | |
72 | * Database sequence specific property.<br/> | |
73 | * Specifies the interval between sequence numbers. | |
74 | * This value can be any positive or negative | |
75 | * integer, but it cannot be 0. | |
76 | * </td> | |
77 | * </tr> | |
78 | * <tr> | |
79 | * <td>seq.maxValue</td> | |
80 | * <td> | |
81 | * Database sequence specific property.<br/> | |
82 | * Set max value for sequence numbers. | |
83 | * </td> | |
84 | * </tr> | |
85 | * <tr> | |
86 | * <td>seq.minValue</td> | |
87 | * <td> | |
88 | * Database sequence specific property.<br/> | |
89 | * Set min value for sequence numbers. | |
90 | * </td> | |
91 | * </tr> | |
92 | * <tr> | |
93 | * <td>seq.cycle</td> | |
94 | * <td> | |
95 | * Database sequence specific property.<br/> | |
96 | * If <em>true</em>, specifies that the sequence continues to generate | |
97 | * values after reaching either its maximum or minimum value. | |
98 | * <br/> | |
99 | * If <em>false</em>, specifies that the sequence cannot generate more values after | |
100 | * reaching its maximum or minimum value. | |
101 | * </td> | |
102 | * </tr> | |
103 | * <tr> | |
104 | * <td>seq.cache</td> | |
105 | * <td> | |
106 | * Database sequence specific property.<br/> | |
107 | * Specifies how many values of the sequence Oracle | |
108 | * preallocates and keeps in memory for faster access. | |
109 | * Allowed values: <em>2</em> or greater. If set <em>0</em>, | |
110 | * an explicite <em>nocache</em> expression will be set. | |
111 | * </td> | |
112 | * </tr> | |
113 | * <tr> | |
114 | * <td>seq.order</td> | |
115 | * <td> | |
116 | * Database sequence specific property.<br/> | |
117 | * If set <em>true</em>, guarantees that sequence numbers | |
118 | * are generated in order of request. | |
119 | * <br/> | |
120 | * If <em>false</em>, a <em>no order</em> expression will be set. | |
121 | * </td> | |
122 | * </tr> | |
123 | * </table> | |
124 | * | |
125 | * @author <a href="mailto:thma@apache.org">Thomas Mahler<a> | |
126 | * @version $Id: PlatformDb2Impl.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $ | |
127 | */ | |
128 | public class PlatformDb2Impl extends PlatformDefaultImpl | |
129 | { | |
130 | /** | |
131 | * Patch provided by Avril Kotzen (hi001@webmail.co.za) | |
132 | * DB2 handles TINYINT (for mapping a byte). | |
133 | */ | |
134 | public void setObjectForStatement(PreparedStatement ps, int index, | |
135 | Object value, int sqlType) throws SQLException | |
136 | { | |
137 | if (sqlType == Types.TINYINT) | |
138 | { | |
139 | ps.setByte(index, ((Byte) value).byteValue()); | |
140 | } | |
141 | else | |
142 | { | |
143 | super.setObjectForStatement(ps, index, value, sqlType); | |
144 | } | |
145 | } | |
146 | ||
147 | public String createSequenceQuery(String sequenceName) | |
148 | { | |
149 | return "create sequence " + sequenceName; | |
150 | } | |
151 | ||
152 | public String createSequenceQuery(String sequenceName, Properties prop) | |
153 | { | |
154 | /* | |
155 | Read syntax diagramSkip visual syntax diagram | |
156 | .-AS INTEGER----. | |
157 | >>-CREATE SEQUENCE--sequence-name--*--+---------------+--*------> | |
158 | '-AS--data-type-' | |
159 | ||
160 | >--+------------------------------+--*--------------------------> | |
161 | '-START WITH--numeric-constant-' | |
162 | ||
163 | .-INCREMENT BY 1-----------------. | |
164 | >--+--------------------------------+--*------------------------> | |
165 | '-INCREMENT BY--numeric-constant-' | |
166 | ||
167 | .-NO MINVALUE----------------. | |
168 | >--+----------------------------+--*----------------------------> | |
169 | '-MINVALUE--numeric-constant-' | |
170 | ||
171 | .-NO MAXVALUE----------------. .-NO CYCLE-. | |
172 | >--+----------------------------+--*--+----------+--*-----------> | |
173 | '-MAXVALUE--numeric-constant-' '-CYCLE----' | |
174 | ||
175 | .-CACHE 20----------------. .-NO ORDER-. | |
176 | >--+-------------------------+--*--+----------+--*------------->< | |
177 | +-CACHE--integer-constant-+ '-ORDER----' | |
178 | '-NO CACHE----------------' | |
179 | */ | |
180 | StringBuffer query = new StringBuffer(createSequenceQuery(sequenceName)); | |
181 | if(prop != null) | |
182 | { | |
183 | Boolean b; | |
184 | Long value; | |
185 | String str; | |
186 | ||
187 | str = SequenceManagerHelper.getSeqAsValue(prop); | |
188 | if(str != null) | |
189 | { | |
190 | query.append(" AS ").append(str); | |
191 | } | |
192 | ||
193 | value = SequenceManagerHelper.getSeqStart(prop); | |
194 | if(value != null) | |
195 | { | |
196 | query.append(" START WITH ").append(value.longValue()); | |
197 | } | |
198 | ||
199 | value = SequenceManagerHelper.getSeqIncrementBy(prop); | |
200 | if(value != null) | |
201 | { | |
202 | query.append(" INCREMENT BY ").append(value.longValue()); | |
203 | } | |
204 | ||
205 | value = SequenceManagerHelper.getSeqMinValue(prop); | |
206 | if(value != null) | |
207 | { | |
208 | query.append(" MINVALUE ").append(value.longValue()); | |
209 | } | |
210 | ||
211 | value = SequenceManagerHelper.getSeqMaxValue(prop); | |
212 | if(value != null) | |
213 | { | |
214 | query.append(" MAXVALUE ").append(value.longValue()); | |
215 | } | |
216 | ||
217 | b = SequenceManagerHelper.getSeqCycleValue(prop); | |
218 | if(b != null) | |
219 | { | |
220 | if(b.booleanValue()) query.append(" CYCLE"); | |
221 | else query.append(" NO CYCLE"); | |
222 | } | |
223 | ||
224 | value = SequenceManagerHelper.getSeqCacheValue(prop); | |
225 | if(value != null) | |
226 | { | |
227 | query.append(" CACHE ").append(value.longValue()); | |
228 | } | |
229 | ||
230 | b = SequenceManagerHelper.getSeqOrderValue(prop); | |
231 | if(b != null) | |
232 | { | |
233 | if(b.booleanValue()) query.append(" ORDER"); | |
234 | else query.append(" NO ORDER"); | |
235 | } | |
236 | } | |
237 | return query.toString(); | |
238 | } | |
239 | ||
240 | public String nextSequenceQuery(String sequenceName) | |
241 | { | |
242 | return "values nextval for "+ sequenceName; | |
243 | } | |
244 | ||
245 | public String dropSequenceQuery(String sequenceName) | |
246 | { | |
247 | return "drop sequence " + sequenceName; | |
248 | } | |
249 | ||
250 | public String getLastInsertIdentityQuery(String tableName) | |
251 | { | |
252 | // matthias.roth@impart.ch | |
253 | // the function is used by the org.apache.ojb.broker.util.sequence.SequenceManagerNativeImpl | |
254 | // this call must be made before commit the insert command, so you | |
255 | // must turn off autocommit by seting the useAutoCommit="2" | |
256 | // or use useAutoCommit="1" or use a connection with autoCommit set false | |
257 | // by default (e.g. in managed environments) | |
258 | // transaction demarcation is mandatory | |
259 | return "values IDENTITY_VAL_LOCAL()"; | |
260 | } | |
261 | } |