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_DBACCESS_SOURCE_CORE_DATAACCESS_DATASOURCE_HXX
21 #define INCLUDED_DBACCESS_SOURCE_CORE_DATAACCESS_DATASOURCE_HXX
22 
23 #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
24 #include <com/sun/star/lang/XServiceInfo.hpp>
25 #include <com/sun/star/sdbc/XDataSource.hpp>
26 #include <com/sun/star/container/XContainerListener.hpp>
27 #include <com/sun/star/sdb/XBookmarksSupplier.hpp>
28 #include <com/sun/star/sdb/XQueryDefinitionsSupplier.hpp>
29 #include <com/sun/star/sdbc/XIsolatedConnection.hpp>
30 #include <com/sun/star/util/XNumberFormatter.hpp>
31 #include <com/sun/star/document/XEventListener.hpp>
32 #include <com/sun/star/util/XFlushable.hpp>
33 #include <cppuhelper/propshlp.hxx>
34 #include <comphelper/proparrhlp.hxx>
35 #include <cppuhelper/weakref.hxx>
36 #include <cppuhelper/compbase.hxx>
37 #include <com/sun/star/embed/XTransactionListener.hpp>
38 #include <apitools.hxx>
39 #include <bookmarkcontainer.hxx>
40 #include <rtl/ref.hxx>
41 #include <connectivity/CommonTools.hxx>
42 #include <com/sun/star/beans/PropertyAttribute.hpp>
43 #include <com/sun/star/beans/PropertyValue.hpp>
44 #include <com/sun/star/sdb/XCompletedConnection.hpp>
45 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
46 #include <com/sun/star/embed/XStorage.hpp>
47 #include <ContentHelper.hxx>
48 #include <com/sun/star/document/XStorageBasedDocument.hpp>
49 #include <com/sun/star/embed/ElementModes.hpp>
50 #include <com/sun/star/util/XRefreshable.hpp>
51 #include <com/sun/star/sdb/XDocumentDataSource.hpp>
52 #include <ModelImpl.hxx>
53 
54 namespace dbaccess
55 {
56 
57 class OSharedConnectionManager;
58 
59 // ODatabaseSource
60 typedef ::cppu::WeakComponentImplHelper<   css::lang::XServiceInfo
61                                        ,   css::sdbc::XDataSource
62                                        ,   css::sdb::XBookmarksSupplier
63                                        ,   css::sdb::XQueryDefinitionsSupplier
64                                        ,   css::sdb::XCompletedConnection
65                                        ,   css::container::XContainerListener
66                                        ,   css::sdbc::XIsolatedConnection
67                                        ,   css::sdbcx::XTablesSupplier
68                                        ,   css::util::XFlushable
69                                        ,   css::util::XFlushListener
70                                        ,   css::sdb::XDocumentDataSource
71                                        ,   css::lang::XInitialization
72                                        >   ODatabaseSource_Base;
73 
74 class ODatabaseSource   :public ModelDependentComponent // must be first
75                         ,public ODatabaseSource_Base
76                         ,public ::cppu::OPropertySetHelper
77                         ,public ::comphelper::OPropertyArrayUsageHelper < ODatabaseSource >
78 {
79     friend class ODatabaseContext;
80     friend class OConnection;
81     friend class OSharedConnectionManager;
82 
83 private:
84     using ODatabaseSource_Base::rBHelper;
85     // note: this thing uses the ref-count of "this", see OBookmarkContainer::acquire!
86     OBookmarkContainer m_Bookmarks;
87     ::comphelper::OInterfaceContainerHelper2 m_aFlushListeners;
88 
89 private:
90     virtual ~ODatabaseSource() override;
91 
92 public:
93     explicit ODatabaseSource( const ::rtl::Reference< ODatabaseModelImpl >& _pImpl );
94 
DBContextAccessdbaccess::ODatabaseSource::DBContextAccess95     struct DBContextAccess { friend class ODatabaseContext; private: DBContextAccess() { } };
96 
97     /** sets a new name for the data source
98 
99         The name of a data source (our m_sName member) is the registration name, *if* the
100         data source actually *is* registered at the database context.
101 
102         Normally, this name is passed at time of creation of the ODatabaseModelImpl instance,
103         but if a newly created data source is registered, then it must be possible to propagate
104         the new registration name.
105     */
106     static void setName(
107             const css::uno::Reference< css::sdb::XDocumentDataSource >& _rxDocument,
108             const OUString& _rNewName,
109             DBContextAccess
110         );
111 
112     // XContainerListener
113     virtual void SAL_CALL elementInserted( const css::container::ContainerEvent& Event ) override;
114     virtual void SAL_CALL elementRemoved( const css::container::ContainerEvent& Event ) override;
115     virtual void SAL_CALL elementReplaced( const css::container::ContainerEvent& Event ) override;
116     // css::sdbcx::XTablesSupplier
117     virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getTables(  ) override;
118 
119 // css::lang::XTypeProvider
120     virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override;
121     virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override;
122 
123 // css::uno::XInterface
124     virtual css::uno::Any SAL_CALL queryInterface( const css::uno::Type & rType ) override;
125     virtual void SAL_CALL acquire() throw( ) override;
126     virtual void SAL_CALL release() throw( ) override;
127 
128 // css::lang::XServiceInfo
129     virtual OUString SAL_CALL getImplementationName(  ) override;
130     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
131     virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) override;
132 
133 // OComponentHelper
134     virtual void SAL_CALL disposing() override;
135 
136 // css::beans::XPropertySet
137     virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo(  ) override;
138     // XEventListener
139     virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;
140 
141 // comphelper::OPropertyArrayUsageHelper
142     virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const override;
143 
144 // cppu::OPropertySetHelper
145     virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper() override;
146 
147     virtual sal_Bool SAL_CALL convertFastPropertyValue(
148                             css::uno::Any & rConvertedValue,
149                             css::uno::Any & rOldValue,
150                             sal_Int32 nHandle,
151                             const css::uno::Any& rValue ) override;
152     virtual void SAL_CALL setFastPropertyValue_NoBroadcast(
153                                 sal_Int32 nHandle,
154                                 const css::uno::Any& rValue
155                                                  ) override;
156     virtual void SAL_CALL getFastPropertyValue( css::uno::Any& rValue, sal_Int32 nHandle ) const override;
157 
158 // css::sdb::XCompletedConnection
159     virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL connectWithCompletion( const css::uno::Reference< css::task::XInteractionHandler >& handler ) override;
160 
161 // css::sdbc::XDataSource
162     virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getConnection( const OUString& user, const OUString& password ) override;
163     virtual void SAL_CALL setLoginTimeout( sal_Int32 seconds ) override;
164     virtual sal_Int32 SAL_CALL getLoginTimeout(  ) override;
165 
166 //::css::sdb::XBookmarksSupplier
167     virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getBookmarks(  ) override;
168 
169 //::css::sdb::XQueryDefinitionsSupplier
170     virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getQueryDefinitions(  ) override;
171 
172 // css::sdbc::XIsolatedConnection
173     virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getIsolatedConnection( const OUString& user, const OUString& password ) override;
174     virtual css::uno::Reference< css::sdbc::XConnection > SAL_CALL getIsolatedConnectionWithCompletion( const css::uno::Reference< css::task::XInteractionHandler >& handler ) override;
175 
176 // XFlushable
177     virtual void SAL_CALL flush(  ) override;
178     virtual void SAL_CALL addFlushListener( const css::uno::Reference< css::util::XFlushListener >& l ) override;
179     virtual void SAL_CALL removeFlushListener( const css::uno::Reference< css::util::XFlushListener >& l ) override;
180 
181     // XFlushListener
182     virtual void SAL_CALL flushed( const css::lang::EventObject& rEvent ) override;
183 
184     // XDocumentDataSource
185     virtual css::uno::Reference< css::sdb::XOfficeDatabaseDocument > SAL_CALL getDatabaseDocument() override;
186 
187     // XInitialization
188     virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
189 
190 protected:
191     // ModelDependentComponent overridables
192     virtual css::uno::Reference< css::uno::XInterface > getThis() const override;
193 
194 private:
195 // helper
196     /** open a connection for the current settings. this is the simple connection we get from the driver
197         manager, so it can be used as a master for a "high level" sdb connection.
198     */
199     css::uno::Reference< css::sdbc::XConnection > buildLowLevelConnection(
200         const OUString& _rUid, const OUString& _rPwd
201         );
202 
203     css::uno::Reference< css::sdbc::XConnection > buildIsolatedConnection(
204         const OUString& user, const OUString& password
205         );
206 
207     /// @throws css::sdbc::SQLException
208     /// @throws css::uno::RuntimeException
209     css::uno::Reference< css::sdbc::XConnection > getConnection( const OUString& user, const OUString& password , bool _bIsolated);
210     /// @throws css::sdbc::SQLException
211     /// @throws css::uno::RuntimeException
212     css::uno::Reference< css::sdbc::XConnection > connectWithCompletion( const css::uno::Reference< css::task::XInteractionHandler >& handler , bool _bIsolated);
213 
214 protected:
215     using ::cppu::OPropertySetHelper::getFastPropertyValue;
216 };
217 
218 }   // namespace dbaccess
219 
220 #endif // _DBA_COREDATAACCESS_DATALINK_HXX_
221 
222 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
223