bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / commontools / statementcomposer.cxx
blob5057aaf158f95af6a22797561c75382fd4ab6c9f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <connectivity/statementcomposer.hxx>
22 #include <connectivity/dbtools.hxx>
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/lang/XMultiServiceFactory.hpp>
30 #include <unotools/sharedunocomponent.hxx>
31 #include <tools/diagnose_ex.h>
32 #include <comphelper/property.hxx>
35 namespace dbtools
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::sdbc::XConnection;
41 using ::com::sun::star::sdb::XSingleSelectQueryComposer;
42 using ::com::sun::star::lang::NullPointerException;
43 using ::com::sun::star::uno::Exception;
44 using ::com::sun::star::lang::XComponent;
45 using ::com::sun::star::uno::UNO_QUERY_THROW;
46 using ::com::sun::star::sdb::XQueriesSupplier;
47 using ::com::sun::star::container::XNameAccess;
48 using ::com::sun::star::uno::UNO_QUERY;
49 using ::com::sun::star::beans::XPropertySet;
50 using ::com::sun::star::lang::XMultiServiceFactory;
51 using ::com::sun::star::sdbc::SQLException;
53 namespace CommandType = ::com::sun::star::sdb::CommandType;
55 struct StatementComposer_Data
57 const Reference< XConnection > xConnection;
58 Reference< XSingleSelectQueryComposer > xComposer;
59 OUString sCommand;
60 OUString sFilter;
61 OUString sOrder;
62 sal_Int32 nCommandType;
63 bool bEscapeProcessing;
64 bool bComposerDirty;
65 bool bDisposeComposer;
67 StatementComposer_Data( const Reference< XConnection >& _rxConnection )
68 :xConnection( _rxConnection )
69 ,sCommand()
70 ,sFilter()
71 ,sOrder()
72 ,nCommandType( CommandType::COMMAND )
73 ,bEscapeProcessing( true )
74 ,bComposerDirty( true )
75 ,bDisposeComposer( true )
77 if ( !_rxConnection.is() )
78 throw NullPointerException();
83 namespace
86 void lcl_resetComposer( StatementComposer_Data& _rData )
88 if ( _rData.bDisposeComposer && _rData.xComposer.is() )
90 try
92 Reference< XComponent > xComposerComponent( _rData.xComposer, UNO_QUERY_THROW );
93 xComposerComponent->dispose();
95 catch( const Exception& )
97 DBG_UNHANDLED_EXCEPTION();
100 _rData.xComposer.clear();
104 bool lcl_ensureUpToDateComposer_nothrow( StatementComposer_Data& _rData )
106 if ( !_rData.bComposerDirty )
107 return _rData.xComposer.is();
108 lcl_resetComposer( _rData );
112 OUString sStatement;
113 switch ( _rData.nCommandType )
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;
121 case CommandType::TABLE:
123 if ( _rData.sCommand.isEmpty() )
124 break;
126 sStatement = "SELECT * FROM ";
128 OUString sCatalog, sSchema, sTable;
129 qualifiedNameComponents( _rData.xConnection->getMetaData(), _rData.sCommand, sCatalog, sSchema, sTable, eInDataManipulation );
131 sStatement += composeTableNameForSelect( _rData.xConnection, sCatalog, sSchema, sTable );
133 break;
135 case CommandType::QUERY:
137 // ask the connection for the query
138 Reference< XQueriesSupplier > xSupplyQueries( _rData.xConnection, UNO_QUERY_THROW );
139 Reference< XNameAccess > xQueries( xSupplyQueries->getQueries(), UNO_QUERY_THROW );
141 if ( !xQueries->hasByName( _rData.sCommand ) )
142 break;
144 Reference< XPropertySet > xQuery( xQueries->getByName( _rData.sCommand ), UNO_QUERY_THROW );
146 // a native query ?
147 bool bQueryEscapeProcessing = false;
148 xQuery->getPropertyValue("EscapeProcessing") >>= bQueryEscapeProcessing;
149 if ( !bQueryEscapeProcessing )
150 break;
152 // the command used by the query
153 xQuery->getPropertyValue("Command") >>= sStatement;
154 if ( sStatement.isEmpty() )
155 break;
157 // use a composer to build a statement from the query filter/order props
158 Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
159 ::utl::SharedUNOComponent< XSingleSelectQueryComposer > xComposer;
160 xComposer.set(
161 xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"),
162 UNO_QUERY_THROW
165 // the "basic" statement
166 xComposer->setElementaryQuery( sStatement );
168 // the sort order
169 const OUString sPropOrder( "Order" );
170 if ( ::comphelper::hasProperty( sPropOrder, xQuery ) )
172 OUString sOrder;
173 OSL_VERIFY( xQuery->getPropertyValue( sPropOrder ) >>= sOrder );
174 xComposer->setOrder( sOrder );
177 // the filter
178 bool bApplyFilter = true;
179 const OUString sPropApply( "ApplyFilter" );
180 if ( ::comphelper::hasProperty( sPropApply, xQuery ) )
182 OSL_VERIFY( xQuery->getPropertyValue( sPropApply ) >>= bApplyFilter );
185 if ( bApplyFilter )
187 OUString sFilter;
188 OSL_VERIFY( xQuery->getPropertyValue("Filter") >>= sFilter );
189 xComposer->setFilter( sFilter );
192 // the composed statement
193 sStatement = xComposer->getQuery();
195 break;
197 default:
198 OSL_FAIL("lcl_ensureUpToDateComposer_nothrow: no table, no query, no statement - what else ?!");
199 break;
202 if ( !sStatement.isEmpty() )
204 // create an composer
205 Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
206 Reference< XSingleSelectQueryComposer > xComposer( xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"),
207 UNO_QUERY_THROW );
208 xComposer->setElementaryQuery( sStatement );
210 // append sort/filter
211 xComposer->setOrder( _rData.sOrder );
212 xComposer->setFilter( _rData.sFilter );
214 sStatement = xComposer->getQuery();
216 _rData.xComposer = xComposer;
217 _rData.bComposerDirty = false;
220 catch( const SQLException& )
222 // allowed to leave here
224 catch( const Exception& )
226 DBG_UNHANDLED_EXCEPTION();
229 return _rData.xComposer.is();
233 StatementComposer::StatementComposer( const Reference< XConnection >& _rxConnection,
234 const OUString& _rCommand, const sal_Int32 _nCommandType, const bool _bEscapeProcessing )
235 :m_pData( new StatementComposer_Data( _rxConnection ) )
237 OSL_PRECOND( _rxConnection.is(), "StatementComposer::StatementComposer: illegal connection!" );
238 m_pData->sCommand = _rCommand;
239 m_pData->nCommandType = _nCommandType;
240 m_pData->bEscapeProcessing = _bEscapeProcessing;
244 StatementComposer::~StatementComposer()
246 lcl_resetComposer( *m_pData );
250 void StatementComposer::setDisposeComposer( bool _bDoDispose )
252 m_pData->bDisposeComposer = _bDoDispose;
256 void StatementComposer::setFilter( const OUString& _rFilter )
258 m_pData->sFilter = _rFilter;
259 m_pData->bComposerDirty = true;
263 void StatementComposer::setOrder( const OUString& _rOrder )
265 m_pData->sOrder = _rOrder;
266 m_pData->bComposerDirty = true;
270 Reference< XSingleSelectQueryComposer > StatementComposer::getComposer()
272 lcl_ensureUpToDateComposer_nothrow( *m_pData );
273 return m_pData->xComposer;
277 OUString StatementComposer::getQuery()
279 if ( lcl_ensureUpToDateComposer_nothrow( *m_pData ) )
281 return m_pData->xComposer->getQuery();
284 return OUString();
288 } // namespace dbtools
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */