KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > dbms > oracle > O8iConnection


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.dbms.oracle;
24
25 import java.sql.*;
26 import java.util.*;
27
28 import org.xquark.jdbc.typing.DBMSInfo;
29 import org.xquark.mapper.RepositoryException;
30 import org.xquark.mapper.dbms.*;
31 import org.xquark.mapper.metadata.Repository;
32 import org.xquark.mapper.metadata.RepositoryConstants;
33 import org.xquark.mapper.util.LongList;
34
35 /**
36  * Implementation of AbstractConnection for Oracle 8 databases.
37  *
38  */

39 public class O8iConnection extends JDBC2Connection
40 {
41     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
42     private static final String JavaDoc RCSName = "$Name: $";
43     
44     private static final String JavaDoc SEQ_QUERY = "select SEQUENCE_NAME from USER_SEQUENCES where SEQUENCE_NAME LIKE('";
45     
46    /** Creates new O8iConnection */
47     public O8iConnection(Connection connection, short dbms, DBMSInfo dbmsInfo)
48     throws SQLException
49     {
50         super(connection, dbms, dbmsInfo);
51     }
52
53     ///////////////////////////////////////////////////////////////
54
// Table methods
55
///////////////////////////////////////////////////////////////
56
public void dropTemporaryTable(String JavaDoc tableName) throws SQLException
57     {
58         Statement stmt = connection.createStatement();
59         try
60         {
61             stmt.executeUpdate("TRUNCATE TABLE "+ tableName);
62         }
63         finally
64         {
65             stmt.close();
66         }
67         dropTable(tableName);
68     }
69     
70     public void createSequence(TableInfo table, short step) throws SQLException
71     {
72         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
73         sql.append("CREATE SEQUENCE ");
74         sql.append(table.getName());
75         sql.append(" START WITH ");
76         sql.append(RepositoryConstants.SEQUENCE_FIRST_VALUE);
77         sql.append(" INCREMENT BY ");
78         sql.append(step);
79         executeUpdate(sql.toString());
80     }
81     
82     public void dropSequence(String JavaDoc seqName) throws SQLException
83     {
84         executeUpdate("DROP SEQUENCE " + seqName);
85     }
86     
87     public long nextSequenceValue(TableInfo table, short step) throws RepositoryException
88     {
89         long next = 1;
90         Statement stmt = null;
91         ResultSet rs = null;
92         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
93         sql.append("SELECT ");
94         sql.append(table.getName());
95         sql.append(".NEXTVAL FROM DUAL");
96         try
97         {
98             stmt = connection.createStatement();
99             rs = stmt.executeQuery(sql.toString());
100             if (rs.next())
101                 next = rs.getLong(1);
102         }
103         catch (SQLException e)
104         {
105             throw new RepositoryException(RepositoryException.DB_ERROR,
106             "JDBC error while querying the " + table.getName() + " table.");
107         }
108         finally
109         {
110             try
111             {
112                 if (rs != null)
113                 {
114                     rs.close();
115                     stmt.close();
116                 }
117             }
118             catch (SQLException e)
119             {
120                 // no op
121
}
122         }
123         return next;
124     }
125     
126     public List getUserSequenceNames(String JavaDoc seqPattern) throws SQLException
127     {
128         Statement stmt = null;
129         ResultSet rs = null;
130         ArrayList seqs = new ArrayList();
131         
132         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
133         sql.append(SEQ_QUERY);
134         sql.append(seqPattern);
135         sql.append("')");
136         try
137         {
138             stmt = connection.createStatement();
139             rs = stmt.executeQuery(sql.toString());
140             while (rs.next())
141                 seqs.add(rs.getString(1));
142         }
143         finally
144         {
145             if (rs != null)
146                 rs.close();
147             if (stmt != null)
148                 stmt.close();
149         }
150         return seqs;
151     }
152     
153     ///////////////////////////////////////////////////////////////
154
// Private utilities
155
///////////////////////////////////////////////////////////////
156
/** Returns a hierachical (or an emulation) SQL statement used by the
157      * longest path set to remove paths.
158      * @return an SQL statement.
159      */

160     private StringBuffer JavaDoc hierarchicalQueryString(LongList path,String JavaDoc select, String JavaDoc tableName)
161     {
162         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
163
164         int len = path.size();
165         sql.append("SELECT "+select);
166         sql.append(" FROM "+ tableName);
167         sql.append(" START WITH label = '"+path.getValue(0)+"' ");
168         sql.append("CONNECT BY (PRIOR target = source) AND (");
169         for(int i=1; i<len; i++) {
170             sql.append(" (level="+(i+1)+" AND label='"+path.getValue(i)+"') OR ");
171         }
172         if (len>1) sql.setLength(sql.length()-4);
173         else sql.setLength(sql.length()-8);
174         sql.append(") ");
175         // __// __debug.pop();
176
return sql;
177     }
178    
179     /** Returns a hierachical (or an emulation) SQL statement used by the
180      * longest path set to remove paths.
181      * @return an SQL statement.
182      */

183     private StringBuffer JavaDoc reversedHierarchicalQueryString(LongList path, String JavaDoc select, String JavaDoc tableName)
184     {
185         StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
186
187         int len = path.size();
188         sql.append("select "+select);
189         sql.append(" from " + tableName);
190         sql.append(" start with label = '"+path.getValue(len-1)+"' \n");
191         sql.append("connect by (prior source = target) AND (\n");
192         for(int i=len-2; i>=0; i--) {
193             sql.append(" (level="+(len-i)+" and label='"+path.getValue(i)+"') or \n");
194         }
195         if(len>1) sql.setLength(sql.length()-4);
196         else sql.setLength(sql.length()-8);
197         sql.append(") \n");
198         return sql;
199     }
200     
201     public int getUniversalError(SQLException e)
202     {
203         switch (e.getErrorCode())
204         {
205             case 955:
206                 return OBJECT_ALREADY_EXISTS;
207             case 942:
208                 return OBJECT_DOES_NOT_EXISTS;
209             default:
210                 return super.getUniversalError(e);
211         }
212     }
213     
214     public void updateStatistics(String JavaDoc tableName) throws SQLException
215     {
216         executeUpdate("ANALYZE TABLE " + tableName + " ESTIMATE STATISTICS");
217     }
218     
219     /** This method is called when a repository is initialized to perform
220      * dbms preparation.
221      */

222     public void onInitRepository(Repository rep) throws SQLException
223     {
224         Statement stmt = null;
225         try
226         {
227         // create dual table
228
String JavaDoc dualTableName = rep.getTableInfo(TableSpec.TYPE_DUAL).getName();
229         stmt = connection.createStatement();
230         stmt.executeUpdate("INSERT INTO " + dualTableName + " VALUES('X')");
231         stmt.executeUpdate("ANALYZE TABLE " + dualTableName + " COMPUTE STATISTICS");
232         }
233         finally
234         {
235             if (stmt != null)
236                 stmt.close();
237         }
238     }
239     public void onDeleteRepository() throws SQLException
240     {
241     }
242     
243     public boolean distinguishNullAndEmptyStrings()
244     {
245         return false;
246     }
247     
248     ///////////////////////////////////////////////////////////////
249
// Overidding static accessors
250
///////////////////////////////////////////////////////////////
251
public String JavaDoc getBitmapIndexClause()
252     {
253         return "BITMAP";
254     }
255 }
256
Popular Tags