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