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