KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > dba > firebird > FirebirdAdapter


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.firebird;
57
58 import java.util.Iterator JavaDoc;
59
60 import org.objectstyle.cayenne.CayenneRuntimeException;
61 import org.objectstyle.cayenne.access.types.CharType;
62 import org.objectstyle.cayenne.access.types.ExtendedTypeMap;
63 import org.objectstyle.cayenne.dba.JdbcAdapter;
64 import org.objectstyle.cayenne.dba.PkGenerator;
65 import org.objectstyle.cayenne.dba.TypesMapping;
66 import org.objectstyle.cayenne.map.DbAttribute;
67 import org.objectstyle.cayenne.map.DbEntity;
68 import org.objectstyle.cayenne.map.DbRelationship;
69 import org.objectstyle.cayenne.map.DerivedDbEntity;
70
71 /**
72  * DbAdapter implementation for <a HREF="http://www.firebirdsql.org">Firebird RDBMS</a>.
73  * Sample <a target="_top" HREF="../../../../../../../developerguide/unit-tests.html">connection
74  * settings</a> to use with Firebird are shown below:
75  *
76  * <pre>
77  * test-firebird.cayenne.adapter = org.objectstyle.cayenne.dba.firebird.FirebirdAdapter
78  * test-firebird.jdbc.username = sysdba
79  * test-firebird.jdbc.password = masterkey
80  * test-firebird.jdbc.url = jdbc:firebirdsql:[host[/port]/]<database>
81  * test-firebird.jdbc.driver = org.firebirdsql.jdbc.FBDriver
82  * </pre>
83  *
84  * @author Heiko Wenzel
85  */

86 public class FirebirdAdapter extends JdbcAdapter {
87     
88     public FirebirdAdapter() {
89         super();
90     }
91
92     public String JavaDoc createTable(DbEntity ent) {
93         if (ent instanceof DerivedDbEntity) {
94             throw new CayenneRuntimeException(
95             "Can't create table for derived DbEntity '" + ent.getName() + "'.");
96         }
97         
98         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
99         buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" (");
100         
101         // columns
102
Iterator JavaDoc it = ent.getAttributes().iterator();
103         boolean first = true;
104         while (it.hasNext()) {
105             if (first) {
106                 first = false;
107             }
108             else {
109                 buf.append(", ");
110             }
111             
112             DbAttribute at = (DbAttribute) it.next();
113             
114             // attribute may not be fully valid, do a simple check
115
if (at.getType() == TypesMapping.NOT_DEFINED) {
116                 throw new CayenneRuntimeException(
117                 "Undefined type for attribute '"
118                 + ent.getFullyQualifiedName()
119                 + "."
120                 + at.getName()
121                 + "'.");
122             }
123             
124             String JavaDoc[] types = externalTypesForJdbcType(at.getType());
125             if (types == null || types.length == 0) {
126                 throw new CayenneRuntimeException(
127                 "Undefined type for attribute '"
128                 + ent.getFullyQualifiedName()
129                 + "."
130                 + at.getName()
131                 + "': "
132                 + at.getType());
133             }
134             
135             String JavaDoc type = types[0];
136             buf.append(at.getName()).append(' ').append(type);
137             
138             // append size and precision (if applicable)
139
if (typeSupportsLength(at.getType())) {
140                 int len = at.getMaxLength();
141                 int prec = TypesMapping.isDecimal(at.getType()) ? at.getPrecision() : -1;
142                 
143                 if (type.compareTo("double precision") == 0) // double precision returns with len = 15
144
len = 0;
145                 
146                 // sanity check
147
if (prec > len) {
148                     prec = -1;
149                 }
150                 
151                 if (len > 0) {
152                     buf.append('(').append(len);
153                     
154                     if (prec >= 0) {
155                         buf.append(", ").append(prec);
156                     }
157                     buf.append(')');
158                 }
159             }
160             
161             if (at.isMandatory()) {
162                 buf.append(" NOT NULL");
163             }
164             
165         }
166         
167         // primary key clause
168
Iterator JavaDoc pkit = ent.getPrimaryKey().iterator();
169         if (pkit.hasNext()) {
170             if (first)
171                 first = false;
172             else
173                 buf.append(", ");
174             
175             buf.append("PRIMARY KEY (");
176             boolean firstPk = true;
177             while (pkit.hasNext()) {
178                 if (firstPk)
179                     firstPk = false;
180                 else
181                     buf.append(", ");
182                 
183                 DbAttribute at = (DbAttribute) pkit.next();
184                 buf.append(at.getName());
185             }
186             buf.append(')');
187         }
188         buf.append(");"); // added ; and so I can execute the DDL Script at once
189
return buf.toString();
190     }
191     
192     private boolean typeSupportsLength(int type) {
193         // "bytea" type does not support length
194
String JavaDoc[] externalTypes = externalTypesForJdbcType(type);
195         if (externalTypes != null && externalTypes.length > 0) {
196             for (int i = 0; i < externalTypes.length; i++) {
197                 if ("blob sub_type 0".equalsIgnoreCase(externalTypes[i])) {
198                     return false;
199                 }
200             }
201         }
202
203         return TypesMapping.supportsLength(type);
204     }
205     
206     /**
207      * Returns a SQL string that can be used to create
208      * a foreign key constraint for the relationship.
209      */

210     public String JavaDoc createFkConstraint(DbRelationship rel) {
211         // added ; and so I can execute the DDL Script at once
212
return super.createFkConstraint(rel) + ";";
213     }
214     
215     public String JavaDoc dropTable(DbEntity ent) {
216         // added ; and so I can execute the DDL Script at once
217
return super.dropTable(ent) + ";";
218     }
219     
220     protected void configureExtendedTypes(ExtendedTypeMap map) {
221         super.configureExtendedTypes(map);
222         map.registerType(new CharType(true, false));
223     }
224     
225     /**
226      * @see JdbcAdapter#createPkGenerator()
227      */

228     protected PkGenerator createPkGenerator() {
229         return new FirebirdPkGenerator();
230     }
231     
232     // TODO: Does Firebird support something like RTRIM?... It really needs it.
233
/* public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) {
234         return new TrimmingQualifierTranslator(queryAssembler, "");
235     } */

236 }
237
Popular Tags