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 "MacabConnection.hxx"
22 #include "MacabAddressBook.hxx"
23 #include "MacabDatabaseMetaData.hxx"
24 #include "MacabStatement.hxx"
25 #include "MacabPreparedStatement.hxx"
26 #include "MacabDriver.hxx"
27 #include "MacabCatalog.hxx"
28 #include <com/sun/star/sdbc/ColumnValue.hpp>
29 #include <com/sun/star/sdbc/TransactionIsolation.hpp>
31 using namespace connectivity::macab
;
32 using namespace com::sun::star::uno
;
33 using namespace com::sun::star::lang
;
34 using namespace com::sun::star::beans
;
35 using namespace com::sun::star::sdbc
;
36 using namespace com::sun::star::sdbcx
;
38 IMPLEMENT_SERVICE_INFO(MacabConnection
, "com.sun.star.sdbc.drivers.MacabConnection", "com.sun.star.sdbc.Connection")
40 MacabConnection::MacabConnection(MacabDriver
* _pDriver
)
41 : OSubComponent
<MacabConnection
, MacabConnection_BASE
>((::cppu::OWeakObject
*)_pDriver
, this),
48 MacabConnection::~MacabConnection()
57 void SAL_CALL
MacabConnection::release() throw()
62 void MacabConnection::construct(const OUString
&, const Sequence
< PropertyValue
>&) throw(SQLException
)
64 osl_atomic_increment( &m_refCount
);
66 // get the Mac OS X shared address book
67 m_pAddressBook
= new MacabAddressBook();
69 osl_atomic_decrement( &m_refCount
);
73 Reference
< XStatement
> SAL_CALL
MacabConnection::createStatement( ) throw(SQLException
, RuntimeException
)
75 ::osl::MutexGuard
aGuard( m_aMutex
);
76 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
79 // the statement can only be executed once
80 Reference
< XStatement
> xReturn
= new MacabStatement(this);
81 m_aStatements
.push_back(WeakReferenceHelper(xReturn
));
85 Reference
< XPreparedStatement
> SAL_CALL
MacabConnection::prepareStatement( const OUString
& _sSql
) throw(SQLException
, RuntimeException
)
87 ::osl::MutexGuard
aGuard( m_aMutex
);
88 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
91 // the statement can only be executed more than once
92 Reference
< XPreparedStatement
> xReturn
= new MacabPreparedStatement(this, _sSql
);
93 m_aStatements
.push_back(WeakReferenceHelper(xReturn
));
97 Reference
< XPreparedStatement
> SAL_CALL
MacabConnection::prepareCall( const OUString
& ) throw(SQLException
, RuntimeException
)
99 ::osl::MutexGuard
aGuard( m_aMutex
);
100 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
102 // not implemented yet :-) a task to do
106 OUString SAL_CALL
MacabConnection::nativeSQL( const OUString
& _sSql
) throw(SQLException
, RuntimeException
)
108 ::osl::MutexGuard
aGuard( m_aMutex
);
109 // when you need to transform SQL92 to you driver specific you can do it here
114 void SAL_CALL
MacabConnection::setAutoCommit( sal_Bool
) throw(SQLException
, RuntimeException
)
116 ::osl::MutexGuard
aGuard( m_aMutex
);
117 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
118 // here you have to set your commit mode please have a look at the jdbc documentation to get a clear explanation
121 sal_Bool SAL_CALL
MacabConnection::getAutoCommit( ) throw(SQLException
, RuntimeException
)
123 ::osl::MutexGuard
aGuard( m_aMutex
);
124 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
125 // you have to distinguish which if you are in autocommit mode or not
126 // at normal case true should be fine here
131 void SAL_CALL
MacabConnection::commit( ) throw(SQLException
, RuntimeException
)
133 ::osl::MutexGuard
aGuard( m_aMutex
);
134 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
136 // when you database does support transactions you should commit here
139 void SAL_CALL
MacabConnection::rollback( ) throw(SQLException
, RuntimeException
)
141 ::osl::MutexGuard
aGuard( m_aMutex
);
142 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
144 // same as commit but for the other case
147 sal_Bool SAL_CALL
MacabConnection::isClosed( ) throw(SQLException
, RuntimeException
)
149 ::osl::MutexGuard
aGuard( m_aMutex
);
151 // just simple -> we are closed when we are disposed, that means someone called dispose(); (XComponent)
152 return MacabConnection_BASE::rBHelper
.bDisposed
;
155 Reference
< XDatabaseMetaData
> SAL_CALL
MacabConnection::getMetaData( ) throw(SQLException
, RuntimeException
)
157 ::osl::MutexGuard
aGuard( m_aMutex
);
158 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
160 // here we have to create the class with biggest interface
161 // The answer is 42 :-)
162 Reference
< XDatabaseMetaData
> xMetaData
= m_xMetaData
;
165 xMetaData
= new MacabDatabaseMetaData(this); // need the connection because it can return it
166 m_xMetaData
= xMetaData
;
172 void SAL_CALL
MacabConnection::setReadOnly( sal_Bool
) throw(SQLException
, RuntimeException
)
174 ::osl::MutexGuard
aGuard( m_aMutex
);
175 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
177 // set you connection to readonly
180 sal_Bool SAL_CALL
MacabConnection::isReadOnly( ) throw(SQLException
, RuntimeException
)
182 ::osl::MutexGuard
aGuard( m_aMutex
);
183 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
185 // return if your connection to readonly
189 void SAL_CALL
MacabConnection::setCatalog( const OUString
& ) throw(SQLException
, RuntimeException
)
191 ::osl::MutexGuard
aGuard( m_aMutex
);
192 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
194 // if your database doesn't work with catalogs you go to next method otherwise you kjnow what to do
197 OUString SAL_CALL
MacabConnection::getCatalog( ) throw(SQLException
, RuntimeException
)
199 ::osl::MutexGuard
aGuard( m_aMutex
);
200 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
203 // return your current catalog
207 void SAL_CALL
MacabConnection::setTransactionIsolation( sal_Int32
) throw(SQLException
, RuntimeException
)
209 ::osl::MutexGuard
aGuard( m_aMutex
);
210 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
212 // set your isolation level
213 // please have a look at @see com.sun.star.sdbc.TransactionIsolation
216 sal_Int32 SAL_CALL
MacabConnection::getTransactionIsolation( ) throw(SQLException
, RuntimeException
)
218 ::osl::MutexGuard
aGuard( m_aMutex
);
219 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
222 // please have a look at @see com.sun.star.sdbc.TransactionIsolation
223 return TransactionIsolation::NONE
;
226 Reference
< ::com::sun::star::container::XNameAccess
> SAL_CALL
MacabConnection::getTypeMap( ) throw(SQLException
, RuntimeException
)
228 ::osl::MutexGuard
aGuard( m_aMutex
);
229 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
231 // if your driver has special database types you can return it here
236 void SAL_CALL
MacabConnection::setTypeMap( const Reference
< ::com::sun::star::container::XNameAccess
>& ) throw(SQLException
, RuntimeException
)
238 // the other way around
242 void SAL_CALL
MacabConnection::close( ) throw(SQLException
, RuntimeException
)
245 ::osl::MutexGuard
aGuard( m_aMutex
);
246 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
252 Any SAL_CALL
MacabConnection::getWarnings( ) throw(SQLException
, RuntimeException
)
254 // when you collected some warnings -> return it
258 void SAL_CALL
MacabConnection::clearWarnings( ) throw(SQLException
, RuntimeException
)
260 // you should clear your collected warnings here
263 void MacabConnection::disposing()
265 // we noticed that we should be destroied in near future so we have to dispose our statements
266 ::osl::MutexGuard
aGuard(m_aMutex
);
268 for (OWeakRefArray::iterator i
= m_aStatements
.begin(); m_aStatements
.end() != i
; ++i
)
270 Reference
< XComponent
> xComp(i
->get(), UNO_QUERY
);
274 m_aStatements
.clear();
276 if (m_pAddressBook
!= NULL
)
278 delete m_pAddressBook
;
279 m_pAddressBook
= NULL
;
282 m_xMetaData
= ::com::sun::star::uno::WeakReference
< ::com::sun::star::sdbc::XDatabaseMetaData
>();
285 MacabConnection_BASE::disposing();
288 Reference
< XTablesSupplier
> SAL_CALL
MacabConnection::createCatalog()
290 ::osl::MutexGuard
aGuard( m_aMutex
);
292 Reference
< XTablesSupplier
> xTab
= m_xCatalog
;
293 if (!m_xCatalog
.is())
295 MacabCatalog
*pCat
= new MacabCatalog(this);
302 MacabAddressBook
* MacabConnection::getAddressBook() const
304 return m_pAddressBook
;
307 extern "C" SAL_DLLPUBLIC_EXPORT
void* SAL_CALL
createMacabConnection( void* _pDriver
)
309 MacabConnection
* pConnection
= new MacabConnection( static_cast< MacabDriver
* >( _pDriver
) );
310 // by definition, the pointer crossing library boundaries as void ptr is acquired once
311 pConnection
->acquire();
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */