KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > sybase > Loader


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.extractor.sybase;
24
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.sql.*;
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32 import org.xquark.extractor.metadata.QNameEncoder;
33 import org.xquark.jdbc.typing.DBMSInfo;
34
35
36 public final class Loader extends org.xquark.extractor.metadata.Loader {
37
38     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
39     private static final String JavaDoc RCSName = "$Name: $";
40
41     public Loader(Connection JavaDoc connection, DBMSInfo dbmsInfo, QNameEncoder encoder)
42     throws SQLException JavaDoc {
43         super(connection, dbmsInfo, encoder);
44     }
45
46     protected String JavaDoc getDefaultSchema() throws SQLException JavaDoc {
47         Statement JavaDoc statement = null;
48         ResultSet rs = null;
49         try {
50             statement = _connection.createStatement();
51             rs = statement.executeQuery("select user");
52             rs.next();
53             return rs.getString(1);
54         } finally {
55             if (rs != null) rs.close();
56             if (statement != null) statement.close();
57         }
58     }
59
60     protected String JavaDoc createQuery(String JavaDoc catalogName, String JavaDoc schemaName, String JavaDoc origTableName){
61         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
62
63         retVal.append("SELECT * FROM ");
64         if (null != catalogName && 0 < catalogName.trim().length()) {
65             retVal.append(catalogName.trim());
66             retVal.append('.');
67         }
68         if (null != schemaName && 0 < schemaName.trim().length()) {
69             retVal.append(schemaName.trim());
70             retVal.append('.');
71         }
72         if (null != origTableName && 0 < origTableName.trim().length()) {
73             retVal.append(origTableName.trim());
74         }
75         retVal.append(" WHERE 0=1");
76         return retVal.toString();
77     }
78
79     protected PreparedStatement JavaDoc createPreparedStatement(Connection JavaDoc jdbcConnection,String JavaDoc command) throws SQLException JavaDoc {
80         try {
81             Class JavaDoc[] argTypes = { String JavaDoc.class, Boolean.TYPE };
82             Method JavaDoc method = jdbcConnection.getClass().getMethod("prepareStatement", argTypes);
83             Object JavaDoc[] args = { command, Boolean.TRUE };
84             return (PreparedStatement JavaDoc)method.invoke(jdbcConnection, args);
85         } catch (InvocationTargetException JavaDoc ex) {
86             throw (SQLException JavaDoc)ex.getTargetException();
87         } catch (IllegalAccessException JavaDoc ex) {
88             throw new SQLException JavaDoc("Could not create prepared statement");
89         } catch (NoSuchMethodException JavaDoc ex) {
90             throw new SQLException JavaDoc("Could not create prepared statement");
91         }
92     }
93
94 }
95
Popular Tags