1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef DBACCESS_CONNECTIONTOOLS_HXX
28 #include "connectiontools.hxx"
29 #endif
30 
31 #ifndef DBACCESS_SOURCE_SDBTOOLS_CONNECTION_TABLENAME_HXX
32 #include "tablename.hxx"
33 #endif
34 #ifndef DBACCESS_SOURCE_SDBTOOLS_CONNECTION_OBJECTNAMES_HXX
35 #include "objectnames.hxx"
36 #endif
37 #ifndef DBACCESS_DATASOURCEMETADATA_HXX
38 #include "datasourcemetadata.hxx"
39 #endif
40 
41 /** === begin UNO includes === **/
42 /** === end UNO includes === **/
43 
44 #ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX
45 #include <comphelper/namedvaluecollection.hxx>
46 #endif
47 
48 #include <connectivity/dbtools.hxx>
49 #include <connectivity/statementcomposer.hxx>
50 
51 #include <algorithm>
52 
createRegistryInfo_ConnectionTools()53 extern "C" void SAL_CALL createRegistryInfo_ConnectionTools()
54 {
55 	::sdbtools::OAutoRegistration< ::sdbtools::ConnectionTools > aRegistration;
56 }
57 
58 //........................................................................
59 namespace sdbtools
60 {
61 //........................................................................
62 
63 	/** === begin UNO using === **/
64     using namespace ::com::sun::star;
65     using namespace ::com::sun::star::uno;
66     using ::com::sun::star::uno::Reference;
67     using ::com::sun::star::uno::RuntimeException;
68     using ::com::sun::star::sdb::tools::XTableName;
69     using ::com::sun::star::sdb::tools::XObjectNames;
70     using ::com::sun::star::sdb::tools::XDataSourceMetaData;
71     using ::com::sun::star::uno::Sequence;
72     using ::com::sun::star::uno::XInterface;
73     using ::com::sun::star::uno::Any;
74     using ::com::sun::star::uno::Exception;
75     using ::com::sun::star::sdbc::XConnection;
76     using ::com::sun::star::lang::IllegalArgumentException;
77     using ::com::sun::star::uno::XComponentContext;
78 	/** === end UNO using === **/
79 
80 	//====================================================================
81 	//= ConnectionTools
82 	//====================================================================
83 	//--------------------------------------------------------------------
ConnectionTools(const::comphelper::ComponentContext & _rContext)84     ConnectionTools::ConnectionTools( const ::comphelper::ComponentContext& _rContext )
85         :ConnectionDependentComponent( _rContext )
86     {
87     }
88 
89     //--------------------------------------------------------------------
~ConnectionTools()90     ConnectionTools::~ConnectionTools()
91     {
92     }
93 
94     //--------------------------------------------------------------------
createTableName()95     Reference< XTableName > SAL_CALL ConnectionTools::createTableName() throw (RuntimeException)
96     {
97         EntryGuard aGuard( *this );
98         return new TableName( getContext(), getConnection() );
99     }
100 
101     //--------------------------------------------------------------------
getObjectNames()102     Reference< XObjectNames > SAL_CALL ConnectionTools::getObjectNames() throw (RuntimeException)
103     {
104         EntryGuard aGuard( *this );
105         return new ObjectNames( getContext(), getConnection() );
106     }
107 
108     //--------------------------------------------------------------------
getDataSourceMetaData()109     Reference< XDataSourceMetaData > SAL_CALL ConnectionTools::getDataSourceMetaData() throw (RuntimeException)
110     {
111         EntryGuard aGuard( *this );
112         return new DataSourceMetaData( getContext(), getConnection() );
113     }
114     //--------------------------------------------------------------------
getFieldsByCommandDescriptor(::sal_Int32 commandType,const::rtl::OUString & command,Reference<lang::XComponent> & keepFieldsAlive)115     Reference< container::XNameAccess > SAL_CALL ConnectionTools::getFieldsByCommandDescriptor( ::sal_Int32 commandType, const ::rtl::OUString& command, Reference< lang::XComponent >& keepFieldsAlive ) throw (sdbc::SQLException, RuntimeException)
116     {
117         EntryGuard aGuard( *this );
118         ::dbtools::SQLExceptionInfo aErrorInfo;
119         Reference< container::XNameAccess > xRet = ::dbtools::getFieldsByCommandDescriptor(getConnection(),commandType,command,keepFieldsAlive,&aErrorInfo);
120         if ( aErrorInfo.isValid() )
121             aErrorInfo.doThrow();
122         return xRet;
123     }
124     //--------------------------------------------------------------------
getComposer(::sal_Int32 commandType,const::rtl::OUString & command)125     Reference< sdb::XSingleSelectQueryComposer > SAL_CALL ConnectionTools::getComposer( ::sal_Int32 commandType, const ::rtl::OUString& command ) throw (::com::sun::star::uno::RuntimeException)
126     {
127         EntryGuard aGuard( *this );
128         dbtools::StatementComposer aComposer(getConnection(), command, commandType, sal_True );
129         aComposer.setDisposeComposer(sal_False);
130         return aComposer.getComposer();
131     }
132 
133     //--------------------------------------------------------------------
getImplementationName()134     ::rtl::OUString SAL_CALL ConnectionTools::getImplementationName() throw (RuntimeException)
135     {
136         return getImplementationName_static();
137     }
138 
139     //--------------------------------------------------------------------
supportsService(const::rtl::OUString & _ServiceName)140     ::sal_Bool SAL_CALL ConnectionTools::supportsService(const ::rtl::OUString & _ServiceName) throw (RuntimeException)
141     {
142         Sequence< ::rtl::OUString > aSupported( getSupportedServiceNames() );
143         const ::rtl::OUString* begin = aSupported.getConstArray();
144         const ::rtl::OUString* end = aSupported.getConstArray() + aSupported.getLength();
145         return ::std::find( begin, end, _ServiceName ) != end;
146     }
147 
148     //--------------------------------------------------------------------
getSupportedServiceNames()149     Sequence< ::rtl::OUString > SAL_CALL ConnectionTools::getSupportedServiceNames() throw (RuntimeException)
150     {
151         return getSupportedServiceNames_static();
152     }
153 
154     //--------------------------------------------------------------------
getImplementationName_static()155     ::rtl::OUString SAL_CALL ConnectionTools::getImplementationName_static()
156     {
157         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.dbaccess.ConnectionTools" ) );
158     }
159 
160     //--------------------------------------------------------------------
getSupportedServiceNames_static()161     Sequence< ::rtl::OUString > SAL_CALL ConnectionTools::getSupportedServiceNames_static()
162     {
163         Sequence< ::rtl::OUString > aSupported( 1 );
164         aSupported[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.tools.ConnectionTools" ) );
165         return aSupported;
166     }
167 
168     //--------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)169 	Reference< XInterface > SAL_CALL ConnectionTools::Create(const Reference< XComponentContext >& _rxContext )
170     {
171         return *( new ConnectionTools( ::comphelper::ComponentContext( _rxContext ) ) );
172     }
173 
174     //--------------------------------------------------------------------
initialize(const Sequence<Any> & _rArguments)175     void SAL_CALL ConnectionTools::initialize(const Sequence< Any > & _rArguments) throw (RuntimeException, Exception)
176     {
177         ::osl::MutexGuard aGuard( getMutex() );
178 
179         ::comphelper::NamedValueCollection aArguments( _rArguments );
180 
181         Reference< XConnection > xConnection;
182         aArguments.get( "Connection" ) >>= xConnection;
183         if ( !xConnection.is() )
184             throw IllegalArgumentException();
185 
186         setWeakConnection( xConnection );
187     }
188 
189 
190 //........................................................................
191 } // namespace sdbtools
192 //........................................................................
193 
194