KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > platform > database > TimesTenPlatform


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.platform.database;
23
24 import java.io.*;
25 import java.util.Calendar JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28 import oracle.toplink.essentials.expressions.ExpressionOperator;
29 import oracle.toplink.essentials.internal.databaseaccess.FieldTypeDefinition;
30 import oracle.toplink.essentials.internal.expressions.RelationExpression;
31 import oracle.toplink.essentials.internal.helper.Helper;
32 import oracle.toplink.essentials.queryframework.ValueReadQuery;
33
34 public class TimesTenPlatform extends DatabasePlatform {
35
36     //supportsForeignKeyConstraints is settable because TimesTen does not support circular referencing/self referencing
37
private boolean supportsForeignKeyConstraints;
38     
39     public TimesTenPlatform() {
40         supportsForeignKeyConstraints = true;
41     }
42
43     /**
44      * If using native SQL then print a byte[] literally as a hex string otherwise use ODBC format
45      * as provided in DatabasePlatform.
46      */

47     protected void appendByteArray(byte[] bytes, Writer writer) throws IOException {
48         if (usesNativeSQL()) {
49             writer.write("Ox");
50             Helper.writeHexString(bytes, writer);
51         } else {
52             super.appendByteArray(bytes, writer);
53         }
54     }
55
56     /**
57      * Appends an MySQL specific date if usesNativeSQL is true otherwise use the ODBC format.
58      * Native FORMAT: 'YYYY-MM-DD'
59      */

60     protected void appendDate(java.sql.Date JavaDoc date, Writer writer) throws IOException {
61         if (usesNativeSQL()) {
62             writer.write("DATE '");
63             writer.write(Helper.printDate(date));
64             writer.write("'");
65         } else {
66             super.appendDate(date, writer);
67         }
68     }
69
70     /**
71      * Appends an MySQL specific time if usesNativeSQL is true otherwise use the ODBC format.
72      * Native FORMAT: 'HH:MM:SS'.
73      */

74     protected void appendTime(java.sql.Time JavaDoc time, Writer writer) throws IOException {
75         if (usesNativeSQL()) {
76             writer.write("TIME '");
77             writer.write(Helper.printTime(time));
78             writer.write("'");
79         } else {
80             super.appendTime(time, writer);
81         }
82     }
83
84     /**
85      * Appends an MySQL specific Timestamp, if usesNativeSQL is true otherwise use the ODBC format.
86      * Native Format: 'YYYY-MM-DD HH:MM:SS'
87      */

88     protected void appendTimestamp(java.sql.Timestamp JavaDoc timestamp, Writer writer) throws IOException {
89         if (usesNativeSQL()) {
90             writer.write("TIMESTAMP '");
91             writer.write(Helper.printTimestampWithoutNanos(timestamp));
92             writer.write("'");
93         } else {
94             super.appendTimestamp(timestamp, writer);
95         }
96     }
97
98     /**
99      * Appends an MySQL specific Timestamp, if usesNativeSQL is true otherwise use the ODBC format.
100      * Native Format: 'YYYY-MM-DD HH:MM:SS'
101      */

102     protected void appendCalendar(Calendar JavaDoc calendar, Writer writer) throws IOException {
103         if (usesNativeSQL()) {
104             writer.write("TIMESTAMP '");
105             writer.write(Helper.printCalendarWithoutNanos(calendar));
106             writer.write("'");
107         } else {
108             super.appendCalendar(calendar, writer);
109         }
110     }
111
112     /**
113      * Return the mapping of class types to database types for the schema framework.
114      */

115     protected Hashtable JavaDoc buildFieldTypes() {
116         Hashtable JavaDoc fieldTypeMapping;
117
118         fieldTypeMapping = new Hashtable JavaDoc();
119         fieldTypeMapping.put(Boolean JavaDoc.class, new FieldTypeDefinition("TINYINT", false));
120
121         fieldTypeMapping.put(Integer JavaDoc.class, new FieldTypeDefinition("INTEGER", false));
122         fieldTypeMapping.put(Long JavaDoc.class, new FieldTypeDefinition("BIGINT", false));
123         fieldTypeMapping.put(Float JavaDoc.class, new FieldTypeDefinition("FLOAT", false));
124         fieldTypeMapping.put(Double JavaDoc.class, new FieldTypeDefinition("DOUBLE", false));
125         fieldTypeMapping.put(Short JavaDoc.class, new FieldTypeDefinition("SMALLINT", false));
126         fieldTypeMapping.put(Byte JavaDoc.class, new FieldTypeDefinition("TINYINT", false));
127         fieldTypeMapping.put(java.math.BigInteger JavaDoc.class, new FieldTypeDefinition("BIGINT", false));
128         fieldTypeMapping.put(java.math.BigDecimal JavaDoc.class, new FieldTypeDefinition("DECIMAL(38)", false));
129         fieldTypeMapping.put(Number JavaDoc.class, new FieldTypeDefinition("DECIMAL(38)", false));
130
131         fieldTypeMapping.put(String JavaDoc.class, new FieldTypeDefinition("VARCHAR", 255));
132         fieldTypeMapping.put(Character JavaDoc.class, new FieldTypeDefinition("CHAR", 1));
133
134         fieldTypeMapping.put(Byte JavaDoc[].class, new FieldTypeDefinition("VARBINARY", 64000));
135         fieldTypeMapping.put(Character JavaDoc[].class, new FieldTypeDefinition("VARCHAR", 64000));
136         fieldTypeMapping.put(byte[].class, new FieldTypeDefinition("VARBINARY", 64000));
137         fieldTypeMapping.put(char[].class, new FieldTypeDefinition("VARCHAR", 64000));
138         fieldTypeMapping.put(java.sql.Blob JavaDoc.class, new FieldTypeDefinition("VARBINARY", 64000));
139         fieldTypeMapping.put(java.sql.Clob JavaDoc.class, new FieldTypeDefinition("VARCHAR", 64000));
140         
141         fieldTypeMapping.put(java.sql.Date JavaDoc.class, new FieldTypeDefinition("DATE", false));
142         fieldTypeMapping.put(java.sql.Time JavaDoc.class, new FieldTypeDefinition("TIME", false));
143         fieldTypeMapping.put(java.sql.Timestamp JavaDoc.class, new FieldTypeDefinition("TIMESTAMP", false));
144
145         return fieldTypeMapping;
146     }
147
148     /**
149      * INTERNAL:
150      * Produce a DataReadQuery which updates(!) the sequence number in the db
151      * and returns it.
152      * @param sequenceName Name known by TimesTen to be a defined sequence
153      */

154     public ValueReadQuery buildSelectQueryForNativeSequence(String JavaDoc seqName, Integer JavaDoc size) {
155         return new ValueReadQuery("SELECT " + getQualifiedSequenceName(seqName) + ".NEXTVAL FROM DUAL");
156     }
157
158     /**
159      * INTERNAL:
160      * Used for view creation.
161      */

162     public String JavaDoc getCreateViewString() {
163         return "CREATE MATERIALIZED VIEW ";
164     }
165     
166     /**
167      * Prepend sequence name with table qualifier (if any)
168      */

169     protected String JavaDoc getQualifiedSequenceName(String JavaDoc seqName) {
170         if (getTableQualifier().equals("")) {
171             return seqName;
172         } else {
173             return getTableQualifier() + "." + seqName;
174         }
175     }
176
177     /**
178      * INTERNAL:
179      * Used for pessimistic locking.
180      */

181     public String JavaDoc getSelectForUpdateString() {
182         return " FOR UPDATE";
183     }
184     
185     /**
186      * PUBLIC:
187      * This method returns the query to select the timestamp
188      * from the server for TimesTen.
189      */

190     public ValueReadQuery getTimestampQuery() {
191         if (timestampQuery == null) {
192             timestampQuery = new ValueReadQuery();
193             timestampQuery.setSQLString("SELECT SYSDATE FROM DUAL");
194         }
195         return timestampQuery;
196     }
197
198     /**
199      * Initialize any platform-specific operators
200      */

201     protected void initializePlatformOperators() {
202         super.initializePlatformOperators();
203         addOperator(ExpressionOperator.simpleTwoArgumentFunction(ExpressionOperator.Concat, "CONCAT"));
204         addOperator(operatorOuterJoin());
205         addOperator(ExpressionOperator.ifNull());
206     }
207
208     /**
209      * Answers whether platform is TimesTen
210      */

211     public boolean isTimesTen() {
212         return true;
213     }
214
215     /**
216      * Create the outer join operator for this platform
217      */

218     protected ExpressionOperator operatorOuterJoin() {
219         ExpressionOperator result = new ExpressionOperator();
220         result.setSelector(ExpressionOperator.EqualOuterJoin);
221         Vector JavaDoc v = new Vector JavaDoc(2);
222         v.addElement(" (+) = ");
223         result.printsAs(v);
224         result.bePostfix();
225         result.setNodeClass(RelationExpression.class);
226         return result;
227     }
228
229     /**
230      * Some database require outer joins to be given in the where clause, others require it in the from clause.
231      */

232     public boolean shouldPrintOuterJoinInWhereClause() {
233         return true;
234     }
235
236     /**
237      * Return true if the receiver uses host sequence numbers, generated on the database.
238      * TimesTen does through global sequence objects.
239      */

240     public boolean supportsNativeSequenceNumbers() {
241         return true;
242     }
243
244     public boolean supportsForeignKeyConstraints() {
245         return supportsForeignKeyConstraints;
246     }
247
248     public void setSupportsForeignKeyConstraints(boolean supportsForeignKeyConstraints) {
249         this.supportsForeignKeyConstraints = supportsForeignKeyConstraints;
250     }
251 }
Popular Tags