1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 .
24 #include <connectivity/CommonTools.hxx>
25 #include <cppuhelper/compbase.hxx>
26 #include <cppuhelper/weakref.hxx>
28 #include <OTypeInfo.hxx>
29 #include <unotools/tempfile.hxx>
31 #include <com/sun/star/beans/PropertyValue.hpp>
32 #include <com/sun/star/document/DocumentEvent.hpp>
33 #include <com/sun/star/document/XDocumentEventListener.hpp>
34 #include <com/sun/star/embed/XStorage.hpp>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/lang/XUnoTunnel.hpp>
37 #include <com/sun/star/sdbc/XBlob.hpp>
38 #include <com/sun/star/sdbc/XClob.hpp>
39 #include <com/sun/star/sdbc/XConnection.hpp>
40 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
41 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
42 #include <com/sun/star/util/XModifiable.hpp>
44 namespace connectivity::firebird
47 typedef ::cppu::WeakComponentImplHelper
< css::document::XDocumentEventListener
,
48 css::lang::XServiceInfo
,
49 css::lang::XUnoTunnel
,
50 css::sdbc::XConnection
,
51 css::sdbc::XWarningsSupplier
54 class OStatementCommonBase
;
56 class ODatabaseMetaData
;
59 typedef std::vector
< ::connectivity::OTypeInfo
> TTypeInfoVector
;
60 typedef std::vector
< css::uno::WeakReferenceHelper
> OWeakRefArray
;
62 class Connection final
: public Connection_BASE
64 ::osl::Mutex m_aMutex
;
66 TTypeInfoVector m_aTypeInfo
; // vector containing an entry
67 // for each row returned by
68 // DatabaseMetaData.getTypeInfo.
70 /** The URL passed to us when opening, i.e. of the form sdbc:* */
71 OUString m_sConnectionURL
;
73 * The URL passed to firebird, i.e. either a local file (for a
74 * temporary .fdb extracted from a .odb or a normal local file) or
77 OUString m_sFirebirdURL
;
79 /* EMBEDDED MODE DATA */
80 /** Denotes that we have a database stored within a .odb file. */
84 * Handle for the parent DatabaseDocument. We need to notify this
85 * whenever any data is written to our temporary database so that
86 * the user is able to save this back to the .odb file.
88 * Note that this is ONLY set in embedded mode.
90 css::uno::Reference
< css::util::XModifiable
>
94 * Handle for the folder within the .odb where we store our .fbk
95 * (Only used if m_bIsEmbedded is true).
97 css::uno::Reference
< css::embed::XStorage
>
100 * The temporary folder where we extract the .fbk from a .odb,
101 * and also store the temporary .fdb
102 * It is only valid if m_bIsEmbedded is true.
104 * The extracted .fbk is written in firebird.fbk, the temporary
105 * .fdb is stored as firebird.fdb.
107 std::unique_ptr
< ::utl::TempFile
> m_pDatabaseFileDir
;
109 * Path for our extracted .fbk file.
111 * (The temporary .fdb is our m_sFirebirdURL.)
115 void loadDatabaseFile(const OUString
& pSrcLocation
, const OUString
& pTmpLocation
);
118 * Run the backup service, use nAction =
119 * isc_action_svc_backup to backup, nAction = isc_action_svc_restore
122 void runBackupService(const short nAction
);
124 isc_svc_handle
attachServiceManager();
126 void detachServiceManager(isc_svc_handle pServiceHandle
);
128 /** We are using an external (local) file */
131 /* CONNECTION PROPERTIES */
132 bool m_bIsAutoCommit
;
134 sal_Int32 m_aTransactionIsolation
;
136 isc_db_handle m_aDBHandle
;
137 isc_tr_handle m_aTransactionHandle
;
139 css::uno::WeakReference
< css::sdbcx::XTablesSupplier
>
141 css::uno::WeakReference
< css::sdbc::XDatabaseMetaData
>
143 /** Statements owned by this connection. */
144 OWeakRefArray m_aStatements
;
146 /// @throws css::sdbc::SQLException
147 void buildTypeInfo();
150 * Creates a new transaction with the desired parameters, if
151 * necessary discarding an existing transaction. This has to be done
152 * anytime we change the transaction isolation, or autocommitting.
154 * @throws css::sdbc::SQLException
156 void setupTransaction();
157 void disposeStatements();
160 explicit Connection();
161 virtual ~Connection() override
;
163 /// @throws css::sdbc::SQLException
164 /// @throws css::uno::RuntimeException
165 void construct( const OUString
& url
,
166 const css::uno::Sequence
< css::beans::PropertyValue
>& info
);
168 const OUString
& getConnectionURL() const {return m_sConnectionURL
;}
169 bool isEmbedded() const {return m_bIsEmbedded
;}
170 isc_db_handle
& getDBHandle() {return m_aDBHandle
;}
171 /// @throws css::sdbc::SQLException
172 isc_tr_handle
& getTransaction();
175 * Must be called anytime the underlying database is likely to have
178 * This is used to notify the database document of any changes, so
179 * that the user is informed of any pending changes needing to be
182 void notifyDatabaseModified();
185 * Create a new Blob tied to this connection. Blobs are tied to a
186 * transaction and not to a statement, hence the connection should
187 * deal with their management.
189 * @throws css::sdbc::SQLException
190 * @throws css::uno::RuntimeException
192 css::uno::Reference
< css::sdbc::XBlob
>
193 createBlob(ISC_QUAD
const * pBlobID
);
194 /// @throws css::sdbc::SQLException
195 /// @throws css::uno::RuntimeException
196 css::uno::Reference
< css::sdbc::XClob
>
197 createClob(ISC_QUAD
const * pBlobID
);
200 * Create and/or connect to the sdbcx Catalog. This is completely
201 * unrelated to the SQL "Catalog".
203 css::uno::Reference
< css::sdbcx::XTablesSupplier
>
207 virtual void SAL_CALL
disposing() override
;
210 DECLARE_SERVICE_INFO();
212 virtual sal_Int64 SAL_CALL
getSomething(const css::uno::Sequence
<sal_Int8
>& rId
) override
;
213 static css::uno::Sequence
<sal_Int8
> getUnoTunnelId();
215 virtual css::uno::Reference
< css::sdbc::XStatement
> SAL_CALL
createStatement( ) override
;
216 virtual css::uno::Reference
< css::sdbc::XPreparedStatement
> SAL_CALL
prepareStatement( const OUString
& sql
) override
;
217 virtual css::uno::Reference
< css::sdbc::XPreparedStatement
> SAL_CALL
prepareCall( const OUString
& sql
) override
;
218 virtual OUString SAL_CALL
nativeSQL( const OUString
& sql
) override
;
219 virtual void SAL_CALL
setAutoCommit( sal_Bool autoCommit
) override
;
220 virtual sal_Bool SAL_CALL
getAutoCommit( ) override
;
221 virtual void SAL_CALL
commit( ) override
;
222 virtual void SAL_CALL
rollback( ) override
;
223 virtual sal_Bool SAL_CALL
isClosed( ) override
;
224 virtual css::uno::Reference
< css::sdbc::XDatabaseMetaData
> SAL_CALL
getMetaData( ) override
;
225 virtual void SAL_CALL
setReadOnly( sal_Bool readOnly
) override
;
226 virtual sal_Bool SAL_CALL
isReadOnly( ) override
;
227 virtual void SAL_CALL
setCatalog( const OUString
& catalog
) override
;
228 virtual OUString SAL_CALL
getCatalog( ) override
;
229 virtual void SAL_CALL
setTransactionIsolation( sal_Int32 level
) override
;
230 virtual sal_Int32 SAL_CALL
getTransactionIsolation( ) override
;
231 virtual css::uno::Reference
< css::container::XNameAccess
> SAL_CALL
getTypeMap( ) override
;
232 virtual void SAL_CALL
setTypeMap( const css::uno::Reference
< css::container::XNameAccess
>& typeMap
) override
;
234 virtual void SAL_CALL
close( ) override
;
236 virtual css::uno::Any SAL_CALL
getWarnings( ) override
;
237 virtual void SAL_CALL
clearWarnings( ) override
;
238 // XDocumentEventListener
239 virtual void SAL_CALL
documentEventOccured( const css::document::DocumentEvent
& Event
) override
;
240 // css.lang.XEventListener
241 virtual void SAL_CALL
disposing( const css::lang::EventObject
& Source
) override
;
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */