Source code: org/objectstyle/cayenne/dba/firebird/FirebirdAdapter.java
1 /* ====================================================================
2 *
3 * The ObjectStyle Group Software License, Version 1.0
4 *
5 * Copyright (c) 2002-2003 The ObjectStyle Group
6 * and individual authors of the software. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. The end-user documentation included with the redistribution, if
21 * any, must include the following acknowlegement:
22 * "This product includes software developed by the
23 * ObjectStyle Group (http://objectstyle.org/)."
24 * Alternately, this acknowlegement may appear in the software itself,
25 * if and wherever such third-party acknowlegements normally appear.
26 *
27 * 4. The names "ObjectStyle Group" and "Cayenne"
28 * must not be used to endorse or promote products derived
29 * from this software without prior written permission. For written
30 * permission, please contact andrus@objectstyle.org.
31 *
32 * 5. Products derived from this software may not be called "ObjectStyle"
33 * nor may "ObjectStyle" appear in their names without prior written
34 * permission of the ObjectStyle Group.
35 *
36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This software consists of voluntary contributions made by many
51 * individuals on behalf of the ObjectStyle Group. For more
52 * information on the ObjectStyle Group, please see
53 * <http://objectstyle.org/>.
54 *
55 */
56 package org.objectstyle.cayenne.dba.firebird;
57
58 import java.util.Iterator;
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 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 buf = new StringBuffer();
99 buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" (");
100
101 // columns
102 Iterator 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[] 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 type = types[0];
136 buf.append(at.getName()).append(' ').append(type);
137
138 // append size and precision (if applicable)
139 if (TypesMapping.supportsLength(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 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 /**
193 * Returns a SQL string that can be used to create
194 * a foreign key constraint for the relationship.
195 */
196 public String createFkConstraint(DbRelationship rel) {
197 // added ; and so I can execute the DDL Script at once
198 return super.createFkConstraint(rel) + ";";
199 }
200
201 public String dropTable(DbEntity ent) {
202 // added ; and so I can execute the DDL Script at once
203 return super.dropTable(ent) + ";";
204 }
205
206 protected void configureExtendedTypes(ExtendedTypeMap map) {
207 super.configureExtendedTypes(map);
208 map.registerType(new CharType(true, false));
209 }
210
211 /**
212 * @see JdbcAdapter#createPkGenerator()
213 */
214 protected PkGenerator createPkGenerator() {
215 return new FirebirdPkGenerator();
216 }
217
218 // TODO: Does Firebird support something like RTRIM?... It really needs it.
219 /* public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) {
220 return new TrimmingQualifierTranslator(queryAssembler, "");
221 } */
222 }