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