001: package org.apache.ojb.broker.platforms;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.sql.PreparedStatement;
019: import java.sql.SQLException;
020: import java.sql.Types;
021: import java.util.Properties;
022:
023: import org.apache.ojb.broker.util.sequence.SequenceManagerHelper;
024:
025: /**
026: * This class extends <code>PlatformDefaultImpl</code> and defines specific
027: * behavior for the DB2 platform.
028: *
029: * <p/>
030: * Many of the database sequence specific properties can be specified using
031: * <em>custom attributes</em> within the <em>sequence-manager</em> element.
032: * <br/>
033: * The database sequence specific properties are generally speaking, see database user guide
034: * for detailed description.
035: *
036: * <p>
037: * Supported properties on sequence creation:
038: * </p>
039: *
040: * <table cellspacing="2" cellpadding="2" border="3" frame="box">
041: * <tr>
042: * <td><strong>Property Key</strong></td>
043: * <td><strong>Property Values</strong></td>
044: * </tr>
045: * <tr>
046: * <td>seq.as</td>
047: * <td>
048: * Database sequence specific property.<br/>
049: * Specifies the datatype used for the sequence.
050: * Allowed: all numeric datatypes? e.g. <em>INTEGER</em>
051: * </td>
052: * </tr>
053: * <tr>
054: * <td>sequenceStart</td>
055: * <td>
056: * DEPRECATED. Database sequence specific property.<br/>
057: * Specifies the first sequence number to be
058: * generated. Allowed: <em>1</em> or greater.
059: * </td>
060: * </tr>
061: * <tr>
062: * <td>seq.start</td>
063: * <td>
064: * Database sequence specific property.<br/>
065: * Specifies the first sequence number to be
066: * generated. Allowed: <em>1</em> or greater.
067: * </td>
068: * </tr>
069: * <tr>
070: * <td>seq.incrementBy</td>
071: * <td>
072: * Database sequence specific property.<br/>
073: * Specifies the interval between sequence numbers.
074: * This value can be any positive or negative
075: * integer, but it cannot be 0.
076: * </td>
077: * </tr>
078: * <tr>
079: * <td>seq.maxValue</td>
080: * <td>
081: * Database sequence specific property.<br/>
082: * Set max value for sequence numbers.
083: * </td>
084: * </tr>
085: * <tr>
086: * <td>seq.minValue</td>
087: * <td>
088: * Database sequence specific property.<br/>
089: * Set min value for sequence numbers.
090: * </td>
091: * </tr>
092: * <tr>
093: * <td>seq.cycle</td>
094: * <td>
095: * Database sequence specific property.<br/>
096: * If <em>true</em>, specifies that the sequence continues to generate
097: * values after reaching either its maximum or minimum value.
098: * <br/>
099: * 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.10.2.4 2005/12/21 22:26:40 tomdz Exp $
127: */
128: public class PlatformDb2Impl extends PlatformDefaultImpl {
129: /**
130: * Patch provided by Avril Kotzen (hi001@webmail.co.za)
131: * DB2 handles TINYINT (for mapping a byte).
132: */
133: public void setObjectForStatement(PreparedStatement ps, int index,
134: Object value, int sqlType) throws SQLException {
135: if (sqlType == Types.TINYINT) {
136: ps.setByte(index, ((Byte) value).byteValue());
137: } else {
138: super .setObjectForStatement(ps, index, value, sqlType);
139: }
140: }
141:
142: public String createSequenceQuery(String sequenceName) {
143: return "create sequence " + sequenceName;
144: }
145:
146: public String createSequenceQuery(String sequenceName,
147: Properties prop) {
148: /*
149: Read syntax diagramSkip visual syntax diagram
150: .-AS INTEGER----.
151: >>-CREATE SEQUENCE--sequence-name--*--+---------------+--*------>
152: '-AS--data-type-'
153:
154: >--+------------------------------+--*-------------------------->
155: '-START WITH--numeric-constant-'
156:
157: .-INCREMENT BY 1-----------------.
158: >--+--------------------------------+--*------------------------>
159: '-INCREMENT BY--numeric-constant-'
160:
161: .-NO MINVALUE----------------.
162: >--+----------------------------+--*---------------------------->
163: '-MINVALUE--numeric-constant-'
164:
165: .-NO MAXVALUE----------------. .-NO CYCLE-.
166: >--+----------------------------+--*--+----------+--*----------->
167: '-MAXVALUE--numeric-constant-' '-CYCLE----'
168:
169: .-CACHE 20----------------. .-NO ORDER-.
170: >--+-------------------------+--*--+----------+--*-------------><
171: +-CACHE--integer-constant-+ '-ORDER----'
172: '-NO CACHE----------------'
173: */
174: StringBuffer query = new StringBuffer(
175: createSequenceQuery(sequenceName));
176: if (prop != null) {
177: Boolean b;
178: Long value;
179: String str;
180:
181: str = SequenceManagerHelper.getSeqAsValue(prop);
182: if (str != null) {
183: query.append(" AS ").append(str);
184: }
185:
186: value = SequenceManagerHelper.getSeqStart(prop);
187: if (value != null) {
188: query.append(" START WITH ").append(value.longValue());
189: }
190:
191: value = SequenceManagerHelper.getSeqIncrementBy(prop);
192: if (value != null) {
193: query.append(" INCREMENT BY ")
194: .append(value.longValue());
195: }
196:
197: value = SequenceManagerHelper.getSeqMinValue(prop);
198: if (value != null) {
199: query.append(" MINVALUE ").append(value.longValue());
200: }
201:
202: value = SequenceManagerHelper.getSeqMaxValue(prop);
203: if (value != null) {
204: query.append(" MAXVALUE ").append(value.longValue());
205: }
206:
207: b = SequenceManagerHelper.getSeqCycleValue(prop);
208: if (b != null) {
209: if (b.booleanValue())
210: query.append(" CYCLE");
211: else
212: query.append(" NO CYCLE");
213: }
214:
215: value = SequenceManagerHelper.getSeqCacheValue(prop);
216: if (value != null) {
217: query.append(" CACHE ").append(value.longValue());
218: }
219:
220: b = SequenceManagerHelper.getSeqOrderValue(prop);
221: if (b != null) {
222: if (b.booleanValue())
223: query.append(" ORDER");
224: else
225: query.append(" NO ORDER");
226: }
227: }
228: return query.toString();
229: }
230:
231: public String nextSequenceQuery(String sequenceName) {
232: return "values nextval for " + sequenceName;
233: }
234:
235: public String dropSequenceQuery(String sequenceName) {
236: return "drop sequence " + sequenceName;
237: }
238:
239: public String getLastInsertIdentityQuery(String tableName) {
240: // matthias.roth@impart.ch
241: // the function is used by the org.apache.ojb.broker.util.sequence.SequenceManagerNativeImpl
242: // this call must be made before commit the insert command, so you
243: // must turn off autocommit by seting the useAutoCommit="2"
244: // or use useAutoCommit="1" or use a connection with autoCommit set false
245: // by default (e.g. in managed environments)
246: // transaction demarcation is mandatory
247: return "values IDENTITY_VAL_LOCAL()";
248: }
249: }
|