cid#1636677 Uninitialized scalar field
[LibreOffice.git] / connectivity / source / drivers / file / FConnection.cxx
blobfe43ed6477edbdb9de0afd7bdb303412ed50a2c4
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 <file/FConnection.hxx>
25 #include <file/FDatabaseMetaData.hxx>
26 #include <file/FDriver.hxx>
27 #include <file/FStatement.hxx>
28 #include <file/FPreparedStatement.hxx>
29 #include <com/sun/star/container/XChild.hpp>
30 #include <com/sun/star/ucb/ContentCreationException.hpp>
31 #include <com/sun/star/ucb/XContent.hpp>
32 #include <com/sun/star/ucb/XContentIdentifier.hpp>
33 #include <tools/urlobj.hxx>
34 #include <file/FCatalog.hxx>
35 #include <comphelper/configuration.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);
135 if (!comphelper::IsFuzzing())
137 SvtPathOptions aPathOptions;
138 aFileName = aPathOptions.SubstituteVariable(aFileName);
141 aURL.SetSmartURL(aFileName);
143 setURL(aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE));
146 if ( m_nTextEncoding == RTL_TEXTENCODING_DONTKNOW )
148 //m_nTextEncoding = osl_getTextEncodingFromLocale(NULL);
149 m_nTextEncoding = osl_getThreadTextEncoding();
150 m_bDefaultTextEncoding = true;
153 if ( !aExt.isEmpty() )
154 m_aFilenameExtension = aExt;
158 ::ucbhelper::Content aFile;
161 aFile = ::ucbhelper::Content(getURL(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
163 catch(ContentCreationException& e)
165 throwUrlNotValid(getURL(),e.Message);
168 // set fields to fetch
169 Sequence< OUString > aProps { u"Title"_ustr };
173 if (aFile.isFolder())
175 m_xDir = aFile.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
176 m_xContent = aFile.get();
178 else if (aFile.isDocument())
180 Reference<XContent> xParent(Reference<XChild>(aFile.get(),UNO_QUERY_THROW)->getParent(),UNO_QUERY_THROW);
181 Reference<XContentIdentifier> xIdent = xParent->getIdentifier();
182 m_xContent = std::move(xParent);
184 ::ucbhelper::Content aParent(xIdent->getContentIdentifier(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
185 m_xDir = aParent.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
187 else
189 OSL_FAIL("OConnection::construct: ::ucbhelper::Content is neither a folder nor a document! How's that?!");
190 throw SQLException();
193 catch(Exception& e) // an exception is thrown when no file exists
195 throwUrlNotValid(getURL(),e.Message);
197 if(!m_xDir.is() || !m_xContent.is())
198 throwUrlNotValid(getURL(),OUString());
200 if (m_aFilenameExtension.indexOf('*') >= 0 || m_aFilenameExtension.indexOf('?') >= 0)
201 throw SQLException();
203 catch(const Exception&)
205 osl_atomic_decrement( &m_refCount );
206 throw;
209 osl_atomic_decrement( &m_refCount );
211 // XServiceInfo
213 IMPLEMENT_SERVICE_INFO(OConnection, u"com.sun.star.sdbc.drivers.file.Connection"_ustr, u"com.sun.star.sdbc.Connection"_ustr)
216 Reference< XStatement > SAL_CALL OConnection::createStatement( )
218 ::osl::MutexGuard aGuard( m_aMutex );
219 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
222 Reference< XStatement > xReturn = new OStatement(this);
223 m_aStatements.push_back(WeakReferenceHelper(xReturn));
224 return xReturn;
227 Reference< XPreparedStatement > SAL_CALL OConnection::prepareStatement( const OUString& sql )
229 ::osl::MutexGuard aGuard( m_aMutex );
230 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
233 rtl::Reference<OPreparedStatement> pStmt = new OPreparedStatement(this);
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( u"XConnection::prepareCall"_ustr, *this );
244 OUString SAL_CALL OConnection::nativeSQL( const OUString& sql )
246 return sql;
249 void SAL_CALL OConnection::setAutoCommit( sal_Bool autoCommit )
251 ::osl::MutexGuard aGuard( m_aMutex );
252 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
254 m_bAutoCommit = autoCommit;
257 sal_Bool SAL_CALL OConnection::getAutoCommit( )
259 ::osl::MutexGuard aGuard( m_aMutex );
260 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
262 return m_bAutoCommit;
265 void SAL_CALL OConnection::commit( )
269 void SAL_CALL OConnection::rollback( )
273 sal_Bool SAL_CALL OConnection::isClosed( )
275 ::osl::MutexGuard aGuard( m_aMutex );
277 return OConnection_BASE::rBHelper.bDisposed;
280 Reference< XDatabaseMetaData > SAL_CALL OConnection::getMetaData( )
282 ::osl::MutexGuard aGuard( m_aMutex );
283 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
286 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
287 if(!xMetaData.is())
289 xMetaData = new ODatabaseMetaData(this);
290 m_xMetaData = xMetaData;
293 return xMetaData;
296 void SAL_CALL OConnection::setReadOnly( sal_Bool readOnly )
298 ::osl::MutexGuard aGuard( m_aMutex );
299 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
302 m_bReadOnly = readOnly;
305 sal_Bool SAL_CALL OConnection::isReadOnly( )
307 ::osl::MutexGuard aGuard( m_aMutex );
308 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
311 return m_bReadOnly;
314 void SAL_CALL OConnection::setCatalog( const OUString& /*catalog*/ )
316 throwFeatureNotImplementedSQLException( u"XConnection::setCatalog"_ustr, *this );
319 OUString SAL_CALL OConnection::getCatalog( )
321 return OUString();
324 void SAL_CALL OConnection::setTransactionIsolation( sal_Int32 /*level*/ )
326 throwFeatureNotImplementedSQLException( u"XConnection::setTransactionIsolation"_ustr, *this );
329 sal_Int32 SAL_CALL OConnection::getTransactionIsolation( )
331 return 0;
334 Reference< XNameAccess > SAL_CALL OConnection::getTypeMap( )
336 return nullptr;
339 void SAL_CALL OConnection::setTypeMap( const Reference< XNameAccess >& /*typeMap*/ )
343 // XCloseable
344 void SAL_CALL OConnection::close( )
347 ::osl::MutexGuard aGuard( m_aMutex );
348 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
351 dispose();
354 // XWarningsSupplier
355 Any SAL_CALL OConnection::getWarnings( )
357 return Any();
360 void SAL_CALL OConnection::clearWarnings( )
364 void OConnection::disposing()
366 ::osl::MutexGuard aGuard(m_aMutex);
367 OConnection_BASE::disposing();
369 m_xDir.clear();
370 m_xContent.clear();
371 m_xCatalog.clear();
374 Reference< XTablesSupplier > OConnection::createCatalog()
376 ::osl::MutexGuard aGuard( m_aMutex );
377 rtl::Reference< connectivity::sdbcx::OCatalog > xTab = m_xCatalog.get();
378 if(!xTab.is())
380 xTab = new OFileCatalog(this);
381 m_xCatalog = xTab.get();
383 return xTab;
386 Reference< XDynamicResultSet > OConnection::getDir() const
388 Reference<XDynamicResultSet> xContent;
389 Sequence< OUString > aProps { u"Title"_ustr };
392 Reference<XContentIdentifier> xIdent = getContent()->getIdentifier();
393 ::ucbhelper::Content aParent(xIdent->getContentIdentifier(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
394 xContent = aParent.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
396 catch(Exception&)
399 return xContent;
402 sal_Int64 SAL_CALL OConnection::getSomething( const Sequence< sal_Int8 >& rId )
404 return comphelper::getSomethingImpl(rId, this);
407 const Sequence< sal_Int8 > & OConnection::getUnoTunnelId()
409 static const comphelper::UnoIdInit implId;
410 return implId.getSeq();
413 void OConnection::throwUrlNotValid(const OUString & _rsUrl,const OUString & _rsMessage)
415 XConnection* context = this;
416 css::uno::Any next;
417 if (!_rsMessage.isEmpty())
418 next <<= SQLException(_rsMessage, context, OUString(), 0, Any());
419 SQLException aError(
420 getResources().getResourceStringWithSubstitution(STR_NO_VALID_FILE_URL, "$URL$", _rsUrl),
421 context, u"S1000"_ustr, 0, next);
423 throw aError;
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */