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>
30 #include <cppuhelper/weak.hxx>
32 using namespace connectivity::macab
;
33 using namespace com::sun::star::uno
;
34 using namespace com::sun::star::lang
;
35 using namespace com::sun::star::beans
;
36 using namespace com::sun::star::sdbc
;
37 using namespace com::sun::star::sdbcx
;
39 IMPLEMENT_SERVICE_INFO(MacabConnection
, "com.sun.star.sdbc.drivers.MacabConnection", "com.sun.star.sdbc.Connection")
41 MacabConnection::MacabConnection(MacabDriver
* _pDriver
)
42 : m_pAddressBook(nullptr),
48 MacabConnection::~MacabConnection()
57 void MacabConnection::construct(const OUString
&, const Sequence
< PropertyValue
>&)
59 osl_atomic_increment( &m_refCount
);
61 // get the macOS shared address book
62 m_pAddressBook
= new MacabAddressBook();
64 osl_atomic_decrement( &m_refCount
);
68 Reference
< XStatement
> SAL_CALL
MacabConnection::createStatement( )
70 ::osl::MutexGuard
aGuard( m_aMutex
);
71 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
74 // the statement can only be executed once
75 Reference
< XStatement
> xReturn
= new MacabStatement(this);
76 m_aStatements
.push_back(WeakReferenceHelper(xReturn
));
80 Reference
< XPreparedStatement
> SAL_CALL
MacabConnection::prepareStatement( const OUString
& _sSql
)
82 ::osl::MutexGuard
aGuard( m_aMutex
);
83 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
86 // the statement can only be executed more than once
87 Reference
< XPreparedStatement
> xReturn
= new MacabPreparedStatement(this, _sSql
);
88 m_aStatements
.push_back(WeakReferenceHelper(xReturn
));
92 Reference
< XPreparedStatement
> SAL_CALL
MacabConnection::prepareCall( const OUString
& )
94 ::osl::MutexGuard
aGuard( m_aMutex
);
95 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
97 // not implemented yet :-) a task to do
101 OUString SAL_CALL
MacabConnection::nativeSQL( const OUString
& _sSql
)
103 ::osl::MutexGuard
aGuard( m_aMutex
);
104 // when you need to transform SQL92 to you driver specific you can do it here
109 void SAL_CALL
MacabConnection::setAutoCommit( sal_Bool
)
111 ::osl::MutexGuard
aGuard( m_aMutex
);
112 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
113 // here you have to set your commit mode please have a look at the jdbc documentation to get a clear explanation
116 sal_Bool SAL_CALL
MacabConnection::getAutoCommit( )
118 ::osl::MutexGuard
aGuard( m_aMutex
);
119 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
120 // you have to distinguish which if you are in autocommit mode or not
121 // at normal case true should be fine here
126 void SAL_CALL
MacabConnection::commit( )
128 ::osl::MutexGuard
aGuard( m_aMutex
);
129 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
131 // when you database does support transactions you should commit here
134 void SAL_CALL
MacabConnection::rollback( )
136 ::osl::MutexGuard
aGuard( m_aMutex
);
137 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
139 // same as commit but for the other case
142 sal_Bool SAL_CALL
MacabConnection::isClosed( )
147 bool MacabConnection::doIsClosed()
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( )
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
)
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( )
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
& )
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 know what to do
197 OUString SAL_CALL
MacabConnection::getCatalog( )
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
)
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( )
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
< css::container::XNameAccess
> SAL_CALL
MacabConnection::getTypeMap( )
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
< css::container::XNameAccess
>& )
238 // the other way around
242 void SAL_CALL
MacabConnection::close( )
247 void MacabConnection::doClose()
250 ::osl::MutexGuard
aGuard( m_aMutex
);
251 checkDisposed(MacabConnection_BASE::rBHelper
.bDisposed
);
257 Any SAL_CALL
MacabConnection::getWarnings( )
259 // when you collected some warnings -> return it
263 void SAL_CALL
MacabConnection::clearWarnings( )
265 // you should clear your collected warnings here
268 void MacabConnection::disposing()
270 // we noticed that we should be destroyed in near future so we have to dispose our statements
271 ::osl::MutexGuard
aGuard(m_aMutex
);
273 for (auto& rxStatement
: m_aStatements
)
275 Reference
< XComponent
> xComp(rxStatement
.get(), UNO_QUERY
);
279 m_aStatements
.clear();
281 if (m_pAddressBook
!= nullptr)
283 delete m_pAddressBook
;
284 m_pAddressBook
= nullptr;
289 MacabConnection_BASE::disposing();
292 Reference
< XTablesSupplier
> MacabConnection::createCatalog()
294 ::osl::MutexGuard
aGuard( m_aMutex
);
296 Reference
< XTablesSupplier
> xTab
= m_xCatalog
;
297 if (!m_xCatalog
.is())
299 xTab
= new MacabCatalog(this);
305 MacabAddressBook
* MacabConnection::getAddressBook() const
307 return m_pAddressBook
;
310 extern "C" SAL_DLLPUBLIC_EXPORT
void* createMacabConnection( void* _pDriver
)
312 // by definition, the pointer crossing library boundaries as void ptr is acquired once
313 return cppu::acquire(new MacabConnection( static_cast< MacabDriver
* >( _pDriver
) ));
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */