KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dba > postgres > PostgresAdapter


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.dba.postgres;
57
58 import java.sql.PreparedStatement JavaDoc;
59 import java.sql.SQLException JavaDoc;
60 import java.sql.Types JavaDoc;
61 import java.util.Iterator JavaDoc;
62
63 import org.objectstyle.cayenne.CayenneRuntimeException;
64 import org.objectstyle.cayenne.access.DataNode;
65 import org.objectstyle.cayenne.access.trans.QualifierTranslator;
66 import org.objectstyle.cayenne.access.trans.QueryAssembler;
67 import org.objectstyle.cayenne.access.types.ByteArrayType;
68 import org.objectstyle.cayenne.access.types.CharType;
69 import org.objectstyle.cayenne.access.types.ExtendedTypeMap;
70 import org.objectstyle.cayenne.dba.JdbcAdapter;
71 import org.objectstyle.cayenne.dba.PkGenerator;
72 import org.objectstyle.cayenne.dba.TypesMapping;
73 import org.objectstyle.cayenne.map.DbAttribute;
74 import org.objectstyle.cayenne.map.DbEntity;
75 import org.objectstyle.cayenne.map.DerivedDbEntity;
76 import org.objectstyle.cayenne.query.Query;
77 import org.objectstyle.cayenne.query.SQLAction;
78
79 /**
80  * DbAdapter implementation for <a HREF="http://www.postgresql.org">PostgreSQL RDBMS </a>.
81  * Sample <a target="_top"
82  * HREF="../../../../../../../developerguide/unit-tests.html">connection settings </a> to
83  * use with PostgreSQL are shown below:
84  *
85  * <pre>
86  *
87  * test-postgresql.cayenne.adapter = org.objectstyle.cayenne.dba.postgres.PostgresAdapter
88  * test-postgresql.jdbc.username = test
89  * test-postgresql.jdbc.password = secret
90  * test-postgresql.jdbc.url = jdbc:postgresql://serverhostname/cayenne
91  * test-postgresql.jdbc.driver = org.postgresql.Driver
92  *
93  * </pre>
94  *
95  * @author Dirk Olmes
96  * @author Holger Hoffstaette
97  * @author Andrus Adamchik
98  */

99 public class PostgresAdapter extends JdbcAdapter {
100
101     /**
102      * Uses PostgresActionBuilder to create the right action.
103      *
104      * @since 1.2
105      */

106     public SQLAction getAction(Query query, DataNode node) {
107         return query
108                 .createSQLAction(new PostgresActionBuilder(this, node.getEntityResolver()));
109     }
110
111     /**
112      * Installs appropriate ExtendedTypes as converters for passing values between JDBC
113      * and Java layers.
114      */

115     protected void configureExtendedTypes(ExtendedTypeMap map) {
116         super.configureExtendedTypes(map);
117
118         // create specially configured CharType handler
119
map.registerType(new CharType(true, false));
120
121         // create specially configured ByteArrayType handler
122
map.registerType(new PostgresByteArrayType(true, false));
123     }
124
125     public DbAttribute buildAttribute(
126             String JavaDoc name,
127             String JavaDoc typeName,
128             int type,
129             int size,
130             int precision,
131             boolean allowNulls) {
132
133         // "bytea" maps to pretty much any binary type, so
134
// it is up to us to select the most sensible default.
135
// And the winner is LONGVARBINARY
136
if ("bytea".equalsIgnoreCase(typeName)) {
137             type = Types.LONGVARBINARY;
138         }
139         // somehow the driver reverse-engineers "text" as VARCHAR, must be CLOB
140
else if ("text".equalsIgnoreCase(typeName)) {
141             type = Types.CLOB;
142         }
143
144         return super.buildAttribute(name, typeName, type, size, precision, allowNulls);
145     }
146
147     /**
148      * Customizes table creating procedure for PostgreSQL. One difference with generic
149      * implementation is that "bytea" type has no explicit length unlike similar binary
150      * types in other databases.
151      *
152      * @since 1.0.2
153      */

154     public String JavaDoc createTable(DbEntity ent) {
155
156         // later we may support view creation
157
// for derived DbEntities
158
if (ent instanceof DerivedDbEntity) {
159             throw new CayenneRuntimeException("Can't create table for derived DbEntity '"
160                     + ent.getName()
161                     + "'.");
162         }
163
164         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
165         buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" (");
166
167         // columns
168
Iterator JavaDoc it = ent.getAttributes().iterator();
169         boolean first = true;
170         while (it.hasNext()) {
171             if (first) {
172                 first = false;
173             }
174             else {
175                 buf.append(", ");
176             }
177
178             DbAttribute at = (DbAttribute) it.next();
179
180             // attribute may not be fully valid, do a simple check
181
if (at.getType() == TypesMapping.NOT_DEFINED) {
182                 throw new CayenneRuntimeException("Undefined type for attribute '"
183                         + ent.getFullyQualifiedName()
184                         + "."
185                         + at.getName()
186                         + "'.");
187             }
188
189             String JavaDoc[] types = externalTypesForJdbcType(at.getType());
190             if (types == null || types.length == 0) {
191                 throw new CayenneRuntimeException("Undefined type for attribute '"
192                         + ent.getFullyQualifiedName()
193                         + "."
194                         + at.getName()
195                         + "': "
196                         + at.getType());
197             }
198
199             String JavaDoc type = types[0];
200             buf.append(at.getName()).append(' ').append(type);
201
202             // append size and precision (if applicable)
203
if (typeSupportsLength(at.getType())) {
204                 int len = at.getMaxLength();
205                 int prec = TypesMapping.isDecimal(at.getType()) ? at.getPrecision() : -1;
206
207                 // sanity check
208
if (prec > len) {
209                     prec = -1;
210                 }
211
212                 if (len > 0) {
213                     buf.append('(').append(len);
214
215                     if (prec >= 0) {
216                         buf.append(", ").append(prec);
217                     }
218
219                     buf.append(')');
220                 }
221             }
222
223             if (at.isMandatory()) {
224                 buf.append(" NOT NULL");
225             }
226             else {
227                 buf.append(" NULL");
228             }
229         }
230
231         // primary key clause
232
Iterator JavaDoc pkit = ent.getPrimaryKey().iterator();
233         if (pkit.hasNext()) {
234             if (first)
235                 first = false;
236             else
237                 buf.append(", ");
238
239             buf.append("PRIMARY KEY (");
240             boolean firstPk = true;
241             while (pkit.hasNext()) {
242                 if (firstPk)
243                     firstPk = false;
244                 else
245                     buf.append(", ");
246
247                 DbAttribute at = (DbAttribute) pkit.next();
248                 buf.append(at.getName());
249             }
250             buf.append(')');
251         }
252         buf.append(')');
253         return buf.toString();
254     }
255
256     private boolean typeSupportsLength(int type) {
257         // "bytea" type does not support length
258
String JavaDoc[] externalTypes = externalTypesForJdbcType(type);
259         if (externalTypes != null && externalTypes.length > 0) {
260             for (int i = 0; i < externalTypes.length; i++) {
261                 if ("bytea".equalsIgnoreCase(externalTypes[i])) {
262                     return false;
263                 }
264             }
265         }
266
267         return TypesMapping.supportsLength(type);
268     }
269
270     /**
271      * Adds the CASCADE option to the DROP TABLE clause.
272      *
273      * @see JdbcAdapter#dropTable(DbEntity)
274      */

275     public String JavaDoc dropTable(DbEntity ent) {
276         return super.dropTable(ent) + " CASCADE";
277     }
278
279     /**
280      * Returns a trimming translator.
281      */

282     public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) {
283         return new PostgresQualifierTranslator(queryAssembler);
284     }
285
286     /**
287      * @see JdbcAdapter#createPkGenerator()
288      */

289     protected PkGenerator createPkGenerator() {
290         return new PostgresPkGenerator();
291     }
292
293     /**
294      * PostgresByteArrayType is a byte[] type handler that patches the problem with
295      * PostgreSQL JDBC driver. Namely the fact that for some misterious reason PostgreSQL
296      * JDBC driver (as of 7.3.5) completely ignores the existence of LONGVARCHAR type.
297      *
298      * @since 1.0.4
299      */

300     class PostgresByteArrayType extends ByteArrayType {
301
302         public PostgresByteArrayType(boolean trimmingBytes, boolean usingBlobs) {
303             super(trimmingBytes, usingBlobs);
304         }
305
306         public void setJdbcObject(
307                 PreparedStatement JavaDoc st,
308                 Object JavaDoc val,
309                 int pos,
310                 int type,
311                 int precision) throws Exception JavaDoc {
312
313             // patch PGSQL driver LONGVARBINARY ignorance
314
if (type == Types.LONGVARBINARY) {
315                 type = Types.VARBINARY;
316             }
317
318             super.setJdbcObject(st, val, pos, type, precision);
319         }
320     }
321
322     public void bindParameter(
323             PreparedStatement JavaDoc statement,
324             Object JavaDoc object,
325             int pos,
326             int sqlType,
327             int precision) throws SQLException JavaDoc, Exception JavaDoc {
328
329         // Andrus: this addresses a bug with 8.x driver
330
// (I submitted a bug report #1780 to postgres, hopefully they'll resolve it.)
331
if (object == null) {
332             if (sqlType == Types.BLOB) {
333                 sqlType = Types.VARBINARY;
334             }
335             else if (sqlType == Types.CLOB) {
336                 sqlType = Types.VARCHAR;
337             }
338         }
339
340         super.bindParameter(statement, object, pos, sqlType, precision);
341     }
342 }
343
Popular Tags