Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / calc / CConnection.cxx
bloba515c9ddb392e904b8f89e035c303e1f7d2e705b
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: CConnection.cxx,v $
10 * $Revision: 1.18.22.1 $
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"
33 #include "calc/CConnection.hxx"
34 #include "calc/CDatabaseMetaData.hxx"
35 #include "calc/CCatalog.hxx"
36 #ifndef _CONNECTIVITY_CALC_ODRIVER_HXX_
37 #include "calc/CDriver.hxx"
38 #endif
39 #ifndef CONNECTIVITY_RESOURCE_CALC_HRC
40 #include "resource/calc_res.hrc"
41 #endif
42 #include "resource/sharedresources.hxx"
43 #include <com/sun/star/lang/DisposedException.hpp>
44 #include <com/sun/star/frame/XComponentLoader.hpp>
45 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
46 #include <tools/urlobj.hxx>
47 #include "calc/CPreparedStatement.hxx"
48 #include "calc/CStatement.hxx"
49 #include <svtools/pathoptions.hxx>
50 #include <connectivity/dbexception.hxx>
51 #include <cppuhelper/exc_hlp.hxx>
52 #include <rtl/logfile.hxx>
54 using namespace connectivity::calc;
55 using namespace connectivity::file;
57 typedef connectivity::file::OConnection OConnection_BASE;
59 //------------------------------------------------------------------------------
61 using namespace ::com::sun::star::uno;
62 using namespace ::com::sun::star::beans;
63 using namespace ::com::sun::star::sdbcx;
64 using namespace ::com::sun::star::sdbc;
65 using namespace ::com::sun::star::lang;
66 using namespace ::com::sun::star::frame;
67 using namespace ::com::sun::star::sheet;
69 // --------------------------------------------------------------------------------
71 OCalcConnection::OCalcConnection(ODriver* _pDriver) : OConnection(_pDriver),m_nDocCount(0)
73 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::OCalcConnection" );
74 // m_aFilenameExtension is not used
77 OCalcConnection::~OCalcConnection()
81 void OCalcConnection::construct(const ::rtl::OUString& url,const Sequence< PropertyValue >& info)
82 throw(SQLException)
84 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::construct" );
85 // open file
87 sal_Int32 nLen = url.indexOf(':');
88 nLen = url.indexOf(':',nLen+1);
89 ::rtl::OUString aDSN(url.copy(nLen+1));
91 m_aFileName = aDSN;
92 INetURLObject aURL;
93 aURL.SetSmartProtocol(INET_PROT_FILE);
95 SvtPathOptions aPathOptions;
96 m_aFileName = aPathOptions.SubstituteVariable(m_aFileName);
98 aURL.SetSmartURL(m_aFileName);
99 if ( aURL.GetProtocol() == INET_PROT_NOT_VALID )
101 // don't pass invalid URL to loadComponentFromURL
102 throw SQLException();
104 m_aFileName = aURL.GetMainURL(INetURLObject::NO_DECODE);
106 m_sPassword = ::rtl::OUString();
107 const char* pPwd = "password";
109 const PropertyValue *pIter = info.getConstArray();
110 const PropertyValue *pEnd = pIter + info.getLength();
111 for(;pIter != pEnd;++pIter)
113 if(!pIter->Name.compareToAscii(pPwd))
115 pIter->Value >>= m_sPassword;
116 break;
118 } // for(;pIter != pEnd;++pIter)
119 ODocHolder aDocHodler(this); // just to test that the doc can be loaded
120 acquireDoc();
122 // -----------------------------------------------------------------------------
123 Reference< XSpreadsheetDocument> OCalcConnection::acquireDoc()
125 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::acquireDoc" );
126 if ( m_xDoc.is() )
128 osl_incrementInterlockedCount(&m_nDocCount);
129 return m_xDoc;
131 // open read-only as long as updating isn't implemented
132 Sequence<PropertyValue> aArgs(2);
133 aArgs[0].Name = ::rtl::OUString::createFromAscii("Hidden");
134 aArgs[0].Value <<= (sal_Bool) sal_True;
135 aArgs[1].Name = ::rtl::OUString::createFromAscii("ReadOnly");
136 aArgs[1].Value <<= (sal_Bool) sal_True;
138 if ( m_sPassword.getLength() )
140 const sal_Int32 nPos = aArgs.getLength();
141 aArgs.realloc(nPos+1);
142 aArgs[nPos].Name = ::rtl::OUString::createFromAscii("Password");
143 aArgs[nPos].Value <<= m_sPassword;
146 Reference< XComponentLoader > xDesktop( getDriver()->getFactory()->createInstance(
147 ::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop")), UNO_QUERY );
148 if (!xDesktop.is())
150 OSL_ASSERT("no desktop");
151 throw SQLException();
153 Reference< XComponent > xComponent;
154 Any aLoaderException;
157 xComponent = xDesktop->loadComponentFromURL(
158 m_aFileName, ::rtl::OUString::createFromAscii("_blank"), 0, aArgs );
160 catch( const Exception& )
162 aLoaderException = ::cppu::getCaughtException();
165 m_xDoc.set(xComponent, UNO_QUERY );
167 // if the URL is not a spreadsheet document, throw the exception here
168 // instead of at the first access to it
169 if ( !m_xDoc.is() )
171 Any aErrorDetails;
172 if ( aLoaderException.hasValue() )
174 Exception aLoaderError;
175 OSL_VERIFY( aLoaderException >>= aLoaderError );
177 SQLException aDetailException;
178 aDetailException.Message = m_aResources.getResourceStringWithSubstitution(
179 STR_LOAD_FILE_ERROR_MESSAGE,
180 "$exception_type$", aLoaderException.getValueTypeName(),
181 "$error_message$", aLoaderError.Message
183 aErrorDetails <<= aDetailException;
186 const ::rtl::OUString sError( m_aResources.getResourceStringWithSubstitution(
187 STR_COULD_NOT_LOAD_FILE,
188 "$filename$", m_aFileName
189 ) );
190 ::dbtools::throwGenericSQLException( sError, *this, aErrorDetails );
192 osl_incrementInterlockedCount(&m_nDocCount);
193 return m_xDoc;
195 // -----------------------------------------------------------------------------
196 void OCalcConnection::releaseDoc()
198 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::releaseDoc" );
199 if ( osl_decrementInterlockedCount(&m_nDocCount) == 0 )
200 ::comphelper::disposeComponent( m_xDoc );
202 // -----------------------------------------------------------------------------
203 void OCalcConnection::disposing()
205 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::disposing" );
206 ::osl::MutexGuard aGuard(m_aMutex);
208 m_nDocCount = 0;
209 ::comphelper::disposeComponent( m_xDoc );
211 OConnection::disposing();
214 // XServiceInfo
215 // --------------------------------------------------------------------------------
217 IMPLEMENT_SERVICE_INFO(OCalcConnection, "com.sun.star.sdbc.drivers.calc.Connection", "com.sun.star.sdbc.Connection")
219 // --------------------------------------------------------------------------------
221 Reference< XDatabaseMetaData > SAL_CALL OCalcConnection::getMetaData( ) throw(SQLException, RuntimeException)
223 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::getMetaData" );
224 ::osl::MutexGuard aGuard( m_aMutex );
225 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
228 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
229 if(!xMetaData.is())
231 xMetaData = new OCalcDatabaseMetaData(this);
232 m_xMetaData = xMetaData;
235 return xMetaData;
238 //------------------------------------------------------------------------------
240 ::com::sun::star::uno::Reference< XTablesSupplier > OCalcConnection::createCatalog()
242 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::createCatalog" );
243 ::osl::MutexGuard aGuard( m_aMutex );
244 Reference< XTablesSupplier > xTab = m_xCatalog;
245 if(!xTab.is())
247 OCalcCatalog *pCat = new OCalcCatalog(this);
248 xTab = pCat;
249 m_xCatalog = xTab;
251 return xTab;
254 // --------------------------------------------------------------------------------
256 Reference< XStatement > SAL_CALL OCalcConnection::createStatement( ) throw(SQLException, RuntimeException)
258 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::createStatement" );
259 ::osl::MutexGuard aGuard( m_aMutex );
260 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
263 Reference< XStatement > xReturn = new OCalcStatement(this);
264 m_aStatements.push_back(WeakReferenceHelper(xReturn));
265 return xReturn;
268 // --------------------------------------------------------------------------------
270 Reference< XPreparedStatement > SAL_CALL OCalcConnection::prepareStatement( const ::rtl::OUString& sql )
271 throw(SQLException, RuntimeException)
273 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::prepareStatement" );
274 ::osl::MutexGuard aGuard( m_aMutex );
275 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
278 OCalcPreparedStatement* pStmt = new OCalcPreparedStatement(this);
279 Reference< XPreparedStatement > xHoldAlive = pStmt;
280 pStmt->construct(sql);
281 m_aStatements.push_back(WeakReferenceHelper(*pStmt));
282 return pStmt;
285 // --------------------------------------------------------------------------------
287 Reference< XPreparedStatement > SAL_CALL OCalcConnection::prepareCall( const ::rtl::OUString& /*sql*/ )
288 throw(SQLException, RuntimeException)
290 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "calc", "Ocke.Janssen@sun.com", "OCalcConnection::prepareCall" );
291 ::osl::MutexGuard aGuard( m_aMutex );
292 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
294 ::dbtools::throwFeatureNotImplementedException( "XConnection::prepareCall", *this );
295 return NULL;
297 // -----------------------------------------------------------------------------