KickJava   Java API By Example, From Geeks To Geeks.

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


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: DB2Adapter.java,v 1.4 2003/02/26 00:22:40 jackknifebarber Exp $
9  */

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

31
32 class DB2Adapter extends DatabaseAdapter
33 {
34     /**
35      * Constructs a DB2 adapter based on the given JDBC metadata.
36      *
37      * @param metadata the database metadata.
38      */

39
40     protected DB2Adapter(DatabaseMetaData JavaDoc metadata)
41     {
42         super(metadata);
43
44         maxConstraintNameLength = 18;
45         maxIndexNameLength = 18;
46     }
47
48     public String JavaDoc getVendorID()
49     {
50         return "db2";
51     }
52
53     public String JavaDoc getSchemaName(Connection JavaDoc conn) throws SQLException JavaDoc
54     {
55         Statement JavaDoc stmt = conn.createStatement();
56         
57         try
58         {
59             String JavaDoc stmtText = "VALUES (CURRENT SCHEMA)";
60             ResultSet JavaDoc rs = stmt.executeQuery(stmtText);
61
62             try
63             {
64                 if (!rs.next())
65                     throw new JDOFatalDataStoreException("No result returned from " + stmtText);
66
67                 return rs.getString(1).trim();
68             }
69             finally
70             {
71                 rs.close();
72             }
73         }
74         finally
75         {
76             stmt.close();
77         }
78     }
79
80     public boolean supportsBooleanComparison()
81     {
82         return false;
83     }
84
85     public boolean supportsDeferredConstraints()
86     {
87         return false;
88     }
89
90     public boolean supportsNullsInCandidateKeys()
91     {
92         return false;
93     }
94
95     public ColumnInfo newColumnInfo(ResultSet JavaDoc rs)
96     {
97         return new DB2ColumnInfo(rs);
98     }
99
100     public TableExpression newTableExpression(QueryStatement qs, Table table, SQLIdentifier rangeVar)
101     {
102         return new TableExprAsSubquery(qs, table, rangeVar);
103     }
104
105     public int getUnlimitedLengthPrecisionValue(TypeInfo typeInfo)
106     {
107         if (typeInfo.dataType == java.sql.Types.BLOB || typeInfo.dataType == java.sql.Types.CLOB)
108             return 1 << 30;
109         else
110             return super.getUnlimitedLengthPrecisionValue(typeInfo);
111     }
112
113     public String JavaDoc getDropTableStatement(BaseTable table)
114     {
115         return "DROP TABLE " + table.getName();
116     }
117
118     public NumericExpression lengthMethod(CharacterExpression str)
119     {
120         ArrayList JavaDoc args = new ArrayList JavaDoc();
121         args.add(str);
122
123         return new NumericExpression("LENGTH", args);
124     }
125
126     public CharacterExpression substringMethod(CharacterExpression str,
127                                                NumericExpression begin)
128     {
129         ArrayList JavaDoc args = new ArrayList JavaDoc();
130         args.add(str);
131         args.add(begin.add(new IntegerLiteral(str.getQueryStatement(), BigInteger.ONE)));
132
133         return new CharacterExpression("SUBSTR", args);
134     }
135
136     public CharacterExpression substringMethod(CharacterExpression str,
137                                                NumericExpression begin,
138                                                NumericExpression end)
139     {
140         ArrayList JavaDoc args = new ArrayList JavaDoc();
141         args.add(str);
142         args.add(begin.add(new IntegerLiteral(str.getQueryStatement(), BigInteger.ONE)));
143         args.add(end.sub(begin));
144
145         return new CharacterExpression("SUBSTR", args);
146     }
147 }
148
Popular Tags