1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "KConnection.hxx"
22 #include "KDatabaseMetaData.hxx"
23 #include "KStatement.hxx"
24 #include "KPreparedStatement.hxx"
25 #include "KCatalog.hxx"
26 #include <com/sun/star/sdbc/ColumnValue.hpp>
27 #include <com/sun/star/sdbc/TransactionIsolation.hpp>
28 #include <shell/kde_headers.h>
30 using namespace connectivity::kab
;
31 using namespace com::sun::star::uno
;
32 using namespace com::sun::star::lang
;
33 using namespace com::sun::star::beans
;
34 using namespace com::sun::star::sdbc
;
35 using namespace com::sun::star::sdbcx
;
37 IMPLEMENT_SERVICE_INFO(KabConnection
, "com.sun.star.sdbc.drivers.KabConnection", "com.sun.star.sdbc.Connection")
39 KabConnection::KabConnection(
40 css::uno::Reference
<css::uno::XComponentContext
> const & componentContext
,
41 css::uno::Reference
<css::sdbc::XDriver
> const & driver
)
42 : OMetaConnection_BASE(m_aMutex
),
43 OSubComponent
<KabConnection
, KabConnection_BASE
>(driver
, this),
46 m_xComponentContext(componentContext
)
49 KabConnection::~KabConnection()
55 void SAL_CALL
KabConnection::release() throw()
60 //TODO: is doing this after the ctor, and the manual ref counting really
62 void KabConnection::construct()
64 osl_atomic_increment( &m_refCount
);
66 // create a KDE address book object
67 m_pAddressBook
= KABC::StdAddressBook::self();
68 KABC::StdAddressBook::setAutomaticSave(false);
70 osl_atomic_decrement( &m_refCount
);
74 Reference
< XStatement
> SAL_CALL
KabConnection::createStatement( ) throw(SQLException
, RuntimeException
, std::exception
)
76 ::osl::MutexGuard
aGuard( m_aMutex
);
77 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
80 // the statement can only be executed once
81 Reference
< XStatement
> xReturn
= new KabStatement(this);
82 m_aStatements
.push_back(WeakReferenceHelper(xReturn
));
86 Reference
< XPreparedStatement
> SAL_CALL
KabConnection::prepareStatement( const OUString
& _sSql
) throw(SQLException
, RuntimeException
, std::exception
)
88 ::osl::MutexGuard
aGuard( m_aMutex
);
89 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
92 // the statement can only be executed more than once
93 Reference
< XPreparedStatement
> xReturn
= new KabPreparedStatement(this, _sSql
);
94 m_aStatements
.push_back(WeakReferenceHelper(xReturn
));
98 Reference
< XPreparedStatement
> SAL_CALL
KabConnection::prepareCall( const OUString
& ) throw(SQLException
, RuntimeException
, std::exception
)
100 ::osl::MutexGuard
aGuard( m_aMutex
);
101 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
103 // not implemented yet :-) a task to do
107 OUString SAL_CALL
KabConnection::nativeSQL( const OUString
& _sSql
) throw(SQLException
, RuntimeException
, std::exception
)
109 ::osl::MutexGuard
aGuard( m_aMutex
);
110 // when you need to transform SQL92 to you driver specific you can do it here
115 void SAL_CALL
KabConnection::setAutoCommit( sal_Bool
) throw(SQLException
, RuntimeException
, std::exception
)
117 ::osl::MutexGuard
aGuard( m_aMutex
);
118 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
119 // here you have to set your commit mode please have a look at the jdbc documentation to get a clear explanation
122 sal_Bool SAL_CALL
KabConnection::getAutoCommit( ) throw(SQLException
, RuntimeException
, std::exception
)
124 ::osl::MutexGuard
aGuard( m_aMutex
);
125 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
126 // you have to distinguish which if you are in autocommit mode or not
127 // at normal case true should be fine here
132 void SAL_CALL
KabConnection::commit( ) throw(SQLException
, RuntimeException
, std::exception
)
134 ::osl::MutexGuard
aGuard( m_aMutex
);
135 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
137 // when you database does support transactions you should commit here
140 void SAL_CALL
KabConnection::rollback( ) throw(SQLException
, RuntimeException
, std::exception
)
142 ::osl::MutexGuard
aGuard( m_aMutex
);
143 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
145 // same as commit but for the other case
148 sal_Bool SAL_CALL
KabConnection::isClosed( ) throw(SQLException
, RuntimeException
, std::exception
)
150 ::osl::MutexGuard
aGuard( m_aMutex
);
152 // just simple -> we are closed when we are disposed, that means someone called dispose(); (XComponent)
153 return KabConnection_BASE::rBHelper
.bDisposed
;
156 Reference
< XDatabaseMetaData
> SAL_CALL
KabConnection::getMetaData( ) throw(SQLException
, RuntimeException
, std::exception
)
158 ::osl::MutexGuard
aGuard( m_aMutex
);
159 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
161 // here we have to create the class with biggest interface
162 // The answer is 42 :-)
163 Reference
< XDatabaseMetaData
> xMetaData
= m_xMetaData
;
166 xMetaData
= new KabDatabaseMetaData(this); // need the connection because it can return it
167 m_xMetaData
= xMetaData
;
173 void SAL_CALL
KabConnection::setReadOnly( sal_Bool
) throw(SQLException
, RuntimeException
, std::exception
)
175 ::osl::MutexGuard
aGuard( m_aMutex
);
176 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
178 // set you connection to readonly
181 sal_Bool SAL_CALL
KabConnection::isReadOnly( ) throw(SQLException
, RuntimeException
, std::exception
)
183 ::osl::MutexGuard
aGuard( m_aMutex
);
184 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
186 // return if your connection to readonly
190 void SAL_CALL
KabConnection::setCatalog( const OUString
& ) throw(SQLException
, RuntimeException
, std::exception
)
192 ::osl::MutexGuard
aGuard( m_aMutex
);
193 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
195 // if your database doesn't work with catalogs you go to next method otherwise you know what to do
198 OUString SAL_CALL
KabConnection::getCatalog( ) throw(SQLException
, RuntimeException
, std::exception
)
200 ::osl::MutexGuard
aGuard( m_aMutex
);
201 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
204 // return your current catalog
208 void SAL_CALL
KabConnection::setTransactionIsolation( sal_Int32
) throw(SQLException
, RuntimeException
, std::exception
)
210 ::osl::MutexGuard
aGuard( m_aMutex
);
211 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
213 // set your isolation level
214 // please have a look at @see com.sun.star.sdbc.TransactionIsolation
217 sal_Int32 SAL_CALL
KabConnection::getTransactionIsolation( ) throw(SQLException
, RuntimeException
, std::exception
)
219 ::osl::MutexGuard
aGuard( m_aMutex
);
220 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
223 // please have a look at @see com.sun.star.sdbc.TransactionIsolation
224 return TransactionIsolation::NONE
;
227 Reference
< ::com::sun::star::container::XNameAccess
> SAL_CALL
KabConnection::getTypeMap( ) throw(SQLException
, RuntimeException
, std::exception
)
229 ::osl::MutexGuard
aGuard( m_aMutex
);
230 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
232 // if your driver has special database types you can return it here
237 void SAL_CALL
KabConnection::setTypeMap( const Reference
< ::com::sun::star::container::XNameAccess
>& ) throw(SQLException
, RuntimeException
, std::exception
)
239 // the other way around
243 void SAL_CALL
KabConnection::close( ) throw(SQLException
, RuntimeException
, std::exception
)
246 ::osl::MutexGuard
aGuard( m_aMutex
);
247 checkDisposed(KabConnection_BASE::rBHelper
.bDisposed
);
253 Any SAL_CALL
KabConnection::getWarnings( ) throw(SQLException
, RuntimeException
, std::exception
)
255 // when you collected some warnings -> return it
259 void SAL_CALL
KabConnection::clearWarnings( ) throw(SQLException
, RuntimeException
, std::exception
)
261 // you should clear your collected warnings here
264 void KabConnection::disposing()
266 // we noticed that we should be destroied in near future so we have to dispose our statements
267 ::osl::MutexGuard
aGuard(m_aMutex
);
269 for (OWeakRefArray::iterator i
= m_aStatements
.begin(); m_aStatements
.end() != i
; ++i
)
271 Reference
< XComponent
> xComp(i
->get(), UNO_QUERY
);
275 m_aStatements
.clear();
277 if (m_pAddressBook
!= NULL
)
279 KABC::StdAddressBook::close();
280 m_pAddressBook
= NULL
;
283 m_xMetaData
= ::com::sun::star::uno::WeakReference
< ::com::sun::star::sdbc::XDatabaseMetaData
>();
286 KabConnection_BASE::disposing();
289 Reference
< XTablesSupplier
> SAL_CALL
KabConnection::createCatalog()
291 ::osl::MutexGuard
aGuard( m_aMutex
);
293 Reference
< XTablesSupplier
> xTab
= m_xCatalog
;
294 if (!m_xCatalog
.is())
296 KabCatalog
*pCat
= new KabCatalog(this);
303 ::KABC::AddressBook
* KabConnection::getAddressBook() const
305 return m_pAddressBook
;
308 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
* SAL_CALL
310 css::uno::Reference
<css::uno::XComponentContext
> const & componentContext
,
311 css::uno::Reference
<css::sdbc::XDriver
> const & driver
)
313 rtl::Reference
<KabConnection
> con(
314 new KabConnection(componentContext
, driver
));
316 return cppu::acquire(con
.get());
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */