KickJava   Java API By Example, From Geeks To Geeks.

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


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.conv.*;
16 import com.versant.core.jdbc.sql.exp.SqlExp;
17 import com.versant.core.jdbc.sql.exp.UnaryFunctionExp;
18 import com.versant.core.util.CharBuf;
19
20 import java.util.Date JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.sql.*;
23
24 import com.versant.core.common.BindingSupportImpl;
25
26 /**
27  * A driver for Daffodil pure Java database.
28  */

29 public class DaffodilSqlDriver extends SqlDriver {
30
31     /**
32      * Get the name of this driver.
33      */

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

45     protected JdbcTypeMapping getTypeMapping(int jdbcType) {
46         switch (jdbcType) {
47             case Types.BIT:
48                 return new JdbcTypeMapping("BOOLEAN", 0, 0, JdbcTypeMapping.TRUE,
49                         JdbcTypeMapping.TRUE, null);
50             case Types.TINYINT:
51                 return new JdbcTypeMapping("TINYINT",
52                         0, 0, JdbcTypeMapping.TRUE, JdbcTypeMapping.TRUE, null);
53             case Types.BIGINT:
54                 return new JdbcTypeMapping("BIGINT",
55                         0, 0, JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE, null);
56             case Types.DOUBLE:
57                 return new JdbcTypeMapping("DOUBLE PRECISION",
58                         0, 0, JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE, null);
59             case Types.LONGVARCHAR:
60                 return new JdbcTypeMapping("LONG VARCHAR",
61                         0, 0, JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE, null);
62             case Types.CLOB:
63                 return new JdbcTypeMapping("CLOB",
64                         1024, 0, JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE, null);
65             case Types.VARBINARY:
66             case Types.BLOB:
67             case Types.LONGVARBINARY:
68                 return new JdbcTypeMapping("BLOB",
69                         1024, 0, JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE,
70                         bytesConverterFactory);
71         }
72         return super.getTypeMapping(jdbcType);
73     }
74
75     /**
76      * Should PreparedStatement batching be used for this database and
77      * JDBC driver?
78      */

79     public boolean isPreparedStatementPoolingOK() {
80         return false;
81     }
82
83     /**
84      * Get the default field mappings for this driver. These map java classes
85      * to column properties. Subclasses should override this, call super() and
86      * replace mappings as needed.
87      */

88     public HashMap JavaDoc getJavaTypeMappings() {
89         HashMap JavaDoc ans = super.getJavaTypeMappings();
90
91 // BooleanConverter.Factory bcf = new BooleanConverter.Factory();
92
// ((JdbcJavaTypeMapping)ans.get(Boolean.TYPE)).setConverterFactory(bcf);
93
// ((JdbcJavaTypeMapping)ans.get(Boolean.class)).setConverterFactory(bcf);
94

95         DateTimestampConverter.Factory dtcf = new DateTimestampConverter.Factory();
96         ((JdbcJavaTypeMapping)ans.get(Date JavaDoc.class)).setConverterFactory(dtcf);
97
98         return ans;
99     }
100
101     /**
102      * Does the JDBC driver support statement batching?
103      */

104     public boolean isInsertBatchingSupported() {
105         return true;
106     }
107
108     /**
109      * Does the JDBC driver support statement batching for updates?
110      */

111     public boolean isUpdateBatchingSupported() {
112         return true;
113     }
114
115     /**
116      * Does the JDBC driver support scrollable result sets?
117      */

118     public boolean isScrollableResultSetSupported() {
119         return true;
120     }
121
122     /**
123      * Does this driver use the ANSI join syntax (i.e. the join clauses appear
124      * in the from list e.g. postgres)?
125      */

126     public boolean isAnsiJoinSyntax() {
127         return true;
128     }
129
130     /**
131      * Is null a valid value for a column with a foreign key constraint?
132      */

133     public boolean isNullForeignKeyOk() {
134         return true;
135     }
136
137     /**
138      * Create a default name generator instance for JdbcStore's using this
139      * driver.
140      */

141     public JdbcNameGenerator createJdbcNameGenerator() {
142         DefaultJdbcNameGenerator n = createDefaultJdbcNameGenerator();
143         n.setMaxColumnNameLength(128);
144         n.setMaxTableNameLength(128);
145         n.setMaxConstraintNameLength(128);
146         n.setMaxIndexNameLength(128);
147         return n;
148     }
149
150
151     /**
152      * Drop the table and all its constraints etc. This must remove
153      * constraints to this table from other tables so it can be dropped.
154      */

155     public void dropTable(Connection con, String JavaDoc table, Statement stat) throws SQLException {
156         stat.execute("drop table " + table + " cascade");
157     }
158
159     /**
160      * Add the primary key constraint part of a create table statement to s.
161      */

162     protected void appendPrimaryKeyConstraint(JdbcTable t, CharBuf s) {
163         s.append("constraint ");
164         s.append(t.pkConstraintName);
165         s.append(" primary key (");
166         appendColumnNameList(t.pk, s);
167         s.append(')');
168     }
169
170     /**
171      * Append an 'add constraint' statement for c.
172      */

173     protected void appendRefConstraint(final CharBuf s, final JdbcConstraint c) {
174         s.append("alter table ");
175         s.append(c.src.name);
176         s.append(" add constraint ");
177         s.append(c.name);
178         s.append(" foreign key (");
179         appendColumnNameList(c.srcCols, s);
180         s.append(") references ");
181         s.append(c.dest.name);
182         s.append('(');
183         appendColumnNameList(c.dest.pk, s);
184         s.append(")");
185     }
186
187     /**
188      * Append the from list entry for a table.
189      */

190     public void appendSqlFrom(JdbcTable table, String JavaDoc alias,
191             CharBuf s) {
192         s.append(table.name);
193         if (alias != null) {
194             s.append(" AS ");
195             s.append(alias);
196         }
197     }
198
199     /**
200      * Get the name of a function that accepts one argument.
201      * @see com.versant.core.jdbc.sql.exp.UnaryFunctionExp
202      */

203     public String JavaDoc getSqlUnaryFunctionName(int func) {
204         switch (func) {
205             case UnaryFunctionExp.FUNC_TO_LOWER_CASE:
206                 return "lcase";
207         }
208         throw BindingSupportImpl.getInstance().internal("Unknown func: " + func);
209     }
210
211     /**
212      * Append the from list entry for a table that is the right hand table
213      * in a join i.e. it is being joined to.
214      * @param exp This is the expression that joins the tables
215      * @param outer If true then this is an outer join
216      */

217     public void appendSqlFromJoin(JdbcTable table, String JavaDoc alias, SqlExp exp,
218             boolean outer, CharBuf s) {
219         if (outer) s.append(" LEFT JOIN ");
220         else s.append(" JOIN ");
221         s.append(table.name);
222         if (alias != null) {
223             s.append(" AS ");
224             s.append(alias);
225         }
226         if (exp != null) {
227             s.append(" ON (");
228             exp.appendSQL(this, s, null);
229             s.append(')');
230         }
231     }
232
233
234     /**
235      * Get default SQL to test a connection or null if none available. This
236      * must be a query that returns at least one row.
237      */

238     public String JavaDoc getConnectionValidateSQL() {
239         return null;
240     }
241
242     /**
243      * Get default SQL used to init a connection or null if none required.
244      */

245     public String JavaDoc getConnectionInitSQL() {
246         return null;
247     }
248
249     /**
250      * Does this database support comments embedded in SQL?
251      */

252     public boolean isCommentSupported() {
253         return false;
254     }
255
256     /**
257      * Should indexes be used for columns in the order by list that are
258      * also in the select list? This is used for databases that will not
259      * order by a column that is duplicated in the select list (e.g. Oracle).
260      */

261     public boolean isUseIndexesForOrderCols() {
262         return true;
263     }
264
265     /**
266      * Get whatever needs to be appended to a SELECT statement to lock the
267      * rows if this makes sense for the database. This must have a leading
268      * space if not empty.
269      */

270     public char[] getSelectForUpdate() {
271         return null;
272     }
273
274 }
275
Popular Tags