Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PlatformPostgreSQLImpl |
|
| 2.3333333333333335;2.333 |
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.query.LikeCriteria; | |
24 | import org.apache.ojb.broker.util.sequence.SequenceManagerHelper; | |
25 | ||
26 | /** | |
27 | * This class extends <code>PlatformDefaultImpl</code> and defines specific | |
28 | * behavior for the PostgreSQL platform. | |
29 | * | |
30 | * <p/> | |
31 | * Many of the database sequence specific properties can be specified using | |
32 | * <em>custom attributes</em> within the <em>sequence-manager</em> element. | |
33 | * <br/> | |
34 | * The database sequence specific properties are generally speaking, see database user guide | |
35 | * for detailed description. | |
36 | * | |
37 | * <p> | |
38 | * Implementation configuration properties: | |
39 | * </p> | |
40 | * | |
41 | * <table cellspacing="2" cellpadding="2" border="3" frame="box"> | |
42 | * <tr> | |
43 | * <td><strong>Property Key</strong></td> | |
44 | * <td><strong>Property Values</strong></td> | |
45 | * </tr> | |
46 | * <tr> | |
47 | * <td>sequenceStart</td> | |
48 | * <td> | |
49 | * DEPRECATED. Database sequence specific property.<br/> | |
50 | * Specifies the first sequence number to be | |
51 | * generated. Allowed: <em>1</em> or greater. | |
52 | * </td> | |
53 | * </tr> | |
54 | * <tr> | |
55 | * <td>seq.start</td> | |
56 | * <td> | |
57 | * Database sequence specific property.<br/> | |
58 | * Specifies the first sequence number to be | |
59 | * generated. Allowed: <em>1</em> or greater. | |
60 | * </td> | |
61 | * </tr> | |
62 | * <tr> | |
63 | * <td>seq.incrementBy</td> | |
64 | * <td> | |
65 | * Database sequence specific property.<br/> | |
66 | * Specifies the interval between sequence numbers. | |
67 | * This value can be any positive or negative | |
68 | * integer, but it cannot be 0. | |
69 | * </td> | |
70 | * </tr> | |
71 | * <tr> | |
72 | * <td>seq.maxValue</td> | |
73 | * <td> | |
74 | * Database sequence specific property.<br/> | |
75 | * Set max value for sequence numbers. | |
76 | * </td> | |
77 | * </tr> | |
78 | * <tr> | |
79 | * <td>seq.minValue</td> | |
80 | * <td> | |
81 | * Database sequence specific property.<br/> | |
82 | * Set min value for sequence numbers. | |
83 | * </td> | |
84 | * </tr> | |
85 | * <tr> | |
86 | * <td>seq.cycle</td> | |
87 | * <td> | |
88 | * Database sequence specific property.<br/> | |
89 | * If <em>true</em>, specifies that the sequence continues to generate | |
90 | * values after reaching either its maximum or minimum value. | |
91 | * <br/> | |
92 | * If <em>false</em>, specifies that the sequence cannot generate more values after | |
93 | * reaching its maximum or minimum value. | |
94 | * </td> | |
95 | * </tr> | |
96 | * <tr> | |
97 | * <td>seq.cache</td> | |
98 | * <td> | |
99 | * Database sequence specific property.<br/> | |
100 | * Specifies how many values of the sequence Oracle | |
101 | * preallocates and keeps in memory for faster access. | |
102 | * Allowed values: <em>2</em> or greater. If set <em>0</em>, | |
103 | * an explicite <em>nocache</em> expression will be set. | |
104 | * </td> | |
105 | * </tr> | |
106 | * <tr> | |
107 | * <td>seq.order</td> | |
108 | * <td> | |
109 | * Database sequence specific property.<br/> | |
110 | * If set <em>true</em>, guarantees that sequence numbers | |
111 | * are generated in order of request. | |
112 | * <br/> | |
113 | * If <em>false</em>, a <em>no order</em> expression will be set. | |
114 | * </td> | |
115 | * </tr> | |
116 | * </table> | |
117 | * | |
118 | * @author <a href="mailto:thma@apache.org">Thomas Mahler<a> | |
119 | * @version $Id: PlatformPostgreSQLImpl.java,v 1.1 2007-08-24 22:17:35 ewestfal Exp $ | |
120 | */ | |
121 | ||
122 | public class PlatformPostgreSQLImpl extends PlatformDefaultImpl | |
123 | { | |
124 | public void setObjectForStatement(PreparedStatement ps, int index, Object value, int sqlType) throws SQLException | |
125 | { | |
126 | if ((value instanceof byte[]) && (sqlType == Types.LONGVARBINARY)) | |
127 | { | |
128 | ps.setBytes(index, (byte[]) value); | |
129 | } | |
130 | else | |
131 | { | |
132 | super.setObjectForStatement(ps, index, value, sqlType); | |
133 | } | |
134 | } | |
135 | ||
136 | public String createSequenceQuery(String sequenceName) | |
137 | { | |
138 | return "create sequence " + sequenceName; | |
139 | } | |
140 | ||
141 | public String createSequenceQuery(String sequenceName, Properties prop) | |
142 | { | |
143 | /* | |
144 | CREATE [ TEMPORARY | TEMP ] SEQUENCE seqname [ INCREMENT increment ] | |
145 | [ MINVALUE minvalue ] [ MAXVALUE maxvalue ] | |
146 | [ START start ] [ CACHE cache ] [ CYCLE ] | |
147 | */ | |
148 | StringBuffer query = new StringBuffer(createSequenceQuery(sequenceName)); | |
149 | if(prop != null) | |
150 | { | |
151 | Boolean b; | |
152 | Long value; | |
153 | ||
154 | value = SequenceManagerHelper.getSeqIncrementBy(prop); | |
155 | if(value != null) | |
156 | { | |
157 | query.append(" INCREMENT ").append(value.longValue()); | |
158 | } | |
159 | ||
160 | value = SequenceManagerHelper.getSeqMinValue(prop); | |
161 | if(value != null) | |
162 | { | |
163 | query.append(" MINVALUE ").append(value.longValue()); | |
164 | } | |
165 | ||
166 | value = SequenceManagerHelper.getSeqMaxValue(prop); | |
167 | if(value != null) | |
168 | { | |
169 | query.append(" MAXVALUE ").append(value.longValue()); | |
170 | } | |
171 | ||
172 | value = SequenceManagerHelper.getSeqStart(prop); | |
173 | if(value != null) | |
174 | { | |
175 | query.append(" START ").append(value.longValue()); | |
176 | } | |
177 | ||
178 | value = SequenceManagerHelper.getSeqCacheValue(prop); | |
179 | if(value != null) | |
180 | { | |
181 | query.append(" CACHE ").append(value.longValue()); | |
182 | } | |
183 | ||
184 | b = SequenceManagerHelper.getSeqCycleValue(prop); | |
185 | if(b != null) | |
186 | { | |
187 | if(b.booleanValue()) query.append(" CYCLE"); | |
188 | } | |
189 | } | |
190 | return query.toString(); | |
191 | } | |
192 | ||
193 | public String nextSequenceQuery(String sequenceName) | |
194 | { | |
195 | return "select nextval('" + sequenceName + "')"; | |
196 | } | |
197 | ||
198 | public String dropSequenceQuery(String sequenceName) | |
199 | { | |
200 | return "drop sequence " + sequenceName; | |
201 | } | |
202 | ||
203 | /* (non-Javadoc) | |
204 | * @see org.apache.ojb.broker.platforms.Platform#addPagingSql(java.lang.StringBuffer) | |
205 | */ | |
206 | public void addPagingSql(StringBuffer anSqlString) | |
207 | { | |
208 | anSqlString.append(" LIMIT ? OFFSET ?"); | |
209 | } | |
210 | ||
211 | /* (non-Javadoc) | |
212 | * @see org.apache.ojb.broker.platforms.Platform#supportsPaging() | |
213 | */ | |
214 | public boolean supportsPaging() | |
215 | { | |
216 | return true; | |
217 | } | |
218 | ||
219 | /* (non-Javadoc) | |
220 | * @see org.apache.ojb.broker.platforms.Platform#bindPagingParameters(java.sql.PreparedStatement, int, int, int) | |
221 | */ | |
222 | public int bindPagingParameters(PreparedStatement ps, int index, int startAt, int endAt) throws SQLException | |
223 | { | |
224 | ps.setInt(index, endAt - (startAt - 1)); // number of rows to fetch | |
225 | index++; | |
226 | ps.setInt(index, startAt - 1); // zero based start | |
227 | index++; | |
228 | return index; | |
229 | } | |
230 | ||
231 | /** | |
232 | * @see org.apache.ojb.broker.platforms.Platform#getEscapeClause(org.apache.ojb.broker.query.LikeCriteria) | |
233 | */ | |
234 | public String getEscapeClause(LikeCriteria aCriteria) | |
235 | { | |
236 | if (LikeCriteria.getEscapeCharacter() != LikeCriteria.DEFAULT_ESCPAPE_CHARACTER) | |
237 | { | |
238 | // the default escape character is \, so there's no need for an escape clause | |
239 | return super.getEscapeClause(aCriteria); | |
240 | } | |
241 | else | |
242 | { | |
243 | return ""; | |
244 | } | |
245 | } | |
246 | } |