Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_driver.cxx
blobdf0be6184f9b718eaf6c46dca878e1072016ae4d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * Effective License of whole file:
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software Foundation.
10 * This library 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.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
20 * Parts "Copyright by Sun Microsystems, Inc" prior to August 2011:
22 * The Contents of this file are made available subject to the terms of
23 * the GNU Lesser General Public License Version 2.1
25 * Copyright: 2000 by Sun Microsystems, Inc.
27 * Contributor(s): Joerg Budischewski
29 * All parts contributed on or after August 2011:
31 * This Source Code Form is subject to the terms of the Mozilla Public
32 * License, v. 2.0. If a copy of the MPL was not distributed with this
33 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
35 ************************************************************************/
37 #include <stdio.h>
39 #include <comphelper/processfactory.hxx>
40 #include <cppuhelper/factory.hxx>
41 #include <cppuhelper/compbase1.hxx>
42 #include <cppuhelper/compbase2.hxx>
43 #include <cppuhelper/implementationentry.hxx>
44 #include <cppuhelper/supportsservice.hxx>
46 #include "pq_driver.hxx"
48 using osl::MutexGuard;
50 using cppu::WeakComponentImplHelper2;
52 using com::sun::star::lang::XSingleComponentFactory;
53 using com::sun::star::lang::XServiceInfo;
54 using com::sun::star::lang::XComponent;
56 using com::sun::star::uno::RuntimeException;
57 using com::sun::star::uno::Exception;
58 using com::sun::star::uno::Sequence;
59 using com::sun::star::uno::Reference;
60 using com::sun::star::uno::XInterface;
61 using com::sun::star::uno::UNO_QUERY;
62 using com::sun::star::uno::XComponentContext;
63 using com::sun::star::uno::Any;
65 using com::sun::star::beans::PropertyValue;
67 using com::sun::star::sdbc::XConnection;
68 using com::sun::star::sdbc::SQLException;
69 using com::sun::star::sdbc::DriverPropertyInfo;
71 using com::sun::star::sdbcx::XTablesSupplier;
74 namespace pq_sdbc_driver
77 OUString DriverGetImplementationName()
79 static OUString *p;
80 if (! p )
82 MutexGuard guard( osl::Mutex::getGlobalMutex() );
83 static OUString instance( "org.openoffice.comp.connectivity.pq.Driver.noext" );
84 p = &instance;
86 return *p;
89 Sequence< OUString > DriverGetSupportedServiceNames()
91 static Sequence< OUString > *p;
92 if( ! p )
94 MutexGuard guard( osl::Mutex::getGlobalMutex() );
95 OUString tmp( "com.sun.star.sdbc.Driver" );
96 static Sequence< OUString > instance( &tmp,1 );
97 p = &instance;
99 return *p;
102 Reference< XConnection > Driver::connect(
103 const OUString& url,const Sequence< PropertyValue >& info )
104 throw (SQLException, RuntimeException, std::exception)
106 if( ! acceptsURL( url ) ) // XDriver spec tells me to do so ...
107 return Reference< XConnection > ();
109 Sequence< Any > seq ( 2 );
110 seq[0] <<= url;
111 seq[1] <<= info;
112 return Reference< XConnection> (
113 m_smgr->createInstanceWithArgumentsAndContext(
114 OUString("org.openoffice.comp.connectivity.pq.Connection.noext" ),
115 seq, m_ctx ),
116 UNO_QUERY );
119 sal_Bool Driver::acceptsURL( const OUString& url )
120 throw (SQLException, RuntimeException, std::exception)
122 return url.startsWith( "sdbc:postgresql:" );
125 Sequence< DriverPropertyInfo > Driver::getPropertyInfo(
126 const OUString& url,const Sequence< PropertyValue >& info )
127 throw (SQLException, RuntimeException, std::exception)
129 (void)url; (void)info;
130 return Sequence< DriverPropertyInfo > ();
133 sal_Int32 Driver::getMajorVersion( ) throw (RuntimeException, std::exception)
135 return PQ_SDBC_MAJOR;
139 sal_Int32 Driver::getMinorVersion( ) throw (RuntimeException, std::exception)
141 return PQ_SDBC_MINOR;
144 // XServiceInfo
145 OUString SAL_CALL Driver::getImplementationName()
146 throw(::com::sun::star::uno::RuntimeException, std::exception)
148 return DriverGetImplementationName();
151 sal_Bool Driver::supportsService(const OUString& ServiceName)
152 throw(::com::sun::star::uno::RuntimeException, std::exception)
154 return cppu::supportsService(this, ServiceName);
157 Sequence< OUString > Driver::getSupportedServiceNames(void)
158 throw(::com::sun::star::uno::RuntimeException, std::exception)
160 return DriverGetSupportedServiceNames();
163 // XComponent
164 void Driver::disposing()
170 Reference< XTablesSupplier > Driver::getDataDefinitionByConnection(
171 const Reference< XConnection >& connection )
172 throw (SQLException, RuntimeException, std::exception)
174 return Reference< XTablesSupplier >( connection , UNO_QUERY );
177 Reference< XTablesSupplier > Driver::getDataDefinitionByURL(
178 const OUString& url, const Sequence< PropertyValue >& info )
179 throw (SQLException, RuntimeException, std::exception)
181 return Reference< XTablesSupplier > ( connect( url, info ), UNO_QUERY );
185 Reference< XInterface > DriverCreateInstance( const Reference < XComponentContext > & ctx )
187 Reference< XInterface > ret = * new Driver( ctx );
188 return ret;
194 class OOneInstanceComponentFactory :
195 public MutexHolder,
196 public WeakComponentImplHelper2< XSingleComponentFactory, XServiceInfo >
198 public:
199 OOneInstanceComponentFactory(
200 const OUString & rImplementationName_,
201 cppu::ComponentFactoryFunc fptr,
202 const Sequence< OUString > & serviceNames,
203 const Reference< XComponentContext > & defaultContext) :
204 WeakComponentImplHelper2< XSingleComponentFactory, XServiceInfo >( this->m_mutex ),
205 m_create( fptr ),
206 m_serviceNames( serviceNames ),
207 m_implName( rImplementationName_ ),
208 m_defaultContext( defaultContext )
212 // XSingleComponentFactory
213 virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
214 Reference< XComponentContext > const & xContext )
215 throw (Exception, RuntimeException, std::exception) SAL_OVERRIDE;
216 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
217 Sequence< Any > const & rArguments,
218 Reference< XComponentContext > const & xContext )
219 throw (Exception, RuntimeException, std::exception) SAL_OVERRIDE;
221 // XServiceInfo
222 OUString SAL_CALL getImplementationName()
223 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
225 return m_implName;
227 sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
228 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
230 for( int i = 0 ; i < m_serviceNames.getLength() ; i ++ )
231 if( m_serviceNames[i] == ServiceName )
232 return sal_True;
233 return sal_False;
235 Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
236 throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE
238 return m_serviceNames;
241 // XComponent
242 virtual void SAL_CALL disposing() SAL_OVERRIDE;
244 private:
245 cppu::ComponentFactoryFunc m_create;
246 Sequence< OUString > m_serviceNames;
247 OUString m_implName;
248 Reference< XInterface > m_theInstance;
249 Reference< XComponentContext > m_defaultContext;
252 Reference< XInterface > OOneInstanceComponentFactory::createInstanceWithArgumentsAndContext(
253 Sequence< Any > const &rArguments, const Reference< XComponentContext > & ctx )
254 throw( RuntimeException, Exception, std::exception )
256 (void)rArguments;
257 return createInstanceWithContext( ctx );
260 Reference< XInterface > OOneInstanceComponentFactory::createInstanceWithContext(
261 const Reference< XComponentContext > & ctx )
262 throw( RuntimeException, Exception, std::exception )
264 if( ! m_theInstance.is() )
266 // work around the problem in sdbc
267 Reference< XComponentContext > useCtx = ctx;
268 if( ! useCtx.is() )
269 useCtx = m_defaultContext;
270 Reference< XInterface > theInstance = m_create( useCtx );
271 MutexGuard guard( osl::Mutex::getGlobalMutex() );
272 if( ! m_theInstance.is () )
274 m_theInstance = theInstance;
277 return m_theInstance;
280 void OOneInstanceComponentFactory::disposing()
282 Reference< XComponent > rComp;
284 MutexGuard guard( osl::Mutex::getGlobalMutex() );
285 rComp = Reference< XComponent >( m_theInstance, UNO_QUERY );
286 m_theInstance.clear();
288 if( rComp.is() )
289 rComp->dispose();
292 // Reference< XSingleComponentFactory > createOneInstanceComponentFactory(
293 // cppu::ComponentFactoryFunc fptr,
294 // OUString const & rImplementationName,
295 // ::com::sun::star::uno::Sequence< OUString > const & rServiceNames,
296 // rtl_ModuleCount * pModCount = 0 )
297 // SAL_THROW(())
298 // {
299 // return new OOneInstanceComponentFactory( rImplementationName, fptr , rServiceNames);
300 // }
304 static const struct cppu::ImplementationEntry g_entries[] =
307 pq_sdbc_driver::DriverCreateInstance, pq_sdbc_driver::DriverGetImplementationName,
308 pq_sdbc_driver::DriverGetSupportedServiceNames, 0,
309 0 , 0
311 { 0, 0, 0, 0, 0, 0 }
314 extern "C"
317 SAL_DLLPUBLIC_EXPORT void * SAL_CALL postgresql_sdbc_component_getFactory(
318 const sal_Char * pImplName, void * pServiceManager, void * )
320 // need to extract the defaultcontext, because the way, sdbc
321 // bypasses the servicemanager, does not allow to use the
322 // XSingleComponentFactory interface ...
323 void * pRet = 0;
324 Reference< XSingleComponentFactory > xFactory;
325 Reference< com::sun::star::lang::XMultiServiceFactory > xSmgr(
326 static_cast< XInterface * >(pServiceManager),
327 com::sun::star::uno::UNO_QUERY_THROW );
329 for( sal_Int32 i = 0 ; g_entries[i].create ; i ++ )
331 OUString implName = g_entries[i].getImplementationName();
332 if( implName.equalsAscii( pImplName ) )
334 Reference< XComponentContext > defaultContext(
335 comphelper::getComponentContext( xSmgr ) );
336 xFactory = new pq_sdbc_driver::OOneInstanceComponentFactory(
337 implName,
338 g_entries[i].create,
339 g_entries[i].getSupportedServiceNames(),
340 defaultContext );
344 if( xFactory.is() )
346 xFactory->acquire();
347 pRet = xFactory.get();
349 return pRet;
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */