1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ 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 #pragma once 21 22 #include <ibase.h> 23 24 #include <connectivity/CommonTools.hxx> 25 #include <cppuhelper/compbase.hxx> 26 #include <cppuhelper/weakref.hxx> 27 #include <memory> 28 #include <OTypeInfo.hxx> 29 #include <unotools/tempfile.hxx> 30 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> 43 44 namespace connectivity::firebird 45 { 46 47 typedef ::cppu::WeakComponentImplHelper< css::document::XDocumentEventListener, 48 css::lang::XServiceInfo, 49 css::lang::XUnoTunnel, 50 css::sdbc::XConnection, 51 css::sdbc::XWarningsSupplier 52 > Connection_BASE; 53 54 class OStatementCommonBase; 55 class FirebirdDriver; 56 class ODatabaseMetaData; 57 58 59 typedef std::vector< ::connectivity::OTypeInfo> TTypeInfoVector; 60 typedef std::vector< css::uno::WeakReferenceHelper > OWeakRefArray; 61 62 class Connection final : public Connection_BASE 63 { 64 ::osl::Mutex m_aMutex; 65 66 TTypeInfoVector m_aTypeInfo; // vector containing an entry 67 // for each row returned by 68 // DatabaseMetaData.getTypeInfo. 69 70 /** The URL passed to us when opening, i.e. of the form sdbc:* */ 71 OUString m_sConnectionURL; 72 /** 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 75 * a remote url. 76 */ 77 OUString m_sFirebirdURL; 78 79 /* EMBEDDED MODE DATA */ 80 /** Denotes that we have a database stored within a .odb file. */ 81 bool m_bIsEmbedded; 82 83 /** 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. 87 * 88 * Note that this is ONLY set in embedded mode. 89 */ 90 css::uno::Reference< css::util::XModifiable > 91 m_xParentDocument; 92 93 /** 94 * Handle for the folder within the .odb where we store our .fbk 95 * (Only used if m_bIsEmbedded is true). 96 */ 97 css::uno::Reference< css::embed::XStorage > 98 m_xEmbeddedStorage; 99 /** 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. 103 * 104 * The extracted .fbk is written in firebird.fbk, the temporary 105 * .fdb is stored as firebird.fdb. 106 */ 107 std::unique_ptr< ::utl::TempFile > m_pDatabaseFileDir; 108 /** 109 * Path for our extracted .fbk file. 110 * 111 * (The temporary .fdb is our m_sFirebirdURL.) 112 */ 113 OUString m_sFBKPath; 114 115 void loadDatabaseFile(const OUString& pSrcLocation, const OUString& pTmpLocation); 116 117 /** 118 * Run the backup service, use nAction = 119 * isc_action_svc_backup to backup, nAction = isc_action_svc_restore 120 * to restore. 121 */ 122 void runBackupService(const short nAction); 123 124 isc_svc_handle attachServiceManager(); 125 126 void detachServiceManager(isc_svc_handle pServiceHandle); 127 128 /** We are using an external (local) file */ 129 bool m_bIsFile; 130 131 /* CONNECTION PROPERTIES */ 132 bool m_bIsAutoCommit; 133 bool m_bIsReadOnly; 134 sal_Int32 m_aTransactionIsolation; 135 136 isc_db_handle m_aDBHandle; 137 isc_tr_handle m_aTransactionHandle; 138 139 css::uno::WeakReference< css::sdbcx::XTablesSupplier> 140 m_xCatalog; 141 css::uno::WeakReference< css::sdbc::XDatabaseMetaData > 142 m_xMetaData; 143 /** Statements owned by this connection. */ 144 OWeakRefArray m_aStatements; 145 146 /// @throws css::sdbc::SQLException 147 void buildTypeInfo(); 148 149 /** 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. 153 * 154 * @throws css::sdbc::SQLException 155 */ 156 void setupTransaction(); 157 void disposeStatements(); 158 159 public: 160 explicit Connection(); 161 virtual ~Connection() override; 162 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); 167 getConnectionURL() const168 const OUString& getConnectionURL() const {return m_sConnectionURL;} isEmbedded() const169 bool isEmbedded() const {return m_bIsEmbedded;} getDBHandle()170 isc_db_handle& getDBHandle() {return m_aDBHandle;} 171 /// @throws css::sdbc::SQLException 172 isc_tr_handle& getTransaction(); 173 174 /** 175 * Must be called anytime the underlying database is likely to have 176 * changed. 177 * 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 180 * saved. 181 */ 182 void notifyDatabaseModified(); 183 184 /** 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. 188 * 189 * @throws css::sdbc::SQLException 190 * @throws css::uno::RuntimeException 191 */ 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); 198 199 /** 200 * Create and/or connect to the sdbcx Catalog. This is completely 201 * unrelated to the SQL "Catalog". 202 */ 203 css::uno::Reference< css::sdbcx::XTablesSupplier > 204 createCatalog(); 205 206 // OComponentHelper 207 virtual void SAL_CALL disposing() override; 208 209 // XServiceInfo 210 DECLARE_SERVICE_INFO(); 211 // XUnoTunnel 212 virtual sal_Int64 SAL_CALL getSomething(const css::uno::Sequence<sal_Int8>& rId) override; 213 static css::uno::Sequence<sal_Int8> getUnoTunnelId(); 214 // XConnection 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; 233 // XCloseable 234 virtual void SAL_CALL close( ) override; 235 // XWarningsSupplier 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; 242 243 }; 244 245 } 246 247 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ 248