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 "MacabConnection.hxx"
32*cdf0e10cSrcweir #include "MacabAddressBook.hxx"
33*cdf0e10cSrcweir #include "MacabDatabaseMetaData.hxx"
34*cdf0e10cSrcweir #include "MacabStatement.hxx"
35*cdf0e10cSrcweir #include "MacabPreparedStatement.hxx"
36*cdf0e10cSrcweir #include "MacabDriver.hxx"
37*cdf0e10cSrcweir #include "MacabCatalog.hxx"
38*cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/sdbc/TransactionIsolation.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace connectivity::macab;
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(MacabConnection, "com.sun.star.sdbc.drivers.MacabConnection", "com.sun.star.sdbc.Connection")
49*cdf0e10cSrcweir //-----------------------------------------------------------------------------
50*cdf0e10cSrcweir MacabConnection::MacabConnection(MacabDriver*	_pDriver)
51*cdf0e10cSrcweir 		 : OSubComponent<MacabConnection, MacabConnection_BASE>((::cppu::OWeakObject*)_pDriver, this),
52*cdf0e10cSrcweir 		 m_pAddressBook(NULL),
53*cdf0e10cSrcweir 		 m_pDriver(_pDriver)
54*cdf0e10cSrcweir {
55*cdf0e10cSrcweir 	m_pDriver->acquire();
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir //-----------------------------------------------------------------------------
58*cdf0e10cSrcweir MacabConnection::~MacabConnection()
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir 	if (!isClosed())
61*cdf0e10cSrcweir 		close();
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	m_pDriver->release();
64*cdf0e10cSrcweir 	m_pDriver = NULL;
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir //-----------------------------------------------------------------------------
67*cdf0e10cSrcweir void SAL_CALL MacabConnection::release() throw()
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir 	relase_ChildImpl();
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir // -----------------------------------------------------------------------------
72*cdf0e10cSrcweir void MacabConnection::construct(const ::rtl::OUString&, const Sequence< PropertyValue >&) throw(SQLException)
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir 	osl_incrementInterlockedCount( &m_refCount );
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 	// get the Mac OS X shared address book
77*cdf0e10cSrcweir 	m_pAddressBook = new MacabAddressBook();
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	osl_decrementInterlockedCount( &m_refCount );
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir // XServiceInfo
82*cdf0e10cSrcweir // --------------------------------------------------------------------------------
83*cdf0e10cSrcweir Reference< XStatement > SAL_CALL MacabConnection::createStatement(  ) throw(SQLException, RuntimeException)
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
86*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	// create a statement
89*cdf0e10cSrcweir 	// the statement can only be executed once
90*cdf0e10cSrcweir 	Reference< XStatement > xReturn = new MacabStatement(this);
91*cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
92*cdf0e10cSrcweir 	return xReturn;
93*cdf0e10cSrcweir }
94*cdf0e10cSrcweir // --------------------------------------------------------------------------------
95*cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL MacabConnection::prepareStatement( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
98*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	// create a statement
101*cdf0e10cSrcweir 	// the statement can only be executed more than once
102*cdf0e10cSrcweir 	Reference< XPreparedStatement > xReturn = new MacabPreparedStatement(this, _sSql);
103*cdf0e10cSrcweir 	m_aStatements.push_back(WeakReferenceHelper(xReturn));
104*cdf0e10cSrcweir 	return xReturn;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir // --------------------------------------------------------------------------------
107*cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL MacabConnection::prepareCall( const ::rtl::OUString& ) throw(SQLException, RuntimeException)
108*cdf0e10cSrcweir {
109*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
110*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 	// not implemented yet :-) a task to do
113*cdf0e10cSrcweir 	return NULL;
114*cdf0e10cSrcweir }
115*cdf0e10cSrcweir // --------------------------------------------------------------------------------
116*cdf0e10cSrcweir ::rtl::OUString SAL_CALL MacabConnection::nativeSQL( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
119*cdf0e10cSrcweir 	// when you need to transform SQL92 to you driver specific you can do it here
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	return _sSql;
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir // --------------------------------------------------------------------------------
124*cdf0e10cSrcweir void SAL_CALL MacabConnection::setAutoCommit( sal_Bool ) throw(SQLException, RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
127*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
128*cdf0e10cSrcweir 	// here you  have to set your commit mode please have a look at the jdbc documentation to get a clear explanation
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir // --------------------------------------------------------------------------------
131*cdf0e10cSrcweir sal_Bool SAL_CALL MacabConnection::getAutoCommit(  ) throw(SQLException, RuntimeException)
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
134*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
135*cdf0e10cSrcweir 	// you have to distinguish which if you are in autocommit mode or not
136*cdf0e10cSrcweir 	// at normal case true should be fine here
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	return sal_True;
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir // --------------------------------------------------------------------------------
141*cdf0e10cSrcweir void SAL_CALL MacabConnection::commit(  ) throw(SQLException, RuntimeException)
142*cdf0e10cSrcweir {
143*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
144*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 	// when you database does support transactions you should commit here
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir // --------------------------------------------------------------------------------
149*cdf0e10cSrcweir void SAL_CALL MacabConnection::rollback(  ) throw(SQLException, RuntimeException)
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
152*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	// same as commit but for the other case
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir // --------------------------------------------------------------------------------
157*cdf0e10cSrcweir sal_Bool SAL_CALL MacabConnection::isClosed(  ) throw(SQLException, RuntimeException)
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 	// just simple -> we are closed when we are disposed, that means someone called dispose(); (XComponent)
162*cdf0e10cSrcweir 	return MacabConnection_BASE::rBHelper.bDisposed;
163*cdf0e10cSrcweir }
164*cdf0e10cSrcweir // --------------------------------------------------------------------------------
165*cdf0e10cSrcweir Reference< XDatabaseMetaData > SAL_CALL MacabConnection::getMetaData(  ) throw(SQLException, RuntimeException)
166*cdf0e10cSrcweir {
167*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
168*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir 	// here we have to create the class with biggest interface
171*cdf0e10cSrcweir 	// The answer is 42 :-)
172*cdf0e10cSrcweir 	Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
173*cdf0e10cSrcweir 	if (!xMetaData.is())
174*cdf0e10cSrcweir 	{
175*cdf0e10cSrcweir 		xMetaData = new MacabDatabaseMetaData(this); // need the connection because it can return it
176*cdf0e10cSrcweir 		m_xMetaData = xMetaData;
177*cdf0e10cSrcweir 	}
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	return xMetaData;
180*cdf0e10cSrcweir }
181*cdf0e10cSrcweir // --------------------------------------------------------------------------------
182*cdf0e10cSrcweir void SAL_CALL MacabConnection::setReadOnly( sal_Bool ) throw(SQLException, RuntimeException)
183*cdf0e10cSrcweir {
184*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
185*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 	// set you connection to readonly
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir // --------------------------------------------------------------------------------
190*cdf0e10cSrcweir sal_Bool SAL_CALL MacabConnection::isReadOnly(  ) throw(SQLException, RuntimeException)
191*cdf0e10cSrcweir {
192*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
193*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir 	// return if your connection to readonly
196*cdf0e10cSrcweir 	return sal_False;
197*cdf0e10cSrcweir }
198*cdf0e10cSrcweir // --------------------------------------------------------------------------------
199*cdf0e10cSrcweir void SAL_CALL MacabConnection::setCatalog( const ::rtl::OUString& ) throw(SQLException, RuntimeException)
200*cdf0e10cSrcweir {
201*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
202*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 	// if your database doesn't work with catalogs you go to next method otherwise you kjnow what to do
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir // --------------------------------------------------------------------------------
207*cdf0e10cSrcweir ::rtl::OUString SAL_CALL MacabConnection::getCatalog(  ) throw(SQLException, RuntimeException)
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
210*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir 	// return your current catalog
214*cdf0e10cSrcweir 	return ::rtl::OUString();
215*cdf0e10cSrcweir }
216*cdf0e10cSrcweir // --------------------------------------------------------------------------------
217*cdf0e10cSrcweir void SAL_CALL MacabConnection::setTransactionIsolation( sal_Int32 ) throw(SQLException, RuntimeException)
218*cdf0e10cSrcweir {
219*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
220*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 	// set your isolation level
223*cdf0e10cSrcweir 	// please have a look at @see com.sun.star.sdbc.TransactionIsolation
224*cdf0e10cSrcweir }
225*cdf0e10cSrcweir // --------------------------------------------------------------------------------
226*cdf0e10cSrcweir sal_Int32 SAL_CALL MacabConnection::getTransactionIsolation(  ) throw(SQLException, RuntimeException)
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
229*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 	// please have a look at @see com.sun.star.sdbc.TransactionIsolation
233*cdf0e10cSrcweir 	return TransactionIsolation::NONE;
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir // --------------------------------------------------------------------------------
236*cdf0e10cSrcweir Reference< ::com::sun::star::container::XNameAccess > SAL_CALL MacabConnection::getTypeMap(  ) throw(SQLException, RuntimeException)
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
239*cdf0e10cSrcweir 	checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 	// if your driver has special database types you can return it here
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir 	return NULL;
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir // --------------------------------------------------------------------------------
246*cdf0e10cSrcweir void SAL_CALL MacabConnection::setTypeMap( const Reference< ::com::sun::star::container::XNameAccess >& ) throw(SQLException, RuntimeException)
247*cdf0e10cSrcweir {
248*cdf0e10cSrcweir 	// the other way around
249*cdf0e10cSrcweir }
250*cdf0e10cSrcweir // --------------------------------------------------------------------------------
251*cdf0e10cSrcweir // XCloseable
252*cdf0e10cSrcweir void SAL_CALL MacabConnection::close(  ) throw(SQLException, RuntimeException)
253*cdf0e10cSrcweir {
254*cdf0e10cSrcweir 	{
255*cdf0e10cSrcweir 		::osl::MutexGuard aGuard( m_aMutex );
256*cdf0e10cSrcweir 		checkDisposed(MacabConnection_BASE::rBHelper.bDisposed);
257*cdf0e10cSrcweir 	}
258*cdf0e10cSrcweir 	dispose();
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir // --------------------------------------------------------------------------------
261*cdf0e10cSrcweir // XWarningsSupplier
262*cdf0e10cSrcweir Any SAL_CALL MacabConnection::getWarnings(  ) throw(SQLException, RuntimeException)
263*cdf0e10cSrcweir {
264*cdf0e10cSrcweir 	// when you collected some warnings -> return it
265*cdf0e10cSrcweir 	return Any();
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir // --------------------------------------------------------------------------------
268*cdf0e10cSrcweir void SAL_CALL MacabConnection::clearWarnings(  ) throw(SQLException, RuntimeException)
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir 	// you should clear your collected warnings here
271*cdf0e10cSrcweir }
272*cdf0e10cSrcweir //------------------------------------------------------------------------------
273*cdf0e10cSrcweir void MacabConnection::disposing()
274*cdf0e10cSrcweir {
275*cdf0e10cSrcweir 	// we noticed that we should be destroied in near future so we have to dispose our statements
276*cdf0e10cSrcweir 	::osl::MutexGuard aGuard(m_aMutex);
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	for (OWeakRefArray::iterator i = m_aStatements.begin(); m_aStatements.end() != i; ++i)
279*cdf0e10cSrcweir 	{
280*cdf0e10cSrcweir 		Reference< XComponent > xComp(i->get(), UNO_QUERY);
281*cdf0e10cSrcweir 		if (xComp.is())
282*cdf0e10cSrcweir 			xComp->dispose();
283*cdf0e10cSrcweir 	}
284*cdf0e10cSrcweir 	m_aStatements.clear();
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 	if (m_pAddressBook != NULL)
287*cdf0e10cSrcweir 	{
288*cdf0e10cSrcweir 		delete m_pAddressBook;
289*cdf0e10cSrcweir 		m_pAddressBook = NULL;
290*cdf0e10cSrcweir 	}
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 	m_xMetaData = ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XDatabaseMetaData>();
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 	dispose_ChildImpl();
295*cdf0e10cSrcweir 	MacabConnection_BASE::disposing();
296*cdf0e10cSrcweir }
297*cdf0e10cSrcweir // -----------------------------------------------------------------------------
298*cdf0e10cSrcweir Reference< XTablesSupplier > SAL_CALL MacabConnection::createCatalog()
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir 	::osl::MutexGuard aGuard( m_aMutex );
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 	Reference< XTablesSupplier > xTab = m_xCatalog;
303*cdf0e10cSrcweir 	if (!m_xCatalog.is())
304*cdf0e10cSrcweir 	{
305*cdf0e10cSrcweir 		MacabCatalog *pCat = new MacabCatalog(this);
306*cdf0e10cSrcweir 		xTab = pCat;
307*cdf0e10cSrcweir 		m_xCatalog = xTab;
308*cdf0e10cSrcweir 	}
309*cdf0e10cSrcweir 	return xTab;
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir // -----------------------------------------------------------------------------
312*cdf0e10cSrcweir MacabAddressBook* MacabConnection::getAddressBook() const
313*cdf0e10cSrcweir {
314*cdf0e10cSrcweir     return m_pAddressBook;
315*cdf0e10cSrcweir }
316*cdf0e10cSrcweir // -----------------------------------------------------------------------------
317*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void*  SAL_CALL createMacabConnection( void* _pDriver )
318*cdf0e10cSrcweir {
319*cdf0e10cSrcweir     MacabConnection* pConnection = new MacabConnection( static_cast< MacabDriver* >( _pDriver ) );
320*cdf0e10cSrcweir     // by definition, the pointer crossing library boundaries as void ptr is acquired once
321*cdf0e10cSrcweir     pConnection->acquire();
322*cdf0e10cSrcweir     return pConnection;
323*cdf0e10cSrcweir }
324*cdf0e10cSrcweir 
325