update dev300-m58
[ooovba.git] / connectivity / source / drivers / kab / KConnection.cxx
blobac381db10b8e0cab63890197bf815b94f841bcb2
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: KConnection.cxx,v $
10 * $Revision: 1.8 $
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 "KConnection.hxx"
35 #include "KDatabaseMetaData.hxx"
36 #include "KStatement.hxx"
37 #include "KPreparedStatement.hxx"
38 #include "KDriver.hxx"
39 #include "KCatalog.hxx"
40 #include <com/sun/star/sdbc/ColumnValue.hpp>
41 #include <com/sun/star/sdbc/TransactionIsolation.hpp>
42 #include <vcl/kde_headers.h>
44 using namespace connectivity::kab;
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(KabConnection, "com.sun.star.sdbc.drivers.KabConnection", "com.sun.star.sdbc.Connection")
52 //-----------------------------------------------------------------------------
53 KabConnection::KabConnection(KabDriver* _pDriver)
54 : OMetaConnection_BASE(m_aMutex),
55 OSubComponent<KabConnection, KabConnection_BASE>((::cppu::OWeakObject*)_pDriver, this),
56 m_xMetaData(NULL),
57 m_pAddressBook(NULL),
58 m_pDriver(_pDriver)
60 m_pDriver->acquire();
62 //-----------------------------------------------------------------------------
63 KabConnection::~KabConnection()
65 if (!isClosed())
66 close();
68 m_pDriver->release();
69 m_pDriver = NULL;
71 //-----------------------------------------------------------------------------
72 void SAL_CALL KabConnection::release() throw()
74 relase_ChildImpl();
76 // -----------------------------------------------------------------------------
77 void KabConnection::construct(const ::rtl::OUString&, const Sequence< PropertyValue >&) throw(SQLException)
79 osl_incrementInterlockedCount( &m_refCount );
81 // create a KDE address book object
82 m_pAddressBook = KABC::StdAddressBook::self();
83 m_pAddressBook->setAutomaticSave(false);
84 // perharps we should analyze the URL to know whether the addressbook is local, over LDAP, etc...
85 // perharps we should get some user and password information from "info" properties
87 osl_decrementInterlockedCount( &m_refCount );
89 // XServiceInfo
90 // --------------------------------------------------------------------------------
91 Reference< XStatement > SAL_CALL KabConnection::createStatement( ) throw(SQLException, RuntimeException)
93 ::osl::MutexGuard aGuard( m_aMutex );
94 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
96 // create a statement
97 // the statement can only be executed once
98 Reference< XStatement > xReturn = new KabStatement(this);
99 m_aStatements.push_back(WeakReferenceHelper(xReturn));
100 return xReturn;
102 // --------------------------------------------------------------------------------
103 Reference< XPreparedStatement > SAL_CALL KabConnection::prepareStatement( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
105 ::osl::MutexGuard aGuard( m_aMutex );
106 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
108 // create a statement
109 // the statement can only be executed more than once
110 Reference< XPreparedStatement > xReturn = new KabPreparedStatement(this, _sSql);
111 m_aStatements.push_back(WeakReferenceHelper(xReturn));
112 return xReturn;
114 // --------------------------------------------------------------------------------
115 Reference< XPreparedStatement > SAL_CALL KabConnection::prepareCall( const ::rtl::OUString& ) throw(SQLException, RuntimeException)
117 ::osl::MutexGuard aGuard( m_aMutex );
118 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
120 // not implemented yet :-) a task to do
121 return NULL;
123 // --------------------------------------------------------------------------------
124 ::rtl::OUString SAL_CALL KabConnection::nativeSQL( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException)
126 ::osl::MutexGuard aGuard( m_aMutex );
127 // when you need to transform SQL92 to you driver specific you can do it here
129 return _sSql;
131 // --------------------------------------------------------------------------------
132 void SAL_CALL KabConnection::setAutoCommit( sal_Bool ) throw(SQLException, RuntimeException)
134 ::osl::MutexGuard aGuard( m_aMutex );
135 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
136 // here you have to set your commit mode please have a look at the jdbc documentation to get a clear explanation
138 // --------------------------------------------------------------------------------
139 sal_Bool SAL_CALL KabConnection::getAutoCommit( ) throw(SQLException, RuntimeException)
141 ::osl::MutexGuard aGuard( m_aMutex );
142 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
143 // you have to distinguish which if you are in autocommit mode or not
144 // at normal case true should be fine here
146 return sal_True;
148 // --------------------------------------------------------------------------------
149 void SAL_CALL KabConnection::commit( ) throw(SQLException, RuntimeException)
151 ::osl::MutexGuard aGuard( m_aMutex );
152 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
154 // when you database does support transactions you should commit here
156 // --------------------------------------------------------------------------------
157 void SAL_CALL KabConnection::rollback( ) throw(SQLException, RuntimeException)
159 ::osl::MutexGuard aGuard( m_aMutex );
160 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
162 // same as commit but for the other case
164 // --------------------------------------------------------------------------------
165 sal_Bool SAL_CALL KabConnection::isClosed( ) throw(SQLException, RuntimeException)
167 ::osl::MutexGuard aGuard( m_aMutex );
169 // just simple -> we are closed when we are disposed, that means someone called dispose(); (XComponent)
170 return KabConnection_BASE::rBHelper.bDisposed;
172 // --------------------------------------------------------------------------------
173 Reference< XDatabaseMetaData > SAL_CALL KabConnection::getMetaData( ) throw(SQLException, RuntimeException)
175 ::osl::MutexGuard aGuard( m_aMutex );
176 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
178 // here we have to create the class with biggest interface
179 // The answer is 42 :-)
180 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
181 if (!xMetaData.is())
183 xMetaData = new KabDatabaseMetaData(this); // need the connection because it can return it
184 m_xMetaData = xMetaData;
187 return xMetaData;
189 // --------------------------------------------------------------------------------
190 void SAL_CALL KabConnection::setReadOnly( sal_Bool ) throw(SQLException, RuntimeException)
192 ::osl::MutexGuard aGuard( m_aMutex );
193 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
195 // set you connection to readonly
197 // --------------------------------------------------------------------------------
198 sal_Bool SAL_CALL KabConnection::isReadOnly( ) throw(SQLException, RuntimeException)
200 ::osl::MutexGuard aGuard( m_aMutex );
201 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
203 // return if your connection to readonly
204 return sal_False;
206 // --------------------------------------------------------------------------------
207 void SAL_CALL KabConnection::setCatalog( const ::rtl::OUString& ) throw(SQLException, RuntimeException)
209 ::osl::MutexGuard aGuard( m_aMutex );
210 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
212 // if your database doesn't work with catalogs you go to next method otherwise you kjnow what to do
214 // --------------------------------------------------------------------------------
215 ::rtl::OUString SAL_CALL KabConnection::getCatalog( ) throw(SQLException, RuntimeException)
217 ::osl::MutexGuard aGuard( m_aMutex );
218 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
221 // return your current catalog
222 return ::rtl::OUString();
224 // --------------------------------------------------------------------------------
225 void SAL_CALL KabConnection::setTransactionIsolation( sal_Int32 ) throw(SQLException, RuntimeException)
227 ::osl::MutexGuard aGuard( m_aMutex );
228 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
230 // set your isolation level
231 // please have a look at @see com.sun.star.sdbc.TransactionIsolation
233 // --------------------------------------------------------------------------------
234 sal_Int32 SAL_CALL KabConnection::getTransactionIsolation( ) throw(SQLException, RuntimeException)
236 ::osl::MutexGuard aGuard( m_aMutex );
237 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
240 // please have a look at @see com.sun.star.sdbc.TransactionIsolation
241 return TransactionIsolation::NONE;
243 // --------------------------------------------------------------------------------
244 Reference< ::com::sun::star::container::XNameAccess > SAL_CALL KabConnection::getTypeMap( ) throw(SQLException, RuntimeException)
246 ::osl::MutexGuard aGuard( m_aMutex );
247 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
249 // if your driver has special database types you can return it here
251 return NULL;
253 // --------------------------------------------------------------------------------
254 void SAL_CALL KabConnection::setTypeMap( const Reference< ::com::sun::star::container::XNameAccess >& ) throw(SQLException, RuntimeException)
256 // the other way around
258 // --------------------------------------------------------------------------------
259 // XCloseable
260 void SAL_CALL KabConnection::close( ) throw(SQLException, RuntimeException)
263 ::osl::MutexGuard aGuard( m_aMutex );
264 checkDisposed(KabConnection_BASE::rBHelper.bDisposed);
266 dispose();
268 // --------------------------------------------------------------------------------
269 // XWarningsSupplier
270 Any SAL_CALL KabConnection::getWarnings( ) throw(SQLException, RuntimeException)
272 // when you collected some warnings -> return it
273 return Any();
275 // --------------------------------------------------------------------------------
276 void SAL_CALL KabConnection::clearWarnings( ) throw(SQLException, RuntimeException)
278 // you should clear your collected warnings here
280 //------------------------------------------------------------------------------
281 void KabConnection::disposing()
283 // we noticed that we should be destroied in near future so we have to dispose our statements
284 ::osl::MutexGuard aGuard(m_aMutex);
286 for (OWeakRefArray::iterator i = m_aStatements.begin(); m_aStatements.end() != i; ++i)
288 Reference< XComponent > xComp(i->get(), UNO_QUERY);
289 if (xComp.is())
290 xComp->dispose();
292 m_aStatements.clear();
294 if (m_pAddressBook != NULL)
296 m_pAddressBook->close();
297 m_pAddressBook = NULL;
300 m_xMetaData = ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XDatabaseMetaData>();
302 dispose_ChildImpl();
303 KabConnection_BASE::disposing();
305 // -----------------------------------------------------------------------------
306 Reference< XTablesSupplier > SAL_CALL KabConnection::createCatalog()
308 ::osl::MutexGuard aGuard( m_aMutex );
310 Reference< XTablesSupplier > xTab = m_xCatalog;
311 if (!m_xCatalog.is())
313 KabCatalog *pCat = new KabCatalog(this);
314 xTab = pCat;
315 m_xCatalog = xTab;
317 return xTab;
319 // -----------------------------------------------------------------------------
320 ::KABC::AddressBook* KabConnection::getAddressBook() const
322 return m_pAddressBook;
324 // -----------------------------------------------------------------------------
325 extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL createKabConnection( void* _pDriver )
327 KabConnection* pConnection = new KabConnection( static_cast< KabDriver* >( _pDriver ) );
328 // by definition, the pointer crossing library boundaries as void ptr is acquired once
329 pConnection->acquire();
330 return pConnection;