001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.sql;
012:
013: import com.versant.core.jdbc.metadata.*;
014: import com.versant.core.jdbc.sql.exp.SqlExp;
015: import com.versant.core.util.CharBuf;
016:
017: import java.io.PrintWriter;
018: import java.util.HashMap;
019: import java.sql.Types;
020:
021: import com.versant.core.util.CharBuf;
022:
023: /**
024: * A driver for McKoi.
025: */
026: public class MckoiSqlDriver extends SqlDriver {
027:
028: /**
029: * Get the name of this driver.
030: */
031: public String getName() {
032: return "mckoi";
033: }
034:
035: /**
036: * Get the default type mapping for the supplied JDBC type code from
037: * java.sql.Types or null if the type is not supported. There is no
038: * need to set the database or jdbcType on the mapping as this is done
039: * after this call returns. Subclasses should override this and to
040: * customize type mappings.
041: */
042: protected JdbcTypeMapping getTypeMapping(int jdbcType) {
043: switch (jdbcType) {
044: case Types.LONGVARCHAR:
045: case Types.CLOB:
046: return new JdbcTypeMapping("LONGVARCHAR", 0, 0,
047: JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE, null);
048: case Types.LONGVARBINARY:
049: case Types.BLOB:
050: return new JdbcTypeMapping("LONGVARBINARY", 0, 0,
051: JdbcTypeMapping.TRUE, JdbcTypeMapping.FALSE,
052: bytesConverterFactory);
053: }
054: return super .getTypeMapping(jdbcType);
055: }
056:
057: /**
058: * Get the default field mappings for this driver. These map java classes
059: * to column properties. Subclasses should override this, call super() and
060: * replace mappings as needed.
061: */
062: public HashMap getJavaTypeMappings() {
063: HashMap ans = super .getJavaTypeMappings();
064: return ans;
065: }
066:
067: /**
068: * Does the JDBC driver support statement batching?
069: */
070: public boolean isInsertBatchingSupported() {
071: return false;
072: }
073:
074: /**
075: * Does the JDBC driver support statement batching for updates?
076: */
077: public boolean isUpdateBatchingSupported() {
078: return false;
079: }
080:
081: /**
082: * Does this driver use the ANSI join syntax (i.e. the join clauses appear
083: * in the from list e.g. postgres)?
084: */
085: public boolean isAnsiJoinSyntax() {
086: return true;
087: }
088:
089: /**
090: * Is null a valid value for a column with a foreign key constraint?
091: */
092: public boolean isNullForeignKeyOk() {
093: return true;
094: }
095:
096: /**
097: * Must 'exists (select ...)' clauses be converted into a join and
098: * distinct be added to the select (e.g. MySQL) ?
099: */
100: public boolean isConvertExistsToDistinctJoin() {
101: return true;
102: }
103:
104: /**
105: * Create a default name generator instance for JdbcStore's using this
106: * driver.
107: */
108: public JdbcNameGenerator createJdbcNameGenerator() {
109: DefaultJdbcNameGenerator n = createDefaultJdbcNameGenerator();
110: n.setMaxColumnNameLength(31);
111: n.setMaxTableNameLength(31);
112: n.setMaxConstraintNameLength(31);
113: n.setMaxTableNameLength(31);
114: return n;
115: }
116:
117: /**
118: * Append the allow nulls part of the definition for a column in a
119: * create table statement.
120: */
121: protected void appendCreateColumnNulls(JdbcTable t, JdbcColumn c,
122: CharBuf s) {
123: if (!c.nulls)
124: s.append(" not null");
125: }
126:
127: /**
128: * Add the primary key constraint part of a create table statement to s.
129: */
130: protected void appendPrimaryKeyConstraint(JdbcTable t, CharBuf s) {
131: s.append("constraint ");
132: s.append(t.pkConstraintName);
133: s.append(" primary key (");
134: appendColumnNameList(t.pk, s);
135: s.append(')');
136: }
137:
138: /**
139: * Append an 'add constraint' statement for c.
140: */
141: protected void appendRefConstraint(CharBuf s, JdbcConstraint c) {
142: s.append("alter table ");
143: s.append(c.src.name);
144: s.append(" add constraint ");
145: s.append(c.name);
146: s.append(" foreign key (");
147: appendColumnNameList(c.srcCols, s);
148: s.append(") references ");
149: s.append(c.dest.name);
150: s.append('(');
151: appendColumnNameList(c.dest.pk, s);
152: s.append(')');
153: }
154:
155: /**
156: * Write an SQL statement to a script with appropriate separator.
157: */
158: protected void print(PrintWriter out, String sql) {
159: out.print(sql);
160: out.println(";");
161: out.println();
162: }
163:
164: /**
165: * Append the from list entry for a table.
166: */
167: public void appendSqlFrom(JdbcTable table, String alias, CharBuf s) {
168: s.append(table.name);
169: if (alias != null) {
170: s.append(" AS ");
171: s.append(alias);
172: }
173: }
174:
175: /**
176: * Append the from list entry for a table that is the right hand table
177: * in a join i.e. it is being joined to.
178: * @param exp This is the expression that joins the tables
179: * @param outer If true then this is an outer join
180: */
181: public void appendSqlFromJoin(JdbcTable table, String alias,
182: SqlExp exp, boolean outer, CharBuf s) {
183: if (outer)
184: s.append(" LEFT JOIN ");
185: else
186: 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: }
|