KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > database > DBVendorType


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 in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
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 Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.spi.persistence.support.sqlstore.database;
26
27 import com.sun.jdo.api.persistence.support.JDOFatalInternalException;
28 import com.sun.jdo.api.persistence.support.JDOFatalUserException;
29 import com.sun.jdo.api.persistence.support.JDOUserException;
30 import com.sun.jdo.api.persistence.support.SpecialDBOperation;
31 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperSQLStore;
32 import com.sun.jdo.spi.persistence.utility.FieldTypeEnumeration;
33 import com.sun.jdo.spi.persistence.utility.I18NHelper;
34 import com.sun.jdo.spi.persistence.utility.PropertyHelper;
35 import com.sun.jdo.spi.persistence.utility.database.DBVendorTypeHelper;
36 import com.sun.jdo.spi.persistence.utility.logging.Logger;
37
38 import java.io.IOException JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.DatabaseMetaData JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.Properties JavaDoc;
43 import java.util.ResourceBundle JavaDoc;
44
45
46
47 /**
48  */

49 public class DBVendorType {
50     /**
51      * Map from property name to property value.
52      */

53     private HashMap JavaDoc dbMap;
54
55     /**
56      * Instance of specialDBOperation for this vendor type. Please look at
57      * newSpecialDBOperationInstance() to find out how this member is
58      * initialized.
59      */

60     private SpecialDBOperation specialDBOperation;
61
62     private static final SpecialDBOperation DEFAULT_SPECIAL_DB_OPERATION =
63         new BaseSpecialDBOperation();
64
65     /**
66      * VendorType as returned from {@link DBVendorTypeHelper#getDBType(java.lang.String)}
67      */

68     private String JavaDoc vendorType;
69
70     /**
71      * VendorType as returned from {@link DBVendorTypeHelper#getEnumDBType(java.lang.String)}
72      */

73     private int enumVendorType;
74
75     /**
76      * The logger
77      */

78     private final static Logger logger;
79
80     /**
81      * I18N message handler
82      */

83     private final static ResourceBundle JavaDoc messages;
84
85     /**
86      * Default properties
87      */

88     private static Properties JavaDoc defaultProperties;
89
90     private final static String JavaDoc EXT = ".properties"; // NOI18N
91
private final static String JavaDoc SPACE = " "; // NOI18N
92
private final static String JavaDoc NONE = ""; // NOI18N
93

94     private final static String JavaDoc PATH = "com/sun/jdo/spi/persistence/support/sqlstore/database/"; // NOI18N
95
private final static String JavaDoc PROPERTY_OVERRIDE_FILE = ".tpersistence.properties"; // NOI18N
96

97
98     private final static String JavaDoc FOR_UPDATE = "FOR_UPDATE"; // NOI18N
99
private final static String JavaDoc HOLDLOCK = "HOLDLOCK"; //NOI18N
100
private final static String JavaDoc SUPPORTS_UPDATE_LOCK = "SUPPORTS_UPDATE_LOCK"; //NOI18N
101
private final static String JavaDoc SUPPORTS_LOCK_COLUMN_LIST = "SUPPORTS_LOCK_COLUMN_LIST"; //NOI18N
102
private final static String JavaDoc SUPPORTS_DISTINCT_WITH_UPDATE_LOCK = "SUPPORTS_DISTINCT_WITH_UPDATE_LOCK"; //NOI18N
103
private final static String JavaDoc NATIVE_OUTER_JOIN = "NATIVE_OUTER_JOIN"; //NOI18N
104
private final static String JavaDoc LEFT_JOIN = "LEFT_JOIN"; //NOI18N
105
private final static String JavaDoc LEFT_JOIN_APPEND = "LEFT_JOIN_APPEND"; //NOI18N
106
private final static String JavaDoc RIGHT_JOIN = "RIGHT_JOIN"; //NOI18N
107
private final static String JavaDoc RIGHT_JOIN_PRE = "RIGHT_JOIN_PRE"; //NOI18N
108
private final static String JavaDoc IS_NULL = "IS_NULL"; //NOI18N
109
private final static String JavaDoc IS_NOT_NULL = "IS_NOT_NULL"; //NOI18N
110
private final static String JavaDoc ANSI_TRIM = "ANSI_TRIM"; //NOI18N
111
private final static String JavaDoc RTRIM = "RTRIM"; //NOI18N
112
private final static String JavaDoc RTRIM_POST = "RTRIM_POST"; //NOI18N
113
private final static String JavaDoc TABLE_LIST_START = "TABLE_LIST_START"; //NOI18N
114
private final static String JavaDoc TABLE_LIST_END = "TABLE_LIST_END"; //NOI18N
115
private final static String JavaDoc STRING_CONCAT = "STRING_CONCAT"; //NOI18N
116
private final static String JavaDoc QUOTE_CHAR_START = "QUOTE_CHAR_START"; //NOI18N
117
private final static String JavaDoc QUOTE_CHAR_END = "QUOTE_CHAR_END"; //NOI18N
118
private final static String JavaDoc QUOTE_SPECIAL_ONLY = "QUOTE_SPECIAL_ONLY"; //NOI18N
119
private final static String JavaDoc CHAR_LENGTH = "CHAR_LENGTH"; //NOI18N
120
private final static String JavaDoc SQRT = "SQRT"; //NOI18N
121
private final static String JavaDoc ABS = "ABS"; //NOI18N
122
private final static String JavaDoc SUBSTRING = "SUBSTRING"; //NOI18N
123
private final static String JavaDoc SUBSTRING_FROM = "SUBSTRING_FROM"; //NOI18N
124
private final static String JavaDoc SUBSTRING_FOR = "SUBSTRING_FOR"; //NOI18N
125
private final static String JavaDoc POSITION = "POSITION"; //NOI18N
126
private final static String JavaDoc POSITION_SEP = "POSITION_SEP"; //NOI18N
127
private final static String JavaDoc POSITION_SEARCH_SOURCE = "POSITION_SEARCH_SOURCE"; //NOI18N
128
private final static String JavaDoc POSITION_THREE_ARGS = "POSITION_THREE_ARGS"; //NOI18N
129
private final static String JavaDoc MAP_EMPTY_STRING_TO_NULL = "MAP_EMPTY_STRING_TO_NULL"; //NOI18N
130
private final static String JavaDoc SPECIAL_DB_OPERATION = "SPECIAL_DB_OPERATION"; //NOI18N
131
private final static String JavaDoc SUPPORTS_LIKE_ESCAPE = "SUPPORTS_LIKE_ESCAPE"; //NOI18N
132
private final static String JavaDoc LEFT_LIKE_ESCAPE = "LEFT_LIKE_ESCAPE"; //NOI18N
133
private final static String JavaDoc RIGHT_LIKE_ESCAPE = "RIGHT_LIKE_ESCAPE"; //NOI18N
134
private final static String JavaDoc NULL_COMPARISON_FUNCTION_NAME = "NULL_COMPARISON_FUNCTION_NAME"; //NOI18N
135
private final static String JavaDoc MOD_FUNCTION_NAME = "MOD_FUNCTION_NAME"; //NOI18N
136
private final static String JavaDoc CONCAT_CAST = "CONCAT_CAST"; //NOI18N
137
private final static String JavaDoc PARAMETER_CAST = "PARAMETER_CAST"; //NOI18N
138
private final static String JavaDoc INLINE_NUMERIC = "INLINE_NUMERIC"; //NOI18N
139

140     private static final String JavaDoc[] props = new String JavaDoc[] { FOR_UPDATE,
141             HOLDLOCK, SUPPORTS_UPDATE_LOCK, SUPPORTS_LOCK_COLUMN_LIST,
142             SUPPORTS_DISTINCT_WITH_UPDATE_LOCK,
143             NATIVE_OUTER_JOIN, LEFT_JOIN, LEFT_JOIN_APPEND, RIGHT_JOIN, RIGHT_JOIN_PRE,
144             IS_NULL, IS_NOT_NULL, ANSI_TRIM, RTRIM, RTRIM_POST,
145             TABLE_LIST_START, TABLE_LIST_END,
146             QUOTE_CHAR_START, QUOTE_CHAR_END, QUOTE_SPECIAL_ONLY,
147             STRING_CONCAT, CHAR_LENGTH, SQRT, ABS,
148             SUBSTRING, SUBSTRING_FROM, SUBSTRING_FOR,
149             POSITION, POSITION_SEP, POSITION_SEARCH_SOURCE, POSITION_THREE_ARGS,
150             MAP_EMPTY_STRING_TO_NULL, SPECIAL_DB_OPERATION,
151             SUPPORTS_LIKE_ESCAPE, LEFT_LIKE_ESCAPE, RIGHT_LIKE_ESCAPE,
152             NULL_COMPARISON_FUNCTION_NAME, MOD_FUNCTION_NAME,
153             CONCAT_CAST, PARAMETER_CAST, INLINE_NUMERIC
154     };
155
156     /**
157      * Initialize static fields.
158      */

159     static {
160         logger = LogHelperSQLStore.getLogger();
161         messages = I18NHelper.loadBundle(
162             "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
163
DBVendorType.class.getClassLoader());
164
165         defaultProperties = initializeDefaultProperties();
166     }
167
168     /**
169      * @param databaseMetaData Instance of DatabaseMetaData
170      * @param identifier identifier of the caller creating a new instance
171      * of SQLStoreManager.
172      */

173     public DBVendorType(DatabaseMetaData JavaDoc databaseMetaData, String JavaDoc identifier)
174         throws SQLException JavaDoc {
175
176         String JavaDoc vendorName = databaseMetaData.getDatabaseProductName();
177         String JavaDoc vendorType = DBVendorTypeHelper.getDBType(vendorName);
178
179         if (logger.isLoggable()) {
180             Object JavaDoc[] items = new Object JavaDoc[] {vendorName,vendorType};
181             logger.fine("sqlstore.database.dbvendor.vendorname", items); // NOI18N
182
}
183
184         this.vendorType = vendorType;
185         enumVendorType = DBVendorTypeHelper.getEnumDBType(vendorType);
186         dbMap = getDBPropertiesMap(vendorType,vendorName);
187         specialDBOperation = newSpecialDBOperationInstance((String JavaDoc)dbMap.get(SPECIAL_DB_OPERATION),
188                                 databaseMetaData, identifier);
189     }
190
191     /**
192      * get properties map for given vendorType and vendorName
193      */

194     private static HashMap JavaDoc getDBPropertiesMap(String JavaDoc vendorType, String JavaDoc vendorName) {
195         //Initialize returned map to default
196
HashMap JavaDoc dbHashMap = new HashMap JavaDoc(defaultProperties);
197         Properties JavaDoc dbProperties = loadDBProperties(vendorType, vendorName);
198         dbHashMap.putAll(dbProperties);
199
200         return dbHashMap;
201     }
202
203     /**
204      * Initialize default properties.
205      */

206     private static Properties JavaDoc initializeDefaultProperties() {
207         //Load default properties if not already loaded
208
if (defaultProperties == null) {
209             // Load default (sql92) Properties
210
defaultProperties = new Properties JavaDoc();
211             try {
212                 loadFromResource(DBVendorTypeHelper.DEFAULT_DB, defaultProperties);
213             } catch (IOException JavaDoc e) {
214                 throw new JDOFatalInternalException(I18NHelper.getMessage(messages,
215                                 "sqlstore.database.dbvendor.cantloadDefaultProperties"), // NOI18N
216
e);
217             }
218         }
219
220         return defaultProperties;
221      }
222
223     /**
224      * Load properties for database specified by vendorType and vendorName
225      */

226     private static Properties JavaDoc loadDBProperties(String JavaDoc vendorType, String JavaDoc vendorName) {
227         // Load actual Properties. Even if it is unknown db,
228
Properties JavaDoc dbProperties = new Properties JavaDoc();
229         if (!vendorType.equals(DBVendorTypeHelper.DEFAULT_DB)) {
230             try {
231                 loadFromResource(vendorType, dbProperties);
232             } catch (IOException JavaDoc e) {
233                 // else ignore
234
if (logger.isLoggable()) {
235                     logger.fine("sqlstore.database.dbvendor.init.default", vendorType); // NOI18N
236
}
237             }
238         }
239         overrideProperties(dbProperties, vendorName);
240         return dbProperties;
241     }
242
243     /**
244      * Overrides default properties with the ones provided
245      * by the user
246      */

247     private static void overrideProperties(Properties JavaDoc dbProperties, String JavaDoc vendorName) {
248         boolean debug = logger.isLoggable();
249
250         Properties JavaDoc overridingProperties = new Properties JavaDoc();
251         try {
252             PropertyHelper.loadFromFile(overridingProperties, PROPERTY_OVERRIDE_FILE);
253         } catch (Exception JavaDoc e) {
254             if (debug) {
255                 logger.fine("sqlstore.database.dbvendor.overrideproperties"); // NOI18N
256
}
257             return; // nothing to override
258
}
259
260         //Prepare a clean vendor name by replacing all ' ' and '/' with underscores.
261
String JavaDoc cleanVendorName = vendorName.toLowerCase().replace(' ', '_');
262         cleanVendorName = cleanVendorName.replace('/', '_');
263
264         String JavaDoc propertyPrefix = "database." + cleanVendorName + "."; // prefix // NOI18N
265

266         for (int i = 0; i < props.length; i++) {
267             String JavaDoc o = overridingProperties.getProperty(propertyPrefix + props[i]);
268             if (o != null) {
269                 if (debug) {
270                     Object JavaDoc[] items = new Object JavaDoc[] {props[i],o};
271                     logger.fine("sqlstore.database.dbvendor.overrideproperties.with", items); // NOI18N
272
}
273                 dbProperties.setProperty(props[i], o);
274             }
275         }
276     }
277
278     /**
279      * loads database properties list from the specified resource
280      * into specified Properties object.
281      * @param resourceName Name of resource.
282      * @param properties Properties object to load
283      */

284     private static void loadFromResource(String JavaDoc resourceName, Properties JavaDoc properties)
285             throws IOException JavaDoc {
286         String JavaDoc fullResourceName = PATH + resourceName + EXT;
287         PropertyHelper.loadFromResource(properties, fullResourceName, DBVendorType.class.getClassLoader());
288     }
289
290     /**
291      * Returns new instance of class specified by specialDBOpClassName.
292      * The class is required to implement interface SpecialDBOperation.
293      * If specialDBOpClassName is null or cannot be loaded, then an instance
294      * of BaseSpecialDBOperation is returned.
295      * @param specialDBOpClassName Name of a class that implements
296      * SpecialDBOperation
297      * @param databaseMetaData DatabaseMetaData for the connection for which
298      * this SpecialDBOperation is created
299      * @param identifier Identifier of pmf used to obtain databaseMetaData.
300      * This can be null in non managed environment.
301      * @return An instance of SpecialDBOperation specified by specialDBOpClassName.
302      */

303     private SpecialDBOperation newSpecialDBOperationInstance(
304         final String JavaDoc specialDBOpClassName, DatabaseMetaData JavaDoc databaseMetaData,
305         String JavaDoc identifier) {
306         SpecialDBOperation retInstance = null;
307         if (specialDBOpClassName != null) {
308             final ClassLoader JavaDoc loader = DBVendorType.class.getClassLoader();
309             Class JavaDoc clz = (Class JavaDoc)java.security.AccessController.doPrivileged(
310                 new java.security.PrivilegedAction JavaDoc() {
311                     public Object JavaDoc run() {
312                         try {
313                             if (loader != null) {
314                                 return Class.forName(specialDBOpClassName,
315                                     true, loader);
316                             } else {
317                                 return Class.forName(specialDBOpClassName);
318                             }
319                         } catch(Exception JavaDoc ex) {
320                             if (logger.isLoggable()) {
321                                 logger.log(Logger.INFO,
322                                     "core.configuration.cantloadclass", // NOI18N
323
specialDBOpClassName);
324                             }
325                             return null;
326                         }
327                     }
328                 }
329             );
330
331             if (clz != null) {
332                 try {
333                     retInstance = (SpecialDBOperation)clz.newInstance();
334                     retInstance.initialize(databaseMetaData, identifier);
335                 } catch(Exception JavaDoc ex) {
336                     throw new JDOFatalUserException(
337                         I18NHelper.getMessage(messages,
338                         "sqlstore.database.dbvendor.cantinstantiateclass", // NOI18N
339
specialDBOpClassName), ex);
340                 }
341             }
342         }
343         return (retInstance != null)? retInstance : DEFAULT_SPECIAL_DB_OPERATION;
344     }
345
346     /**
347      * Returns the string that represents "left join" clause
348      * for this database
349      */

350     public String JavaDoc getLeftJoin() {
351         String JavaDoc s = (String JavaDoc)dbMap.get(LEFT_JOIN);
352         if (s == null)
353             s = NONE;
354
355         if (logger.isLoggable()) {
356             logger.fine("sqlstore.database.dbvendor.getleftjoin", s); // NOI18N
357
}
358
359         return SPACE + s;
360     }
361
362
363     /**
364      * Returns true if this database supports update lock
365      */

366     public boolean isUpdateLockSupported() {
367         String JavaDoc s = (String JavaDoc)dbMap.get(SUPPORTS_UPDATE_LOCK);
368         Boolean JavaDoc b = Boolean.valueOf(s);
369
370         if (logger.isLoggable()) {
371             logger.fine("sqlstore.database.dbvendor.isupdatelocksupported", b); // NOI18N
372
}
373
374         return b.booleanValue();
375     }
376
377     /**
378      * Returns true if this database supports update 'of column list'
379      */

380     public boolean isLockColumnListSupported() {
381         String JavaDoc s = (String JavaDoc)dbMap.get(SUPPORTS_LOCK_COLUMN_LIST);
382         Boolean JavaDoc b = Boolean.valueOf(s);
383
384         if (logger.isLoggable()) {
385             logger.fine("sqlstore.database.dbvendor.islockcolumnlistsupported", b); // NOI18N
386
}
387
388         return b.booleanValue();
389     }
390
391     /**
392      * Returns true if this database supports distinct clause with update lock
393      */

394     public boolean isDistinctSupportedWithUpdateLock() {
395         String JavaDoc s = (String JavaDoc)dbMap.get(SUPPORTS_DISTINCT_WITH_UPDATE_LOCK);
396         Boolean JavaDoc b = Boolean.valueOf(s);
397
398         if (logger.isLoggable()) {
399             logger.fine("sqlstore.database.dbvendor.isdistinctupdatelocksupported", b); // NOI18N
400
}
401
402         return b.booleanValue();
403     }
404
405
406     /**
407      * Returns the string that represents "holdlock" clause
408      * for this database
409      */

410     public String JavaDoc getHoldlock() {
411         String JavaDoc s = (String JavaDoc)dbMap.get(HOLDLOCK);
412         if (s == null)
413             s = NONE;
414
415         if (logger.isLoggable()) {
416             logger.fine("sqlstore.database.dbvendor.getholdlock", s); // NOI18N
417
}
418
419         return SPACE + s;
420     }
421
422     /**
423      * Returns true if the this database needs native outer join semantics.
424      */

425     public boolean isNativeOuterJoin() {
426         String JavaDoc s = (String JavaDoc)dbMap.get(NATIVE_OUTER_JOIN);
427         Boolean JavaDoc b = Boolean.valueOf(s);
428
429         if (logger.isLoggable()) {
430             logger.fine("sqlstore.database.dbvendor.isNativeOuterJoin", b); // NOI18N
431
}
432
433         return b.booleanValue();
434     }
435
436     /**
437      * Returns the string that represents postfix for "left join" clause
438      * for this database
439      */

440     public String JavaDoc getLeftJoinPost() {
441         String JavaDoc s = (String JavaDoc)dbMap.get(LEFT_JOIN_APPEND);
442         if (s == null)
443             s = NONE;
444
445         if (logger.isLoggable()) {
446             logger.fine("sqlstore.database.dbvendor.getleftjoinpost", s); // NOI18N
447
}
448
449         return SPACE + s;
450     }
451
452     /**
453      * Returns the string that represents "right join" clause
454      * for this database
455      */

456     public String JavaDoc getRightJoin() {
457         String JavaDoc s = (String JavaDoc)dbMap.get(RIGHT_JOIN);
458         if (s == null)
459             s = NONE;
460
461         if (logger.isLoggable()) {
462             logger.fine("sqlstore.database.dbvendor.getrightjoin", s); // NOI18N
463
}
464
465         return SPACE + s;
466     }
467
468     /**
469      * Returns the string that represents prefix for "right join" clause
470      * for this database
471      */

472     public String JavaDoc getRightJoinPre() {
473         String JavaDoc s = (String JavaDoc)dbMap.get(RIGHT_JOIN_PRE);
474         if (s == null)
475             s = NONE;
476
477         if (logger.isLoggable()) {
478             logger.fine("sqlstore.database.dbvendor.getrightjoinipre", s); // NOI18N
479
}
480
481         return SPACE + s;
482     }
483
484     /**
485      * Returns the string that represents "is NULL" clause
486      * for this database
487      */

488     public String JavaDoc getIsNull() {
489         String JavaDoc s = (String JavaDoc)dbMap.get(IS_NULL);
490         if (s == null)
491             s = NONE;
492
493         if (logger.isLoggable()) {
494             logger.fine("sqlstore.database.dbvendor.getisnull", s); // NOI18N
495
}
496
497         return SPACE + s;
498     }
499
500     /**
501      * Returns the string that represents "is not NULL" clause
502      * for this database
503      */

504     public String JavaDoc getIsNotNull() {
505         String JavaDoc s = (String JavaDoc)dbMap.get(IS_NOT_NULL);
506         if (s == null)
507             s = NONE;
508
509         if (logger.isLoggable()) {
510             logger.fine("sqlstore.database.dbvendor.getisnotnull", s); // NOI18N
511
}
512
513         return SPACE + s;
514     }
515
516     /**
517      * Returns true if this database need ansi style rtrim semantics.
518      */

519     public boolean isAnsiTrim() {
520         String JavaDoc s = (String JavaDoc)dbMap.get(ANSI_TRIM);
521         Boolean JavaDoc b = Boolean.valueOf(s);
522
523         if (logger.isLoggable()) {
524             logger.fine("sqlstore.database.dbvendor.isAnsiTrim", b); // NOI18N
525
}
526
527         return b.booleanValue();
528     }
529
530     /**
531      * Returns the string that represents "RTRIM" clause
532      * for this database
533      */

534     public String JavaDoc getRtrim() {
535         String JavaDoc s = (String JavaDoc)dbMap.get(RTRIM);
536         if (s == null)
537             s = NONE;
538
539         if (logger.isLoggable()) {
540             logger.fine("sqlstore.database.dbvendor.getrtrim", s); // NOI18N
541
}
542
543         return SPACE + s;
544     }
545
546     /**
547      * Returns the string that represents postfix for "RTRIM" clause
548      * for this database
549      */

550     public String JavaDoc getRtrimPost() {
551         String JavaDoc s = (String JavaDoc)dbMap.get(RTRIM_POST);
552         if (s == null)
553             s = NONE;
554
555         if (logger.isLoggable()) {
556             logger.fine("sqlstore.database.dbvendor.getrtrimpost", s); // NOI18N
557
}
558
559         return SPACE + s;
560     }
561
562     /**
563      * Returns the string that represents prefix for "Char_length" clause
564      * for this database
565      */

566     public String JavaDoc getCharLength() {
567         String JavaDoc s = (String JavaDoc)dbMap.get(CHAR_LENGTH);
568         if (s == null)
569             s = NONE;
570
571         if (logger.isLoggable()) {
572             logger.fine("sqlstore.database.dbvendor.getcharlength", s); // NOI18N
573
}
574
575         return SPACE + s;
576     }
577
578     /**
579      * Returns the string that represents prefix for "Sqrt" clause
580      * for this database
581      */

582     public String JavaDoc getSqrt() {
583         String JavaDoc s = (String JavaDoc)dbMap.get(SQRT);
584         if (s == null) {
585            throw new JDOUserException(I18NHelper.getMessage(messages,
586                "core.constraint.illegalop", // NOI18N
587
"Sqrt"));
588         }
589
590         if (logger.isLoggable()) {
591             logger.fine("sqlstore.database.dbvendor.getsqrt", s); // NOI18N
592
}
593
594         return SPACE + s;
595     }
596
597     /**
598      * Returns the string that represents prefix for "abs" clause
599      * for this database
600      */

601     public String JavaDoc getAbs() {
602         String JavaDoc s = (String JavaDoc)dbMap.get(ABS);
603         if (s == null) {
604            throw new JDOUserException(I18NHelper.getMessage(messages,
605                "core.constraint.illegalop", // NOI18N
606
"Abs"));
607         }
608
609         if (logger.isLoggable()) {
610             logger.fine("sqlstore.database.dbvendor.getabs", s); // NOI18N
611
}
612
613         return SPACE + s;
614     }
615
616     /**
617      * Returns the string that represents "for update" clause
618      * for this database
619      */

620     public String JavaDoc getForUpdate() {
621         String JavaDoc s = (String JavaDoc)dbMap.get(FOR_UPDATE);
622         if (s == null)
623             s = NONE;
624
625         if (logger.isLoggable()) {
626             logger.fine("sqlstore.database.dbvendor.getforupdate", s); // NOI18N
627
}
628
629         return SPACE + s;
630     }
631
632     /**
633      * Returns the string that represents start of table list
634      * for this database
635      */

636     public String JavaDoc getTableListStart() {
637         String JavaDoc s = (String JavaDoc)dbMap.get(TABLE_LIST_START);
638         if (s == null)
639             s = NONE;
640
641         if (logger.isLoggable()) {
642             logger.fine("sqlstore.database.dbvendor.gettableliststart", s); // NOI18N
643
}
644
645         return SPACE + s;
646     }
647
648     /**
649      * Returns the string that represents end of table list
650      * for this database
651      */

652     public String JavaDoc getTableListEnd() {
653         String JavaDoc s = (String JavaDoc)dbMap.get(TABLE_LIST_END);
654         if (s == null)
655             s = NONE;
656
657         if (logger.isLoggable()) {
658             logger.fine("sqlstore.database.dbvendor.gettablelistend", s); // NOI18N
659
}
660
661         return SPACE + s;
662     }
663
664     /**
665      * Returns the string that represents concatenation operation
666      * for this database
667      */

668     public String JavaDoc getStringConcat() {
669         String JavaDoc s = (String JavaDoc)dbMap.get(STRING_CONCAT);
670         if (s == null)
671             s = NONE;
672
673         if (logger.isLoggable()) {
674             logger.fine("sqlstore.database.dbvendor.getstringconcat", s); // NOI18N
675
}
676
677         return SPACE + s + SPACE;
678     }
679
680     /**
681      * Returns the start identifier quote character for this database, or
682      * an empty string, if there is none.
683      */

684     public String JavaDoc getQuoteCharStart() {
685         String JavaDoc s = (String JavaDoc)dbMap.get(QUOTE_CHAR_START);
686         if (s == null)
687             s = NONE;
688
689         if (logger.isLoggable()) {
690             logger.fine("sqlstore.database.dbvendor.getquotecharstart", s); // NOI18N
691
}
692
693         return s;
694     }
695
696     /**
697      * Returns the end identifier quote character for this database, or
698      * an empty string, if there is none.
699      */

700     public String JavaDoc getQuoteCharEnd() {
701         String JavaDoc s = (String JavaDoc)dbMap.get(QUOTE_CHAR_END);
702         if (s == null)
703             s = NONE;
704
705         if (logger.isLoggable()) {
706             logger.fine("sqlstore.database.dbvendor.getquotecharend", s); // NOI18N
707
}
708
709         return s;
710     }
711
712     /**
713      * Returns true if only identifiers with special characters should be quoted
714      * for this database
715      */

716     public boolean getQuoteSpecialOnly() {
717         String JavaDoc s = (String JavaDoc)dbMap.get(QUOTE_SPECIAL_ONLY);
718         Boolean JavaDoc b = Boolean.valueOf(s);
719
720         if (logger.isLoggable()) {
721             logger.fine("sqlstore.database.dbvendor.getquotespecialonly", b); // NOI18N
722
}
723
724         return b.booleanValue();
725     }
726
727     /**
728      * Returns the string that represents prefix for "Substring" clause
729      * for this database
730      */

731     public String JavaDoc getSubstring() {
732         String JavaDoc s = (String JavaDoc)dbMap.get(SUBSTRING);
733         if (s == null) {
734            throw new JDOUserException(I18NHelper.getMessage(messages,
735                "core.constraint.illegalop", // NOI18N
736
"substring"));
737         }
738
739         if (logger.isLoggable()) {
740             logger.fine("sqlstore.database.dbvendor.getsubstring", s); // NOI18N
741
}
742
743         return SPACE + s;
744     }
745
746     /**
747      * Returns the string that represents from part for "Substring" clause
748      * for this database
749      */

750     public String JavaDoc getSubstringFrom() {
751         String JavaDoc s = (String JavaDoc)dbMap.get(SUBSTRING_FROM);
752         if (s == null) {
753            throw new JDOUserException(I18NHelper.getMessage(messages,
754                "core.constraint.illegalop", // NOI18N
755
"from part of substring"));
756         }
757
758         if (logger.isLoggable()) {
759             logger.fine("sqlstore.database.dbvendor.getsubstringfrom", s); // NOI18N
760
}
761
762         return SPACE + s + SPACE;
763     }
764
765     /**
766      * Returns the string that represents for part for "Substr" clause
767      * for this database
768      */

769     public String JavaDoc getSubstringFor() {
770         String JavaDoc s = (String JavaDoc)dbMap.get(SUBSTRING_FOR);
771         if (s == null) {
772            throw new JDOUserException(I18NHelper.getMessage(messages,
773                "core.constraint.illegalop", // NOI18N
774
"for part of substring"));
775         }
776
777         if (logger.isLoggable()) {
778             logger.fine("sqlstore.database.dbvendor.getsubstringfor", s); // NOI18N
779
}
780
781         return SPACE + s + SPACE;
782     }
783
784     /**
785      * Returns the string that represents "Position" clause
786      * for this database
787      */

788     public String JavaDoc getPosition() {
789         String JavaDoc s = (String JavaDoc)dbMap.get(POSITION);
790         if (s == null) {
791            throw new JDOUserException(I18NHelper.getMessage(messages,
792                "core.constraint.illegalop", // NOI18N
793
"position"));
794         }
795
796         if (logger.isLoggable()) {
797             logger.fine("sqlstore.database.dbvendor.getposition", s); // NOI18N
798
}
799
800         return SPACE + s;
801     }
802
803     /**
804      * Returns the string that represents separator part of "Position" clause
805      * for this database
806      */

807     public String JavaDoc getPositionSep() {
808         String JavaDoc s = (String JavaDoc)dbMap.get(POSITION_SEP);
809         if (s == null) {
810            throw new JDOUserException(I18NHelper.getMessage(messages,
811                "core.constraint.illegalop", // NOI18N
812
"in part of position"));
813         }
814
815         if (logger.isLoggable()) {
816             logger.fine("sqlstore.database.dbvendor.getpositionin", s); // NOI18N
817
}
818
819         return SPACE + s;
820     }
821
822     /**
823      * Returns true if first position argument is Search String and second
824      * argument is Source String for this database
825      */

826     public boolean isPositionSearchSource() {
827         String JavaDoc s = (String JavaDoc)dbMap.get(POSITION_SEARCH_SOURCE);
828         Boolean JavaDoc b = Boolean.valueOf(s);
829
830         if (logger.isLoggable()) {
831             logger.fine("sqlstore.database.dbvendor.getpositionsrchsrc", b); // NOI18N
832
}
833
834         return b.booleanValue();
835     }
836
837     /**
838      * Returns true if position has three argument for this database
839      */

840     public boolean isPositionThreeArgs() {
841         String JavaDoc s = (String JavaDoc)dbMap.get(POSITION_THREE_ARGS);
842         Boolean JavaDoc b = Boolean.valueOf(s);
843
844         if (logger.isLoggable()) {
845             logger.fine("sqlstore.database.dbvendor.getposition3args", b); // NOI18N
846
}
847
848         return b.booleanValue();
849     }
850
851     /**
852      * Returns true if this database maps empty Strings to NULL
853      */

854     public boolean mapEmptyStringToNull() {
855         String JavaDoc s = (String JavaDoc)dbMap.get(MAP_EMPTY_STRING_TO_NULL);
856         Boolean JavaDoc b = Boolean.valueOf(s);
857
858         if (logger.isLoggable()) {
859             logger.fine("sqlstore.database.dbvendor.mapemptystrtonull", b); // NOI18N
860
}
861
862         return b.booleanValue();
863     }
864
865     /**
866      * Returns true if this database supports "LIKE ESCAPE" clause
867      */

868     public boolean supportsLikeEscape() {
869         String JavaDoc s = (String JavaDoc)dbMap.get(SUPPORTS_LIKE_ESCAPE);
870         Boolean JavaDoc b = Boolean.valueOf(s);
871
872         if (logger.isLoggable()) {
873             logger.fine("sqlstore.database.dbvendor.supportslikeescape", b); // NOI18N
874
}
875
876         return b.booleanValue();
877     }
878
879     /**
880      * Returns left string that represents "LIKE ESCAPE" clause
881      * for this database
882      */

883     public String JavaDoc getLeftLikeEscape() {
884         String JavaDoc s = (String JavaDoc)dbMap.get(LEFT_LIKE_ESCAPE);
885         if (s == null)
886             s = NONE;
887
888         if (logger.isLoggable()) {
889             logger.fine("sqlstore.database.dbvendor.getleftlikeescape", s); // NOI18N
890
}
891
892         return SPACE + s;
893     }
894
895     /**
896      * Returns right string that represents "LIKE ESCAPE" clause
897      * for this database
898      */

899     public String JavaDoc getRightLikeEscape() {
900         String JavaDoc s = (String JavaDoc)dbMap.get(RIGHT_LIKE_ESCAPE);
901         if (s == null)
902             s = NONE;
903
904         if (logger.isLoggable()) {
905             logger.fine("sqlstore.database.dbvendor.getrightlikeescape", s); // NOI18N
906
}
907
908         return SPACE + s;
909     }
910
911     /**
912      * Returns function name for comparing null value for this database
913      */

914     public String JavaDoc getNullComparisonFunctionName() {
915         String JavaDoc s = (String JavaDoc)dbMap.get(NULL_COMPARISON_FUNCTION_NAME);
916         if (s == null)
917             s = NONE;
918         else
919             s = s.trim();
920         if (logger.isLoggable()) {
921             logger.fine("sqlstore.database.dbvendor.getNullComparisonFunctionName", s); // NOI18N
922
}
923         return s;
924     }
925
926
927     /**
928      * Returns true if modulo operation uses function, false otherwise.
929      */

930     public boolean isModOperationUsingFunction() {
931         return getModFunctionName().length() != 0;
932     }
933
934     /**
935      * Returns function name for MOD.
936      */

937     public String JavaDoc getModFunctionName() {
938         String JavaDoc s = (String JavaDoc)dbMap.get(MOD_FUNCTION_NAME);
939         if (s == null) {
940            throw new JDOUserException(I18NHelper.getMessage(messages,
941                "core.constraint.illegalop", // NOI18N
942
" % "));
943         }
944
945         if (logger.isLoggable()) {
946             logger.fine("sqlstore.database.dbvendor.getModFunctionName", s); // NOI18N
947
}
948
949         return s;
950     }
951
952     /**
953      * Returns cast name that surrounds concat operation.
954      */

955     public String JavaDoc getConcatCast() {
956         String JavaDoc s = (String JavaDoc)dbMap.get(CONCAT_CAST);
957         if (s == null)
958             s = NONE;
959         else
960             s = s.trim();
961         if (logger.isLoggable()) {
962             logger.fine("sqlstore.database.dbvendor.getConcatCast", s); // NOI18N
963
}
964         return s;
965     }
966
967     /**
968      * Returns true if parameters need to be casted for this database
969      */

970     public boolean isParameterCast() {
971         String JavaDoc s = (String JavaDoc)dbMap.get(PARAMETER_CAST);
972         Boolean JavaDoc b = Boolean.valueOf(s);
973
974         if (logger.isLoggable()) {
975             logger.fine("sqlstore.database.dbvendor.isParameterCast", b); // NOI18N
976
}
977
978         return b.booleanValue();
979     }
980
981     /**
982      * Returns true if numeric parameters are inlined for this database
983      */

984     public boolean isInlineNumeric() {
985         String JavaDoc s = (String JavaDoc)dbMap.get(INLINE_NUMERIC);
986         Boolean JavaDoc b = Boolean.valueOf(s);
987
988         if (logger.isLoggable()) {
989             logger.fine("sqlstore.database.dbvendor.isInlineNumeric", b); // NOI18N
990
}
991
992         return b.booleanValue();
993     }
994
995
996     /**
997      * Returns database vendor type
998      */

999     public String JavaDoc getName() {
1000        return vendorType;
1001    }
1002
1003    /**
1004     * Returns a SpecialDBOperation object
1005     */

1006    public SpecialDBOperation getSpecialDBOperation() {
1007        if (logger.isLoggable()) {
1008            logger.fine("sqlstore.database.dbvendor.getSpecialDBOperation", specialDBOperation); // NOI18N
1009
}
1010
1011        return specialDBOperation;
1012    }
1013
1014    /**
1015     * Gets string used as parameter marker. Parameters must be
1016     * casted on DB2/Derby.
1017     * @param type type for which parameter marker is required.
1018     * @return parameter marker for <code>type</code>.
1019     */

1020    public String JavaDoc getParameterMarker(int type) {
1021        String JavaDoc paramMarker = "?"; // NOI18N
1022
if (isParameterCast()) {
1023            String JavaDoc castType = getCastType(type);
1024            if (castType != null) {
1025                paramMarker = "CAST (? AS " + castType + ")"; // NOI18N
1026
}
1027        }
1028
1029        return paramMarker;
1030    }
1031
1032    /**
1033     * Gets string used as parameter marker. Parameters must be
1034     * casted on DB2/Derby. The cast type should match the actual
1035     * JDBC call used to bind the parameter.
1036     * @param type type for which cast type is required.
1037     * @return String used as cast type for input type.
1038     * <code>null</code> if cast type is not required.
1039     */

1040    private static String JavaDoc getCastType(int type) {
1041    //
1042
//DB2type length(bits) JavaType
1043
//----------------------------------------------
1044
//SMALLINT 16 boolean, byte, short
1045
//INTEGER 32 int, char(16 bit unsigned)
1046
//BIGINT 64 long
1047
//REAL 32 float
1048
//DOUBLE 64 double
1049
//
1050
String JavaDoc castType = null;
1051        switch(type) {
1052            case FieldTypeEnumeration.BOOLEAN_PRIMITIVE :
1053            case FieldTypeEnumeration.BOOLEAN :
1054                castType = "SMALLINT";
1055                break;
1056
1057            // CHARACTER_PRIMITIVE and CHARACTER are bound
1058
// to a PreparedStatement as String.
1059
case FieldTypeEnumeration.CHARACTER_PRIMITIVE :
1060            case FieldTypeEnumeration.CHARACTER :
1061            case FieldTypeEnumeration.STRING :
1062                castType = "VARCHAR(32672)"; //Max length of varchar col on DB2
1063
break;
1064
1065            case FieldTypeEnumeration.BYTE_PRIMITIVE :
1066            case FieldTypeEnumeration.BYTE :
1067                castType = "SMALLINT";
1068                break;
1069
1070            case FieldTypeEnumeration.SHORT_PRIMITIVE :
1071            case FieldTypeEnumeration.SHORT :
1072                castType = "SMALLINT";
1073                break;
1074
1075            case FieldTypeEnumeration.INTEGER_PRIMITIVE :
1076            case FieldTypeEnumeration.INTEGER :
1077                castType = "INTEGER";
1078                break;
1079
1080            case FieldTypeEnumeration.LONG_PRIMITIVE :
1081            case FieldTypeEnumeration.LONG :
1082                castType = "BIGINT";
1083                break;
1084
1085            case FieldTypeEnumeration.FLOAT_PRIMITIVE :
1086            case FieldTypeEnumeration.FLOAT :
1087                castType = "REAL";
1088                break;
1089
1090            case FieldTypeEnumeration.DOUBLE_PRIMITIVE :
1091            case FieldTypeEnumeration.DOUBLE :
1092                castType = "DOUBLE";
1093                break;
1094    /* Decide on what should this be casted to ?
1095            case FieldTypeEnumeration.ENUMTYPE_BIGDECIMAL :
1096            case FieldTypeEnumeration.ENUMTYPE_BIGINTEGER :
1097            break;
1098    */

1099            case FieldTypeEnumeration.UTIL_DATE :
1100            case FieldTypeEnumeration.SQL_TIMESTAMP :
1101                castType = "TIMESTAMP";
1102                break;
1103
1104            case FieldTypeEnumeration.SQL_DATE :
1105                castType = "DATE";
1106                break;
1107
1108            case FieldTypeEnumeration.SQL_TIME :
1109                castType = "TIME";
1110                break;
1111
1112        }
1113        return castType;
1114    }
1115}
1116
Popular Tags