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 #ifndef INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_FIREBIRD_CONNECTION_HXX
21 #define INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_FIREBIRD_CONNECTION_HXX
22 
23 #include "Clob.hxx"
24 #include "Blob.hxx"
25 #include "SubComponent.hxx"
26 
27 #include <ibase.h>
28 
29 #include <connectivity/CommonTools.hxx>
30 #include <cppuhelper/compbase.hxx>
31 #include <cppuhelper/weakref.hxx>
32 #include <map>
33 #include <memory>
34 #include <OTypeInfo.hxx>
35 #include <unotools/tempfile.hxx>
36 
37 #include <com/sun/star/beans/PropertyValue.hpp>
38 #include <com/sun/star/document/DocumentEvent.hpp>
39 #include <com/sun/star/document/XDocumentEventListener.hpp>
40 #include <com/sun/star/embed/XStorage.hpp>
41 #include <com/sun/star/lang/DisposedException.hpp>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/lang/XUnoTunnel.hpp>
44 #include <com/sun/star/sdbc/SQLWarning.hpp>
45 #include <com/sun/star/sdbc/XConnection.hpp>
46 #include <com/sun/star/sdbc/XWarningsSupplier.hpp>
47 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
48 #include <com/sun/star/util/XModifiable.hpp>
49 
50 namespace connectivity
51 {
52     namespace firebird
53     {
54 
55         typedef ::cppu::WeakComponentImplHelper< css::document::XDocumentEventListener,
56                                                  css::lang::XServiceInfo,
57                                                  css::sdbc::XConnection,
58                                                  css::sdbc::XWarningsSupplier
59                                                > Connection_BASE;
60 
61         class OStatementCommonBase;
62         class FirebirdDriver;
63         class ODatabaseMetaData;
64 
65 
66         typedef std::vector< ::connectivity::OTypeInfo>   TTypeInfoVector;
67         typedef std::vector< css::uno::WeakReferenceHelper > OWeakRefArray;
68 
69         class Connection final : public Connection_BASE
70         {
71             ::osl::Mutex        m_aMutex;
72 
73             TTypeInfoVector     m_aTypeInfo;    //  vector containing an entry
74                                                                     //  for each row returned by
75                                                                     //  DatabaseMetaData.getTypeInfo.
76 
77             /** The URL passed to us when opening, i.e. of the form sdbc:* */
78             OUString     m_sConnectionURL;
79             /**
80              * The URL passed to firebird, i.e. either a local file (for a
81              * temporary .fdb extracted from a .odb or a normal local file) or
82              * a remote url.
83              */
84             OUString     m_sFirebirdURL;
85 
86             /* EMBEDDED MODE DATA */
87             /** Denotes that we have a database stored within a .odb file. */
88             bool            m_bIsEmbedded;
89 
90             /**
91              * Handle for the parent DatabaseDocument. We need to notify this
92              * whenever any data is written to our temporary database so that
93              * the user is able to save this back to the .odb file.
94              *
95              * Note that this is ONLY set in embedded mode.
96              */
97             css::uno::Reference< css::util::XModifiable >
98                 m_xParentDocument;
99 
100             /**
101              * Handle for the folder within the .odb where we store our .fbk
102              * (Only used if m_bIsEmbedded is true).
103              */
104             css::uno::Reference< css::embed::XStorage >
105                 m_xEmbeddedStorage;
106             /**
107              * The temporary folder where we extract the .fbk from a .odb,
108              * and also store the temporary .fdb
109              * It is only valid if m_bIsEmbedded is true.
110              *
111              * The extracted .fbk is written in firebird.fbk, the temporary
112              * .fdb is stored as firebird.fdb.
113              */
114             std::unique_ptr< ::utl::TempFile >  m_pDatabaseFileDir;
115             /**
116              * Path for our extracted .fbk file.
117              *
118              * (The temporary .fdb is our m_sFirebirdURL.)
119              */
120             OUString m_sFBKPath;
121 
122             void loadDatabaseFile(const OUString& pSrcLocation, const OUString& pTmpLocation);
123 
124             /**
125              * Run the backup service, use nAction =
126              * isc_action_svc_backup to backup, nAction = isc_action_svc_restore
127              * to restore.
128              */
129             void runBackupService(const short nAction);
130 
131             isc_svc_handle attachServiceManager();
132 
133             void detachServiceManager(isc_svc_handle pServiceHandle);
134 
135             /** We are using an external (local) file */
136             bool                m_bIsFile;
137 
138             /* CONNECTION PROPERTIES */
139             bool                m_bIsAutoCommit;
140             bool                m_bIsReadOnly;
141             sal_Int32           m_aTransactionIsolation;
142 
143             isc_db_handle       m_aDBHandle;
144             isc_tr_handle       m_aTransactionHandle;
145 
146             css::uno::WeakReference< css::sdbcx::XTablesSupplier>
147                                 m_xCatalog;
148             css::uno::WeakReference< css::sdbc::XDatabaseMetaData >
149                                 m_xMetaData;
150             /** Statements owned by this connection. */
151             OWeakRefArray       m_aStatements;
152 
153             /// @throws css::sdbc::SQLException
154             void buildTypeInfo();
155 
156             /**
157              * Creates a new transaction with the desired parameters, if
158              * necessary discarding an existing transaction. This has to be done
159              * anytime we change the transaction isolation, or autocommitting.
160              *
161              * @throws css::sdbc::SQLException
162              */
163             void setupTransaction();
164             void disposeStatements();
165 
166         public:
167             explicit Connection();
168             virtual ~Connection() override;
169 
170             /// @throws css::sdbc::SQLException
171             /// @throws css::uno::RuntimeException
172             void construct( const OUString& url,
173                                     const css::uno::Sequence< css::beans::PropertyValue >& info);
174 
getConnectionURL() const175             const OUString& getConnectionURL()  const   {return m_sConnectionURL;}
isEmbedded() const176             bool            isEmbedded()        const   {return m_bIsEmbedded;}
getDBHandle()177             isc_db_handle&  getDBHandle()               {return m_aDBHandle;}
178             /// @throws css::sdbc::SQLException
179             isc_tr_handle&  getTransaction();
180 
181             /**
182               * Must be called anytime the underlying database is likely to have
183               * changed.
184               *
185               * This is used to notify the database document of any changes, so
186               * that the user is informed of any pending changes needing to be
187               * saved.
188               */
189             void notifyDatabaseModified();
190 
191             /**
192              * Create a new Blob tied to this connection. Blobs are tied to a
193              * transaction and not to a statement, hence the connection should
194              * deal with their management.
195              *
196              * @throws css::sdbc::SQLException
197              * @throws css::uno::RuntimeException
198              */
199             css::uno::Reference< css::sdbc::XBlob>
200                 createBlob(ISC_QUAD const * pBlobID);
201             /// @throws css::sdbc::SQLException
202             /// @throws css::uno::RuntimeException
203             css::uno::Reference< css::sdbc::XClob>
204                 createClob(ISC_QUAD const * pBlobID);
205 
206             /**
207              * Create and/or connect to the sdbcx Catalog. This is completely
208              * unrelated to the SQL "Catalog".
209              */
210             css::uno::Reference< css::sdbcx::XTablesSupplier >
211                 createCatalog();
212 
213             // OComponentHelper
214             virtual void SAL_CALL disposing() override;
215 
216             // XServiceInfo
217             DECLARE_SERVICE_INFO();
218             // XConnection
219             virtual css::uno::Reference< css::sdbc::XStatement > SAL_CALL createStatement(  ) override;
220             virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareStatement( const OUString& sql ) override;
221             virtual css::uno::Reference< css::sdbc::XPreparedStatement > SAL_CALL prepareCall( const OUString& sql ) override;
222             virtual OUString SAL_CALL nativeSQL( const OUString& sql ) override;
223             virtual void SAL_CALL setAutoCommit( sal_Bool autoCommit ) override;
224             virtual sal_Bool SAL_CALL getAutoCommit(  ) override;
225             virtual void SAL_CALL commit(  ) override;
226             virtual void SAL_CALL rollback(  ) override;
227             virtual sal_Bool SAL_CALL isClosed(  ) override;
228             virtual css::uno::Reference< css::sdbc::XDatabaseMetaData > SAL_CALL getMetaData(  ) override;
229             virtual void SAL_CALL setReadOnly( sal_Bool readOnly ) override;
230             virtual sal_Bool SAL_CALL isReadOnly(  ) override;
231             virtual void SAL_CALL setCatalog( const OUString& catalog ) override;
232             virtual OUString SAL_CALL getCatalog(  ) override;
233             virtual void SAL_CALL setTransactionIsolation( sal_Int32 level ) override;
234             virtual sal_Int32 SAL_CALL getTransactionIsolation(  ) override;
235             virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getTypeMap(  ) override;
236             virtual void SAL_CALL setTypeMap( const css::uno::Reference< css::container::XNameAccess >& typeMap ) override;
237             // XCloseable
238             virtual void SAL_CALL close(  ) override;
239             // XWarningsSupplier
240             virtual css::uno::Any SAL_CALL getWarnings(  ) override;
241             virtual void SAL_CALL clearWarnings(  ) override;
242             // XDocumentEventListener
243             virtual void SAL_CALL documentEventOccured( const css::document::DocumentEvent& Event ) override;
244             // css.lang.XEventListener
245             virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
246 
247         };
248     }
249 }
250 #endif // INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_FIREBIRD_CONNECTION_HXX
251 
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
253