1*c3ab0d6aSAndrew Rist /**************************************************************
2*c3ab0d6aSAndrew Rist  *
3*c3ab0d6aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c3ab0d6aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c3ab0d6aSAndrew Rist  * distributed with this work for additional information
6*c3ab0d6aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c3ab0d6aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c3ab0d6aSAndrew Rist  * "License"); you may not use this file except in compliance
9*c3ab0d6aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c3ab0d6aSAndrew Rist  *
11*c3ab0d6aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c3ab0d6aSAndrew Rist  *
13*c3ab0d6aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c3ab0d6aSAndrew Rist  * software distributed under the License is distributed on an
15*c3ab0d6aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c3ab0d6aSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c3ab0d6aSAndrew Rist  * specific language governing permissions and limitations
18*c3ab0d6aSAndrew Rist  * under the License.
19*c3ab0d6aSAndrew Rist  *
20*c3ab0d6aSAndrew Rist  *************************************************************/
21*c3ab0d6aSAndrew Rist 
22cdf0e10cSrcweir /*
23cdf0e10cSrcweir  * To change this template, choose Tools | Templates
24cdf0e10cSrcweir  * and open the template in the editor.
25cdf0e10cSrcweir  */
26cdf0e10cSrcweir 
27cdf0e10cSrcweir package connectivity.tools.sdb;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
30cdf0e10cSrcweir import com.sun.star.sdb.XSingleSelectQueryComposer;
31cdf0e10cSrcweir import com.sun.star.sdbc.SQLException;
32cdf0e10cSrcweir import com.sun.star.sdbc.XConnection;
33cdf0e10cSrcweir import com.sun.star.sdbc.XDatabaseMetaData;
34cdf0e10cSrcweir import com.sun.star.sdbc.XPreparedStatement;
35cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet;
36cdf0e10cSrcweir import com.sun.star.sdbc.XStatement;
37cdf0e10cSrcweir import com.sun.star.sdbcx.XTablesSupplier;
38cdf0e10cSrcweir import com.sun.star.uno.Exception;
39cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40cdf0e10cSrcweir import com.sun.star.util.XRefreshable;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /**
43cdf0e10cSrcweir  * is a convenience wrapper around a SDB-level connection object
44cdf0e10cSrcweir  */
45cdf0e10cSrcweir public class Connection
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     private final   XConnection m_connection;
48cdf0e10cSrcweir 
Connection( final XConnection _connection )49cdf0e10cSrcweir     public Connection( final XConnection _connection )
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir         m_connection = _connection;
52cdf0e10cSrcweir     }
53cdf0e10cSrcweir 
getXConnection()54cdf0e10cSrcweir     public XConnection  getXConnection()
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         return m_connection;
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
execute( final String _sql )59cdf0e10cSrcweir     public boolean execute( final String _sql ) throws SQLException
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         XStatement statement = createStatement();
62cdf0e10cSrcweir         return statement.execute( _sql );
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
executeQuery( final String _sql )65cdf0e10cSrcweir     public XResultSet executeQuery( final String _sql ) throws SQLException
66cdf0e10cSrcweir     {
67cdf0e10cSrcweir         XStatement statement = createStatement();
68cdf0e10cSrcweir         return statement.executeQuery( _sql );
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir 
executeUpdate( final String _sql )71cdf0e10cSrcweir     public int executeUpdate( final String _sql ) throws SQLException
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         XStatement statement = createStatement();
74cdf0e10cSrcweir         return statement.executeUpdate( _sql );
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
refreshTables()77cdf0e10cSrcweir     public void refreshTables()
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         final XTablesSupplier suppTables = UnoRuntime.queryInterface(XTablesSupplier.class, m_connection);
80cdf0e10cSrcweir         final XRefreshable refresh = UnoRuntime.queryInterface( XRefreshable.class, suppTables.getTables() );
81cdf0e10cSrcweir         refresh.refresh();
82cdf0e10cSrcweir     }
83cdf0e10cSrcweir 
createSingleSelectQueryComposer()84cdf0e10cSrcweir     public XSingleSelectQueryComposer createSingleSelectQueryComposer() throws Exception
85cdf0e10cSrcweir     {
86cdf0e10cSrcweir         final XMultiServiceFactory connectionFactory = UnoRuntime.queryInterface( XMultiServiceFactory.class, m_connection );
87cdf0e10cSrcweir         return UnoRuntime.queryInterface(
88cdf0e10cSrcweir             XSingleSelectQueryComposer.class, connectionFactory.createInstance( "com.sun.star.sdb.SingleSelectQueryComposer" ) );
89cdf0e10cSrcweir     }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir     public
createStatement()92cdf0e10cSrcweir     XStatement createStatement() throws SQLException
93cdf0e10cSrcweir     {
94cdf0e10cSrcweir         return m_connection.createStatement();
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     public
prepareStatement( String _sql )98cdf0e10cSrcweir     XPreparedStatement prepareStatement( String _sql ) throws SQLException
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         return m_connection.prepareStatement( _sql );
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir     public
getMetaData()104cdf0e10cSrcweir     XDatabaseMetaData getMetaData() throws SQLException
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         return m_connection.getMetaData();
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir     public
close()110cdf0e10cSrcweir     void close() throws SQLException
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         m_connection.close();
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir }
115