KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > MckoiSqlDriver


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql;
13
14 import com.versant.core.jdbc.metadata.*;
15 import com.versant.core.jdbc.sql.exp.SqlExp;
16 import com.versant.core.util.CharBuf;
17
18 import java.io.PrintWriter JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.sql.Types JavaDoc;
21
22 import com.versant.core.util.CharBuf;
23
24 /**
25  * A driver for McKoi.
26  */

27 public class MckoiSqlDriver extends SqlDriver {
28
29     /**
30      * Get the name of this driver.
31      */

32     public String JavaDoc getName() {
33         return "mckoi";
34     }
35
36     /**
37      * Get the default type mapping for the supplied JDBC type code from
38      * java.sql.Types or null if the type is not supported. There is no
39      * need to set the database or jdbcType on the mapping as this is done
40      * after this call returns. Subclasses should override this and to
41      * customize type mappings.
42      */

43     protected JdbcTypeMapping getTypeMapping(int jdbcType) {
44         switch (jdbcType) {
45             case Types.LONGVARCHAR:
46             case Types.CLOB:
47                 return new JdbcTypeMapping("LONGVARCHAR",
48                     0, 0, JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE,
49                     null);
50             case Types.LONGVARBINARY:
51             case Types.BLOB:
52                 return new JdbcTypeMapping("LONGVARBINARY",
53                     0, 0, JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE,
54                     bytesConverterFactory);
55         }
56         return super.getTypeMapping(jdbcType);
57     }
58
59     /**
60      * Get the default field mappings for this driver. These map java classes
61      * to column properties. Subclasses should override this, call super() and
62      * replace mappings as needed.
63      */

64     public HashMap JavaDoc getJavaTypeMappings() {
65         HashMap JavaDoc ans = super.getJavaTypeMappings();
66         return ans;
67     }
68
69     /**
70      * Does the JDBC driver support statement batching?
71      */

72     public boolean isInsertBatchingSupported() {
73         return false;
74     }
75
76     /**
77      * Does the JDBC driver support statement batching for updates?
78      */

79     public boolean isUpdateBatchingSupported() {
80         return false;
81     }
82
83     /**
84      * Does this driver use the ANSI join syntax (i.e. the join clauses appear
85      * in the from list e.g. postgres)?
86      */

87     public boolean isAnsiJoinSyntax() {
88         return true;
89     }
90
91     /**
92      * Is null a valid value for a column with a foreign key constraint?
93      */

94     public boolean isNullForeignKeyOk() {
95         return true;
96     }
97
98     /**
99      * Must 'exists (select ...)' clauses be converted into a join and
100      * distinct be added to the select (e.g. MySQL) ?
101      */

102     public boolean isConvertExistsToDistinctJoin() {
103         return true;
104     }
105
106     /**
107      * Create a default name generator instance for JdbcStore's using this
108      * driver.
109      */

110     public JdbcNameGenerator createJdbcNameGenerator() {
111         DefaultJdbcNameGenerator n = createDefaultJdbcNameGenerator();
112         n.setMaxColumnNameLength(31);
113         n.setMaxTableNameLength(31);
114         n.setMaxConstraintNameLength(31);
115         n.setMaxTableNameLength(31);
116         return n;
117     }
118
119     /**
120      * Append the allow nulls part of the definition for a column in a
121      * create table statement.
122      */

123     protected void appendCreateColumnNulls(JdbcTable t, JdbcColumn c,
124             CharBuf s) {
125         if (!c.nulls) s.append(" not null");
126     }
127
128     /**
129      * Add the primary key constraint part of a create table statement to s.
130      */

131     protected void appendPrimaryKeyConstraint(JdbcTable t, CharBuf s) {
132         s.append("constraint ");
133         s.append(t.pkConstraintName);
134         s.append(" primary key (");
135         appendColumnNameList(t.pk, s);
136         s.append(')');
137     }
138
139     /**
140      * Append an 'add constraint' statement for c.
141      */

142     protected void appendRefConstraint(CharBuf s, JdbcConstraint c) {
143         s.append("alter table ");
144         s.append(c.src.name);
145         s.append(" add constraint ");
146         s.append(c.name);
147         s.append(" foreign key (");
148         appendColumnNameList(c.srcCols, s);
149         s.append(") references ");
150         s.append(c.dest.name);
151         s.append('(');
152         appendColumnNameList(c.dest.pk, s);
153         s.append(')');
154     }
155
156     /**
157      * Write an SQL statement to a script with appropriate separator.
158      */

159     protected void print(PrintWriter JavaDoc out, String JavaDoc sql) {
160         out.print(sql);
161         out.println(";");
162         out.println();
163     }
164
165     /**
166      * Append the from list entry for a table.
167      */

168     public void appendSqlFrom(JdbcTable table, String JavaDoc alias,
169             CharBuf s) {
170         s.append(table.name);
171         if (alias != null) {
172             s.append(" AS ");
173             s.append(alias);
174         }
175     }
176
177     /**
178      * Append the from list entry for a table that is the right hand table
179      * in a join i.e. it is being joined to.
180      * @param exp This is the expression that joins the tables
181      * @param outer If true then this is an outer join
182      */

183     public void appendSqlFromJoin(JdbcTable table, String JavaDoc alias, SqlExp exp,
184             boolean outer, CharBuf s) {
185         if (outer) s.append(" LEFT JOIN ");
186         else s.append(" JOIN ");
187         s.append(table.name);
188         if (alias != null) {
189             s.append(" AS ");
190             s.append(alias);
191         }
192         if (exp != null) {
193             s.append(" ON (");
194             exp.appendSQL(this, s, null);
195             s.append(')');
196         }
197     }
198
199 }
200
Popular Tags