1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #include "dbastrings.hrc"
28 #include "module_dba.hxx"
29 
30 /** === begin UNO includes === **/
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/beans/PropertyAttribute.hpp>
33 #include <com/sun/star/sdb/CommandType.hpp>
34 #include <com/sun/star/sdbc/XConnection.hpp>
35 #include <com/sun/star/sdbc/XResultSet.hpp>
36 #include <com/sun/star/sdb/XDataAccessDescriptorFactory.hpp>
37 /** === end UNO includes === **/
38 
39 #include <comphelper/broadcasthelper.hxx>
40 #include <comphelper/componentcontext.hxx>
41 #include <comphelper/proparrhlp.hxx>
42 #include <comphelper/propertycontainer.hxx>
43 #include <comphelper/uno3.hxx>
44 #include <cppuhelper/implbase1.hxx>
45 #include <cppuhelper/implbase2.hxx>
46 
47 //........................................................................
48 namespace dbaccess
49 {
50 //........................................................................
51 
52     /** === begin UNO using === **/
53     using ::com::sun::star::uno::Reference;
54     using ::com::sun::star::uno::XInterface;
55     using ::com::sun::star::uno::UNO_QUERY;
56     using ::com::sun::star::uno::UNO_QUERY_THROW;
57     using ::com::sun::star::uno::UNO_SET_THROW;
58     using ::com::sun::star::uno::Exception;
59     using ::com::sun::star::uno::RuntimeException;
60     using ::com::sun::star::uno::Any;
61     using ::com::sun::star::uno::makeAny;
62     using ::com::sun::star::uno::Sequence;
63     using ::com::sun::star::lang::XServiceInfo;
64     using ::com::sun::star::lang::XMultiServiceFactory;
65     using ::com::sun::star::beans::XPropertySetInfo;
66     using ::com::sun::star::beans::Property;
67     using ::com::sun::star::sdbc::XConnection;
68     using ::com::sun::star::sdbc::XResultSet;
69     using ::com::sun::star::sdb::XDataAccessDescriptorFactory;
70     using ::com::sun::star::beans::XPropertySet;
71     using ::com::sun::star::uno::XComponentContext;
72     using ::com::sun::star::beans::PropertyValue;
73     /** === end UNO using === **/
74 
75     namespace PropertyAttribute = ::com::sun::star::beans::PropertyAttribute;
76     namespace CommandType = ::com::sun::star::sdb::CommandType;
77 
78     //====================================================================
79     //= DataAccessDescriptor
80     //====================================================================
81     typedef ::comphelper::OMutexAndBroadcastHelper  DataAccessDescriptor_MutexBase;
82 
83     typedef ::cppu::WeakImplHelper1             <   XServiceInfo
84                                                 >   DataAccessDescriptor_TypeBase;
85 
86     typedef ::comphelper::OPropertyContainer        DataAccessDescriptor_PropertyBase;
87 
88     class DataAccessDescriptor  :public DataAccessDescriptor_MutexBase
89                                 ,public DataAccessDescriptor_TypeBase
90                                 ,public DataAccessDescriptor_PropertyBase
91 		                        ,public ::comphelper::OPropertyArrayUsageHelper< DataAccessDescriptor >
92     {
93     public:
94         DataAccessDescriptor( const ::comphelper::ComponentContext& _rContext );
95 
96         // UNO
97         DECLARE_XINTERFACE()
98         DECLARE_XTYPEPROVIDER()
99 
100         // XServiceInfo
101         virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (RuntimeException);
102         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException);
103         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException);
104 
105     protected:
106         ~DataAccessDescriptor();
107 
108     protected:
109 	    // XPropertySet
110 	    virtual Reference< XPropertySetInfo > SAL_CALL getPropertySetInfo() throw(RuntimeException);
111 	    virtual ::cppu::IPropertyArrayHelper& SAL_CALL getInfoHelper();
112 
113 	    // OPropertyArrayUsageHelper
114 	    virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
115 
116     private:
117         ::comphelper::ComponentContext  m_aContext;
118 
119         // </properties>
120         ::rtl::OUString             m_sDataSourceName;
121         ::rtl::OUString             m_sDatabaseLocation;
122         ::rtl::OUString             m_sConnectionResource;
123         Sequence< PropertyValue >   m_aConnectionInfo;
124         Reference< XConnection >    m_xActiveConnection;
125         ::rtl::OUString             m_sCommand;
126         sal_Int32                   m_nCommandType;
127         ::rtl::OUString             m_sFilter;
128         ::rtl::OUString             m_sOrder;
129         ::rtl::OUString             m_sHavingClause;
130         ::rtl::OUString             m_sGroupBy;
131         sal_Bool                    m_bEscapeProcessing;
132         Reference< XResultSet >     m_xResultSet;
133         Sequence< Any >             m_aSelection;
134         sal_Bool                    m_bBookmarkSelection;
135         ::rtl::OUString             m_sColumnName;
136         Reference< XPropertySet >   m_xColumn;
137         // </properties>
138     };
139 
140 #define REGISTER_PROPERTY( propname, member ) \
141     registerProperty( PROPERTY_##propname, PROPERTY_ID_##propname, PropertyAttribute::BOUND, &member, ::getCppuType( &member ) )
142 
143     //--------------------------------------------------------------------
DataAccessDescriptor(const::comphelper::ComponentContext & _rContext)144     DataAccessDescriptor::DataAccessDescriptor( const ::comphelper::ComponentContext& _rContext )
145         :DataAccessDescriptor_MutexBase()
146         ,DataAccessDescriptor_TypeBase()
147         ,DataAccessDescriptor_PropertyBase( m_aBHelper )
148         ,m_aContext( _rContext )
149         ,m_sDataSourceName()
150         ,m_sDatabaseLocation()
151         ,m_sConnectionResource()
152         ,m_aConnectionInfo()
153         ,m_xActiveConnection()
154         ,m_sCommand()
155         ,m_nCommandType( CommandType::COMMAND )
156         ,m_sFilter()
157         ,m_sOrder()
158         ,m_sHavingClause()
159         ,m_sGroupBy()
160         ,m_bEscapeProcessing( sal_True )
161         ,m_xResultSet()
162         ,m_aSelection()
163         ,m_bBookmarkSelection( sal_True )
164         ,m_sColumnName()
165         ,m_xColumn()
166     {
167         REGISTER_PROPERTY( DATASOURCENAME,      m_sDataSourceName );
168         REGISTER_PROPERTY( DATABASE_LOCATION,   m_sDatabaseLocation );
169         REGISTER_PROPERTY( CONNECTION_RESOURCE, m_sConnectionResource );
170         REGISTER_PROPERTY( CONNECTION_INFO,     m_aConnectionInfo );
171         REGISTER_PROPERTY( ACTIVE_CONNECTION,   m_xActiveConnection );
172         REGISTER_PROPERTY( COMMAND,             m_sCommand );
173         REGISTER_PROPERTY( COMMAND_TYPE,        m_nCommandType );
174         REGISTER_PROPERTY( FILTER,              m_sFilter );
175         REGISTER_PROPERTY( ORDER,               m_sOrder );
176         REGISTER_PROPERTY( HAVING_CLAUSE,       m_sHavingClause );
177         REGISTER_PROPERTY( GROUP_BY,            m_sGroupBy );
178         REGISTER_PROPERTY( ESCAPE_PROCESSING,   m_bEscapeProcessing );
179         REGISTER_PROPERTY( RESULT_SET,          m_xResultSet );
180         REGISTER_PROPERTY( SELECTION,           m_aSelection );
181         REGISTER_PROPERTY( BOOKMARK_SELECTION,  m_bBookmarkSelection );
182         REGISTER_PROPERTY( COLUMN_NAME,         m_sColumnName );
183         REGISTER_PROPERTY( COLUMN,              m_xColumn );
184     }
185 
186     //--------------------------------------------------------------------
~DataAccessDescriptor()187     DataAccessDescriptor::~DataAccessDescriptor()
188     {
189     }
190 
191     //--------------------------------------------------------------------
192     IMPLEMENT_FORWARD_XINTERFACE2( DataAccessDescriptor, DataAccessDescriptor_TypeBase, DataAccessDescriptor_PropertyBase );
193 
194     //--------------------------------------------------------------------
195     IMPLEMENT_FORWARD_XTYPEPROVIDER2( DataAccessDescriptor, DataAccessDescriptor_TypeBase, DataAccessDescriptor_PropertyBase );
196 
197     //--------------------------------------------------------------------
getImplementationName()198     ::rtl::OUString SAL_CALL DataAccessDescriptor::getImplementationName() throw (RuntimeException)
199     {
200         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.dba.DataAccessDescriptor" ) );
201     }
202 
203     //--------------------------------------------------------------------
supportsService(const::rtl::OUString & rServiceName)204     ::sal_Bool SAL_CALL DataAccessDescriptor::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
205     {
206         Sequence< ::rtl::OUString > aServices( getSupportedServiceNames() );
207         const ::rtl::OUString* pStart = aServices.getConstArray();
208         const ::rtl::OUString* pEnd = aServices.getConstArray() + aServices.getLength();
209         return ::std::find( pStart, pEnd, rServiceName ) != pEnd;
210     }
211 
212     //--------------------------------------------------------------------
getSupportedServiceNames()213     Sequence< ::rtl::OUString > SAL_CALL DataAccessDescriptor::getSupportedServiceNames(  ) throw (RuntimeException)
214     {
215         Sequence< ::rtl::OUString > aServices(1);
216         aServices[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.DataAccessDescriptor" ) );
217         return aServices;
218     }
219 
220     //-------------------------------------------------------------------------
getPropertySetInfo()221     Reference< XPropertySetInfo > SAL_CALL DataAccessDescriptor::getPropertySetInfo() throw(RuntimeException)
222     {
223 	    Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
224 	    return xInfo;
225     }
226 
227     //-------------------------------------------------------------------------
getInfoHelper()228     ::cppu::IPropertyArrayHelper& DataAccessDescriptor::getInfoHelper()
229     {
230 	    return *getArrayHelper();
231     }
232 
233     //------------------------------------------------------------------------------
createArrayHelper() const234     ::cppu::IPropertyArrayHelper* DataAccessDescriptor::createArrayHelper( ) const
235     {
236 	    Sequence< Property > aProps;
237 	    describeProperties( aProps );
238 	    return new ::cppu::OPropertyArrayHelper( aProps );
239     }
240 
241     //====================================================================
242     //= DataAccessDescriptorFactory
243     //====================================================================
244     typedef ::cppu::WeakImplHelper2 <   XServiceInfo
245                                     ,   XDataAccessDescriptorFactory
246                                     >   DataAccessDescriptorFactory_Base;
247     class DataAccessDescriptorFactory : public DataAccessDescriptorFactory_Base
248     {
249     public:
250         // XServiceInfo
251         virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (RuntimeException);
252         virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException);
253         virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (RuntimeException);
254 
255         // XServiceInfo - static versions
256         static Sequence< ::rtl::OUString >  getSupportedServiceNames_static(void) throw( RuntimeException );
257         static ::rtl::OUString              getImplementationName_static(void) throw( RuntimeException );
258         static Reference< XInterface >      Create(const Reference< XComponentContext >& _rxContext);
259         static ::rtl::OUString              getSingletonName_static();
260 
261         // XDataAccessDescriptorFactory
262         virtual Reference< XPropertySet > SAL_CALL createDataAccessDescriptor(  ) throw (RuntimeException);
263 
264     protected:
265         DataAccessDescriptorFactory( const Reference< XComponentContext >& _rxContext );
266         ~DataAccessDescriptorFactory();
267 
268     private:
269         ::comphelper::ComponentContext  m_aContext;
270     };
271 
272     //--------------------------------------------------------------------
DataAccessDescriptorFactory(const Reference<XComponentContext> & _rxContext)273     DataAccessDescriptorFactory::DataAccessDescriptorFactory( const Reference< XComponentContext >& _rxContext )
274         :m_aContext( _rxContext )
275     {
276     }
277 
278     //--------------------------------------------------------------------
~DataAccessDescriptorFactory()279     DataAccessDescriptorFactory::~DataAccessDescriptorFactory()
280     {
281     }
282 
283     //--------------------------------------------------------------------
getSingletonName_static()284     ::rtl::OUString DataAccessDescriptorFactory::getSingletonName_static()
285     {
286         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.DataAccessDescriptorFactory" ) );
287     }
288 
289     //--------------------------------------------------------------------
getSupportedServiceNames_static()290     Sequence< ::rtl::OUString > DataAccessDescriptorFactory::getSupportedServiceNames_static() throw( RuntimeException )
291     {
292         Sequence< ::rtl::OUString > aServices(1);
293         aServices[0] = getSingletonName_static();
294         return aServices;
295     }
296 
297     //--------------------------------------------------------------------
getImplementationName_static()298     ::rtl::OUString DataAccessDescriptorFactory::getImplementationName_static() throw( RuntimeException )
299     {
300         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.dba.DataAccessDescriptorFactory" ) );
301     }
302 
303     //--------------------------------------------------------------------
Create(const Reference<XComponentContext> & _rxContext)304     Reference< XInterface > DataAccessDescriptorFactory::Create( const Reference< XComponentContext >& _rxContext )
305     {
306         return *( new DataAccessDescriptorFactory( _rxContext ) );
307     }
308 
309     //--------------------------------------------------------------------
getImplementationName()310     ::rtl::OUString SAL_CALL DataAccessDescriptorFactory::getImplementationName() throw (RuntimeException)
311     {
312         return getImplementationName_static();
313     }
314 
315     //--------------------------------------------------------------------
supportsService(const::rtl::OUString & rServiceName)316     ::sal_Bool SAL_CALL DataAccessDescriptorFactory::supportsService( const ::rtl::OUString& rServiceName ) throw (RuntimeException)
317     {
318         Sequence< ::rtl::OUString > aServices( getSupportedServiceNames_static() );
319         const ::rtl::OUString* pStart = aServices.getConstArray();
320         const ::rtl::OUString* pEnd = aServices.getConstArray() + aServices.getLength();
321         return ::std::find( pStart, pEnd, rServiceName ) != pEnd;
322     }
323 
324     //--------------------------------------------------------------------
getSupportedServiceNames()325     Sequence< ::rtl::OUString > SAL_CALL DataAccessDescriptorFactory::getSupportedServiceNames(  ) throw (RuntimeException)
326     {
327         return getSupportedServiceNames_static();
328     }
329 
330     //--------------------------------------------------------------------
createDataAccessDescriptor()331     Reference< XPropertySet > SAL_CALL DataAccessDescriptorFactory::createDataAccessDescriptor(  ) throw (RuntimeException)
332     {
333         return new DataAccessDescriptor( m_aContext );
334     }
335 
336 //........................................................................
337 } // namespace dbaccess
338 //........................................................................
339 
340 //--------------------------------------------------------------------------
createRegistryInfo_DataAccessDescriptorFactory()341 extern "C" void SAL_CALL createRegistryInfo_DataAccessDescriptorFactory()
342 {
343     static ::dba::OSingletonRegistration< ::dbaccess::DataAccessDescriptorFactory > aAutoRegistration;
344 }
345