Bump for 3.6-28
[LibreOffice.git] / connectivity / source / drivers / postgresql / pq_driver.cxx
blobed8480c318c4231aa4889896a53aea975f465c88
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 * Version: MPL 1.1 / GPLv3+ / LGPLv2.1+
33 * The contents of this file are subject to the Mozilla Public License Version
34 * 1.1 (the "License"); you may not use this file except in compliance with
35 * the License or as specified alternatively below. You may obtain a copy of
36 * the License at http://www.mozilla.org/MPL/
38 * Software distributed under the License is distributed on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
40 * for the specific language governing rights and limitations under the
41 * License.
43 * Major Contributor(s):
44 * [ Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu> ]
46 * All Rights Reserved.
48 * For minor contributions see the git repository.
50 * Alternatively, the contents of this file may be used under the terms of
51 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
52 * the GNU Lesser General Public License Version 2.1 or later (the "LGPLv2.1+"),
53 * in which case the provisions of the GPLv3+ or the LGPLv2.1+ are applicable
54 * instead of those above.
56 ************************************************************************/
58 #include <stdio.h>
60 #include <cppuhelper/factory.hxx>
61 #include <cppuhelper/compbase1.hxx>
62 #include <cppuhelper/compbase2.hxx>
63 #include <cppuhelper/implementationentry.hxx>
65 #include <com/sun/star/beans/XPropertySet.hpp>
67 #include "pq_driver.hxx"
69 using rtl::OUString;
70 using rtl::OUStringToOString;
71 using osl::MutexGuard;
73 using cppu::WeakComponentImplHelper2;
75 using com::sun::star::lang::XSingleComponentFactory;
76 using com::sun::star::lang::XServiceInfo;
77 using com::sun::star::lang::XComponent;
79 using com::sun::star::uno::RuntimeException;
80 using com::sun::star::uno::Exception;
81 using com::sun::star::uno::Sequence;
82 using com::sun::star::uno::Reference;
83 using com::sun::star::uno::XInterface;
84 using com::sun::star::uno::UNO_QUERY;
85 using com::sun::star::uno::XComponentContext;
86 using com::sun::star::uno::Any;
88 using com::sun::star::beans::PropertyValue;
89 using com::sun::star::beans::XPropertySet;
91 using com::sun::star::sdbc::XConnection;
92 using com::sun::star::sdbc::SQLException;
93 using com::sun::star::sdbc::DriverPropertyInfo;
95 using com::sun::star::sdbcx::XTablesSupplier;
98 namespace pq_sdbc_driver
100 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
102 OUString DriverGetImplementationName()
104 static OUString *p;
105 if (! p )
107 MutexGuard guard( osl::Mutex::getGlobalMutex() );
108 static OUString instance(
109 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.connectivity.pq.Driver.noext" ) );
110 p = &instance;
112 return *p;
115 Sequence< OUString > DriverGetSupportedServiceNames()
117 static Sequence< OUString > *p;
118 if( ! p )
120 MutexGuard guard( osl::Mutex::getGlobalMutex() );
121 OUString tmp( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdbc.Driver" ) );
122 static Sequence< OUString > instance( &tmp,1 );
123 p = &instance;
125 return *p;
128 Reference< XConnection > Driver::connect(
129 const OUString& url,const Sequence< PropertyValue >& info )
130 throw (SQLException, RuntimeException)
132 if( ! acceptsURL( url ) ) // XDriver spec tells me to do so ...
133 return Reference< XConnection > ();
135 Sequence< Any > seq ( 2 );
136 seq[0] <<= url;
137 seq[1] <<= info;
138 return Reference< XConnection> (
139 m_smgr->createInstanceWithArgumentsAndContext(
140 OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.connectivity.pq.Connection.noext" ) ),
141 seq, m_ctx ),
142 UNO_QUERY );
145 sal_Bool Driver::acceptsURL( const ::rtl::OUString& url )
146 throw (SQLException, RuntimeException)
148 return url.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "sdbc:postgresql:" ) );
151 Sequence< DriverPropertyInfo > Driver::getPropertyInfo(
152 const OUString& url,const Sequence< PropertyValue >& info )
153 throw (SQLException, RuntimeException)
155 (void)url; (void)info;
156 return Sequence< DriverPropertyInfo > ();
159 sal_Int32 Driver::getMajorVersion( ) throw (RuntimeException)
161 return PQ_SDBC_MAJOR;
165 sal_Int32 Driver::getMinorVersion( ) throw (RuntimeException)
167 return PQ_SDBC_MINOR;
170 // XServiceInfo
171 OUString SAL_CALL Driver::getImplementationName()
172 throw(::com::sun::star::uno::RuntimeException)
174 return DriverGetImplementationName();
177 sal_Bool Driver::supportsService(const OUString& ServiceName)
178 throw(::com::sun::star::uno::RuntimeException)
180 Sequence< OUString > serviceNames = DriverGetSupportedServiceNames();
181 for( int i = 0 ; i < serviceNames.getLength() ; i ++ )
182 if( serviceNames[i] == ServiceName )
183 return sal_True;
184 return sal_False;
187 Sequence< OUString > Driver::getSupportedServiceNames(void)
188 throw(::com::sun::star::uno::RuntimeException)
190 return DriverGetSupportedServiceNames();
193 // XComponent
194 void Driver::disposing()
200 Reference< XTablesSupplier > Driver::getDataDefinitionByConnection(
201 const Reference< XConnection >& connection )
202 throw (SQLException, RuntimeException)
204 return Reference< XTablesSupplier >( connection , UNO_QUERY );
207 Reference< XTablesSupplier > Driver::getDataDefinitionByURL(
208 const ::rtl::OUString& url, const Sequence< PropertyValue >& info )
209 throw (SQLException, RuntimeException)
211 return Reference< XTablesSupplier > ( connect( url, info ), UNO_QUERY );
215 Reference< XInterface > DriverCreateInstance( const Reference < XComponentContext > & ctx )
217 Reference< XInterface > ret = * new Driver( ctx );
218 return ret;
224 class OOneInstanceComponentFactory :
225 public MutexHolder,
226 public WeakComponentImplHelper2< XSingleComponentFactory, XServiceInfo >
228 public:
229 OOneInstanceComponentFactory(
230 const OUString & rImplementationName_,
231 cppu::ComponentFactoryFunc fptr,
232 const Sequence< OUString > & serviceNames,
233 const Reference< XComponentContext > & defaultContext) :
234 WeakComponentImplHelper2< XSingleComponentFactory, XServiceInfo >( this->m_mutex ),
235 m_create( fptr ),
236 m_serviceNames( serviceNames ),
237 m_implName( rImplementationName_ ),
238 m_defaultContext( defaultContext )
242 // XSingleComponentFactory
243 virtual Reference< XInterface > SAL_CALL createInstanceWithContext(
244 Reference< XComponentContext > const & xContext )
245 throw (Exception, RuntimeException);
246 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext(
247 Sequence< Any > const & rArguments,
248 Reference< XComponentContext > const & xContext )
249 throw (Exception, RuntimeException);
251 // XServiceInfo
252 OUString SAL_CALL getImplementationName()
253 throw(::com::sun::star::uno::RuntimeException)
255 return m_implName;
257 sal_Bool SAL_CALL supportsService(const OUString& ServiceName)
258 throw(::com::sun::star::uno::RuntimeException)
260 for( int i = 0 ; i < m_serviceNames.getLength() ; i ++ )
261 if( m_serviceNames[i] == ServiceName )
262 return sal_True;
263 return sal_False;
265 Sequence< OUString > SAL_CALL getSupportedServiceNames(void)
266 throw(::com::sun::star::uno::RuntimeException)
268 return m_serviceNames;
271 // XComponent
272 virtual void SAL_CALL disposing();
274 private:
275 cppu::ComponentFactoryFunc m_create;
276 Sequence< OUString > m_serviceNames;
277 OUString m_implName;
278 Reference< XInterface > m_theInstance;
279 Reference< XComponentContext > m_defaultContext;
282 Reference< XInterface > OOneInstanceComponentFactory::createInstanceWithArgumentsAndContext(
283 Sequence< Any > const &rArguments, const Reference< XComponentContext > & ctx )
284 throw( RuntimeException, Exception )
286 (void)rArguments;
287 return createInstanceWithContext( ctx );
290 Reference< XInterface > OOneInstanceComponentFactory::createInstanceWithContext(
291 const Reference< XComponentContext > & ctx )
292 throw( RuntimeException, Exception )
294 if( ! m_theInstance.is() )
296 // work around the problem in sdbc
297 Reference< XComponentContext > useCtx = ctx;
298 if( ! useCtx.is() )
299 useCtx = m_defaultContext;
300 Reference< XInterface > theInstance = m_create( useCtx );
301 MutexGuard guard( osl::Mutex::getGlobalMutex() );
302 if( ! m_theInstance.is () )
304 m_theInstance = theInstance;
307 return m_theInstance;
310 void OOneInstanceComponentFactory::disposing()
312 Reference< XComponent > rComp;
314 MutexGuard guard( osl::Mutex::getGlobalMutex() );
315 rComp = Reference< XComponent >( m_theInstance, UNO_QUERY );
316 m_theInstance.clear();
318 if( rComp.is() )
319 rComp->dispose();
322 // Reference< XSingleComponentFactory > createOneInstanceComponentFactory(
323 // cppu::ComponentFactoryFunc fptr,
324 // ::rtl::OUString const & rImplementationName,
325 // ::com::sun::star::uno::Sequence< ::rtl::OUString > const & rServiceNames,
326 // rtl_ModuleCount * pModCount = 0 )
327 // SAL_THROW(())
328 // {
329 // return new OOneInstanceComponentFactory( rImplementationName, fptr , rServiceNames);
330 // }
334 static struct cppu::ImplementationEntry g_entries[] =
337 pq_sdbc_driver::DriverCreateInstance, pq_sdbc_driver::DriverGetImplementationName,
338 pq_sdbc_driver::DriverGetSupportedServiceNames, 0,
339 0 , 0
341 { 0, 0, 0, 0, 0, 0 }
344 extern "C"
347 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
348 const sal_Char * pImplName, void * pServiceManager, void * )
350 // need to extract the defaultcontext, because the way, sdbc
351 // bypasses the servicemanager, does not allow to use the
352 // XSingleComponentFactory interface ...
353 void * pRet = 0;
354 Reference< XSingleComponentFactory > xFactory;
355 Reference< XInterface > xSmgr( (XInterface * ) pServiceManager );
357 for( sal_Int32 i = 0 ; g_entries[i].create ; i ++ )
359 OUString implName = g_entries[i].getImplementationName();
360 if( 0 == implName.compareToAscii( pImplName ) )
362 Reference< XComponentContext > defaultContext;
363 Reference< XPropertySet > propSet( xSmgr, UNO_QUERY );
364 if( propSet.is() )
368 propSet->getPropertyValue( ASCII_STR( "DefaultContext" ) ) >>= defaultContext;
370 catch( com::sun::star::uno::Exception & )
372 // if there is no default context, ignore it
375 xFactory = new pq_sdbc_driver::OOneInstanceComponentFactory(
376 implName,
377 g_entries[i].create,
378 g_entries[i].getSupportedServiceNames(),
379 defaultContext );
383 if( xFactory.is() )
385 xFactory->acquire();
386 pRet = xFactory.get();
388 return pRet;