Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / file / FConnection.cxx
blobe3befff7e5d94bdb164ae09329024b8c5fb31e74
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 <cppuhelper/typeprovider.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/sdbc/ColumnValue.hpp>
30 #include <com/sun/star/sdbc/XRow.hpp>
31 #include <com/sun/star/lang/DisposedException.hpp>
32 #include <com/sun/star/container/XChild.hpp>
33 #include <com/sun/star/ucb/XContent.hpp>
34 #include <com/sun/star/ucb/XContentIdentifier.hpp>
35 #include <tools/urlobj.hxx>
36 #include "file/FCatalog.hxx"
37 #include <unotools/pathoptions.hxx>
38 #include <ucbhelper/content.hxx>
39 #include <connectivity/dbcharset.hxx>
40 #include <connectivity/dbexception.hxx>
41 #include <osl/thread.h>
42 #include <osl/nlsupport.h>
43 #include "resource/file_res.hrc"
45 using namespace connectivity::file;
46 using namespace dbtools;
48 using namespace com::sun::star::uno;
49 using namespace com::sun::star::lang;
50 using namespace com::sun::star::beans;
51 using namespace com::sun::star::sdbc;
52 using namespace com::sun::star::sdbcx;
53 using namespace com::sun::star::container;
54 using namespace com::sun::star::ucb;
55 using namespace ::ucbhelper;
56 typedef connectivity::OMetaConnection OConnection_BASE;
58 OConnection::OConnection(OFileDriver* _pDriver)
59 : OSubComponent<OConnection, OConnection_BASE>((::cppu::OWeakObject*)_pDriver, this)
60 , m_pDriver(_pDriver)
61 , m_bClosed(false)
62 , m_bAutoCommit(false)
63 , m_bReadOnly(false)
64 , m_bShowDeleted(false)
65 , m_bCaseSensitiveExtension( true )
66 , m_bCheckSQL92(false)
67 , m_bDefaultTextEncoding(false)
69 m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
72 OConnection::~OConnection()
74 if(!isClosed( ))
75 close();
78 void SAL_CALL OConnection::release() throw()
80 relase_ChildImpl();
84 bool OConnection::matchesExtension( const OUString& _rExt ) const
86 if ( isCaseSensitveExtension() )
87 return ( getExtension() == _rExt );
89 OUString sMyExtension( getExtension().toAsciiLowerCase() );
90 OUString sExt( _rExt.toAsciiLowerCase() );
92 return sMyExtension == sExt;
96 void OConnection::construct(const OUString& url,const Sequence< PropertyValue >& info)
97 throw( css::sdbc::SQLException,
98 css::uno::RuntimeException,
99 css::uno::DeploymentException)
101 osl_atomic_increment( &m_refCount );
103 OUString aExt;
104 const PropertyValue *pIter = info.getConstArray();
105 const PropertyValue *pEnd = pIter + info.getLength();
106 for(;pIter != pEnd;++pIter)
108 if( pIter->Name.equalsAscii("Extension"))
109 OSL_VERIFY( pIter->Value >>= aExt );
110 else if( pIter->Name.equalsAscii("CharSet"))
112 OUString sIanaName;
113 OSL_VERIFY( pIter->Value >>= sIanaName );
115 ::dbtools::OCharsetMap aLookupIanaName;
116 ::dbtools::OCharsetMap::const_iterator aLookup = aLookupIanaName.find(sIanaName, ::dbtools::OCharsetMap::IANA());
117 if (aLookup != aLookupIanaName.end())
118 m_nTextEncoding = (*aLookup).getEncoding();
119 else
120 m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
122 else if( pIter->Name.equalsAscii("ShowDeleted"))
124 OSL_VERIFY( pIter->Value >>= m_bShowDeleted );
126 else if( pIter->Name.equalsAscii("EnableSQL92Check"))
128 pIter->Value >>= m_bCheckSQL92;
130 } // for(;pIter != pEnd;++pIter)
133 sal_Int32 nLen = url.indexOf(':');
134 nLen = url.indexOf(':',nLen+1);
135 OUString aDSN(url.copy(nLen+1));
137 OUString aFileName = aDSN;
138 INetURLObject aURL;
139 aURL.SetSmartProtocol(INET_PROT_FILE);
141 SvtPathOptions aPathOptions;
142 aFileName = aPathOptions.SubstituteVariable(aFileName);
145 aURL.SetSmartURL(aFileName);
147 setURL(aURL.GetMainURL(INetURLObject::NO_DECODE));
150 if ( m_nTextEncoding == RTL_TEXTENCODING_DONTKNOW )
152 //m_nTextEncoding = osl_getTextEncodingFromLocale(NULL);
153 m_nTextEncoding = osl_getThreadTextEncoding();
154 m_bDefaultTextEncoding = true;
157 if ( !aExt.isEmpty() )
158 m_aFilenameExtension = aExt;
162 ::ucbhelper::Content aFile;
165 aFile = ::ucbhelper::Content(getURL(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
167 catch(ContentCreationException& e)
169 throwUrlNotValid(getURL(),e.Message);
172 // set fields to fetch
173 Sequence< OUString > aProps(1);
174 OUString* pProps = aProps.getArray();
175 pProps[ 0 ] = "Title";
179 if (aFile.isFolder())
181 m_xDir = aFile.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
182 m_xContent = aFile.get();
184 else if (aFile.isDocument())
186 Reference<XContent> xParent(Reference<XChild>(aFile.get(),UNO_QUERY)->getParent(),UNO_QUERY);
187 Reference<XContentIdentifier> xIdent = xParent->getIdentifier();
188 m_xContent = xParent;
190 ::ucbhelper::Content aParent(xIdent->getContentIdentifier(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
191 m_xDir = aParent.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
193 else
195 OSL_FAIL("OConnection::construct: ::ucbhelper::Content isn't a folde nor a document! How that?!");
196 throw SQLException();
199 catch(Exception& e) // a exception is thrown when no file exists
201 throwUrlNotValid(getURL(),e.Message);
203 if(!m_xDir.is() || !m_xContent.is())
204 throwUrlNotValid(getURL(),OUString());
206 if (m_aFilenameExtension.indexOf('*') >= 0 || m_aFilenameExtension.indexOf('?') >= 0)
207 throw SQLException();
209 catch(const Exception&)
211 osl_atomic_decrement( &m_refCount );
212 throw;
215 osl_atomic_decrement( &m_refCount );
217 // XServiceInfo
219 IMPLEMENT_SERVICE_INFO(OConnection, "com.sun.star.sdbc.drivers.file.Connection", "com.sun.star.sdbc.Connection")
222 Reference< XStatement > SAL_CALL OConnection::createStatement( ) throw(SQLException, RuntimeException, std::exception)
224 ::osl::MutexGuard aGuard( m_aMutex );
225 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
228 Reference< XStatement > xReturn = new OStatement(this);
229 m_aStatements.push_back(WeakReferenceHelper(xReturn));
230 return xReturn;
233 Reference< XPreparedStatement > SAL_CALL OConnection::prepareStatement( const OUString& sql ) throw(SQLException, RuntimeException, std::exception)
235 ::osl::MutexGuard aGuard( m_aMutex );
236 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
239 OPreparedStatement* pStmt = new OPreparedStatement(this);
240 Reference< XPreparedStatement > xHoldAlive = pStmt;
241 pStmt->construct(sql);
242 m_aStatements.push_back(WeakReferenceHelper(*pStmt));
243 return pStmt;
246 Reference< XPreparedStatement > SAL_CALL OConnection::prepareCall( const OUString& /*sql*/ ) throw(SQLException, RuntimeException, std::exception)
248 throwFeatureNotImplementedException( "XConnection::prepareCall", *this );
249 return NULL;
252 OUString SAL_CALL OConnection::nativeSQL( const OUString& sql ) throw(SQLException, RuntimeException, std::exception)
254 return sql;
257 void SAL_CALL OConnection::setAutoCommit( sal_Bool autoCommit ) throw(SQLException, RuntimeException, std::exception)
259 ::osl::MutexGuard aGuard( m_aMutex );
260 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
262 m_bAutoCommit = autoCommit;
265 sal_Bool SAL_CALL OConnection::getAutoCommit( ) throw(SQLException, RuntimeException, std::exception)
267 ::osl::MutexGuard aGuard( m_aMutex );
268 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
270 return m_bAutoCommit;
273 void SAL_CALL OConnection::commit( ) throw(SQLException, RuntimeException, std::exception)
277 void SAL_CALL OConnection::rollback( ) throw(SQLException, RuntimeException, std::exception)
281 sal_Bool SAL_CALL OConnection::isClosed( ) throw(SQLException, RuntimeException, std::exception)
283 ::osl::MutexGuard aGuard( m_aMutex );
285 return OConnection_BASE::rBHelper.bDisposed;
288 Reference< XDatabaseMetaData > SAL_CALL OConnection::getMetaData( ) throw(SQLException, RuntimeException, std::exception)
290 ::osl::MutexGuard aGuard( m_aMutex );
291 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
294 Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
295 if(!xMetaData.is())
297 xMetaData = new ODatabaseMetaData(this);
298 m_xMetaData = xMetaData;
301 return xMetaData;
304 void SAL_CALL OConnection::setReadOnly( sal_Bool readOnly ) throw(SQLException, RuntimeException, std::exception)
306 ::osl::MutexGuard aGuard( m_aMutex );
307 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
310 m_bReadOnly = readOnly;
313 sal_Bool SAL_CALL OConnection::isReadOnly( ) throw(SQLException, RuntimeException, std::exception)
315 ::osl::MutexGuard aGuard( m_aMutex );
316 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
319 return m_bReadOnly;
322 void SAL_CALL OConnection::setCatalog( const OUString& /*catalog*/ ) throw(SQLException, RuntimeException, std::exception)
324 throwFeatureNotImplementedException( "XConnection::setCatalog", *this );
327 OUString SAL_CALL OConnection::getCatalog( ) throw(SQLException, RuntimeException, std::exception)
329 return OUString();
332 void SAL_CALL OConnection::setTransactionIsolation( sal_Int32 /*level*/ ) throw(SQLException, RuntimeException, std::exception)
334 throwFeatureNotImplementedException( "XConnection::setTransactionIsolation", *this );
337 sal_Int32 SAL_CALL OConnection::getTransactionIsolation( ) throw(SQLException, RuntimeException, std::exception)
339 return 0;
342 Reference< XNameAccess > SAL_CALL OConnection::getTypeMap( ) throw(SQLException, RuntimeException, std::exception)
344 return NULL;
347 void SAL_CALL OConnection::setTypeMap( const Reference< XNameAccess >& /*typeMap*/ ) throw(SQLException, RuntimeException, std::exception)
351 // XCloseable
352 void SAL_CALL OConnection::close( ) throw(SQLException, RuntimeException, std::exception)
355 ::osl::MutexGuard aGuard( m_aMutex );
356 checkDisposed(OConnection_BASE::rBHelper.bDisposed);
359 dispose();
362 // XWarningsSupplier
363 Any SAL_CALL OConnection::getWarnings( ) throw(SQLException, RuntimeException, std::exception)
365 return Any();
368 void SAL_CALL OConnection::clearWarnings( ) throw(SQLException, RuntimeException, std::exception)
372 void OConnection::disposing()
374 ::osl::MutexGuard aGuard(m_aMutex);
375 OConnection_BASE::disposing();
377 m_bClosed = true;
378 m_xDir.clear();
379 m_xContent.clear();
380 m_xCatalog = WeakReference< XTablesSupplier>();
382 dispose_ChildImpl();
385 Reference< XTablesSupplier > OConnection::createCatalog()
387 ::osl::MutexGuard aGuard( m_aMutex );
388 Reference< XTablesSupplier > xTab = m_xCatalog;
389 if(!xTab.is())
391 xTab = new OFileCatalog(this);
392 m_xCatalog = xTab;
394 return xTab;
397 Reference< XDynamicResultSet > OConnection::getDir() const
399 Reference<XDynamicResultSet> xContent;
400 Sequence< OUString > aProps(1);
401 OUString* pProps = aProps.getArray();
402 pProps[ 0 ] = "Title";
405 Reference<XContentIdentifier> xIdent = getContent()->getIdentifier();
406 ::ucbhelper::Content aParent(xIdent->getContentIdentifier(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
407 xContent = aParent.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
409 catch(Exception&)
412 return xContent;
415 sal_Int64 SAL_CALL OConnection::getSomething( const Sequence< sal_Int8 >& rId ) throw (RuntimeException, std::exception)
417 return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
418 ? reinterpret_cast< sal_Int64 >( this )
419 : (sal_Int64)0;
422 Sequence< sal_Int8 > OConnection::getUnoTunnelImplementationId()
424 static ::cppu::OImplementationId * pId = 0;
425 if (! pId)
427 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
428 if (! pId)
430 static ::cppu::OImplementationId aId;
431 pId = &aId;
434 return pId->getImplementationId();
437 void OConnection::throwUrlNotValid(const OUString & _rsUrl,const OUString & _rsMessage)
439 SQLException aError;
440 aError.Message = getResources().getResourceStringWithSubstitution(
441 STR_NO_VALID_FILE_URL,
442 "$URL$", _rsUrl
445 aError.SQLState = "S1000";
446 aError.ErrorCode = 0;
447 aError.Context = static_cast< XConnection* >(this);
448 if (!_rsMessage.isEmpty())
449 aError.NextException <<= SQLException(_rsMessage, aError.Context, OUString(), 0, Any());
451 throw aError;
457 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */