KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > db2 > DB2Adapter


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 package org.apache.cayenne.dba.db2;
21
22 import java.sql.PreparedStatement JavaDoc;
23 import java.util.Iterator 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.types.BooleanType;
29 import org.apache.cayenne.access.types.ByteArrayType;
30 import org.apache.cayenne.access.types.CharType;
31 import org.apache.cayenne.access.types.ExtendedTypeMap;
32 import org.apache.cayenne.dba.JdbcAdapter;
33 import org.apache.cayenne.dba.PkGenerator;
34 import org.apache.cayenne.dba.TypesMapping;
35 import org.apache.cayenne.map.DbAttribute;
36 import org.apache.cayenne.map.DbEntity;
37 import org.apache.cayenne.map.DerivedDbEntity;
38
39 /**
40  * DbAdapter implementation for the <a HREF="http://www.ibm.com/db2/"> DB2 RDBMS </a>.
41  * Sample <a target="_top"
42  * HREF="../../../../../../../developerguide/unit-tests.html">connection settings </a> to
43  * use with DB2 are shown below:
44  *
45  * <pre>
46  *
47  * test-db2.cayenne.adapter = org.apache.cayenne.dba.db2.DB2Adapter
48  * test-db2.jdbc.username = test
49  * test-db2.jdbc.password = secret
50  * test-db2.jdbc.url = jdbc:db2://servername:50000/databasename
51  * test-db2.jdbc.driver = com.ibm.db2.jcc.DB2Driver
52  *
53  * </pre>
54  *
55  * @author Holger Hoffstaette
56  */

57 public class DB2Adapter extends JdbcAdapter {
58
59     /**
60      * Creates a DB2 specific PK Generator.
61      */

62     protected PkGenerator createPkGenerator() {
63         return new DB2PkGenerator();
64     }
65
66     protected void configureExtendedTypes(ExtendedTypeMap map) {
67         super.configureExtendedTypes(map);
68
69         // create specially configured CharType handler
70
map.registerType(new CharType(true, true));
71
72         // configure boolean type to work with numeric columns
73
map.registerType(new DB2BooleanType());
74
75         map.registerType(new ByteArrayType(false, false));
76     }
77
78     /**
79      * Returns a SQL string that can be used to create database table corresponding to
80      * <code>ent</code> parameter.
81      */

82     public String JavaDoc createTable(DbEntity ent) {
83         // later we may support view creation
84
// for derived DbEntities
85
if (ent instanceof DerivedDbEntity) {
86             throw new CayenneRuntimeException("Can't create table for derived DbEntity '"
87                     + ent.getName()
88                     + "'.");
89         }
90
91         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
92         buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" (");
93
94         // columns
95
Iterator JavaDoc it = ent.getAttributes().iterator();
96         boolean first = true;
97         while (it.hasNext()) {
98             if (first) {
99                 first = false;
100             }
101             else {
102                 buf.append(", ");
103             }
104
105             DbAttribute at = (DbAttribute) it.next();
106
107             // attribute may not be fully valid, do a simple check
108
if (at.getType() == TypesMapping.NOT_DEFINED) {
109                 throw new CayenneRuntimeException("Undefined type for attribute '"
110                         + ent.getFullyQualifiedName()
111                         + "."
112                         + at.getName()
113                         + "'.");
114             }
115
116             String JavaDoc[] types = externalTypesForJdbcType(at.getType());
117             if (types == null || types.length == 0) {
118                 throw new CayenneRuntimeException("Undefined type for attribute '"
119                         + ent.getFullyQualifiedName()
120                         + "."
121                         + at.getName()
122                         + "': "
123                         + at.getType());
124             }
125
126             String JavaDoc type = types[0];
127             buf.append(at.getName()).append(' ').append(type);
128
129             // append size and precision (if applicable)
130
if (TypesMapping.supportsLength(at.getType())) {
131                 int len = at.getMaxLength();
132                 int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1;
133
134                 // sanity check
135
if (scale > len) {
136                     scale = -1;
137                 }
138
139                 if (len > 0) {
140                     buf.append('(').append(len);
141
142                     if (scale >= 0) {
143                         buf.append(", ").append(scale);
144                     }
145
146                     buf.append(')');
147                 }
148             }
149
150             if (at.isMandatory()) {
151                 buf.append(" NOT NULL");
152             }
153         }
154
155         // primary key clause
156
Iterator JavaDoc pkit = ent.getPrimaryKey().iterator();
157         if (pkit.hasNext()) {
158             if (first)
159                 first = false;
160             else
161                 buf.append(", ");
162
163             buf.append("PRIMARY KEY (");
164             boolean firstPk = true;
165             while (pkit.hasNext()) {
166                 if (firstPk)
167                     firstPk = false;
168                 else
169                     buf.append(", ");
170
171                 DbAttribute at = (DbAttribute) pkit.next();
172                 buf.append(at.getName());
173             }
174             buf.append(')');
175         }
176         buf.append(')');
177         return buf.toString();
178     }
179
180     /**
181      * Returns a trimming translator.
182      */

183     public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) {
184         return new DB2QualifierTranslator(queryAssembler, "RTRIM");
185     }
186
187     final class DB2BooleanType extends BooleanType {
188
189         public void setJdbcObject(
190                 PreparedStatement JavaDoc st,
191                 Object JavaDoc val,
192                 int pos,
193                 int type,
194                 int precision) throws Exception JavaDoc {
195
196             if (val != null) {
197                 st.setInt(pos, ((Boolean JavaDoc) val).booleanValue() ? 1 : 0);
198             }
199             else {
200                 st.setNull(pos, type);
201             }
202         }
203     }
204
205 }
206
Popular Tags