1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "KConnection.hxx"
28cdf0e10cSrcweir #include "KDatabaseMetaData.hxx"
29cdf0e10cSrcweir #include "KStatement.hxx"
30cdf0e10cSrcweir #include "KPreparedStatement.hxx"
31cdf0e10cSrcweir #include "KDriver.hxx"
32cdf0e10cSrcweir #include "KCatalog.hxx"
33cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
34cdf0e10cSrcweir #include <com/sun/star/sdbc/TransactionIsolation.hpp>
35cdf0e10cSrcweir #include <shell/kde_headers.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace connectivity::kab;
38cdf0e10cSrcweir using namespace com::sun::star::uno;
39cdf0e10cSrcweir using namespace com::sun::star::lang;
40cdf0e10cSrcweir using namespace com::sun::star::beans;
41cdf0e10cSrcweir using namespace com::sun::star::sdbc;
42cdf0e10cSrcweir using namespace com::sun::star::sdbcx;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir IMPLEMENT_SERVICE_INFO(KabConnection, "com.sun.star.sdbc.drivers.KabConnection", "com.sun.star.sdbc.Connection")
45cdf0e10cSrcweir //-----------------------------------------------------------------------------
KabConnection(KabDriver * _pDriver)46cdf0e10cSrcweir KabConnection::KabConnection(KabDriver*	_pDriver)
47cdf0e10cSrcweir 		 : OMetaConnection_BASE(m_aMutex),
48cdf0e10cSrcweir 		 OSubComponent<KabConnection, KabConnection_BASE>((::cppu::OWeakObject*)_pDriver, this),
49cdf0e10cSrcweir 		 m_xMetaData(NULL),
50cdf0e10cSrcweir 		 m_pAddressBook(NULL),
51cdf0e10cSrcweir 		 m_pDriver(_pDriver)
52cdf0e10cSrcweir {
53cdf0e10cSrcweir 	m_pDriver->acquire();
54cdf0e10cSrcweir }
55cdf0e10cSrcweir //-----------------------------------------------------------------------------
~KabConnection()56cdf0e10cSrcweir KabConnection::~KabConnection()
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	if (!isClosed())
59cdf0e10cSrcweir 		close();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	m_pDriver->release();
62cdf0e10cSrcweir 	m_pDriver = NULL;
63cdf0e10cSrcweir }
64cdf0e10cSrcweir //-----------------------------------------------------------------------------
release()65cdf0e10cSrcweir void SAL_CALL KabConnection::release() throw()
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	relase_ChildImpl();
68cdf0e10cSrcweir }
69cdf0e10cSrcweir // -----------------------------------------------------------------------------
construct(const::rtl::OUString &,const Sequence<PropertyValue> &)70cdf0e10cSrcweir void KabConnection::construct(const ::rtl::OUString&, const Sequence< PropertyValue >&) throw(SQLException)
71cdf0e10cSrcweir {
72cdf0e10cSrcweir 	osl_incrementInterlockedCount( &m_refCount );
73cdf0e10cSrcweir 
74cdf0e10cSrcweir 	// create a KDE address book object
75cdf0e10cSrcweir 	m_pAddressBook = KABC::StdAddressBook::self();
76cdf0e10cSrcweir 	m_pAddressBook->setAutomaticSave(false);
77cdf0e10cSrcweir // perharps we should analyze the URL to know whether the addressbook is local, over LDAP, etc...
78cdf0e10cSrcweir // perharps we should get some user and password information from "info" properties
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	osl_decrementInterlockedCount( &m_refCount );
81cdf0e10cSrcweir }
82cdf0e10cSrcweir // XServiceInfo
83cdf0e10cSrcweir // --------------------------------------------------------------------------------
createStatement()84cdf0e10cSrcweir Reference< XStatement > SAL_CALL KabConnection::createStatement(  ) throw(SQLException, RuntimeException)
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
87cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	// create a statement
90cdf0e10cSrcweir 	// the statement can only be executed once
91cdf0e10cSrcweir 	Reference< XStatement > xReturn = new KabStatement(this);
92cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
93cdf0e10cSrcweir 	return xReturn;
94cdf0e10cSrcweir }
95cdf0e10cSrcweir // --------------------------------------------------------------------------------
prepareStatement(const::rtl::OUString & _sSql)96cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL KabConnection::prepareStatement( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
99cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 	// create a statement
102cdf0e10cSrcweir 	// the statement can only be executed more than once
103cdf0e10cSrcweir 	Reference< XPreparedStatement > xReturn = new KabPreparedStatement(this, _sSql);
104cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
105cdf0e10cSrcweir 	return xReturn;
106cdf0e10cSrcweir }
107cdf0e10cSrcweir // --------------------------------------------------------------------------------
prepareCall(const::rtl::OUString &)108cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL KabConnection::prepareCall( const ::rtl::OUString& ) throw(SQLException, RuntimeException)
109cdf0e10cSrcweir {
110cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
111cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	// not implemented yet :-) a task to do
114cdf0e10cSrcweir 	return NULL;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir // --------------------------------------------------------------------------------
nativeSQL(const::rtl::OUString & _sSql)117cdf0e10cSrcweir ::rtl::OUString SAL_CALL KabConnection::nativeSQL( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
120cdf0e10cSrcweir 	// when you need to transform SQL92 to you driver specific you can do it here
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	return _sSql;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir // --------------------------------------------------------------------------------
setAutoCommit(sal_Bool)125cdf0e10cSrcweir void SAL_CALL KabConnection::setAutoCommit( sal_Bool ) throw(SQLException, RuntimeException)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
128cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
129cdf0e10cSrcweir 	// here you  have to set your commit mode please have a look at the jdbc documentation to get a clear explanation
130cdf0e10cSrcweir }
131cdf0e10cSrcweir // --------------------------------------------------------------------------------
getAutoCommit()132cdf0e10cSrcweir sal_Bool SAL_CALL KabConnection::getAutoCommit(  ) throw(SQLException, RuntimeException)
133cdf0e10cSrcweir {
134cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
135cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
136cdf0e10cSrcweir 	// you have to distinguish which if you are in autocommit mode or not
137cdf0e10cSrcweir 	// at normal case true should be fine here
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	return sal_True;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir // --------------------------------------------------------------------------------
commit()142cdf0e10cSrcweir void SAL_CALL KabConnection::commit(  ) throw(SQLException, RuntimeException)
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
145cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	// when you database does support transactions you should commit here
148cdf0e10cSrcweir }
149cdf0e10cSrcweir // --------------------------------------------------------------------------------
rollback()150cdf0e10cSrcweir void SAL_CALL KabConnection::rollback(  ) throw(SQLException, RuntimeException)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
153cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	// same as commit but for the other case
156cdf0e10cSrcweir }
157cdf0e10cSrcweir // --------------------------------------------------------------------------------
isClosed()158cdf0e10cSrcweir sal_Bool SAL_CALL KabConnection::isClosed(  ) throw(SQLException, RuntimeException)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	// just simple -> we are closed when we are disposed, that means someone called dispose(); (XComponent)
163cdf0e10cSrcweir 	return KabConnection_BASE::rBHelper.bDisposed;
164cdf0e10cSrcweir }
165cdf0e10cSrcweir // --------------------------------------------------------------------------------
getMetaData()166cdf0e10cSrcweir Reference< XDatabaseMetaData > SAL_CALL KabConnection::getMetaData(  ) throw(SQLException, RuntimeException)
167cdf0e10cSrcweir {
168cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
169cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	// here we have to create the class with biggest interface
172cdf0e10cSrcweir 	// The answer is 42 :-)
173cdf0e10cSrcweir 	Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
174cdf0e10cSrcweir 	if (!xMetaData.is())
175cdf0e10cSrcweir 	{
176cdf0e10cSrcweir 		xMetaData = new KabDatabaseMetaData(this); // need the connection because it can return it
177cdf0e10cSrcweir 		m_xMetaData = xMetaData;
178cdf0e10cSrcweir 	}
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 	return xMetaData;
181cdf0e10cSrcweir }
182cdf0e10cSrcweir // --------------------------------------------------------------------------------
setReadOnly(sal_Bool)183cdf0e10cSrcweir void SAL_CALL KabConnection::setReadOnly( sal_Bool ) throw(SQLException, RuntimeException)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
186cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	// set you connection to readonly
189cdf0e10cSrcweir }
190cdf0e10cSrcweir // --------------------------------------------------------------------------------
isReadOnly()191cdf0e10cSrcweir sal_Bool SAL_CALL KabConnection::isReadOnly(  ) throw(SQLException, RuntimeException)
192cdf0e10cSrcweir {
193cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
194cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 	// return if your connection to readonly
197cdf0e10cSrcweir 	return sal_False;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir // --------------------------------------------------------------------------------
setCatalog(const::rtl::OUString &)200cdf0e10cSrcweir void SAL_CALL KabConnection::setCatalog( const ::rtl::OUString& ) throw(SQLException, RuntimeException)
201cdf0e10cSrcweir {
202cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
203cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	// if your database doesn't work with catalogs you go to next method otherwise you kjnow what to do
206cdf0e10cSrcweir }
207cdf0e10cSrcweir // --------------------------------------------------------------------------------
getCatalog()208cdf0e10cSrcweir ::rtl::OUString SAL_CALL KabConnection::getCatalog(  ) throw(SQLException, RuntimeException)
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
211cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 	// return your current catalog
215cdf0e10cSrcweir 	return ::rtl::OUString();
216cdf0e10cSrcweir }
217cdf0e10cSrcweir // --------------------------------------------------------------------------------
setTransactionIsolation(sal_Int32)218cdf0e10cSrcweir void SAL_CALL KabConnection::setTransactionIsolation( sal_Int32 ) throw(SQLException, RuntimeException)
219cdf0e10cSrcweir {
220cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
221cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 	// set your isolation level
224cdf0e10cSrcweir 	// please have a look at @see com.sun.star.sdbc.TransactionIsolation
225cdf0e10cSrcweir }
226cdf0e10cSrcweir // --------------------------------------------------------------------------------
getTransactionIsolation()227cdf0e10cSrcweir sal_Int32 SAL_CALL KabConnection::getTransactionIsolation(  ) throw(SQLException, RuntimeException)
228cdf0e10cSrcweir {
229cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
230cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
231cdf0e10cSrcweir 
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 	// please have a look at @see com.sun.star.sdbc.TransactionIsolation
234cdf0e10cSrcweir 	return TransactionIsolation::NONE;
235cdf0e10cSrcweir }
236cdf0e10cSrcweir // --------------------------------------------------------------------------------
getTypeMap()237cdf0e10cSrcweir Reference< ::com::sun::star::container::XNameAccess > SAL_CALL KabConnection::getTypeMap(  ) throw(SQLException, RuntimeException)
238cdf0e10cSrcweir {
239cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
240cdf0e10cSrcweir 	checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 	// if your driver has special database types you can return it here
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	return NULL;
245cdf0e10cSrcweir }
246cdf0e10cSrcweir // --------------------------------------------------------------------------------
setTypeMap(const Reference<::com::sun::star::container::XNameAccess> &)247cdf0e10cSrcweir void SAL_CALL KabConnection::setTypeMap( const Reference< ::com::sun::star::container::XNameAccess >& ) throw(SQLException, RuntimeException)
248cdf0e10cSrcweir {
249cdf0e10cSrcweir 	// the other way around
250cdf0e10cSrcweir }
251cdf0e10cSrcweir // --------------------------------------------------------------------------------
252cdf0e10cSrcweir // XCloseable
close()253cdf0e10cSrcweir void SAL_CALL KabConnection::close(  ) throw(SQLException, RuntimeException)
254cdf0e10cSrcweir {
255cdf0e10cSrcweir 	{
256cdf0e10cSrcweir 		::osl::MutexGuard aGuard( m_aMutex );
257cdf0e10cSrcweir 		checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
258cdf0e10cSrcweir 	}
259cdf0e10cSrcweir 	dispose();
260cdf0e10cSrcweir }
261cdf0e10cSrcweir // --------------------------------------------------------------------------------
262cdf0e10cSrcweir // XWarningsSupplier
getWarnings()263cdf0e10cSrcweir Any SAL_CALL KabConnection::getWarnings(  ) throw(SQLException, RuntimeException)
264cdf0e10cSrcweir {
265cdf0e10cSrcweir 	// when you collected some warnings -> return it
266cdf0e10cSrcweir 	return Any();
267cdf0e10cSrcweir }
268cdf0e10cSrcweir // --------------------------------------------------------------------------------
clearWarnings()269cdf0e10cSrcweir void SAL_CALL KabConnection::clearWarnings(  ) throw(SQLException, RuntimeException)
270cdf0e10cSrcweir {
271cdf0e10cSrcweir 	// you should clear your collected warnings here
272cdf0e10cSrcweir }
273cdf0e10cSrcweir //------------------------------------------------------------------------------
disposing()274cdf0e10cSrcweir void KabConnection::disposing()
275cdf0e10cSrcweir {
276cdf0e10cSrcweir 	// we noticed that we should be destroied in near future so we have to dispose our statements
277cdf0e10cSrcweir 	::osl::MutexGuard aGuard(m_aMutex);
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	for (OWeakRefArray::iterator i = m_aStatements.begin(); m_aStatements.end() != i; ++i)
280cdf0e10cSrcweir 	{
281cdf0e10cSrcweir 		Reference< XComponent > xComp(i->get(), UNO_QUERY);
282cdf0e10cSrcweir 		if (xComp.is())
283cdf0e10cSrcweir 			xComp->dispose();
284cdf0e10cSrcweir 	}
285cdf0e10cSrcweir 	m_aStatements.clear();
286cdf0e10cSrcweir 
287cdf0e10cSrcweir 	if (m_pAddressBook != NULL)
288cdf0e10cSrcweir 	{
289cdf0e10cSrcweir 		m_pAddressBook->close();
290cdf0e10cSrcweir 		m_pAddressBook = NULL;
291cdf0e10cSrcweir 	}
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 	m_xMetaData = ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XDatabaseMetaData>();
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	dispose_ChildImpl();
296cdf0e10cSrcweir 	KabConnection_BASE::disposing();
297cdf0e10cSrcweir }
298cdf0e10cSrcweir // -----------------------------------------------------------------------------
createCatalog()299cdf0e10cSrcweir Reference< XTablesSupplier > SAL_CALL KabConnection::createCatalog()
300cdf0e10cSrcweir {
301cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 	Reference< XTablesSupplier > xTab = m_xCatalog;
304cdf0e10cSrcweir 	if (!m_xCatalog.is())
305cdf0e10cSrcweir 	{
306cdf0e10cSrcweir 		KabCatalog *pCat = new KabCatalog(this);
307cdf0e10cSrcweir 		xTab = pCat;
308cdf0e10cSrcweir 		m_xCatalog = xTab;
309cdf0e10cSrcweir 	}
310cdf0e10cSrcweir 	return xTab;
311cdf0e10cSrcweir }
312cdf0e10cSrcweir // -----------------------------------------------------------------------------
getAddressBook() const313cdf0e10cSrcweir ::KABC::AddressBook* KabConnection::getAddressBook() const
314cdf0e10cSrcweir {
315cdf0e10cSrcweir     return m_pAddressBook;
316cdf0e10cSrcweir }
317cdf0e10cSrcweir // -----------------------------------------------------------------------------
createKabConnection(void * _pDriver)318cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void*  SAL_CALL createKabConnection( void* _pDriver )
319cdf0e10cSrcweir {
320cdf0e10cSrcweir     KabConnection* pConnection = new KabConnection( static_cast< KabDriver* >( _pDriver ) );
321cdf0e10cSrcweir     // by definition, the pointer crossing library boundaries as void ptr is acquired once
322cdf0e10cSrcweir     pConnection->acquire();
323cdf0e10cSrcweir     return pConnection;
324cdf0e10cSrcweir }
325