nss: upgrade to release 3.73
[LibreOffice.git] / connectivity / source / drivers / file / FConnection.cxx
blob5c56ce4d5e5d379fe74a5217d08bc66001724ad3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <sal/config.h>
22 #include <comphelper/processfactory.hxx>
23 #include <comphelper/servicehelper.hxx>
24 #include <cppuhelper/typeprovider.hxx>
25 #include <file/FConnection.hxx>
26 #include <file/FDatabaseMetaData.hxx>
27 #include <file/FDriver.hxx>
28 #include <file/FStatement.hxx>
29 #include <file/FPreparedStatement.hxx>
30 #include <com/sun/star/container/XChild.hpp>
31 #include <com/sun/star/ucb/ContentCreationException.hpp>
32 #include <com/sun/star/ucb/XContent.hpp>
33 #include <com/sun/star/ucb/XContentIdentifier.hpp>
34 #include <tools/urlobj.hxx>
35 #include <file/FCatalog.hxx>
36 #include <unotools/pathoptions.hxx>
37 #include <ucbhelper/content.hxx>
38 #include <connectivity/dbcharset.hxx>
39 #include <connectivity/dbexception.hxx>
40 #include <o3tl/any.hxx>
41 #include <osl/thread.h>
42 #include <strings.hrc>
44 using namespace connectivity::file;
45 using namespace dbtools;
47 using namespace com::sun::star::uno;
48 using namespace com::sun::star::lang;
49 using namespace com::sun::star::beans;
50 using namespace com::sun::star::sdbc;
51 using namespace com::sun::star::sdbcx;
52 using namespace com::sun::star::container;
53 using namespace com::sun::star::ucb;
54 using namespace ::ucbhelper;
55 typedef connectivity::OMetaConnection OConnection_BASE;
57 OConnection::OConnection(OFileDriver* _pDriver)
58 : m_pDriver(_pDriver)
59 , m_bAutoCommit(false)
60 , m_bReadOnly(false)
61 , m_bShowDeleted(false)
62 , m_bCaseSensitiveExtension( true )
63 , m_bCheckSQL92(false)
64 , m_bDefaultTextEncoding(false)
66 m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
69 OConnection::~OConnection()
71 if(!isClosed( ))
72 close();
75 bool OConnection::matchesExtension( const OUString& _rExt ) const
77 if ( isCaseSensitiveExtension() )
78 return ( getExtension() == _rExt );
80 OUString sMyExtension( getExtension().toAsciiLowerCase() );
81 OUString sExt( _rExt.toAsciiLowerCase() );
83 return sMyExtension == sExt;
87 void OConnection::construct(const OUString& url,const Sequence< PropertyValue >& info)
89 osl_atomic_increment( &m_refCount );
91 OUString aExt;
92 const PropertyValue *pIter = info.getConstArray();
93 const PropertyValue *pEnd = pIter + info.getLength();
94 for(;pIter != pEnd;++pIter)
96 if( pIter->Name == "Extension" )
97 OSL_VERIFY( pIter->Value >>= aExt );
98 else if( pIter->Name == "CharSet" )
100 if (auto const numeric = o3tl::tryAccess<sal_uInt16>(pIter->Value))
102 m_nTextEncoding = *numeric;
104 else
106 OUString sIanaName;
107 OSL_VERIFY( pIter->Value >>= sIanaName );
109 ::dbtools::OCharsetMap aLookupIanaName;
110 ::dbtools::OCharsetMap::const_iterator aLookup = aLookupIanaName.findIanaName(sIanaName);
111 if (aLookup != aLookupIanaName.end())
112 m_nTextEncoding = (*aLookup).getEncoding();
113 else
114 m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
117 else if( pIter->Name == "ShowDeleted" )
119 OSL_VERIFY( pIter->Value >>= m_bShowDeleted );
121 else if( pIter->Name == "EnableSQL92Check" )
123 pIter->Value >>= m_bCheckSQL92;
125 } // for(;pIter != pEnd;++pIter)
128 sal_Int32 nLen = url.indexOf(':');
129 nLen = url.indexOf(':',nLen+1);
130 OUString aDSN(url.copy(nLen+1));
132 OUString aFileName = aDSN;
133 INetURLObject aURL;
134 aURL.SetSmartProtocol(INetProtocol::File);
136 SvtPathOptions aPathOptions;
137 aFileName = aPathOptions.SubstituteVariable(aFileName);
140 aURL.SetSmartURL(aFileName);
142 setURL(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE));
145 if ( m_nTextEncoding == RTL_TEXTENCODING_DONTKNOW )
147 //m_nTextEncoding = osl_getTextEncodingFromLocale(NULL);
148 m_nTextEncoding = osl_getThreadTextEncoding();
149 m_bDefaultTextEncoding = true;
152 if ( !aExt.isEmpty() )
153 m_aFilenameExtension = aExt;
157 ::ucbhelper::Content aFile;
160 aFile = ::ucbhelper::Content(getURL(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
162 catch(ContentCreationException& e)
164 throwUrlNotValid(getURL(),e.Message);
167 // set fields to fetch
168 Sequence< OUString > aProps { "Title" };
172 if (aFile.isFolder())
174 m_xDir = aFile.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
175 m_xContent = aFile.get();
177 else if (aFile.isDocument())
179 Reference<XContent> xParent(Reference<XChild>(aFile.get(),UNO_QUERY_THROW)->getParent(),UNO_QUERY_THROW);
180 Reference<XContentIdentifier> xIdent = xParent->getIdentifier();
181 m_xContent = xParent;
183 ::ucbhelper::Content aParent(xIdent->getContentIdentifier(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
184 m_xDir = aParent.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
186 else
188 OSL_FAIL("OConnection::construct: ::ucbhelper::Content is neither a folder nor a document! How's that?!");
189 throw SQLException();
192 catch(Exception& e) // an exception is thrown when no file exists
194 throwUrlNotValid(getURL(),e.Message);
196 if(!m_xDir.is() || !m_xContent.is())
197 throwUrlNotValid(getURL(),OUString());
199 if (m_aFilenameExtension.indexOf('*') >= 0 || m_aFilenameExtension.indexOf('?') >= 0)
200 throw SQLException();
202 catch(const Exception&)
204 osl_atomic_decrement( &m_refCount );
205 throw;
208 osl_atomic_decrement( &m_refCount );
210 // XServiceInfo
212 IMPLEMENT_SERVICE_INFO(OConnection, "com.sun.star.sdbc.drivers.file.Connection", "com.sun.star.sdbc.Connection")
215 Reference< XStatement > SAL_CALL OConnection::createStatement( )
217 ::osl::MutexGuard aGuard( m_aMutex );
218 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
221 Reference< XStatement > xReturn = new OStatement(this);
222 m_aStatements.push_back(WeakReferenceHelper(xReturn));
223 return xReturn;
226 Reference< XPreparedStatement > SAL_CALL OConnection::prepareStatement( const OUString& sql )
228 ::osl::MutexGuard aGuard( m_aMutex );
229 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
232 OPreparedStatement* pStmt = new OPreparedStatement(this);
233 Reference< XPreparedStatement > xHoldAlive = pStmt;
234 pStmt->construct(sql);
235 m_aStatements.push_back(WeakReferenceHelper(*pStmt));
236 return pStmt;
239 Reference< XPreparedStatement > SAL_CALL OConnection::prepareCall( const OUString& /*sql*/ )
241 throwFeatureNotImplementedSQLException( "XConnection::prepareCall", *this );
242 return nullptr;
245 OUString SAL_CALL OConnection::nativeSQL( const OUString& sql )
247 return sql;
250 void SAL_CALL OConnection::setAutoCommit( sal_Bool autoCommit )
252 ::osl::MutexGuard aGuard( m_aMutex );
253 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
255 m_bAutoCommit = autoCommit;
258 sal_Bool SAL_CALL OConnection::getAutoCommit( )
260 ::osl::MutexGuard aGuard( m_aMutex );
261 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
263 return m_bAutoCommit;
266 void SAL_CALL OConnection::commit( )
270 void SAL_CALL OConnection::rollback( )
274 sal_Bool SAL_CALL OConnection::isClosed( )
276 ::osl::MutexGuard aGuard( m_aMutex );
278 return OConnection_BASE::rBHelper.bDisposed;
281 Reference< XDatabaseMetaData > SAL_CALL OConnection::getMetaData( )
283 ::osl::MutexGuard aGuard( m_aMutex );
284 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
287 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
288 if(!xMetaData.is())
290 xMetaData = new ODatabaseMetaData(this);
291 m_xMetaData = xMetaData;
294 return xMetaData;
297 void SAL_CALL OConnection::setReadOnly( sal_Bool readOnly )
299 ::osl::MutexGuard aGuard( m_aMutex );
300 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
303 m_bReadOnly = readOnly;
306 sal_Bool SAL_CALL OConnection::isReadOnly( )
308 ::osl::MutexGuard aGuard( m_aMutex );
309 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
312 return m_bReadOnly;
315 void SAL_CALL OConnection::setCatalog( const OUString& /*catalog*/ )
317 throwFeatureNotImplementedSQLException( "XConnection::setCatalog", *this );
320 OUString SAL_CALL OConnection::getCatalog( )
322 return OUString();
325 void SAL_CALL OConnection::setTransactionIsolation( sal_Int32 /*level*/ )
327 throwFeatureNotImplementedSQLException( "XConnection::setTransactionIsolation", *this );
330 sal_Int32 SAL_CALL OConnection::getTransactionIsolation( )
332 return 0;
335 Reference< XNameAccess > SAL_CALL OConnection::getTypeMap( )
337 return nullptr;
340 void SAL_CALL OConnection::setTypeMap( const Reference< XNameAccess >& /*typeMap*/ )
344 // XCloseable
345 void SAL_CALL OConnection::close( )
348 ::osl::MutexGuard aGuard( m_aMutex );
349 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
352 dispose();
355 // XWarningsSupplier
356 Any SAL_CALL OConnection::getWarnings( )
358 return Any();
361 void SAL_CALL OConnection::clearWarnings( )
365 void OConnection::disposing()
367 ::osl::MutexGuard aGuard(m_aMutex);
368 OConnection_BASE::disposing();
370 m_xDir.clear();
371 m_xContent.clear();
372 m_xCatalog = WeakReference< XTablesSupplier>();
375 Reference< XTablesSupplier > OConnection::createCatalog()
377 ::osl::MutexGuard aGuard( m_aMutex );
378 Reference< XTablesSupplier > xTab = m_xCatalog;
379 if(!xTab.is())
381 xTab = new OFileCatalog(this);
382 m_xCatalog = xTab;
384 return xTab;
387 Reference< XDynamicResultSet > OConnection::getDir() const
389 Reference<XDynamicResultSet> xContent;
390 Sequence< OUString > aProps { "Title" };
393 Reference<XContentIdentifier> xIdent = getContent()->getIdentifier();
394 ::ucbhelper::Content aParent(xIdent->getContentIdentifier(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
395 xContent = aParent.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
397 catch(Exception&)
400 return xContent;
403 sal_Int64 SAL_CALL OConnection::getSomething( const Sequence< sal_Int8 >& rId )
405 return (isUnoTunnelId<OConnection>(rId))
406 ? reinterpret_cast< sal_Int64 >( this )
407 : sal_Int64(0);
410 Sequence< sal_Int8 > OConnection::getUnoTunnelId()
412 static ::cppu::OImplementationId implId;
414 return implId.getImplementationId();
417 void OConnection::throwUrlNotValid(const OUString & _rsUrl,const OUString & _rsMessage)
419 SQLException aError;
420 aError.Message = getResources().getResourceStringWithSubstitution(
421 STR_NO_VALID_FILE_URL,
422 "$URL$", _rsUrl
425 aError.SQLState = "S1000";
426 aError.ErrorCode = 0;
427 aError.Context = static_cast< XConnection* >(this);
428 if (!_rsMessage.isEmpty())
429 aError.NextException <<= SQLException(_rsMessage, aError.Context, OUString(), 0, Any());
431 throw aError;
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */