KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > OracleAdapter


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: OracleAdapter.java,v 1.10 2004/03/30 06:15:56 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.model.FieldMetaData;
14 import java.math.BigInteger JavaDoc;
15 import java.sql.Connection JavaDoc;
16 import java.sql.DatabaseMetaData JavaDoc;
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.SQLException JavaDoc;
19 import java.sql.Statement JavaDoc;
20 import java.sql.Types JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import javax.jdo.JDOFatalDataStoreException;
23
24
25 /**
26  * Provides methods for adapting SQL language elements to the Oracle database.
27  *
28  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
29  * @version $Revision: 1.10 $
30  *
31  * @see DatabaseAdapter
32  */

33
34 class OracleAdapter extends DatabaseAdapter
35 {
36     /**
37      * Constructs an Oracle adapter based on the given JDBC metadata.
38      *
39      * @param metadata the database metadata.
40      */

41
42     public OracleAdapter(DatabaseMetaData JavaDoc metadata)
43     {
44         super(metadata);
45
46         /* Override mappings for some classes. */
47         typeMappings.put(boolean.class, BooleanCharMapping.class);
48         typeMappings.put(Boolean JavaDoc.class, BooleanCharMapping.class);
49         typeMappings.put(byte[].class, OracleBlobMapping.class);
50     }
51
52     public String JavaDoc getVendorID()
53     {
54         return "oracle";
55     }
56
57     protected void createTypeInfo(DatabaseMetaData JavaDoc metadata) throws SQLException JavaDoc
58     {
59         super.createTypeInfo(metadata);
60
61         /*
62          * If the Oracle JDBC driver does not provide info for the CLOB type,
63          * spoof some and add it.
64          */

65         Integer JavaDoc clobKey = new Integer JavaDoc(Types.CLOB);
66         if (typesByTypeNumber.get(clobKey) == null)
67         {
68             TypeInfo ti = new TypeInfo("CLOB",
69                                        (short)Types.CLOB,
70                                         1073741823,
71                                         "'",
72                                         "'",
73                                         null,
74                                         1,
75                                         true,
76                                         (short)0,
77                                         false,
78                                         false,
79                                         false,
80                                         "CLOB",
81                                         (short)0,
82                                         (short)0,
83                                         10);
84
85             typesByTypeNumber.put(clobKey, ti);
86         }
87     }
88
89
90     public TypeInfo newTypeInfo(ResultSet JavaDoc rs)
91     {
92         TypeInfo ti = new OracleTypeInfo(rs);
93
94         /*
95          * Ignore the problematic 9i TIMESTAMP types for now. To access them
96          * will require a special mapping class that uses oracle.sql.TIMESTAMP
97          * etc. and the right methods must be used with the right timestamp
98          * types.
99          *
100          * Instead we use the older DATE for everything, which works just fine.
101          */

102         if (ti.dataType == Types.TIMESTAMP)
103         {
104             if (!ti.typeName.equalsIgnoreCase("date"))
105             {
106                 ti.typeName = "DATE";
107                 ti.localTypeName = "DATE";
108             }
109         }
110         else if (ti.typeName.toLowerCase().startsWith("timestamp"))
111             return null;
112
113         return ti;
114     }
115
116
117     public Mapping getMapping(ClassBaseTable table, int relativeFieldNumber)
118     {
119         FieldMetaData fmd = table.getClassMetaData().getFieldRelative(relativeFieldNumber);
120
121         if (fmd.getType().equals(String JavaDoc.class))
122         {
123             String JavaDoc option = fmd.getLength();
124
125             if (option != null && option.toLowerCase().equals("unlimited"))
126                 return new OracleClobMapping(table, relativeFieldNumber);
127             else
128                 return new OracleStringMapping(table, relativeFieldNumber);
129         }
130         else
131             return super.getMapping(table, relativeFieldNumber);
132     }
133
134     public ColumnMapping getMapping(Column col)
135     {
136         if (col.getType().equals(String JavaDoc.class))
137             return new OracleStringMapping(col);
138         else
139             return super.getMapping(col);
140     }
141
142
143     public boolean isEmbeddedType(Class JavaDoc c)
144     {
145         if (c.equals(String JavaDoc.class))
146             return !OIDMapping.class.isAssignableFrom(OracleStringMapping.class);
147         else
148             return super.isEmbeddedType(c);
149     }
150
151
152     public String JavaDoc getSchemaName(Connection JavaDoc conn) throws SQLException JavaDoc
153     {
154         Statement JavaDoc stmt = conn.createStatement();
155         
156         try
157         {
158             String JavaDoc stmtText = "SELECT SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') FROM DUAL";
159             ResultSet JavaDoc rs = stmt.executeQuery(stmtText);
160
161             try
162             {
163                 if (!rs.next())
164                     throw new JDOFatalDataStoreException("No result returned from " + stmtText);
165
166                 return rs.getString(1);
167             }
168             finally
169             {
170                 rs.close();
171             }
172         }
173         finally
174         {
175             stmt.close();
176         }
177     }
178
179     public boolean supportsBooleanComparison()
180     {
181         return false;
182     }
183
184     public QueryStatement newQueryStatement(Table table)
185     {
186         return new OracleQueryStatement(table);
187     }
188
189     public QueryStatement newQueryStatement(Table table, SQLIdentifier rangeVar)
190     {
191         return new OracleQueryStatement(table, rangeVar);
192     }
193
194     public TableExpression newTableExpression(QueryStatement qs, Table table, SQLIdentifier rangeVar)
195     {
196         return new TableExprAsSubquery(qs, table, rangeVar);
197     }
198
199     public String JavaDoc getDropTableStatement(BaseTable table)
200     {
201         return "DROP TABLE " + table.getName() + " CASCADE CONSTRAINTS";
202     }
203
204     public NumericExpression lengthMethod(CharacterExpression str)
205     {
206         ArrayList JavaDoc args = new ArrayList JavaDoc();
207         args.add(str);
208
209         return new NumericExpression("LENGTH", args);
210     }
211
212     public CharacterExpression substringMethod(CharacterExpression str,
213                                                NumericExpression begin)
214     {
215         ArrayList JavaDoc args = new ArrayList JavaDoc();
216         args.add(str);
217         args.add(begin.add(new IntegerLiteral(str.getQueryStatement(), BigInteger.ONE)));
218
219         return new CharacterExpression("SUBSTR", args);
220     }
221
222     public CharacterExpression substringMethod(CharacterExpression str,
223                                                NumericExpression begin,
224                                                NumericExpression end)
225     {
226         ArrayList JavaDoc args = new ArrayList JavaDoc();
227         args.add(str);
228         args.add(begin.add(new IntegerLiteral(str.getQueryStatement(), BigInteger.ONE)));
229         args.add(end.sub(begin));
230
231         return new CharacterExpression("SUBSTR", args);
232     }
233 }
234
Popular Tags