KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > platforms > PlatformSapdbImpl


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 JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.sql.Types JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 import org.apache.ojb.broker.util.sequence.SequenceManagerHelper;
24
25 /**
26  * SapDB specific Platform implementation.
27  *
28  * <p/>
29  * Many of the database sequence specific properties can be specified using
30  * <em>custom attributes</em> within the <em>sequence-manager</em> element.
31  * <br/>
32  * The database sequence specific properties are generally speaking, see database user guide
33  * for detailed description.
34  *
35  * <p>
36  * Implementation configuration properties:
37  * </p>
38  *
39  * <table cellspacing="2" cellpadding="2" border="3" frame="box">
40  * <tr>
41  * <td><strong>Property Key</strong></td>
42  * <td><strong>Property Values</strong></td>
43  * </tr>
44  * <tr>
45  * <td>sequenceStart</td>
46  * <td>
47  * DEPRECATED. Database sequence specific property.<br/>
48  * Specifies the first sequence number to be
49  * generated. Allowed: <em>1</em> or greater.
50  * </td>
51  * </tr>
52  * <tr>
53  * <td>seq.start</td>
54  * <td>
55  * Database sequence specific property.<br/>
56  * Specifies the first sequence number to be
57  * generated. Allowed: <em>1</em> or greater.
58  * </td>
59  * </tr>
60  * <tr>
61  * <td>seq.incrementBy</td>
62  * <td>
63  * Database sequence specific property.<br/>
64  * Specifies the interval between sequence numbers.
65  * This value can be any positive or negative
66  * integer, but it cannot be 0.
67  * </td>
68  * </tr>
69  * <tr>
70  * <td>seq.maxValue</td>
71  * <td>
72  * Database sequence specific property.<br/>
73  * Set max value for sequence numbers.
74  * </td>
75  * </tr>
76  * <tr>
77  * <td>seq.minValue</td>
78  * <td>
79  * Database sequence specific property.<br/>
80  * Set min value for sequence numbers.
81  * </td>
82  * </tr>
83  * <tr>
84  * <td>seq.cycle</td>
85  * <td>
86  * Database sequence specific property.<br/>
87  * If <em>true</em>, specifies that the sequence continues to generate
88  * values after reaching either its maximum or minimum value.
89  * <br/>
90  * If <em>false</em>, specifies that the sequence cannot generate more values after
91  * reaching its maximum or minimum value.
92  * </td>
93  * </tr>
94  * <tr>
95  * <td>seq.cache</td>
96  * <td>
97  * Database sequence specific property.<br/>
98  * Specifies how many values of the sequence Oracle
99  * preallocates and keeps in memory for faster access.
100  * Allowed values: <em>2</em> or greater. If set <em>0</em>,
101  * an explicite <em>nocache</em> expression will be set.
102  * </td>
103  * </tr>
104  * <tr>
105  * <td>seq.order</td>
106  * <td>
107  * Database sequence specific property.<br/>
108  * If set <em>true</em>, guarantees that sequence numbers
109  * are generated in order of request.
110  * <br/>
111  * If <em>false</em>, a <em>no order</em> expression will be set.
112  * </td>
113  * </tr>
114  * </table>
115  *
116  * @author Justin A. Stanczak
117  * @author Matthew Baird (mattb
118  * @version $Id: PlatformSapdbImpl.java,v 1.10.2.3 2005/12/21 22:26:39 tomdz Exp $
119  */

120 public class PlatformSapdbImpl extends PlatformDefaultImpl
121 {
122     public void setObjectForStatement(
123             PreparedStatement JavaDoc ps,
124             int index,
125             Object JavaDoc value,
126             int sqlType)
127             throws SQLException JavaDoc
128     {
129         if(((sqlType == Types.VARBINARY) || (sqlType == Types.LONGVARBINARY))
130                 && (value instanceof byte[]))
131         {
132             byte buf[] = (byte[]) value;
133             ps.setBytes(index, buf);
134         }
135         else
136         {
137             super.setObjectForStatement(ps, index, value, sqlType);
138         }
139     }
140
141     /**
142      * Get join syntax type for this RDBMS - one on of the constants from JoinSyntaxType interface
143      */

144     public byte getJoinSyntaxType()
145     {
146         return ORACLE_JOIN_SYNTAX;
147     }
148
149     /**
150      * Override default ResultSet size determination (rs.last();rs.getRow())
151      * with select count(*) operation
152      * SAP db doesn't let you use the .last, .getRow() mechanism (.getRow() will return -1)
153      */

154     public boolean useCountForResultsetSize()
155     {
156         return true;
157     }
158
159     public String JavaDoc createSequenceQuery(String JavaDoc sequenceName)
160     {
161         return "CREATE SEQUENCE " + sequenceName;
162     }
163
164     public String JavaDoc createSequenceQuery(String JavaDoc sequenceName, Properties JavaDoc prop)
165     {
166         /*
167         CREATE SEQUENCE [<schema_name>.]<sequence_name>
168             [INCREMENT BY <integer>]
169             [START WITH <integer>]
170             [MAXVALUE <integer> | NOMAXVALUE]
171             [MINVALUE <integer> | NOMINVALUE]
172             [CYCLE | NOCYCLE]
173             [CACHE <unsigned_integer> | NOCACHE]
174             [ORDER|NOORDER]
175         */

176         StringBuffer JavaDoc query = new StringBuffer JavaDoc(createSequenceQuery(sequenceName));
177         if(prop != null)
178         {
179             Boolean JavaDoc b;
180             Long JavaDoc value;
181
182             value = SequenceManagerHelper.getSeqIncrementBy(prop);
183             if(value != null)
184             {
185                 query.append(" INCREMENT BY ").append(value.longValue());
186             }
187
188             value = SequenceManagerHelper.getSeqStart(prop);
189             if(value != null)
190             {
191                 query.append(" START WITH ").append(value.longValue());
192             }
193
194             value = SequenceManagerHelper.getSeqMaxValue(prop);
195             if(value != null)
196             {
197                 query.append(" MAXVALUE ").append(value.longValue());
198             }
199
200             value = SequenceManagerHelper.getSeqMinValue(prop);
201             if(value != null)
202             {
203                 query.append(" MINVALUE ").append(value.longValue());
204             }
205
206             b = SequenceManagerHelper.getSeqCycleValue(prop);
207             if(b != null)
208             {
209                 if(b.booleanValue()) query.append(" CYCLE");
210                 else query.append(" NOCYCLE");
211             }
212
213             value = SequenceManagerHelper.getSeqCacheValue(prop);
214             if(value != null)
215             {
216                 query.append(" CACHE ").append(value.longValue());
217             }
218
219             b = SequenceManagerHelper.getSeqOrderValue(prop);
220             if(b != null)
221             {
222                 if(b.booleanValue()) query.append(" ORDER");
223                 else query.append(" NOORDER");
224             }
225         }
226         return query.toString();
227     }
228
229     public String JavaDoc nextSequenceQuery(String JavaDoc sequenceName)
230     {
231         return "select " + sequenceName + ".nextval from dual";
232     }
233
234     public String JavaDoc dropSequenceQuery(String JavaDoc sequenceName)
235     {
236         return "drop sequence " + sequenceName;
237     }
238
239     /* (non-Javadoc)
240     * @see org.apache.ojb.broker.platforms.Platform#addPagingSql(java.lang.StringBuffer)
241     */

242     public void addPagingSql(StringBuffer JavaDoc anSqlString)
243     {
244         anSqlString.append(" ROWNO <= ? ");
245     }
246
247     /* (non-Javadoc)
248      * @see org.apache.ojb.broker.platforms.Platform#bindPagingParameters(java.sql.PreparedStatement, int, int, int)
249      */

250     public int bindPagingParameters(PreparedStatement JavaDoc ps, int index, int startAt, int endAt) throws SQLException JavaDoc
251     {
252
253         ps.setInt(index, endAt - 1); // IGNORE startAt !!
254
index++;
255         return index;
256     }
257
258     /* (non-Javadoc)
259     * @see org.apache.ojb.broker.platforms.Platform#supportsPaging()
260     */

261     public boolean supportsPaging()
262     {
263         return true;
264     }
265 }
266
Popular Tags