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