KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dba > ingres > IngresAdapter


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.ingres;
21
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.cayenne.access.trans.TrimmingQualifierTranslator;
25 import org.apache.cayenne.access.trans.QueryAssembler;
26 import org.apache.cayenne.access.trans.QualifierTranslator;
27 import org.apache.cayenne.CayenneRuntimeException;
28 import org.apache.cayenne.access.types.ExtendedTypeMap;
29 import org.apache.cayenne.dba.JdbcAdapter;
30 import org.apache.cayenne.dba.PkGenerator;
31 import org.apache.cayenne.dba.TypesMapping;
32 import org.apache.cayenne.map.DbAttribute;
33 import org.apache.cayenne.map.DbEntity;
34 import org.apache.cayenne.map.DerivedDbEntity;
35
36 /**
37  * DbAdapter implementation for <a HREF="http://opensource.ca.com/projects/ingres/">Ingres</a>.
38  * Sample <a target="_top"
39  * HREF="../../../../../../../developerguide/unit-tests.html">connection settings</a> to
40  * use with Ingres are shown below:
41  *
42  * <pre>
43  * ingres.cayenne.adapter = org.apache.cayenne.dba.ingres.IngresAdapter
44  * ingres.jdbc.username = test
45  * ingres.jdbc.password = secret
46  * ingres.jdbc.url = jdbc:ingres://serverhostname:II7/cayenne
47  * ingres.jdbc.driver = ca.ingres.jdbc.IngresDriver
48  * </pre>
49  */

50 public class IngresAdapter extends JdbcAdapter {
51     public static final String JavaDoc TRIM_FUNCTION = "TRIM";
52     
53     public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler) {
54         return new TrimmingQualifierTranslator(
55                 queryAssembler,
56                 IngresAdapter.TRIM_FUNCTION);
57     }
58     /**
59      * Returns a SQL string that can be used to create database table corresponding to
60      * <code>ent</code> parameter.
61      */

62     public String JavaDoc createTable(DbEntity ent) {
63         // later we may support view creation
64
// for derived DbEntities
65
if (ent instanceof DerivedDbEntity) {
66             throw new CayenneRuntimeException("Can't create table for derived DbEntity '"
67                     + ent.getName()
68                     + "'.");
69         }
70
71         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
72         buf.append("CREATE TABLE ").append(ent.getFullyQualifiedName()).append(" (");
73
74         // columns
75
Iterator JavaDoc it = ent.getAttributes().iterator();
76         boolean first = true;
77         while (it.hasNext()) {
78             if (first) {
79                 first = false;
80             }
81             else {
82                 buf.append(", ");
83             }
84
85             DbAttribute at = (DbAttribute) it.next();
86
87             // attribute may not be fully valid, do a simple check
88
if (at.getType() == TypesMapping.NOT_DEFINED) {
89                 throw new CayenneRuntimeException("Undefined type for attribute '"
90                         + ent.getFullyQualifiedName()
91                         + "."
92                         + at.getName()
93                         + "'.");
94             }
95
96             String JavaDoc[] types = externalTypesForJdbcType(at.getType());
97             if (types == null || types.length == 0) {
98                 throw new CayenneRuntimeException("Undefined type for attribute '"
99                         + ent.getFullyQualifiedName()
100                         + "."
101                         + at.getName()
102                         + "': "
103                         + at.getType());
104             }
105
106             String JavaDoc type = types[0];
107             buf.append(at.getName()).append(' ').append(type);
108
109             // append size and precision (if applicable)
110
if (TypesMapping.supportsLength(at.getType())) {
111                 int len = at.getMaxLength();
112                 int scale = TypesMapping.isDecimal(at.getType()) ? at.getScale() : -1;
113
114                 // sanity check
115
if (scale > len) {
116                     scale = -1;
117                 }
118
119                 if (len > 0) {
120                     buf.append('(').append(len);
121
122                     if (scale >= 0) {
123                         buf.append(", ").append(scale);
124                     }
125
126                     buf.append(')');
127                 }
128             }
129
130             // Ingres does not like "null" for non mandatory fields
131
if (at.isMandatory()) {
132                 buf.append(" NOT NULL");
133             }
134         }
135
136         // primary key clause
137
Iterator JavaDoc pkit = ent.getPrimaryKey().iterator();
138         if (pkit.hasNext()) {
139             if (first)
140                 first = false;
141             else
142                 buf.append(", ");
143
144             buf.append("PRIMARY KEY (");
145             boolean firstPk = true;
146             while (pkit.hasNext()) {
147                 if (firstPk)
148                     firstPk = false;
149                 else
150                     buf.append(", ");
151
152                 DbAttribute at = (DbAttribute) pkit.next();
153                 buf.append(at.getName());
154             }
155             buf.append(')');
156         }
157         buf.append(')');
158         return buf.toString();
159     }
160
161     protected void configureExtendedTypes(ExtendedTypeMap map) {
162         super.configureExtendedTypes(map);
163         map.registerType(new IngresCharType());
164     }
165
166     /**
167      * @see JdbcAdapter#createPkGenerator()
168      */

169     protected PkGenerator createPkGenerator() {
170         return new IngresPkGenerator();
171     }
172
173 }
174
Popular Tags