KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > hsqldb > HSQLDBAdapter


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.dba.hsqldb;
22
23 import java.sql.PreparedStatement JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.apache.cayenne.CayenneRuntimeException;
28 import org.apache.cayenne.access.DataNode;
29 import org.apache.cayenne.access.types.DefaultType;
30 import org.apache.cayenne.access.types.ExtendedTypeMap;
31 import org.apache.cayenne.dba.JdbcAdapter;
32 import org.apache.cayenne.map.DbAttribute;
33 import org.apache.cayenne.map.DbEntity;
34 import org.apache.cayenne.map.DbJoin;
35 import org.apache.cayenne.map.DbRelationship;
36 import org.apache.cayenne.query.Query;
37 import org.apache.cayenne.query.SQLAction;
38
39 /**
40  * DbAdapter implementation for the <a HREF="http://hsqldb.sourceforge.net/"> HSQLDB RDBMS
41  * </a>. Sample <a target="_top"
42  * HREF="../../../../../../../developerguide/unit-tests.html">connection settings </a> to
43  * use with HSQLDB are shown below:
44  *
45  * <pre>
46  *
47  * test-hsqldb.cayenne.adapter = org.apache.cayenne.dba.hsqldb.HSQLDBAdapter
48  * test-hsqldb.jdbc.username = test
49  * test-hsqldb.jdbc.password = secret
50  * test-hsqldb.jdbc.url = jdbc:hsqldb:hsql://serverhostname
51  * test-hsqldb.jdbc.driver = org.hsqldb.jdbcDriver
52  *
53  * </pre>
54  *
55  * @author Holger Hoffstaette
56  */

57 public class HSQLDBAdapter extends JdbcAdapter {
58
59     protected void configureExtendedTypes(ExtendedTypeMap map) {
60         super.configureExtendedTypes(map);
61         map.registerType(new ShortType());
62         map.registerType(new ByteType());
63     }
64
65     /**
66      * Generate fully-qualified name for 1.8 and on. Subclass generates unqualified name.
67      *
68      * @since 1.2
69      */

70     protected String JavaDoc getTableName(DbEntity entity) {
71         return entity.getFullyQualifiedName();
72     }
73
74     /**
75      * Generate fully-qualified name for 1.8 and on. Subclass generates unqualified name.
76      *
77      * @since 1.2
78      */

79     protected String JavaDoc getSchemaName(DbEntity entity) {
80         if (entity.getSchema() != null && entity.getSchema().length() > 0) {
81             return entity.getSchema() + ".";
82         }
83
84         return "";
85     }
86
87     /**
88      * Uses special action builder to create the right action.
89      *
90      * @since 1.2
91      */

92     public SQLAction getAction(Query query, DataNode node) {
93         return query
94                 .createSQLAction(new HSQLActionBuilder(this, node.getEntityResolver()));
95     }
96
97     /**
98      * Returns a DDL string to create a unique constraint over a set of columns.
99      *
100      * @since 1.1
101      */

102     public String JavaDoc createUniqueConstraint(DbEntity source, Collection JavaDoc columns) {
103         if (columns == null || columns.isEmpty()) {
104             throw new CayenneRuntimeException(
105                     "Can't create UNIQUE constraint - no columns specified.");
106         }
107
108         String JavaDoc srcName = getTableName(source);
109
110         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
111
112         buf.append("ALTER TABLE ").append(srcName);
113         buf.append(" ADD CONSTRAINT ");
114
115         buf.append(getSchemaName(source));
116         buf.append("U_");
117         buf.append(source.getName());
118         buf.append("_");
119         buf.append((long) (System.currentTimeMillis() / (Math.random() * 100000)));
120         buf.append(" UNIQUE (");
121
122         Iterator JavaDoc it = columns.iterator();
123         DbAttribute first = (DbAttribute) it.next();
124         buf.append(first.getName());
125
126         while (it.hasNext()) {
127             DbAttribute next = (DbAttribute) it.next();
128             buf.append(", ");
129             buf.append(next.getName());
130         }
131
132         buf.append(")");
133
134         return buf.toString();
135     }
136
137     /**
138      * Adds an ADD CONSTRAINT clause to a relationship constraint.
139      *
140      * @see JdbcAdapter#createFkConstraint(DbRelationship)
141      */

142     public String JavaDoc createFkConstraint(DbRelationship rel) {
143         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
144         StringBuffer JavaDoc refBuf = new StringBuffer JavaDoc();
145
146         String JavaDoc srcName = getTableName((DbEntity) rel.getSourceEntity());
147         String JavaDoc dstName = getTableName((DbEntity) rel.getTargetEntity());
148
149         buf.append("ALTER TABLE ");
150         buf.append(srcName);
151
152         // hsqldb requires the ADD CONSTRAINT statement
153
buf.append(" ADD CONSTRAINT ");
154         buf.append(getSchemaName((DbEntity) rel.getSourceEntity()));
155         buf.append("C_");
156         buf.append(rel.getSourceEntity().getName());
157         buf.append("_");
158         buf.append((long) (System.currentTimeMillis() / (Math.random() * 100000)));
159
160         buf.append(" FOREIGN KEY (");
161
162         Iterator JavaDoc jit = rel.getJoins().iterator();
163         boolean first = true;
164         while (jit.hasNext()) {
165             DbJoin join = (DbJoin) jit.next();
166             if (!first) {
167                 buf.append(", ");
168                 refBuf.append(", ");
169             }
170             else
171                 first = false;
172
173             buf.append(join.getSourceName());
174             refBuf.append(join.getTargetName());
175         }
176
177         buf.append(") REFERENCES ");
178         buf.append(dstName);
179         buf.append(" (");
180         buf.append(refBuf.toString());
181         buf.append(')');
182
183         // also make sure we delete dependent FKs
184
buf.append(" ON DELETE CASCADE");
185
186         return buf.toString();
187     }
188
189     /**
190      * Uses "CREATE CACHED TABLE" instead of "CREATE TABLE".
191      *
192      * @since 1.2
193      */

194     public String JavaDoc createTable(DbEntity ent) {
195         // SET SCHEMA <schemaname>
196

197         String JavaDoc sql = super.createTable(ent);
198
199         if (sql != null && sql.toUpperCase().startsWith("CREATE TABLE ")) {
200             sql = "CREATE CACHED TABLE " + sql.substring("CREATE TABLE ".length());
201         }
202
203         return sql;
204     }
205
206     final class ShortType extends DefaultType {
207
208         ShortType() {
209             super(Short JavaDoc.class.getName());
210         }
211
212         public void setJdbcObject(
213                 PreparedStatement JavaDoc st,
214                 Object JavaDoc val,
215                 int pos,
216                 int type,
217                 int precision) throws Exception JavaDoc {
218
219             if (val == null) {
220                 super.setJdbcObject(st, val, pos, type, precision);
221             }
222             else {
223
224                 short s = ((Number JavaDoc) val).shortValue();
225                 st.setShort(pos, s);
226             }
227         }
228     }
229     
230     final class ByteType extends DefaultType {
231
232         ByteType() {
233             super(Byte JavaDoc.class.getName());
234         }
235
236         public void setJdbcObject(
237                 PreparedStatement JavaDoc st,
238                 Object JavaDoc val,
239                 int pos,
240                 int type,
241                 int precision) throws Exception JavaDoc {
242
243             if (val == null) {
244                 super.setJdbcObject(st, val, pos, type, precision);
245             }
246             else {
247
248                 byte b = ((Number JavaDoc) val).byteValue();
249                 st.setByte(pos, b);
250             }
251         }
252     }
253 }
254
Popular Tags