1 /*************************************************************************
3 * $RCSfile: pq_driver.cxx,v $
7 * last change: $Author: jbu $ $Date: 2004/05/09 19:47:13 $
9 * The Contents of this file are made available subject to the terms of
10 * either of the following licenses
12 * - GNU Lesser General Public License Version 2.1
13 * - Sun Industry Standards Source License Version 1.1
15 * Sun Microsystems Inc., October, 2000
17 * GNU Lesser General Public License Version 2.1
18 * =============================================
19 * Copyright 2000 by Sun Microsystems, Inc.
20 * 901 San Antonio Road, Palo Alto, CA 94303, USA
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Lesser General Public
24 * License version 2.1, as published by the Free Software Foundation.
26 * This library is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29 * Lesser General Public License for more details.
31 * You should have received a copy of the GNU Lesser General Public
32 * License along with this library; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
37 * Sun Industry Standards Source License Version 1.1
38 * =================================================
39 * The contents of this file are subject to the Sun Industry Standards
40 * Source License Version 1.1 (the "License"); You may not use this file
41 * except in compliance with the License. You may obtain a copy of the
42 * License at http://www.openoffice.org/license.html.
44 * Software provided under this License is provided on an "AS IS" basis,
45 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
46 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
47 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
48 * See the License for the specific provisions governing your rights and
49 * obligations concerning the Software.
51 * The Initial Developer of the Original Code is: Joerg Budischewski
53 * Copyright: 2000 by Sun Microsystems, Inc.
55 * All Rights Reserved.
57 * Contributor(s): Joerg Budischewski
60 ************************************************************************/
64 #include <cppuhelper/factory.hxx>
65 #include <cppuhelper/compbase1.hxx>
66 #include <cppuhelper/compbase2.hxx>
67 #include <cppuhelper/implementationentry.hxx>
69 #include <com/sun/star/beans/XPropertySet.hpp>
71 #include "pq_driver.hxx"
74 using rtl::OUStringToOString
;
75 using osl::MutexGuard
;
77 using cppu::WeakComponentImplHelper2
;
79 using com::sun::star::lang::XSingleComponentFactory
;
80 using com::sun::star::lang::XServiceInfo
;
81 using com::sun::star::lang::XComponent
;
83 using com::sun::star::uno::RuntimeException
;
84 using com::sun::star::uno::Exception
;
85 using com::sun::star::uno::Sequence
;
86 using com::sun::star::uno::Reference
;
87 using com::sun::star::uno::XInterface
;
88 using com::sun::star::uno::UNO_QUERY
;
89 using com::sun::star::uno::XComponentContext
;
90 using com::sun::star::uno::Any
;
92 using com::sun::star::beans::PropertyValue
;
93 using com::sun::star::beans::XPropertySet
;
95 using com::sun::star::sdbc::XConnection
;
96 using com::sun::star::sdbc::SQLException
;
97 using com::sun::star::sdbc::DriverPropertyInfo
;
99 using com::sun::star::sdbcx::XTablesSupplier
;
102 namespace pq_sdbc_driver
104 #define ASCII_STR(x) OUString( RTL_CONSTASCII_USTRINGPARAM( x ) )
106 OUString
DriverGetImplementationName()
111 MutexGuard
guard( osl::Mutex::getGlobalMutex() );
112 static OUString
instance(
113 RTL_CONSTASCII_USTRINGPARAM( "org.openoffice.comp.connectivity.pq.Driver" ) );
119 Sequence
< OUString
> DriverGetSupportedServiceNames()
121 static Sequence
< OUString
> *p
;
124 MutexGuard
guard( osl::Mutex::getGlobalMutex() );
125 OUString
tmp( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdbc.Driver" ) );
126 static Sequence
< OUString
> instance( &tmp
,1 );
132 Reference
< XConnection
> Driver::connect(
133 const OUString
& url
,const Sequence
< PropertyValue
>& info
)
134 throw (SQLException
, RuntimeException
)
136 if( ! acceptsURL( url
) ) // XDriver spec tells me to do so ...
137 return Reference
< XConnection
> ();
139 Sequence
< Any
> seq ( 2 );
142 return Reference
< XConnection
> (
143 m_smgr
->createInstanceWithArgumentsAndContext(
144 OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.connectivity.pq.Connection" ) ),
149 sal_Bool
Driver::acceptsURL( const ::rtl::OUString
& url
)
150 throw (SQLException
, RuntimeException
)
152 return url
.compareToAscii( RTL_CONSTASCII_STRINGPARAM( "sdbc:postgresql:" ) ) == 0;
155 Sequence
< DriverPropertyInfo
> Driver::getPropertyInfo(
156 const OUString
& url
,const Sequence
< PropertyValue
>& info
)
157 throw (SQLException
, RuntimeException
)
159 return Sequence
< DriverPropertyInfo
> ();
162 sal_Int32
Driver::getMajorVersion( ) throw (RuntimeException
)
164 return PQ_SDBC_MAJOR
;
168 sal_Int32
Driver::getMinorVersion( ) throw (RuntimeException
)
170 return PQ_SDBC_MINOR
;
174 OUString SAL_CALL
Driver::getImplementationName()
175 throw(::com::sun::star::uno::RuntimeException
)
177 return DriverGetImplementationName();
180 sal_Bool
Driver::supportsService(const OUString
& ServiceName
)
181 throw(::com::sun::star::uno::RuntimeException
)
183 Sequence
< OUString
> serviceNames
= DriverGetSupportedServiceNames();
184 for( int i
= 0 ; i
< serviceNames
.getLength() ; i
++ )
185 if( serviceNames
[i
] == ServiceName
)
190 Sequence
< OUString
> Driver::getSupportedServiceNames(void)
191 throw(::com::sun::star::uno::RuntimeException
)
193 return DriverGetSupportedServiceNames();
197 void Driver::disposing()
203 Reference
< XTablesSupplier
> Driver::getDataDefinitionByConnection(
204 const Reference
< XConnection
>& connection
)
205 throw (SQLException
, RuntimeException
)
207 return Reference
< XTablesSupplier
>( connection
, UNO_QUERY
);
210 Reference
< XTablesSupplier
> Driver::getDataDefinitionByURL(
211 const ::rtl::OUString
& url
, const Sequence
< PropertyValue
>& info
)
212 throw (SQLException
, RuntimeException
)
214 return Reference
< XTablesSupplier
> ( connect( url
, info
), UNO_QUERY
);
218 Reference
< XInterface
> DriverCreateInstance( const Reference
< XComponentContext
> & ctx
)
220 Reference
< XInterface
> ret
= * new Driver( ctx
);
227 class OOneInstanceComponentFactory
:
229 public WeakComponentImplHelper2
< XSingleComponentFactory
, XServiceInfo
>
232 OOneInstanceComponentFactory(
233 const OUString
& rImplementationName_
,
234 cppu::ComponentFactoryFunc fptr
,
235 const Sequence
< OUString
> & serviceNames
,
236 const Reference
< XComponentContext
> & defaultContext
) :
237 WeakComponentImplHelper2
< XSingleComponentFactory
, XServiceInfo
>( this->m_mutex
),
238 m_implName( rImplementationName_
),
240 m_serviceNames( serviceNames
),
241 m_defaultContext( defaultContext
)
245 // XSingleComponentFactory
246 virtual Reference
< XInterface
> SAL_CALL
createInstanceWithContext(
247 Reference
< XComponentContext
> const & xContext
)
248 throw (Exception
, RuntimeException
);
249 virtual Reference
< XInterface
> SAL_CALL
createInstanceWithArgumentsAndContext(
250 Sequence
< Any
> const & rArguments
,
251 Reference
< XComponentContext
> const & xContext
)
252 throw (Exception
, RuntimeException
);
255 OUString SAL_CALL
getImplementationName()
256 throw(::com::sun::star::uno::RuntimeException
)
260 sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
)
261 throw(::com::sun::star::uno::RuntimeException
)
263 for( int i
= 0 ; i
< m_serviceNames
.getLength() ; i
++ )
264 if( m_serviceNames
[i
] == ServiceName
)
268 Sequence
< OUString
> SAL_CALL
getSupportedServiceNames(void)
269 throw(::com::sun::star::uno::RuntimeException
)
271 return m_serviceNames
;
275 virtual void SAL_CALL
disposing();
278 cppu::ComponentFactoryFunc m_create
;
279 Sequence
< OUString
> m_serviceNames
;
281 Reference
< XInterface
> m_theInstance
;
282 Reference
< XComponentContext
> m_defaultContext
;
285 Reference
< XInterface
> OOneInstanceComponentFactory::createInstanceWithArgumentsAndContext(
286 Sequence
< Any
> const &rArguments
, const Reference
< XComponentContext
> & ctx
)
287 throw( RuntimeException
, Exception
)
289 return createInstanceWithContext( ctx
);
292 Reference
< XInterface
> OOneInstanceComponentFactory::createInstanceWithContext(
293 const Reference
< XComponentContext
> & ctx
)
294 throw( RuntimeException
, Exception
)
296 if( ! m_theInstance
.is() )
298 // work around the problem in sdbc
299 Reference
< XComponentContext
> useCtx
= ctx
;
301 useCtx
= m_defaultContext
;
302 Reference
< XInterface
> theInstance
= m_create( useCtx
);
303 MutexGuard
guard( osl::Mutex::getGlobalMutex() );
304 if( ! m_theInstance
.is () )
306 m_theInstance
= theInstance
;
309 return m_theInstance
;
312 void OOneInstanceComponentFactory::disposing()
314 Reference
< XComponent
> rComp
;
316 MutexGuard
guard( osl::Mutex::getGlobalMutex() );
317 rComp
= Reference
< XComponent
>( m_theInstance
, UNO_QUERY
);
318 m_theInstance
.clear();
324 // Reference< XSingleComponentFactory > createOneInstanceComponentFactory(
325 // cppu::ComponentFactoryFunc fptr,
326 // ::rtl::OUString const & rImplementationName,
327 // ::com::sun::star::uno::Sequence< ::rtl::OUString > const & rServiceNames,
328 // rtl_ModuleCount * pModCount = 0 )
331 // return new OOneInstanceComponentFactory( rImplementationName, fptr , rServiceNames);
336 static struct cppu::ImplementationEntry g_entries
[] =
339 pq_sdbc_driver::DriverCreateInstance
, pq_sdbc_driver::DriverGetImplementationName
,
340 pq_sdbc_driver::DriverGetSupportedServiceNames
, 0,
349 //==================================================================================================
350 void SAL_CALL
component_getImplementationEnvironment(
351 const sal_Char
** ppEnvTypeName
, uno_Environment
** ppEnv
)
353 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
355 //==================================================================================================
356 sal_Bool SAL_CALL
component_writeInfo(
357 void * pServiceManager
, void * pRegistryKey
)
359 return cppu::component_writeInfoHelper( pServiceManager
, pRegistryKey
, g_entries
);
361 //==================================================================================================
362 void * SAL_CALL
component_getFactory(
363 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
365 // need to extract the defaultcontext, because the way, sdbc
366 // bypasses the servicemanager, does not allow to use the
367 // XSingleComponentFactory interface ...
369 Reference
< XSingleComponentFactory
> xFactory
;
370 Reference
< XInterface
> xSmgr( (XInterface
* ) pServiceManager
);
372 for( sal_Int32 i
= 0 ; g_entries
[i
].create
; i
++ )
374 OUString implName
= g_entries
[i
].getImplementationName();
375 if( 0 == implName
.compareToAscii( pImplName
) )
377 Reference
< XComponentContext
> defaultContext
;
378 Reference
< XPropertySet
> propSet( xSmgr
, UNO_QUERY
);
383 propSet
->getPropertyValue( ASCII_STR( "DefaultContext" ) ) >>= defaultContext
;
385 catch( com::sun::star::uno::Exception
&e
)
387 // if there is no default context, ignore it
390 xFactory
= new pq_sdbc_driver::OOneInstanceComponentFactory(
393 g_entries
[i
].getSupportedServiceNames(),
401 pRet
= xFactory
.get();