KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > derby > DerbyAdapter


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.derby;
22
23 import java.sql.Types JavaDoc;
24
25 import org.apache.cayenne.CayenneRuntimeException;
26 import org.apache.cayenne.access.trans.QualifierTranslator;
27 import org.apache.cayenne.access.trans.QueryAssembler;
28 import org.apache.cayenne.access.trans.TrimmingQualifierTranslator;
29 import org.apache.cayenne.access.types.ByteType;
30 import org.apache.cayenne.access.types.CharType;
31 import org.apache.cayenne.access.types.ExtendedTypeMap;
32 import org.apache.cayenne.access.types.ShortType;
33 import org.apache.cayenne.dba.JdbcAdapter;
34 import org.apache.cayenne.dba.PkGenerator;
35 import org.apache.cayenne.dba.TypesMapping;
36 import org.apache.cayenne.map.DbAttribute;
37 import org.apache.cayenne.map.DbEntity;
38
39 /**
40  * DbAdapter implementation for the <a HREF="http://incubator.apache.org/derby/"> Derby
41  * RDBMS </a>. Sample <a target="_top"
42  * HREF="../../../../../../../developerguide/unit-tests.html">connection settings </a> to
43  * use with Derby are shown below.
44  * <h3>Embedded</h3>
45  *
46  * <pre>
47  * test-derby.cayenne.adapter = org.apache.cayenne.dba.derby.DerbyAdapter
48  * test-derby.jdbc.url = jdbc:derby:testdb;create=true
49  * test-derby.jdbc.driver = org.apache.derby.jdbc.EmbeddedDriver
50  * </pre>
51  *
52  * <h3>Network Server</h3>
53  *
54  * <pre>
55  * derbynet.cayenne.adapter = org.apache.cayenne.dba.derby.DerbyAdapter
56  * derbynet.jdbc.url = jdbc:derby://localhost/cayenne
57  * derbynet.jdbc.driver = org.apache.derby.jdbc.ClientDriver
58  * derbynet.jdbc.username = someuser
59  * derbynet.jdbc.password = secret;
60  * </pre>
61  */

62 public class DerbyAdapter extends JdbcAdapter {
63
64     static final String JavaDoc FOR_BIT_DATA_SUFFIX = " FOR BIT DATA";
65
66     public DerbyAdapter() {
67         setSupportsGeneratedKeys(true);
68         setSupportsBatchUpdates(true);
69     }
70
71     protected PkGenerator createPkGenerator() {
72         return new DerbyPkGenerator();
73     }
74
75     /**
76      * Installs appropriate ExtendedTypes as converters for passing values between JDBC
77      * and Java layers.
78      */

79     protected void configureExtendedTypes(ExtendedTypeMap map) {
80         super.configureExtendedTypes(map);
81
82         // create specially configured CharType handler
83
map.registerType(new CharType(true, false));
84
85         // address Derby driver inability to handle java.lang.Short and java.lang.Byte
86
map.registerType(new ShortType(true));
87         map.registerType(new ByteType(true));
88     }
89
90     /**
91      * Appends SQL for column creation to CREATE TABLE buffer. Only change for Derby is
92      * that " NULL" is not supported.
93      *
94      * @since 1.2
95      */

96     protected void createTableAppendColumn(StringBuffer JavaDoc sqlBuffer, DbAttribute column) {
97
98         String JavaDoc[] types = externalTypesForJdbcType(column.getType());
99         if (types == null || types.length == 0) {
100             String JavaDoc entityName = column.getEntity() != null ? ((DbEntity) column
101                     .getEntity()).getFullyQualifiedName() : "<null>";
102             throw new CayenneRuntimeException("Undefined type for attribute '"
103                     + entityName
104                     + "."
105                     + column.getName()
106                     + "': "
107                     + column.getType());
108         }
109
110         String JavaDoc type = types[0];
111
112         String JavaDoc length = "";
113         if (typeSupportsLength(column.getType())) {
114             int len = column.getMaxLength();
115             int scale = TypesMapping.isDecimal(column.getType())
116                     ? column.getScale()
117                     : -1;
118
119             // sanity check
120
if (scale > len) {
121                 scale = -1;
122             }
123
124             if (len > 0) {
125                 length = " (" + len;
126
127                 if (scale >= 0) {
128                     length += ", " + scale;
129                 }
130
131                 length += ")";
132             }
133         }
134
135         // assemble...
136
// note that max length for types like XYZ FOR BIT DATA must be entered in the
137
// middle of type name, e.g. VARCHAR (100) FOR BIT DATA.
138

139         sqlBuffer.append(column.getName()).append(' ');
140         if (length.length() > 0 && type.endsWith(FOR_BIT_DATA_SUFFIX)) {
141             sqlBuffer.append(type.substring(0, type.length()
142                     - FOR_BIT_DATA_SUFFIX.length()));
143             sqlBuffer.append(length);
144             sqlBuffer.append(FOR_BIT_DATA_SUFFIX);
145         }
146         else {
147             sqlBuffer.append(type).append(length);
148         }
149
150         if (column.isMandatory()) {
151             sqlBuffer.append(" NOT NULL");
152         }
153
154         if (column.isGenerated()) {
155             sqlBuffer.append(" GENERATED BY DEFAULT AS IDENTITY");
156         }
157     }
158     
159     private boolean typeSupportsLength(int type) {
160         // "BLOB" and "CLOB" type support length. default length is 1M.
161
switch (type) {
162             case Types.BLOB:
163             case Types.CLOB:
164                 return true;
165             default:
166                 return TypesMapping.supportsLength(type);
167         }
168     }
169
170     /**
171      * Returns a trimming translator.
172      */

173     public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) {
174         return new TrimmingQualifierTranslator(queryAssembler, "RTRIM");
175     }
176
177 }
178
Popular Tags