KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dba > openbase > OpenBaseAdapter


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.openbase;
57
58 import java.sql.PreparedStatement JavaDoc;
59 import java.sql.ResultSet JavaDoc;
60 import java.sql.Types JavaDoc;
61 import java.util.Iterator JavaDoc;
62
63 import org.apache.log4j.Logger;
64 import org.objectstyle.cayenne.CayenneRuntimeException;
65 import org.objectstyle.cayenne.access.trans.QualifierTranslator;
66 import org.objectstyle.cayenne.access.trans.QueryAssembler;
67 import org.objectstyle.cayenne.access.types.CharType;
68 import org.objectstyle.cayenne.access.types.DefaultType;
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.DbJoin;
76 import org.objectstyle.cayenne.map.DbRelationship;
77 import org.objectstyle.cayenne.map.DerivedDbEntity;
78
79 /**
80  * DbAdapter implementation for <a HREF="http://www.openbase.com">OpenBase</a>.
81  * Sample <a target="_top" HREF="../../../../../../../developerguide/unit-tests.html">connection
82  * settings</a> to use with OpenBase are shown below:
83  *
84 <pre>
85 test-openbase.cayenne.adapter = org.objectstyle.cayenne.dba.openbase.OpenBaseAdapter
86 test-openbase.jdbc.username = test
87 test-openbase.jdbc.password = secret
88 test-openbase.jdbc.url = jdbc:openbase://serverhostname/cayenne
89 test-openbase.jdbc.driver = com.openbase.jdbc.ObDriver
90 </pre>
91  *
92  * @author <a HREF="mailto:mkienenb@alaska.net">Mike Kienenberger</a>
93  * @author Andrei Adamchik
94  *
95  * @since 1.1
96  */

97 public class OpenBaseAdapter extends JdbcAdapter {
98     private static Logger logObj = Logger.getLogger(OpenBaseAdapter.class);
99
100     public OpenBaseAdapter() {
101         // init defaults
102
this.setSupportsUniqueConstraints(false);
103     }
104
105     protected void configureExtendedTypes(ExtendedTypeMap map) {
106         super.configureExtendedTypes(map);
107
108         // Byte handling doesn't work on read...
109
// need special converter
110
map.registerType(new OpenBaseByteType());
111
112         map.registerType(new OpenBaseCharType());
113     }
114
115     public DbAttribute buildAttribute(
116         String JavaDoc name,
117         String JavaDoc typeName,
118         int type,
119         int size,
120         int precision,
121         boolean allowNulls) {
122
123         // OpenBase makes no distinction between CHAR and VARCHAR
124
// so lets use VARCHAR, since it seems more generic
125
if (type == Types.CHAR) {
126             type = Types.VARCHAR;
127         }
128
129         return super.buildAttribute(name, typeName, type, size, precision, allowNulls);
130     }
131
132     /**
133      * Returns word "go".
134      */

135     public String JavaDoc getBatchTerminator() {
136         return "go";
137     }
138
139     /**
140      * Returns null, since views are not yet supported in openbase.
141      */

142     public String JavaDoc tableTypeForView() {
143         // TODO: according to OpenBase docs views *ARE* supported.
144
return null;
145     }
146
147     /**
148      * Returns OpenBase-specific translator for queries.
149      */

150     public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) {
151         return new OpenBaseQualifierTranslator(queryAssembler);
152     }
153
154     /**
155       * Creates and returns a primary key generator. Overrides superclass
156       * implementation to return an
157       * instance of OpenBasePkGenerator that uses built-in multi-server primary key generation.
158       */

159     protected PkGenerator createPkGenerator() {
160         return new OpenBasePkGenerator();
161     }
162
163     /**
164       * Returns a SQL string that can be used to create database table
165       * corresponding to <code>ent</code> parameter.
166       */

167     public String JavaDoc createTable(DbEntity ent) {
168         // later we may support view creation
169
// for derived DbEntities
170
if (ent instanceof DerivedDbEntity) {
171             throw new CayenneRuntimeException(
172                 "Can't create table for derived DbEntity '" + ent.getName() + "'.");
173         }
174
175         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
176         buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" (");
177
178         // columns
179
Iterator JavaDoc it = ent.getAttributes().iterator();
180         boolean first = true;
181         while (it.hasNext()) {
182             if (first) {
183                 first = false;
184             }
185             else {
186                 buf.append(", ");
187             }
188
189             DbAttribute at = (DbAttribute) it.next();
190
191             // attribute may not be fully valid, do a simple check
192
if (at.getType() == TypesMapping.NOT_DEFINED) {
193                 throw new CayenneRuntimeException(
194                     "Undefined type for attribute '"
195                         + ent.getFullyQualifiedName()
196                         + "."
197                         + at.getName()
198                         + "'.");
199             }
200
201             String JavaDoc[] types = externalTypesForJdbcType(at.getType());
202             if (types == null || types.length == 0) {
203                 throw new CayenneRuntimeException(
204                     "Undefined type for attribute '"
205                         + ent.getFullyQualifiedName()
206                         + "."
207                         + at.getName()
208                         + "': "
209                         + at.getType());
210             }
211
212             String JavaDoc type = types[0];
213             buf.append(at.getName()).append(' ').append(type);
214
215             // append size and precision (if applicable)
216
if (TypesMapping.supportsLength(at.getType())) {
217                 int len = at.getMaxLength();
218                 int prec = TypesMapping.isDecimal(at.getType()) ? at.getPrecision() : -1;
219
220                 // sanity check
221
if (prec > len) {
222                     prec = -1;
223                 }
224
225                 if (len > 0) {
226                     buf.append('(').append(len);
227
228                     if (prec >= 0) {
229                         buf.append(", ").append(prec);
230                     }
231
232                     buf.append(')');
233                 }
234             }
235
236             if (at.isMandatory()) {
237                 buf.append(" NOT NULL");
238             }
239             else {
240                 buf.append(" NULL");
241             }
242         }
243
244         buf.append(')');
245         return buf.toString();
246     }
247
248     /**
249      * Returns a SQL string that can be used to create
250      * a foreign key constraint for the relationship.
251      */

252     public String JavaDoc createFkConstraint(DbRelationship rel) {
253         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
254
255         // OpendBase Specifics is that we need to create a constraint going
256
// from destination to source for this to work...
257

258         DbEntity sourceEntity = (DbEntity) rel.getSourceEntity();
259         DbEntity targetEntity = (DbEntity) rel.getTargetEntity();
260         String JavaDoc toMany = (!rel.isToMany()) ? "'1'" : "'0'";
261
262         // TODO: doesn't seem like OpenBase supports compound joins...
263
// need to doublecheck that
264

265         int joinsLen = rel.getJoins().size();
266         if (joinsLen == 0) {
267             throw new CayenneRuntimeException(
268                 "Relationship has no joins: " + rel.getName());
269         }
270         else if (joinsLen > 1) {
271             logObj.warn(
272                 "Only a single join relationships are supported by OpenBase. Ignoring extra joins.");
273         }
274
275         DbJoin join = (DbJoin) rel.getJoins().get(0);
276
277         buf
278             .append("INSERT INTO _SYS_RELATIONSHIP (")
279             .append("dest_table, dest_column, source_table, source_column, ")
280             .append("block_delete, cascade_delete, one_to_many, operator, relationshipName")
281             .append(") VALUES ('")
282             .append(sourceEntity.getFullyQualifiedName())
283             .append("', '")
284             .append(join.getSourceName())
285             .append("', '")
286             .append(targetEntity.getFullyQualifiedName())
287             .append("', '")
288             .append(join.getTargetName())
289             .append("', 0, 0, ")
290             .append(toMany)
291             .append(", '=', '")
292             .append(rel.getName())
293             .append("')");
294
295         return buf.toString();
296     }
297
298     // OpenBase JDBC driver has trouble reading "integer" as byte
299
// this converter addresses such problem
300
static class OpenBaseByteType extends DefaultType {
301         OpenBaseByteType() {
302             super(Byte JavaDoc.class.getName());
303         }
304
305         public Object JavaDoc materializeObject(ResultSet JavaDoc rs, int index, int type)
306             throws Exception JavaDoc {
307
308             // read value as int, and then narrow it down
309
int val = rs.getInt(index);
310             return (rs.wasNull()) ? null : new Byte JavaDoc((byte) val);
311         }
312     }
313
314     static class OpenBaseCharType extends CharType {
315         OpenBaseCharType() {
316             super(false, true);
317         }
318
319         public void setJdbcObject(
320             PreparedStatement JavaDoc st,
321             Object JavaDoc val,
322             int pos,
323             int type,
324             int precision)
325             throws Exception JavaDoc {
326
327             // These to types map to "text"; and when setting "text" as object
328
// OB assumes that the object is the actual CLOB... weird
329
if (type == Types.CLOB || type == Types.LONGVARCHAR) {
330                 st.setString(pos, (String JavaDoc) val);
331             }
332             else {
333                 super.setJdbcObject(st, val, pos, type, precision);
334             }
335         }
336     }
337 }
338
Popular Tags