Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
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"
44 :
45 : using namespace connectivity::file;
46 : using namespace dbtools;
47 :
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;
57 :
58 38 : OConnection::OConnection(OFileDriver* _pDriver)
59 : : OSubComponent<OConnection, OConnection_BASE>(static_cast<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 38 : , m_bDefaultTextEncoding(false)
68 : {
69 38 : m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
70 38 : }
71 :
72 74 : OConnection::~OConnection()
73 : {
74 37 : if(!isClosed( ))
75 0 : close();
76 37 : }
77 :
78 2673 : void SAL_CALL OConnection::release() throw()
79 : {
80 2673 : relase_ChildImpl();
81 2673 : }
82 :
83 :
84 30 : bool OConnection::matchesExtension( const OUString& _rExt ) const
85 : {
86 30 : if ( isCaseSensitveExtension() )
87 30 : return ( getExtension() == _rExt );
88 :
89 0 : OUString sMyExtension( getExtension().toAsciiLowerCase() );
90 0 : OUString sExt( _rExt.toAsciiLowerCase() );
91 :
92 0 : return sMyExtension == sExt;
93 : }
94 :
95 :
96 29 : void OConnection::construct(const OUString& url,const Sequence< PropertyValue >& info)
97 : throw( css::sdbc::SQLException,
98 : css::uno::RuntimeException,
99 : css::uno::DeploymentException)
100 : {
101 29 : osl_atomic_increment( &m_refCount );
102 :
103 29 : OUString aExt;
104 29 : const PropertyValue *pIter = info.getConstArray();
105 29 : const PropertyValue *pEnd = pIter + info.getLength();
106 118 : for(;pIter != pEnd;++pIter)
107 : {
108 89 : if( pIter->Name == "Extension" )
109 0 : OSL_VERIFY( pIter->Value >>= aExt );
110 89 : else if( pIter->Name == "CharSet" )
111 : {
112 29 : OUString sIanaName;
113 29 : OSL_VERIFY( pIter->Value >>= sIanaName );
114 :
115 58 : ::dbtools::OCharsetMap aLookupIanaName;
116 58 : ::dbtools::OCharsetMap::const_iterator aLookup = aLookupIanaName.find(sIanaName, ::dbtools::OCharsetMap::IANA());
117 29 : if (aLookup != aLookupIanaName.end())
118 29 : m_nTextEncoding = (*aLookup).getEncoding();
119 : else
120 29 : m_nTextEncoding = RTL_TEXTENCODING_DONTKNOW;
121 : }
122 60 : else if( pIter->Name == "ShowDeleted" )
123 : {
124 29 : OSL_VERIFY( pIter->Value >>= m_bShowDeleted );
125 : }
126 31 : else if( pIter->Name == "EnableSQL92Check" )
127 : {
128 29 : pIter->Value >>= m_bCheckSQL92;
129 : }
130 : } // for(;pIter != pEnd;++pIter)
131 :
132 : {
133 29 : sal_Int32 nLen = url.indexOf(':');
134 29 : nLen = url.indexOf(':',nLen+1);
135 29 : OUString aDSN(url.copy(nLen+1));
136 :
137 58 : OUString aFileName = aDSN;
138 58 : INetURLObject aURL;
139 29 : aURL.SetSmartProtocol(INetProtocol::File);
140 : {
141 29 : SvtPathOptions aPathOptions;
142 29 : aFileName = aPathOptions.SubstituteVariable(aFileName);
143 : }
144 :
145 29 : aURL.SetSmartURL(aFileName);
146 :
147 58 : setURL(aURL.GetMainURL(INetURLObject::NO_DECODE));
148 : }
149 :
150 29 : if ( m_nTextEncoding == RTL_TEXTENCODING_DONTKNOW )
151 : {
152 : //m_nTextEncoding = osl_getTextEncodingFromLocale(NULL);
153 22 : m_nTextEncoding = osl_getThreadTextEncoding();
154 22 : m_bDefaultTextEncoding = true;
155 : }
156 :
157 29 : if ( !aExt.isEmpty() )
158 0 : m_aFilenameExtension = aExt;
159 :
160 : try
161 : {
162 29 : ::ucbhelper::Content aFile;
163 : try
164 : {
165 29 : aFile = ::ucbhelper::Content(getURL(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
166 : }
167 0 : catch(ContentCreationException& e)
168 : {
169 0 : throwUrlNotValid(getURL(),e.Message);
170 : }
171 :
172 : // set fields to fetch
173 58 : Sequence< OUString > aProps(1);
174 29 : OUString* pProps = aProps.getArray();
175 29 : pProps[ 0 ] = "Title";
176 :
177 : try
178 : {
179 29 : if (aFile.isFolder())
180 : {
181 29 : m_xDir = aFile.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
182 29 : m_xContent = aFile.get();
183 : }
184 0 : else if (aFile.isDocument())
185 : {
186 0 : Reference<XContent> xParent(Reference<XChild>(aFile.get(),UNO_QUERY)->getParent(),UNO_QUERY);
187 0 : Reference<XContentIdentifier> xIdent = xParent->getIdentifier();
188 0 : m_xContent = xParent;
189 :
190 0 : ::ucbhelper::Content aParent(xIdent->getContentIdentifier(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
191 0 : m_xDir = aParent.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
192 : }
193 : else
194 : {
195 : OSL_FAIL("OConnection::construct: ::ucbhelper::Content isn't a folde nor a document! How that?!");
196 0 : throw SQLException();
197 : }
198 : }
199 0 : catch(Exception& e) // a exception is thrown when no file exists
200 : {
201 0 : throwUrlNotValid(getURL(),e.Message);
202 : }
203 29 : if(!m_xDir.is() || !m_xContent.is())
204 0 : throwUrlNotValid(getURL(),OUString());
205 :
206 29 : if (m_aFilenameExtension.indexOf('*') >= 0 || m_aFilenameExtension.indexOf('?') >= 0)
207 29 : throw SQLException();
208 : }
209 0 : catch(const Exception&)
210 : {
211 0 : osl_atomic_decrement( &m_refCount );
212 0 : throw;
213 : }
214 :
215 29 : osl_atomic_decrement( &m_refCount );
216 29 : }
217 : // XServiceInfo
218 :
219 0 : IMPLEMENT_SERVICE_INFO(OConnection, "com.sun.star.sdbc.drivers.file.Connection", "com.sun.star.sdbc.Connection")
220 :
221 :
222 0 : Reference< XStatement > SAL_CALL OConnection::createStatement( ) throw(SQLException, RuntimeException, std::exception)
223 : {
224 0 : ::osl::MutexGuard aGuard( m_aMutex );
225 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
226 :
227 :
228 0 : Reference< XStatement > xReturn = new OStatement(this);
229 0 : m_aStatements.push_back(WeakReferenceHelper(xReturn));
230 0 : return xReturn;
231 : }
232 :
233 0 : Reference< XPreparedStatement > SAL_CALL OConnection::prepareStatement( const OUString& sql ) throw(SQLException, RuntimeException, std::exception)
234 : {
235 0 : ::osl::MutexGuard aGuard( m_aMutex );
236 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
237 :
238 :
239 0 : OPreparedStatement* pStmt = new OPreparedStatement(this);
240 0 : Reference< XPreparedStatement > xHoldAlive = pStmt;
241 0 : pStmt->construct(sql);
242 0 : m_aStatements.push_back(WeakReferenceHelper(*pStmt));
243 0 : return pStmt;
244 : }
245 :
246 0 : Reference< XPreparedStatement > SAL_CALL OConnection::prepareCall( const OUString& /*sql*/ ) throw(SQLException, RuntimeException, std::exception)
247 : {
248 0 : throwFeatureNotImplementedSQLException( "XConnection::prepareCall", *this );
249 0 : return NULL;
250 : }
251 :
252 0 : OUString SAL_CALL OConnection::nativeSQL( const OUString& sql ) throw(SQLException, RuntimeException, std::exception)
253 : {
254 0 : return sql;
255 : }
256 :
257 0 : void SAL_CALL OConnection::setAutoCommit( sal_Bool autoCommit ) throw(SQLException, RuntimeException, std::exception)
258 : {
259 0 : ::osl::MutexGuard aGuard( m_aMutex );
260 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
261 :
262 0 : m_bAutoCommit = autoCommit;
263 0 : }
264 :
265 0 : sal_Bool SAL_CALL OConnection::getAutoCommit( ) throw(SQLException, RuntimeException, std::exception)
266 : {
267 0 : ::osl::MutexGuard aGuard( m_aMutex );
268 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
269 :
270 0 : return m_bAutoCommit;
271 : }
272 :
273 0 : void SAL_CALL OConnection::commit( ) throw(SQLException, RuntimeException, std::exception)
274 : {
275 0 : }
276 :
277 0 : void SAL_CALL OConnection::rollback( ) throw(SQLException, RuntimeException, std::exception)
278 : {
279 0 : }
280 :
281 37 : sal_Bool SAL_CALL OConnection::isClosed( ) throw(SQLException, RuntimeException, std::exception)
282 : {
283 37 : ::osl::MutexGuard aGuard( m_aMutex );
284 :
285 37 : return OConnection_BASE::rBHelper.bDisposed;
286 : }
287 :
288 0 : Reference< XDatabaseMetaData > SAL_CALL OConnection::getMetaData( ) throw(SQLException, RuntimeException, std::exception)
289 : {
290 0 : ::osl::MutexGuard aGuard( m_aMutex );
291 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
292 :
293 :
294 0 : Reference< XDatabaseMetaData > xMetaData = m_xMetaData;
295 0 : if(!xMetaData.is())
296 : {
297 0 : xMetaData = new ODatabaseMetaData(this);
298 0 : m_xMetaData = xMetaData;
299 : }
300 :
301 0 : return xMetaData;
302 : }
303 :
304 0 : void SAL_CALL OConnection::setReadOnly( sal_Bool readOnly ) throw(SQLException, RuntimeException, std::exception)
305 : {
306 0 : ::osl::MutexGuard aGuard( m_aMutex );
307 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
308 :
309 :
310 0 : m_bReadOnly = readOnly;
311 0 : }
312 :
313 0 : sal_Bool SAL_CALL OConnection::isReadOnly( ) throw(SQLException, RuntimeException, std::exception)
314 : {
315 0 : ::osl::MutexGuard aGuard( m_aMutex );
316 0 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
317 :
318 :
319 0 : return m_bReadOnly;
320 : }
321 :
322 0 : void SAL_CALL OConnection::setCatalog( const OUString& /*catalog*/ ) throw(SQLException, RuntimeException, std::exception)
323 : {
324 0 : throwFeatureNotImplementedSQLException( "XConnection::setCatalog", *this );
325 0 : }
326 :
327 0 : OUString SAL_CALL OConnection::getCatalog( ) throw(SQLException, RuntimeException, std::exception)
328 : {
329 0 : return OUString();
330 : }
331 :
332 0 : void SAL_CALL OConnection::setTransactionIsolation( sal_Int32 /*level*/ ) throw(SQLException, RuntimeException, std::exception)
333 : {
334 0 : throwFeatureNotImplementedSQLException( "XConnection::setTransactionIsolation", *this );
335 0 : }
336 :
337 0 : sal_Int32 SAL_CALL OConnection::getTransactionIsolation( ) throw(SQLException, RuntimeException, std::exception)
338 : {
339 0 : return 0;
340 : }
341 :
342 0 : Reference< XNameAccess > SAL_CALL OConnection::getTypeMap( ) throw(SQLException, RuntimeException, std::exception)
343 : {
344 0 : return NULL;
345 : }
346 :
347 0 : void SAL_CALL OConnection::setTypeMap( const Reference< XNameAccess >& /*typeMap*/ ) throw(SQLException, RuntimeException, std::exception)
348 : {
349 0 : }
350 :
351 : // XCloseable
352 37 : void SAL_CALL OConnection::close( ) throw(SQLException, RuntimeException, std::exception)
353 : {
354 : {
355 37 : ::osl::MutexGuard aGuard( m_aMutex );
356 37 : checkDisposed(OConnection_BASE::rBHelper.bDisposed);
357 :
358 : }
359 37 : dispose();
360 37 : }
361 :
362 : // XWarningsSupplier
363 2 : Any SAL_CALL OConnection::getWarnings( ) throw(SQLException, RuntimeException, std::exception)
364 : {
365 2 : return Any();
366 : }
367 :
368 2 : void SAL_CALL OConnection::clearWarnings( ) throw(SQLException, RuntimeException, std::exception)
369 : {
370 2 : }
371 :
372 37 : void OConnection::disposing()
373 : {
374 37 : ::osl::MutexGuard aGuard(m_aMutex);
375 37 : OConnection_BASE::disposing();
376 :
377 37 : m_bClosed = true;
378 37 : m_xDir.clear();
379 37 : m_xContent.clear();
380 37 : m_xCatalog = WeakReference< XTablesSupplier>();
381 :
382 37 : dispose_ChildImpl();
383 37 : }
384 :
385 0 : Reference< XTablesSupplier > OConnection::createCatalog()
386 : {
387 0 : ::osl::MutexGuard aGuard( m_aMutex );
388 0 : Reference< XTablesSupplier > xTab = m_xCatalog;
389 0 : if(!xTab.is())
390 : {
391 0 : xTab = new OFileCatalog(this);
392 0 : m_xCatalog = xTab;
393 : }
394 0 : return xTab;
395 : }
396 :
397 100 : Reference< XDynamicResultSet > OConnection::getDir() const
398 : {
399 100 : Reference<XDynamicResultSet> xContent;
400 200 : Sequence< OUString > aProps(1);
401 100 : OUString* pProps = aProps.getArray();
402 100 : pProps[ 0 ] = "Title";
403 : try
404 : {
405 100 : Reference<XContentIdentifier> xIdent = getContent()->getIdentifier();
406 200 : ::ucbhelper::Content aParent(xIdent->getContentIdentifier(), Reference< XCommandEnvironment >(), comphelper::getProcessComponentContext());
407 200 : xContent = aParent.createDynamicCursor(aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
408 : }
409 0 : catch(Exception&)
410 : {
411 : }
412 200 : return xContent;
413 : }
414 :
415 38 : sal_Int64 SAL_CALL OConnection::getSomething( const Sequence< sal_Int8 >& rId ) throw (RuntimeException, std::exception)
416 : {
417 152 : return (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
418 : ? reinterpret_cast< sal_Int64 >( this )
419 152 : : (sal_Int64)0;
420 : }
421 :
422 76 : Sequence< sal_Int8 > OConnection::getUnoTunnelImplementationId()
423 : {
424 : static ::cppu::OImplementationId * pId = 0;
425 76 : if (! pId)
426 : {
427 11 : ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
428 11 : if (! pId)
429 : {
430 11 : static ::cppu::OImplementationId aId;
431 11 : pId = &aId;
432 11 : }
433 : }
434 76 : return pId->getImplementationId();
435 : }
436 :
437 0 : void OConnection::throwUrlNotValid(const OUString & _rsUrl,const OUString & _rsMessage)
438 : {
439 0 : SQLException aError;
440 0 : aError.Message = getResources().getResourceStringWithSubstitution(
441 : STR_NO_VALID_FILE_URL,
442 : "$URL$", _rsUrl
443 0 : );
444 :
445 0 : aError.SQLState = "S1000";
446 0 : aError.ErrorCode = 0;
447 0 : aError.Context = static_cast< XConnection* >(this);
448 0 : if (!_rsMessage.isEmpty())
449 0 : aError.NextException <<= SQLException(_rsMessage, aError.Context, OUString(), 0, Any());
450 :
451 0 : throw aError;
452 : }
453 :
454 :
455 :
456 :
457 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|