1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 
31 #ifndef DBACCESS_CONNECTIONTOOLS_HXX
32 #include "connectiontools.hxx"
33 #endif
34 
35 #ifndef DBACCESS_SOURCE_SDBTOOLS_CONNECTION_TABLENAME_HXX
36 #include "tablename.hxx"
37 #endif
38 #ifndef DBACCESS_SOURCE_SDBTOOLS_CONNECTION_OBJECTNAMES_HXX
39 #include "objectnames.hxx"
40 #endif
41 #ifndef DBACCESS_DATASOURCEMETADATA_HXX
42 #include "datasourcemetadata.hxx"
43 #endif
44 
45 /** === begin UNO includes === **/
46 /** === end UNO includes === **/
47 
48 #ifndef COMPHELPER_NAMEDVALUECOLLECTION_HXX
49 #include <comphelper/namedvaluecollection.hxx>
50 #endif
51 
52 #include <connectivity/dbtools.hxx>
53 #include <connectivity/statementcomposer.hxx>
54 
55 #include <algorithm>
56 
57 extern "C" void SAL_CALL createRegistryInfo_ConnectionTools()
58 {
59 	::sdbtools::OAutoRegistration< ::sdbtools::ConnectionTools > aRegistration;
60 }
61 
62 //........................................................................
63 namespace sdbtools
64 {
65 //........................................................................
66 
67 	/** === begin UNO using === **/
68     using namespace ::com::sun::star;
69     using namespace ::com::sun::star::uno;
70     using ::com::sun::star::uno::Reference;
71     using ::com::sun::star::uno::RuntimeException;
72     using ::com::sun::star::sdb::tools::XTableName;
73     using ::com::sun::star::sdb::tools::XObjectNames;
74     using ::com::sun::star::sdb::tools::XDataSourceMetaData;
75     using ::com::sun::star::uno::Sequence;
76     using ::com::sun::star::uno::XInterface;
77     using ::com::sun::star::uno::Any;
78     using ::com::sun::star::uno::Exception;
79     using ::com::sun::star::sdbc::XConnection;
80     using ::com::sun::star::lang::IllegalArgumentException;
81     using ::com::sun::star::uno::XComponentContext;
82 	/** === end UNO using === **/
83 
84 	//====================================================================
85 	//= ConnectionTools
86 	//====================================================================
87 	//--------------------------------------------------------------------
88     ConnectionTools::ConnectionTools( const ::comphelper::ComponentContext& _rContext )
89         :ConnectionDependentComponent( _rContext )
90     {
91     }
92 
93     //--------------------------------------------------------------------
94     ConnectionTools::~ConnectionTools()
95     {
96     }
97 
98     //--------------------------------------------------------------------
99     Reference< XTableName > SAL_CALL ConnectionTools::createTableName() throw (RuntimeException)
100     {
101         EntryGuard aGuard( *this );
102         return new TableName( getContext(), getConnection() );
103     }
104 
105     //--------------------------------------------------------------------
106     Reference< XObjectNames > SAL_CALL ConnectionTools::getObjectNames() throw (RuntimeException)
107     {
108         EntryGuard aGuard( *this );
109         return new ObjectNames( getContext(), getConnection() );
110     }
111 
112     //--------------------------------------------------------------------
113     Reference< XDataSourceMetaData > SAL_CALL ConnectionTools::getDataSourceMetaData() throw (RuntimeException)
114     {
115         EntryGuard aGuard( *this );
116         return new DataSourceMetaData( getContext(), getConnection() );
117     }
118     //--------------------------------------------------------------------
119     Reference< container::XNameAccess > SAL_CALL ConnectionTools::getFieldsByCommandDescriptor( ::sal_Int32 commandType, const ::rtl::OUString& command, Reference< lang::XComponent >& keepFieldsAlive ) throw (sdbc::SQLException, RuntimeException)
120     {
121         EntryGuard aGuard( *this );
122         ::dbtools::SQLExceptionInfo aErrorInfo;
123         Reference< container::XNameAccess > xRet = ::dbtools::getFieldsByCommandDescriptor(getConnection(),commandType,command,keepFieldsAlive,&aErrorInfo);
124         if ( aErrorInfo.isValid() )
125             aErrorInfo.doThrow();
126         return xRet;
127     }
128     //--------------------------------------------------------------------
129     Reference< sdb::XSingleSelectQueryComposer > SAL_CALL ConnectionTools::getComposer( ::sal_Int32 commandType, const ::rtl::OUString& command ) throw (::com::sun::star::uno::RuntimeException)
130     {
131         EntryGuard aGuard( *this );
132         dbtools::StatementComposer aComposer(getConnection(), command, commandType, sal_True );
133         aComposer.setDisposeComposer(sal_False);
134         return aComposer.getComposer();
135     }
136 
137     //--------------------------------------------------------------------
138     ::rtl::OUString SAL_CALL ConnectionTools::getImplementationName() throw (RuntimeException)
139     {
140         return getImplementationName_static();
141     }
142 
143     //--------------------------------------------------------------------
144     ::sal_Bool SAL_CALL ConnectionTools::supportsService(const ::rtl::OUString & _ServiceName) throw (RuntimeException)
145     {
146         Sequence< ::rtl::OUString > aSupported( getSupportedServiceNames() );
147         const ::rtl::OUString* begin = aSupported.getConstArray();
148         const ::rtl::OUString* end = aSupported.getConstArray() + aSupported.getLength();
149         return ::std::find( begin, end, _ServiceName ) != end;
150     }
151 
152     //--------------------------------------------------------------------
153     Sequence< ::rtl::OUString > SAL_CALL ConnectionTools::getSupportedServiceNames() throw (RuntimeException)
154     {
155         return getSupportedServiceNames_static();
156     }
157 
158     //--------------------------------------------------------------------
159     ::rtl::OUString SAL_CALL ConnectionTools::getImplementationName_static()
160     {
161         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.dbaccess.ConnectionTools" ) );
162     }
163 
164     //--------------------------------------------------------------------
165     Sequence< ::rtl::OUString > SAL_CALL ConnectionTools::getSupportedServiceNames_static()
166     {
167         Sequence< ::rtl::OUString > aSupported( 1 );
168         aSupported[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.tools.ConnectionTools" ) );
169         return aSupported;
170     }
171 
172     //--------------------------------------------------------------------
173 	Reference< XInterface > SAL_CALL ConnectionTools::Create(const Reference< XComponentContext >& _rxContext )
174     {
175         return *( new ConnectionTools( ::comphelper::ComponentContext( _rxContext ) ) );
176     }
177 
178     //--------------------------------------------------------------------
179     void SAL_CALL ConnectionTools::initialize(const Sequence< Any > & _rArguments) throw (RuntimeException, Exception)
180     {
181         ::osl::MutexGuard aGuard( getMutex() );
182 
183         ::comphelper::NamedValueCollection aArguments( _rArguments );
184 
185         Reference< XConnection > xConnection;
186         aArguments.get( "Connection" ) >>= xConnection;
187         if ( !xConnection.is() )
188             throw IllegalArgumentException();
189 
190         setWeakConnection( xConnection );
191     }
192 
193 
194 //........................................................................
195 } // namespace sdbtools
196 //........................................................................
197 
198