LibreOffice Module connectivity (master) 1
statementcomposer.cxx
Go to the documentation of this file.
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
21
23
24#include <com/sun/star/sdb/CommandType.hpp>
25#include <com/sun/star/lang/NullPointerException.hpp>
26#include <com/sun/star/lang/XComponent.hpp>
27#include <com/sun/star/sdb/XQueriesSupplier.hpp>
28#include <com/sun/star/sdbc/SQLException.hpp>
29#include <com/sun/star/lang/XMultiServiceFactory.hpp>
30#include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
31#include <com/sun/star/sdbc/XConnection.hpp>
32
36
37
38namespace dbtools
39{
40
41
42 using ::com::sun::star::uno::Reference;
43 using ::com::sun::star::sdbc::XConnection;
44 using ::com::sun::star::sdb::XSingleSelectQueryComposer;
45 using ::com::sun::star::lang::NullPointerException;
46 using ::com::sun::star::uno::Exception;
47 using ::com::sun::star::lang::XComponent;
48 using ::com::sun::star::uno::UNO_QUERY_THROW;
49 using ::com::sun::star::sdb::XQueriesSupplier;
50 using ::com::sun::star::container::XNameAccess;
51 using ::com::sun::star::beans::XPropertySet;
52 using ::com::sun::star::lang::XMultiServiceFactory;
53 using ::com::sun::star::sdbc::SQLException;
54
55 namespace CommandType = ::com::sun::star::sdb::CommandType;
56
58 {
59 const Reference< XConnection > xConnection;
60 Reference< XSingleSelectQueryComposer > xComposer;
61 OUString sCommand;
62 OUString sFilter;
63 OUString sHavingClause;
64 OUString sOrder;
65 sal_Int32 nCommandType;
69
70 explicit StatementComposer_Data( const Reference< XConnection >& _rxConnection )
71 :xConnection( _rxConnection )
73 ,bEscapeProcessing( true )
74 ,bComposerDirty( true )
75 ,bDisposeComposer( true )
76 {
77 if ( !_rxConnection.is() )
78 throw NullPointerException();
79 }
80 };
81
82
83 namespace
84 {
85
86 void lcl_resetComposer( StatementComposer_Data& _rData )
87 {
88 if ( _rData.bDisposeComposer && _rData.xComposer.is() )
89 {
90 try
91 {
92 Reference< XComponent > xComposerComponent( _rData.xComposer, UNO_QUERY_THROW );
93 xComposerComponent->dispose();
94 }
95 catch( const Exception& )
96 {
97 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
98 }
99 }
100 _rData.xComposer.clear();
101 }
102
103
104 bool lcl_ensureUpToDateComposer_nothrow( StatementComposer_Data& _rData )
105 {
106 if ( !_rData.bComposerDirty )
107 return _rData.xComposer.is();
108 lcl_resetComposer( _rData );
109
110 try
111 {
112 OUString sStatement;
113 switch ( _rData.nCommandType )
114 {
115 case CommandType::COMMAND:
116 if ( _rData.bEscapeProcessing )
117 sStatement = _rData.sCommand;
118 // (in case of no escape processing we assume a not parseable statement)
119 break;
120
121 case CommandType::TABLE:
122 {
123 if ( _rData.sCommand.isEmpty() )
124 break;
125
126 sStatement = "SELECT * FROM ";
127
128 OUString sCatalog, sSchema, sTable;
129 qualifiedNameComponents( _rData.xConnection->getMetaData(), _rData.sCommand, sCatalog, sSchema, sTable, EComposeRule::InDataManipulation );
130
131 sStatement += composeTableNameForSelect( _rData.xConnection, sCatalog, sSchema, sTable );
132 }
133 break;
134
135 case CommandType::QUERY:
136 {
137 // ask the connection for the query
138 Reference< XQueriesSupplier > xSupplyQueries( _rData.xConnection, UNO_QUERY_THROW );
139 Reference< XNameAccess > xQueries( xSupplyQueries->getQueries(), css::uno::UNO_SET_THROW );
140
141 if ( !xQueries->hasByName( _rData.sCommand ) )
142 break;
143
144 Reference< XPropertySet > xQuery( xQueries->getByName( _rData.sCommand ), UNO_QUERY_THROW );
145
146 // a native query ?
147 bool bQueryEscapeProcessing = false;
148 xQuery->getPropertyValue("EscapeProcessing") >>= bQueryEscapeProcessing;
149 if ( !bQueryEscapeProcessing )
150 break;
151
152 // the command used by the query
153 xQuery->getPropertyValue("Command") >>= sStatement;
154 if ( sStatement.isEmpty() )
155 break;
156
157 // use a composer to build a statement from the query filter/order props
158 Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
160 xComposer.set(
161 xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"),
162 UNO_QUERY_THROW
163 );
164
165 // the "basic" statement
166 xComposer->setElementaryQuery( sStatement );
167
168 // the sort order
169 static constexpr OUStringLiteral sPropOrder( u"Order" );
170 if ( ::comphelper::hasProperty( sPropOrder, xQuery ) )
171 {
172 OUString sOrder;
173 OSL_VERIFY( xQuery->getPropertyValue( sPropOrder ) >>= sOrder );
174 xComposer->setOrder( sOrder );
175 }
176
177 // the filter
178 bool bApplyFilter = true;
179 static constexpr OUStringLiteral sPropApply( u"ApplyFilter" );
180 if ( ::comphelper::hasProperty( sPropApply, xQuery ) )
181 {
182 OSL_VERIFY( xQuery->getPropertyValue( sPropApply ) >>= bApplyFilter );
183 }
184
185 if ( bApplyFilter )
186 {
187 OUString sFilter;
188 OSL_VERIFY( xQuery->getPropertyValue("Filter") >>= sFilter );
189 xComposer->setFilter( sFilter );
190 OSL_VERIFY( xQuery->getPropertyValue("HavingClause") >>= sFilter );
191 xComposer->setHavingClause( sFilter );
192 }
193
194 // the composed statement
195 sStatement = xComposer->getQuery();
196 }
197 break;
198
199 default:
200 OSL_FAIL("lcl_ensureUpToDateComposer_nothrow: no table, no query, no statement - what else ?!");
201 break;
202 }
203
204 if ( !sStatement.isEmpty() )
205 {
206 // create a composer
207 Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
208 Reference< XSingleSelectQueryComposer > xComposer( xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"),
209 UNO_QUERY_THROW );
210 xComposer->setElementaryQuery( sStatement );
211
212 // append sort/filter
213 xComposer->setOrder( _rData.sOrder );
214 xComposer->setFilter( _rData.sFilter );
215 xComposer->setHavingClause( _rData.sHavingClause );
216
217 sStatement = xComposer->getQuery();
218
219 _rData.xComposer = xComposer;
220 _rData.bComposerDirty = false;
221 }
222 }
223 catch( const SQLException& )
224 {
225 // allowed to leave here
226 }
227 catch( const Exception& )
228 {
229 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
230 }
231
232 return _rData.xComposer.is();
233 }
234 }
235
236 StatementComposer::StatementComposer( const Reference< XConnection >& _rxConnection,
237 const OUString& _rCommand, const sal_Int32 _nCommandType, const bool _bEscapeProcessing )
238 :m_pData( new StatementComposer_Data( _rxConnection ) )
239 {
240 OSL_PRECOND( _rxConnection.is(), "StatementComposer::StatementComposer: illegal connection!" );
241 m_pData->sCommand = _rCommand;
242 m_pData->nCommandType = _nCommandType;
243 m_pData->bEscapeProcessing = _bEscapeProcessing;
244 }
245
246
248 {
249 lcl_resetComposer( *m_pData );
250 }
251
252
254 {
255 m_pData->bDisposeComposer = _bDoDispose;
256 }
257
258
259 void StatementComposer::setFilter( const OUString& _rFilter )
260 {
261 m_pData->sFilter = _rFilter;
262 m_pData->bComposerDirty = true;
263 }
264
265
266 void StatementComposer::setHavingClause( const OUString& _rHavingClause )
267 {
268 m_pData->sHavingClause = _rHavingClause;
269 m_pData->bComposerDirty = true;
270 }
271
272
273 void StatementComposer::setOrder( const OUString& _rOrder )
274 {
275 m_pData->sOrder = _rOrder;
276 m_pData->bComposerDirty = true;
277 }
278
279
280 Reference< XSingleSelectQueryComposer > const & StatementComposer::getComposer()
281 {
282 lcl_ensureUpToDateComposer_nothrow( *m_pData );
283 return m_pData->xComposer;
284 }
285
286
288 {
289 if ( lcl_ensureUpToDateComposer_nothrow( *m_pData ) )
290 {
291 return m_pData->xComposer->getQuery();
292 }
293
294 return OUString();
295 }
296
297
298} // namespace dbtools
299
300
301/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sSchema
OptionalString sCatalog
void setDisposeComposer(bool _bDoDispose)
controls whether or not the instance disposes its XSingleSelectQueryComposer upon destruction
void setFilter(const OUString &_rFilter)
void setHavingClause(const OUString &_rHavingClause)
css::uno::Reference< css::sdb::XSingleSelectQueryComposer > const & getComposer()
returns the composer which has been fed with the current settings
::std::unique_ptr< StatementComposer_Data > m_pData
void setOrder(const OUString &_rOrder)
OUString getQuery()
returns the composer statement
bool set(const css::uno::BaseReference &_rRef, css::uno::UnoReference_Query _query)
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XSingleServiceFactory > xFactory
FmFilterData * m_pData
void qualifiedNameComponents(const Reference< XDatabaseMetaData > &_rxConnMetaData, const OUString &_rQualifiedName, OUString &_rCatalog, OUString &_rSchema, OUString &_rName, EComposeRule _eComposeRule)
Definition: dbtools.cxx:862
OUString composeTableNameForSelect(const Reference< XConnection > &_rxConnection, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName)
Definition: dbtools.cxx:1296
StatementComposer_Data(const Reference< XConnection > &_rxConnection)
const Reference< XConnection > xConnection
Reference< XSingleSelectQueryComposer > xComposer