1*9b5730f6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*9b5730f6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*9b5730f6SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*9b5730f6SAndrew Rist * distributed with this work for additional information 6*9b5730f6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*9b5730f6SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*9b5730f6SAndrew Rist * "License"); you may not use this file except in compliance 9*9b5730f6SAndrew Rist * with the License. You may obtain a copy of the License at 10*9b5730f6SAndrew Rist * 11*9b5730f6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*9b5730f6SAndrew Rist * 13*9b5730f6SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*9b5730f6SAndrew Rist * software distributed under the License is distributed on an 15*9b5730f6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*9b5730f6SAndrew Rist * KIND, either express or implied. See the License for the 17*9b5730f6SAndrew Rist * specific language governing permissions and limitations 18*9b5730f6SAndrew Rist * under the License. 19*9b5730f6SAndrew Rist * 20*9b5730f6SAndrew Rist *************************************************************/ 21*9b5730f6SAndrew Rist 22*9b5730f6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_connectivity.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "MacabConnection.hxx" 28cdf0e10cSrcweir #include "MacabAddressBook.hxx" 29cdf0e10cSrcweir #include "MacabDatabaseMetaData.hxx" 30cdf0e10cSrcweir #include "MacabStatement.hxx" 31cdf0e10cSrcweir #include "MacabPreparedStatement.hxx" 32cdf0e10cSrcweir #include "MacabDriver.hxx" 33cdf0e10cSrcweir #include "MacabCatalog.hxx" 34cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp> 35cdf0e10cSrcweir #include <com/sun/star/sdbc/TransactionIsolation.hpp> 36cdf0e10cSrcweir 37cdf0e10cSrcweir using namespace connectivity::macab; 38cdf0e10cSrcweir using namespace com::sun::star::uno; 39cdf0e10cSrcweir using namespace com::sun::star::lang; 40cdf0e10cSrcweir using namespace com::sun::star::beans; 41cdf0e10cSrcweir using namespace com::sun::star::sdbc; 42cdf0e10cSrcweir using namespace com::sun::star::sdbcx; 43cdf0e10cSrcweir 44cdf0e10cSrcweir IMPLEMENT_SERVICE_INFO(MacabConnection, "com.sun.star.sdbc.drivers.MacabConnection", "com.sun.star.sdbc.Connection") 45cdf0e10cSrcweir //----------------------------------------------------------------------------- 46cdf0e10cSrcweir MacabConnection::MacabConnection(MacabDriver* _pDriver) 47cdf0e10cSrcweir : OSubComponent<MacabConnection, MacabConnection_BASE>((::cppu::OWeakObject*)_pDriver, this), 48cdf0e10cSrcweir m_pAddressBook(NULL), 49cdf0e10cSrcweir m_pDriver(_pDriver) 50cdf0e10cSrcweir { 51cdf0e10cSrcweir m_pDriver->acquire(); 52cdf0e10cSrcweir } 53cdf0e10cSrcweir //----------------------------------------------------------------------------- 54cdf0e10cSrcweir MacabConnection::~MacabConnection() 55cdf0e10cSrcweir { 56cdf0e10cSrcweir if (!isClosed()) 57cdf0e10cSrcweir close(); 58cdf0e10cSrcweir 59cdf0e10cSrcweir m_pDriver->release(); 60cdf0e10cSrcweir m_pDriver = NULL; 61cdf0e10cSrcweir } 62cdf0e10cSrcweir //----------------------------------------------------------------------------- 63cdf0e10cSrcweir void SAL_CALL MacabConnection::release() throw() 64cdf0e10cSrcweir { 65cdf0e10cSrcweir relase_ChildImpl(); 66cdf0e10cSrcweir } 67cdf0e10cSrcweir // ----------------------------------------------------------------------------- 68cdf0e10cSrcweir void MacabConnection::construct(const ::rtl::OUString&, const Sequence< PropertyValue >&) throw(SQLException) 69cdf0e10cSrcweir { 70cdf0e10cSrcweir osl_incrementInterlockedCount( &m_refCount ); 71cdf0e10cSrcweir 72cdf0e10cSrcweir // get the Mac OS X shared address book 73cdf0e10cSrcweir m_pAddressBook = new MacabAddressBook(); 74cdf0e10cSrcweir 75cdf0e10cSrcweir osl_decrementInterlockedCount( &m_refCount ); 76cdf0e10cSrcweir } 77cdf0e10cSrcweir // XServiceInfo 78cdf0e10cSrcweir // -------------------------------------------------------------------------------- 79cdf0e10cSrcweir Reference< XStatement > SAL_CALL MacabConnection::createStatement( ) throw(SQLException, RuntimeException) 80cdf0e10cSrcweir { 81cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 82cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 83cdf0e10cSrcweir 84cdf0e10cSrcweir // create a statement 85cdf0e10cSrcweir // the statement can only be executed once 86cdf0e10cSrcweir Reference< XStatement > xReturn = new MacabStatement(this); 87cdf0e10cSrcweir m_aStatements.push_back(WeakReferenceHelper(xReturn)); 88cdf0e10cSrcweir return xReturn; 89cdf0e10cSrcweir } 90cdf0e10cSrcweir // -------------------------------------------------------------------------------- 91cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL MacabConnection::prepareStatement( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 94cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 95cdf0e10cSrcweir 96cdf0e10cSrcweir // create a statement 97cdf0e10cSrcweir // the statement can only be executed more than once 98cdf0e10cSrcweir Reference< XPreparedStatement > xReturn = new MacabPreparedStatement(this, _sSql); 99cdf0e10cSrcweir m_aStatements.push_back(WeakReferenceHelper(xReturn)); 100cdf0e10cSrcweir return xReturn; 101cdf0e10cSrcweir } 102cdf0e10cSrcweir // -------------------------------------------------------------------------------- 103cdf0e10cSrcweir Reference< XPreparedStatement > SAL_CALL MacabConnection::prepareCall( const ::rtl::OUString& ) throw(SQLException, RuntimeException) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 106cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 107cdf0e10cSrcweir 108cdf0e10cSrcweir // not implemented yet :-) a task to do 109cdf0e10cSrcweir return NULL; 110cdf0e10cSrcweir } 111cdf0e10cSrcweir // -------------------------------------------------------------------------------- 112cdf0e10cSrcweir ::rtl::OUString SAL_CALL MacabConnection::nativeSQL( const ::rtl::OUString& _sSql ) throw(SQLException, RuntimeException) 113cdf0e10cSrcweir { 114cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 115cdf0e10cSrcweir // when you need to transform SQL92 to you driver specific you can do it here 116cdf0e10cSrcweir 117cdf0e10cSrcweir return _sSql; 118cdf0e10cSrcweir } 119cdf0e10cSrcweir // -------------------------------------------------------------------------------- 120cdf0e10cSrcweir void SAL_CALL MacabConnection::setAutoCommit( sal_Bool ) throw(SQLException, RuntimeException) 121cdf0e10cSrcweir { 122cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 123cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 124cdf0e10cSrcweir // here you have to set your commit mode please have a look at the jdbc documentation to get a clear explanation 125cdf0e10cSrcweir } 126cdf0e10cSrcweir // -------------------------------------------------------------------------------- 127cdf0e10cSrcweir sal_Bool SAL_CALL MacabConnection::getAutoCommit( ) throw(SQLException, RuntimeException) 128cdf0e10cSrcweir { 129cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 130cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 131cdf0e10cSrcweir // you have to distinguish which if you are in autocommit mode or not 132cdf0e10cSrcweir // at normal case true should be fine here 133cdf0e10cSrcweir 134cdf0e10cSrcweir return sal_True; 135cdf0e10cSrcweir } 136cdf0e10cSrcweir // -------------------------------------------------------------------------------- 137cdf0e10cSrcweir void SAL_CALL MacabConnection::commit( ) throw(SQLException, RuntimeException) 138cdf0e10cSrcweir { 139cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 140cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 141cdf0e10cSrcweir 142cdf0e10cSrcweir // when you database does support transactions you should commit here 143cdf0e10cSrcweir } 144cdf0e10cSrcweir // -------------------------------------------------------------------------------- 145cdf0e10cSrcweir void SAL_CALL MacabConnection::rollback( ) throw(SQLException, RuntimeException) 146cdf0e10cSrcweir { 147cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 148cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 149cdf0e10cSrcweir 150cdf0e10cSrcweir // same as commit but for the other case 151cdf0e10cSrcweir } 152cdf0e10cSrcweir // -------------------------------------------------------------------------------- 153cdf0e10cSrcweir sal_Bool SAL_CALL MacabConnection::isClosed( ) throw(SQLException, RuntimeException) 154cdf0e10cSrcweir { 155cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 156cdf0e10cSrcweir 157cdf0e10cSrcweir // just simple -> we are closed when we are disposed, that means someone called dispose(); (XComponent) 158cdf0e10cSrcweir return MacabConnection_BASE::rBHelper.bDisposed; 159cdf0e10cSrcweir } 160cdf0e10cSrcweir // -------------------------------------------------------------------------------- 161cdf0e10cSrcweir Reference< XDatabaseMetaData > SAL_CALL MacabConnection::getMetaData( ) throw(SQLException, RuntimeException) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 164cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 165cdf0e10cSrcweir 166cdf0e10cSrcweir // here we have to create the class with biggest interface 167cdf0e10cSrcweir // The answer is 42 :-) 168cdf0e10cSrcweir Reference< XDatabaseMetaData > xMetaData = m_xMetaData; 169cdf0e10cSrcweir if (!xMetaData.is()) 170cdf0e10cSrcweir { 171cdf0e10cSrcweir xMetaData = new MacabDatabaseMetaData(this); // need the connection because it can return it 172cdf0e10cSrcweir m_xMetaData = xMetaData; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir 175cdf0e10cSrcweir return xMetaData; 176cdf0e10cSrcweir } 177cdf0e10cSrcweir // -------------------------------------------------------------------------------- 178cdf0e10cSrcweir void SAL_CALL MacabConnection::setReadOnly( sal_Bool ) throw(SQLException, RuntimeException) 179cdf0e10cSrcweir { 180cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 181cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 182cdf0e10cSrcweir 183cdf0e10cSrcweir // set you connection to readonly 184cdf0e10cSrcweir } 185cdf0e10cSrcweir // -------------------------------------------------------------------------------- 186cdf0e10cSrcweir sal_Bool SAL_CALL MacabConnection::isReadOnly( ) throw(SQLException, RuntimeException) 187cdf0e10cSrcweir { 188cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 189cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 190cdf0e10cSrcweir 191cdf0e10cSrcweir // return if your connection to readonly 192cdf0e10cSrcweir return sal_False; 193cdf0e10cSrcweir } 194cdf0e10cSrcweir // -------------------------------------------------------------------------------- 195cdf0e10cSrcweir void SAL_CALL MacabConnection::setCatalog( const ::rtl::OUString& ) throw(SQLException, RuntimeException) 196cdf0e10cSrcweir { 197cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 198cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 199cdf0e10cSrcweir 200cdf0e10cSrcweir // if your database doesn't work with catalogs you go to next method otherwise you kjnow what to do 201cdf0e10cSrcweir } 202cdf0e10cSrcweir // -------------------------------------------------------------------------------- 203cdf0e10cSrcweir ::rtl::OUString SAL_CALL MacabConnection::getCatalog( ) throw(SQLException, RuntimeException) 204cdf0e10cSrcweir { 205cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 206cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 207cdf0e10cSrcweir 208cdf0e10cSrcweir 209cdf0e10cSrcweir // return your current catalog 210cdf0e10cSrcweir return ::rtl::OUString(); 211cdf0e10cSrcweir } 212cdf0e10cSrcweir // -------------------------------------------------------------------------------- 213cdf0e10cSrcweir void SAL_CALL MacabConnection::setTransactionIsolation( sal_Int32 ) throw(SQLException, RuntimeException) 214cdf0e10cSrcweir { 215cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 216cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 217cdf0e10cSrcweir 218cdf0e10cSrcweir // set your isolation level 219cdf0e10cSrcweir // please have a look at @see com.sun.star.sdbc.TransactionIsolation 220cdf0e10cSrcweir } 221cdf0e10cSrcweir // -------------------------------------------------------------------------------- 222cdf0e10cSrcweir sal_Int32 SAL_CALL MacabConnection::getTransactionIsolation( ) throw(SQLException, RuntimeException) 223cdf0e10cSrcweir { 224cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 225cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 226cdf0e10cSrcweir 227cdf0e10cSrcweir 228cdf0e10cSrcweir // please have a look at @see com.sun.star.sdbc.TransactionIsolation 229cdf0e10cSrcweir return TransactionIsolation::NONE; 230cdf0e10cSrcweir } 231cdf0e10cSrcweir // -------------------------------------------------------------------------------- 232cdf0e10cSrcweir Reference< ::com::sun::star::container::XNameAccess > SAL_CALL MacabConnection::getTypeMap( ) throw(SQLException, RuntimeException) 233cdf0e10cSrcweir { 234cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 235cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 236cdf0e10cSrcweir 237cdf0e10cSrcweir // if your driver has special database types you can return it here 238cdf0e10cSrcweir 239cdf0e10cSrcweir return NULL; 240cdf0e10cSrcweir } 241cdf0e10cSrcweir // -------------------------------------------------------------------------------- 242cdf0e10cSrcweir void SAL_CALL MacabConnection::setTypeMap( const Reference< ::com::sun::star::container::XNameAccess >& ) throw(SQLException, RuntimeException) 243cdf0e10cSrcweir { 244cdf0e10cSrcweir // the other way around 245cdf0e10cSrcweir } 246cdf0e10cSrcweir // -------------------------------------------------------------------------------- 247cdf0e10cSrcweir // XCloseable 248cdf0e10cSrcweir void SAL_CALL MacabConnection::close( ) throw(SQLException, RuntimeException) 249cdf0e10cSrcweir { 250cdf0e10cSrcweir { 251cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 252cdf0e10cSrcweir checkDisposed(MacabConnection_BASE::rBHelper.bDisposed); 253cdf0e10cSrcweir } 254cdf0e10cSrcweir dispose(); 255cdf0e10cSrcweir } 256cdf0e10cSrcweir // -------------------------------------------------------------------------------- 257cdf0e10cSrcweir // XWarningsSupplier 258cdf0e10cSrcweir Any SAL_CALL MacabConnection::getWarnings( ) throw(SQLException, RuntimeException) 259cdf0e10cSrcweir { 260cdf0e10cSrcweir // when you collected some warnings -> return it 261cdf0e10cSrcweir return Any(); 262cdf0e10cSrcweir } 263cdf0e10cSrcweir // -------------------------------------------------------------------------------- 264cdf0e10cSrcweir void SAL_CALL MacabConnection::clearWarnings( ) throw(SQLException, RuntimeException) 265cdf0e10cSrcweir { 266cdf0e10cSrcweir // you should clear your collected warnings here 267cdf0e10cSrcweir } 268cdf0e10cSrcweir //------------------------------------------------------------------------------ 269cdf0e10cSrcweir void MacabConnection::disposing() 270cdf0e10cSrcweir { 271cdf0e10cSrcweir // we noticed that we should be destroied in near future so we have to dispose our statements 272cdf0e10cSrcweir ::osl::MutexGuard aGuard(m_aMutex); 273cdf0e10cSrcweir 274cdf0e10cSrcweir for (OWeakRefArray::iterator i = m_aStatements.begin(); m_aStatements.end() != i; ++i) 275cdf0e10cSrcweir { 276cdf0e10cSrcweir Reference< XComponent > xComp(i->get(), UNO_QUERY); 277cdf0e10cSrcweir if (xComp.is()) 278cdf0e10cSrcweir xComp->dispose(); 279cdf0e10cSrcweir } 280cdf0e10cSrcweir m_aStatements.clear(); 281cdf0e10cSrcweir 282cdf0e10cSrcweir if (m_pAddressBook != NULL) 283cdf0e10cSrcweir { 284cdf0e10cSrcweir delete m_pAddressBook; 285cdf0e10cSrcweir m_pAddressBook = NULL; 286cdf0e10cSrcweir } 287cdf0e10cSrcweir 288cdf0e10cSrcweir m_xMetaData = ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XDatabaseMetaData>(); 289cdf0e10cSrcweir 290cdf0e10cSrcweir dispose_ChildImpl(); 291cdf0e10cSrcweir MacabConnection_BASE::disposing(); 292cdf0e10cSrcweir } 293cdf0e10cSrcweir // ----------------------------------------------------------------------------- 294cdf0e10cSrcweir Reference< XTablesSupplier > SAL_CALL MacabConnection::createCatalog() 295cdf0e10cSrcweir { 296cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 297cdf0e10cSrcweir 298cdf0e10cSrcweir Reference< XTablesSupplier > xTab = m_xCatalog; 299cdf0e10cSrcweir if (!m_xCatalog.is()) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir MacabCatalog *pCat = new MacabCatalog(this); 302cdf0e10cSrcweir xTab = pCat; 303cdf0e10cSrcweir m_xCatalog = xTab; 304cdf0e10cSrcweir } 305cdf0e10cSrcweir return xTab; 306cdf0e10cSrcweir } 307cdf0e10cSrcweir // ----------------------------------------------------------------------------- 308cdf0e10cSrcweir MacabAddressBook* MacabConnection::getAddressBook() const 309cdf0e10cSrcweir { 310cdf0e10cSrcweir return m_pAddressBook; 311cdf0e10cSrcweir } 312cdf0e10cSrcweir // ----------------------------------------------------------------------------- 313cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void* SAL_CALL createMacabConnection( void* _pDriver ) 314cdf0e10cSrcweir { 315cdf0e10cSrcweir MacabConnection* pConnection = new MacabConnection( static_cast< MacabDriver* >( _pDriver ) ); 316cdf0e10cSrcweir // by definition, the pointer crossing library boundaries as void ptr is acquired once 317cdf0e10cSrcweir pConnection->acquire(); 318cdf0e10cSrcweir return pConnection; 319cdf0e10cSrcweir } 320cdf0e10cSrcweir 321