KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > databaseaccess > DatasourcePlatform


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.internal.databaseaccess;
23
24 import java.io.*;
25 import java.util.*;
26 import oracle.toplink.essentials.exceptions.*;
27 import oracle.toplink.essentials.queryframework.*;
28 import oracle.toplink.essentials.internal.helper.*;
29 import oracle.toplink.essentials.expressions.*;
30 import oracle.toplink.essentials.sequencing.*;
31 import oracle.toplink.essentials.internal.sessions.AbstractSession;
32 import oracle.toplink.essentials.sessions.DatabaseSession;
33
34 /**
35  * DatasourcePlatform is private to TopLink. It encapsulates behavior specific to a datasource platform
36  * (eg. Oracle, Sybase, DB2, Attunity, MQSeries), and provides protocol for TopLink to access this behavior.
37  *
38  * @see DatabasePlatform
39  * @see oracle.toplink.essentials.eis.EISPlatform
40  * @see oracle.toplink.essentials.xml.XMLPlatform
41  * @see oracle.toplink.essentials.sdk.SDKPlatform
42  *
43  * @since OracleAS TopLink 10<i>g</i> (10.0.3)
44  */

45 public class DatasourcePlatform implements Platform {
46
47     /** Supporting name scopes in database by prefixing the table names with the table qualifier/creator. */
48     protected String JavaDoc tableQualifier;
49
50     /** Allow for conversion to be customized in the platform. */
51     protected transient ConversionManager conversionManager;
52
53     /** Store the query use to query the current server time. */
54     protected ValueReadQuery timestampQuery;
55
56     /** Operators specific to this platform */
57     protected transient Map platformOperators;
58
59     /** Store the list of Classes that can be converted to from the key. */
60     protected Hashtable dataTypesConvertedFromAClass;
61
62     /** Store the list of Classes that can be converted from to the key. */
63     protected Hashtable dataTypesConvertedToAClass;
64
65     /** Store default sequence */
66     protected Sequence defaultSequence;
67
68     /** Store map of sequence names to sequences */
69     protected Map sequences;
70
71     public DatasourcePlatform() {
72         this.tableQualifier = "";
73     }
74
75     protected void addOperator(ExpressionOperator operator) {
76         platformOperators.put(new Integer JavaDoc(operator.getSelector()), operator);
77     }
78
79     /**
80      * Add the parameter.
81      * Convert the parameter to a string and write it.
82      */

83     public void appendParameter(Call call, Writer writer, Object JavaDoc parameter) {
84         String JavaDoc parameterValue = (String JavaDoc)getConversionManager().convertObject(parameter, ClassConstants.STRING);
85         if (parameterValue == null) {
86             parameterValue = "";
87         }
88         try {
89             writer.write(parameterValue);
90         } catch (IOException exception) {
91             throw ValidationException.fileError(exception);
92         }
93     }
94
95     /**
96      * Allow for the platform to handle the representation of parameters specially.
97      */

98     public Object JavaDoc getCustomModifyValueForCall(Call call, Object JavaDoc value, DatabaseField field, boolean shouldBind) {
99         return value;
100     }
101
102     /**
103      * Used by SQLCall.appendModify(..)
104      * If the field should be passed to customModifyInDatabaseCall, retun true,
105      * otherwise false.
106      * Methods shouldCustomModifyInDatabaseCall and customModifyInDatabaseCall should be
107      * kept in sync: shouldCustomModifyInDatabaseCall should return true if and only if the field
108      * is handled by customModifyInDatabaseCall.
109      */

110     public boolean shouldUseCustomModifyForCall(DatabaseField field) {
111         return false;
112     }
113
114     public Object JavaDoc clone() {
115         try {
116             DatasourcePlatform clone = (DatasourcePlatform)super.clone();
117             clone.sequencesAfterCloneCleanup();
118             return clone;
119         } catch (CloneNotSupportedException JavaDoc exception) {
120             ;//Do nothing
121
}
122
123         return null;
124     }
125
126     protected void sequencesAfterCloneCleanup() {
127         Sequence defaultSequenceClone = null;
128         if (hasDefaultSequence()) {
129             defaultSequenceClone = (Sequence)getDefaultSequence().clone();
130             setDefaultSequence(defaultSequenceClone);
131         }
132         HashMap sequencesDeepClone = null;
133         if (getSequences() != null) {
134             sequencesDeepClone = new HashMap(getSequences().size());
135             Iterator it = getSequences().values().iterator();
136             while (it.hasNext()) {
137                 Sequence sequence = (Sequence)it.next();
138                 if ((defaultSequenceClone != null) && (sequence == getDefaultSequence())) {
139                     sequencesDeepClone.put(defaultSequenceClone.getName(), defaultSequenceClone);
140                 } else {
141                     Sequence sequenceClone = (Sequence)sequence.clone();
142                     if (sequenceClone instanceof DefaultSequence) {
143                         if (!((DefaultSequence)sequenceClone).hasPreallocationSize()) {
144                             continue;
145                         }
146                     }
147                     sequencesDeepClone.put(sequenceClone.getName(), sequenceClone);
148                 }
149             }
150             this.setSequences(sequencesDeepClone);
151         }
152     }
153
154     /**
155      * Convert the object to the appropriate type by invoking the appropriate
156      * ConversionManager method
157      * @param object - the object that must be converted
158      * @param javaClass - the class that the object must be converted to
159      * @exception - ConversionException, all exceptions will be thrown as this type.
160      * @return - the newly converted object
161      */

162     public Object JavaDoc convertObject(Object JavaDoc sourceObject, Class JavaDoc javaClass) throws ConversionException {
163         return getConversionManager().convertObject(sourceObject, javaClass);
164     }
165
166     /**
167      * Copy the state into the new platform.
168      */

169     public void copyInto(Platform platform) {
170         if (!(platform instanceof DatasourcePlatform)) {
171             return;
172         }
173         DatasourcePlatform datasourcePlatform = (DatasourcePlatform)platform;
174         datasourcePlatform.setTableQualifier(getTableQualifier());
175         datasourcePlatform.setTimestampQuery(this.timestampQuery);
176         datasourcePlatform.setConversionManager(getConversionManager());
177         if (hasDefaultSequence()) {
178             datasourcePlatform.setDefaultSequence(getDefaultSequence());
179         }
180         datasourcePlatform.setSequences(getSequences());
181     }
182
183     /**
184      * The platform hold its own instance of conversion manager to allow customization.
185      */

186     public ConversionManager getConversionManager() {
187         // Lazy init for serialization.
188
if (conversionManager == null) {
189             //Clone the default to allow customers to easily override the conversion manager
190
conversionManager = (ConversionManager)ConversionManager.getDefaultManager().clone();
191         }
192         return conversionManager;
193     }
194
195     /**
196      * The platform hold its own instance of conversion manager to allow customization.
197      */

198     public void setConversionManager(ConversionManager conversionManager) {
199         this.conversionManager = conversionManager;
200     }
201
202     /**
203      * Return the operator for the operator constant defined in ExpressionOperator.
204      */

205     public ExpressionOperator getOperator(int selector) {
206         return (ExpressionOperator)getPlatformOperators().get(new Integer JavaDoc(selector));
207     }
208
209     /**
210      * Return any platform-specific operators
211      */

212     public synchronized Map getPlatformOperators() {
213         if (platformOperators == null) {
214             initializePlatformOperators();
215         }
216         return platformOperators;
217     }
218
219     public int getSequencePreallocationSize() {
220         return getDefaultSequence().getPreallocationSize();
221     }
222
223     /**
224      * Return the qualifier for the table. Required by some
225      * databases such as Oracle and DB2
226      */

227     public String JavaDoc getTableQualifier() {
228         return tableQualifier;
229     }
230
231     /**
232      * Answer the timestamp from the server.
233      */

234     public java.sql.Timestamp JavaDoc getTimestampFromServer(AbstractSession session, String JavaDoc sessionName) {
235         if (getTimestampQuery() == null) {
236             return new java.sql.Timestamp JavaDoc(System.currentTimeMillis());
237         } else {
238             getTimestampQuery().setSessionName(sessionName);
239             return (java.sql.Timestamp JavaDoc)session.executeQuery(getTimestampQuery());
240         }
241     }
242
243     /**
244      * This method can be overridden by subclasses to return a
245      * query that will return the timestamp from the server.
246      * return null if the time should be the local time.
247      */

248     public ValueReadQuery getTimestampQuery() {
249         return timestampQuery;
250     }
251
252     /**
253      * Initialize any platform-specific operators
254      */

255     protected void initializePlatformOperators() {
256         this.platformOperators = new HashMap();
257
258         // Outer join
259
addOperator(ExpressionOperator.equalOuterJoin());
260
261         // General
262
addOperator(ExpressionOperator.toUpperCase());
263         addOperator(ExpressionOperator.toLowerCase());
264         addOperator(ExpressionOperator.chr());
265         addOperator(ExpressionOperator.concat());
266         addOperator(ExpressionOperator.hexToRaw());
267         addOperator(ExpressionOperator.initcap());
268         addOperator(ExpressionOperator.instring());
269         addOperator(ExpressionOperator.soundex());
270         addOperator(ExpressionOperator.leftPad());
271         addOperator(ExpressionOperator.leftTrim());
272         addOperator(ExpressionOperator.leftTrim2());
273         addOperator(ExpressionOperator.replace());
274         addOperator(ExpressionOperator.rightPad());
275         addOperator(ExpressionOperator.rightTrim());
276         addOperator(ExpressionOperator.rightTrim2());
277         addOperator(ExpressionOperator.substring());
278         addOperator(ExpressionOperator.toNumber());
279         addOperator(ExpressionOperator.toChar());
280         addOperator(ExpressionOperator.toCharWithFormat());
281         addOperator(ExpressionOperator.translate());
282         addOperator(ExpressionOperator.trim());
283         addOperator(ExpressionOperator.trim2());
284         addOperator(ExpressionOperator.ascii());
285         addOperator(ExpressionOperator.length());
286         addOperator(ExpressionOperator.locate());
287         addOperator(ExpressionOperator.locate2());
288
289         // Date
290
addOperator(ExpressionOperator.addMonths());
291         addOperator(ExpressionOperator.dateToString());
292         addOperator(ExpressionOperator.lastDay());
293         addOperator(ExpressionOperator.monthsBetween());
294         addOperator(ExpressionOperator.nextDay());
295         addOperator(ExpressionOperator.roundDate());
296         addOperator(ExpressionOperator.toDate());
297         addOperator(ExpressionOperator.today());
298         addOperator(ExpressionOperator.currentDate());
299
300         // Math
301
addOperator(ExpressionOperator.simpleMath(ExpressionOperator.Add, "+"));
302         addOperator(ExpressionOperator.simpleMath(ExpressionOperator.Subtract, "-"));
303         addOperator(ExpressionOperator.simpleMath(ExpressionOperator.Multiply, "*"));
304         addOperator(ExpressionOperator.simpleMath(ExpressionOperator.Divide, "/"));
305
306         addOperator(ExpressionOperator.ceil());
307         addOperator(ExpressionOperator.cos());
308         addOperator(ExpressionOperator.cosh());
309         addOperator(ExpressionOperator.abs());
310         addOperator(ExpressionOperator.acos());
311         addOperator(ExpressionOperator.asin());
312         addOperator(ExpressionOperator.atan());
313         addOperator(ExpressionOperator.exp());
314         addOperator(ExpressionOperator.sqrt());
315         addOperator(ExpressionOperator.floor());
316         addOperator(ExpressionOperator.ln());
317         addOperator(ExpressionOperator.log());
318         addOperator(ExpressionOperator.mod());
319         addOperator(ExpressionOperator.power());
320         addOperator(ExpressionOperator.round());
321         addOperator(ExpressionOperator.sign());
322         addOperator(ExpressionOperator.sin());
323         addOperator(ExpressionOperator.sinh());
324         addOperator(ExpressionOperator.tan());
325         addOperator(ExpressionOperator.tanh());
326         addOperator(ExpressionOperator.trunc());
327         addOperator(ExpressionOperator.greatest());
328         addOperator(ExpressionOperator.least());
329
330         // Object-relational
331
addOperator(ExpressionOperator.deref());
332         addOperator(ExpressionOperator.ref());
333         addOperator(ExpressionOperator.refToHex());
334         addOperator(ExpressionOperator.value());
335     }
336
337     public boolean isAccess() {
338         return false;
339     }
340
341     public boolean isAttunity() {
342         return false;
343     }
344
345     public boolean isCloudscape() {
346         return false;
347     }
348
349     public boolean isDerby() {
350         return false;
351     }
352
353     public boolean isDB2() {
354         return false;
355     }
356
357     public boolean isDBase() {
358         return false;
359     }
360
361     public boolean isHSQL() {
362         return false;
363     }
364
365     public boolean isInformix() {
366         return false;
367     }
368
369     public boolean isMySQL() {
370         return false;
371     }
372
373     public boolean isODBC() {
374         return false;
375     }
376
377     public boolean isOracle() {
378         return false;
379     }
380
381     public boolean isPointBase() {
382         return false;
383     }
384
385     public boolean isSQLAnywhere() {
386         return false;
387     }
388
389     public boolean isSQLServer() {
390         return false;
391     }
392
393     public boolean isSybase() {
394         return false;
395     }
396
397     public boolean isTimesTen() {
398         return false;
399     }
400
401     public boolean isPostgreSQL() {
402         return false;
403     }
404     
405     /**
406      * Set the qualifier for the table. Required by some
407      * databases such as Oracle and DB2
408      */

409     public void setTableQualifier(String JavaDoc qualifier) {
410         tableQualifier = qualifier;
411     }
412
413     /**
414      * Can override the default query for returning a timestamp from the server.
415      * See: getTimestampFromServer
416      */

417     public void setTimestampQuery(ValueReadQuery tsQuery) {
418         timestampQuery = tsQuery;
419     }
420
421     public String JavaDoc toString() {
422         return Helper.getShortClassName(this.getClass());
423     }
424
425     /**
426      * PUBLIC:
427      * Return the list of Classes that can be converted to from the passed in javaClass.
428      * @param javaClass - the class that is converted from
429      * @return - a vector of classes
430      */

431     public Vector getDataTypesConvertedFrom(Class JavaDoc javaClass) {
432         return getConversionManager().getDataTypesConvertedFrom(javaClass);
433     }
434
435     /**
436      * PUBLIC:
437      * Return the list of Classes that can be converted from to the passed in javaClass.
438      * @param javaClass - the class that is converted to
439      * @return - a vector of classes
440      */

441     public Vector getDataTypesConvertedTo(Class JavaDoc javaClass) {
442         return getConversionManager().getDataTypesConvertedTo(javaClass);
443     }
444
445     /**
446      * Get default sequence
447      */

448     public Sequence getDefaultSequence() {
449         if (!hasDefaultSequence()) {
450             setDefaultSequence(createPlatformDefaultSequence());
451         }
452         return defaultSequence;
453     }
454
455     /**
456      * Get default sequence
457      */

458     public boolean hasDefaultSequence() {
459         return defaultSequence != null;
460     }
461
462     /**
463      * Set default sequence. In case the passed sequence is of type DefaultSequence - use platformDefaultSequence
464      * with name and size of the passed sequence.
465      */

466     public void setDefaultSequence(Sequence sequence) {
467         if (sequence instanceof DefaultSequence) {
468             Sequence platformDefaultSequence = createPlatformDefaultSequence();
469             if (platformDefaultSequence != null) {
470                 platformDefaultSequence.setName(sequence.getName());
471                 if (((DefaultSequence)sequence).hasPreallocationSize()) {
472                     platformDefaultSequence.setPreallocationSize(sequence.getPreallocationSize());
473                 }
474             }
475             defaultSequence = platformDefaultSequence;
476         } else {
477             defaultSequence = sequence;
478         }
479     }
480
481     /**
482      * Add sequence corresponding to the name
483      */

484     public void addSequence(Sequence sequence) {
485         if (getSequences() == null) {
486             createSequences();
487         }
488         getSequences().put(sequence.getName(), sequence);
489     }
490
491     /**
492      * Get sequence corresponding to the name
493      */

494     public Sequence getSequence(String JavaDoc seqName) {
495         if (seqName == null) {
496             return getDefaultSequence();
497         } else {
498             if (getSequences() != null) {
499                 return (Sequence)getSequences().get(seqName);
500             } else {
501                 return null;
502             }
503         }
504     }
505
506     /**
507      * INTERNAL:
508      * Create platform-default Sequence
509      */

510     protected Sequence createPlatformDefaultSequence() {
511         throw ValidationException.createPlatformDefaultSequenceUndefined(Helper.getShortClassName(this));
512     }
513
514     protected synchronized void createSequences() {
515         if (getSequences() == null) {
516             setSequences(new HashMap());
517         }
518     }
519
520     /**
521      * Remove sequence corresponding to name.
522      * Doesn't remove default sequence.
523      */

524     public Sequence removeSequence(String JavaDoc seqName) {
525         if (getSequences() != null) {
526             return (Sequence)getSequences().remove(seqName);
527         } else {
528             return null;
529         }
530     }
531
532     /**
533      * Remove all sequences, but the default one.
534      */

535     public void removeAllSequences() {
536         sequences = null;
537     }
538
539     /**
540      * INTERNAL:
541      * Returns a map of sequence names to Sequences (may be null).
542      */

543     public Map getSequences() {
544         return sequences;
545     }
546
547     /**
548      * INTERNAL:
549      * Used only for writing into XML or Java.
550      */

551     public Map getSequencesToWrite() {
552         if ((getSequences() == null) || getSequences().isEmpty()) {
553             return null;
554         }
555         Map sequencesToWrite = new HashMap();
556         Iterator it = getSequences().values().iterator();
557         while (it.hasNext()) {
558             Sequence sequence = (Sequence)it.next();
559             if (!(sequence instanceof DefaultSequence) || ((DefaultSequence)sequence).hasPreallocationSize()) {
560                 sequencesToWrite.put(sequence.getName(), sequence);
561             }
562         }
563         return sequencesToWrite;
564     }
565
566     /**
567      * INTERNAL:
568      * Used only for writing into XML or Java.
569      */

570     public Sequence getDefaultSequenceToWrite() {
571         if (usesPlatformDefaultSequence()) {
572             return null;
573         } else {
574             return getDefaultSequence();
575         }
576     }
577
578     /**
579      * INTERNAL:
580      * Sets sequences - for XML support only
581      */

582     public void setSequences(Map sequences) {
583         this.sequences = sequences;
584     }
585
586     /**
587      * INTERNAL:
588      * Indicates whether defaultSequence is the same as platform default sequence.
589      */

590     public boolean usesPlatformDefaultSequence() {
591         if (!hasDefaultSequence()) {
592             return true;
593         } else {
594             return getDefaultSequence().equals(createPlatformDefaultSequence());
595         }
596     }
597
598     /**
599      * INTERNAL:
600      * Platform specific sequencing initialization.
601      * This internal method should only be called by SequencingManager.
602      * By default does nothing.
603      */

604     public void platformSpecificSequencingInitialization(DatabaseSession session) {
605     }
606 }
607
Popular Tags