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 : #ifndef INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_FIREBIRD_CONNECTION_HXX
21 : #define INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_FIREBIRD_CONNECTION_HXX
22 :
23 : #include "Blob.hxx"
24 : #include "SubComponent.hxx"
25 :
26 : #include <ibase.h>
27 :
28 : #include <boost/scoped_ptr.hpp>
29 : #include <connectivity/CommonTools.hxx>
30 : #include <connectivity/OSubComponent.hxx>
31 : #include <cppuhelper/compbase4.hxx>
32 : #include <cppuhelper/weakref.hxx>
33 : #include <map>
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::WeakComponentImplHelper4< ::com::sun::star::document::XDocumentEventListener,
56 : ::com::sun::star::lang::XServiceInfo,
57 : ::com::sun::star::sdbc::XConnection,
58 : ::com::sun::star::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< ::com::sun::star::uno::WeakReferenceHelper > OWeakRefArray;
68 :
69 : class Connection : public Connection_BASE,
70 : public connectivity::OSubComponent<Connection, Connection_BASE>
71 : {
72 : friend class connectivity::OSubComponent<Connection, Connection_BASE>;
73 :
74 : /**
75 : * Location within the .odb that an embedded .fdb will be stored.
76 : * Only relevant for embedded dbs.
77 : */
78 : static const OUString our_sDBLocation;
79 : protected:
80 : ::osl::Mutex m_aMutex;
81 :
82 : TTypeInfoVector m_aTypeInfo; // vector containing an entry
83 : // for each row returned by
84 : // DatabaseMetaData.getTypeInfo.
85 : /** The parent driver that created this connection. */
86 : ::rtl::Reference<FirebirdDriver> m_xDriver;
87 :
88 : /** The URL passed to us when opening, i.e. of the form sdbc:* */
89 : ::rtl::OUString m_sConnectionURL;
90 : /**
91 : * The URL passed to firebird, i.e. either a local file (for a
92 : * temporary .fdb extracted from a .odb or a normal local file) or
93 : * a remote url.
94 : */
95 : ::rtl::OUString m_sFirebirdURL;
96 :
97 : /* EMBEDDED MODE DATA */
98 : /** Denotes that we have a .fdb stored within a .odb file. */
99 : bool m_bIsEmbedded;
100 :
101 : /**
102 : * Handle for the parent DatabaseDocument. We need to notify this
103 : * whenever any data is written to our temporary database so that
104 : * the user is able to save this back to the .odb file.
105 : *
106 : * Note that this is ONLY set in embedded mode.
107 : */
108 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XModifiable >
109 : m_xParentDocument;
110 :
111 : /**
112 : * Handle for the folder within the .odb where we store our .fdb
113 : * (Only used if m_bIsEmbedded is true).
114 : */
115 : ::com::sun::star::uno::Reference< ::com::sun::star::embed::XStorage >
116 : m_xEmbeddedStorage;
117 : /**
118 : * The temporary folder where we extract the .fdb from a .odb.
119 : * It is only valid if m_bIsEmbedded is true.
120 : */
121 : ::boost::scoped_ptr< ::utl::TempFile > m_pExtractedFDBFile;
122 :
123 :
124 : /** We are using an external (local) file */
125 : bool m_bIsFile;
126 :
127 : /* REMOTE CONNECTION DATA */
128 : ::rtl::OUString m_sUser;
129 :
130 : /* CONNECTION PROPERTIES */
131 : bool m_bIsAutoCommit;
132 : bool m_bIsReadOnly;
133 : sal_Int32 m_aTransactionIsolation;
134 :
135 : isc_db_handle m_aDBHandle;
136 : isc_tr_handle m_aTransactionHandle;
137 :
138 : ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbcx::XTablesSupplier>
139 : m_xCatalog;
140 : ::com::sun::star::uno::WeakReference< ::com::sun::star::sdbc::XDatabaseMetaData >
141 : m_xMetaData;
142 : /** Statements owned by this connection. */
143 : OWeakRefArray m_aStatements;
144 :
145 : /**
146 : * Firebird stores binary collations for indexes on Character based
147 : * columns, these can be binary-incompatible between different icu
148 : * version, hence we need to rebuild the indexes when switching icu
149 : * versions.
150 : */
151 : void rebuildIndexes()
152 : throw (css::sdbc::SQLException, css::uno::RuntimeException, std::exception);
153 : void buildTypeInfo()
154 : throw (css::sdbc::SQLException);
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 autocommiting.
160 : */
161 : void setupTransaction()
162 : throw(::com::sun::star::sdbc::SQLException);
163 : void disposeStatements();
164 :
165 : /** transform named parameters into unnamed parameters
166 : @param _sSQL
167 : The SQL statement to transform.
168 : @return
169 : The new statement with unnamed parameters
170 : */
171 : OUString transformPreparedStatement(const OUString& _sSQL);
172 :
173 : public:
174 : Connection(FirebirdDriver* _pDriver);
175 : virtual ~Connection();
176 :
177 : virtual void construct( const ::rtl::OUString& url,
178 : const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& info)
179 : throw(css::sdbc::SQLException,
180 : css::uno::RuntimeException,
181 : std::exception);
182 :
183 4 : ::rtl::OUString getConnectionURL() const {return m_sConnectionURL;}
184 0 : bool isEmbedded() const {return m_bIsEmbedded;}
185 : ::rtl::OUString getUserName() const {return m_sUser;}
186 18 : isc_db_handle& getDBHandle() {return m_aDBHandle;}
187 : isc_tr_handle& getTransaction()
188 : throw(::com::sun::star::sdbc::SQLException);
189 :
190 : /**
191 : * Must be called anytime the underlying database is likely to have
192 : * changed.
193 : *
194 : * This is used to notify the database document of any changes, so
195 : * that the user is informed of any pending changes needing to be
196 : * saved.
197 : */
198 : void notifyDatabaseModified();
199 :
200 : /**
201 : * Create a new Blob tied to this connection. Blobs are tied to a
202 : * transaction and not to a statement, hence the connection should
203 : * deal with their management.
204 : */
205 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XBlob>
206 : createBlob(ISC_QUAD* pBlobID)
207 : throw(::com::sun::star::sdbc::SQLException,
208 : ::com::sun::star::uno::RuntimeException);
209 :
210 : /**
211 : * Create and/or connect to the sdbcx Catalog. This is completely
212 : * unrelated to the SQL "Catalog".
213 : */
214 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbcx::XTablesSupplier >
215 : createCatalog();
216 :
217 : // OComponentHelper
218 : virtual void SAL_CALL disposing(void) SAL_OVERRIDE;
219 : // XInterface
220 : virtual void SAL_CALL release() throw() SAL_OVERRIDE;
221 :
222 : // XServiceInfo
223 : DECLARE_SERVICE_INFO();
224 : // XConnection
225 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XStatement > SAL_CALL createStatement( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
226 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement > SAL_CALL prepareStatement( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
227 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XPreparedStatement > SAL_CALL prepareCall( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
228 : virtual ::rtl::OUString SAL_CALL nativeSQL( const ::rtl::OUString& sql ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
229 : virtual void SAL_CALL setAutoCommit( sal_Bool autoCommit ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
230 : virtual sal_Bool SAL_CALL getAutoCommit( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
231 : virtual void SAL_CALL commit( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
232 : virtual void SAL_CALL rollback( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
233 : virtual sal_Bool SAL_CALL isClosed( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
234 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData > SAL_CALL getMetaData( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
235 : virtual void SAL_CALL setReadOnly( sal_Bool readOnly ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
236 : virtual sal_Bool SAL_CALL isReadOnly( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
237 : virtual void SAL_CALL setCatalog( const ::rtl::OUString& catalog ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
238 : virtual ::rtl::OUString SAL_CALL getCatalog( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
239 : virtual void SAL_CALL setTransactionIsolation( sal_Int32 level ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
240 : virtual sal_Int32 SAL_CALL getTransactionIsolation( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
241 : virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getTypeMap( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
242 : virtual void SAL_CALL setTypeMap( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& typeMap ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
243 : // XCloseable
244 : virtual void SAL_CALL close( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
245 : // XWarningsSupplier
246 : virtual ::com::sun::star::uno::Any SAL_CALL getWarnings( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
247 : virtual void SAL_CALL clearWarnings( ) throw(::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
248 : // XDocumentEventListener
249 : virtual void SAL_CALL documentEventOccured( const ::com::sun::star::document::DocumentEvent& Event ) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
250 : // css.lang.XEventListener
251 : virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw (::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
252 :
253 : };
254 : }
255 : }
256 : #endif // INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_FIREBIRD_CONNECTION_HXX
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|