bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / commontools / statementcomposer.cxx
bloba21c8cf1a41de0e5abed508e3963cfa21ff7e5a4
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/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>
33 #include <unotools/sharedunocomponent.hxx>
34 #include <tools/diagnose_ex.h>
35 #include <comphelper/property.hxx>
38 namespace dbtools
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;
55 namespace CommandType = ::com::sun::star::sdb::CommandType;
57 struct StatementComposer_Data
59 const Reference< XConnection > xConnection;
60 Reference< XSingleSelectQueryComposer > xComposer;
61 OUString sCommand;
62 OUString sFilter;
63 OUString sOrder;
64 sal_Int32 nCommandType;
65 bool bEscapeProcessing;
66 bool bComposerDirty;
67 bool bDisposeComposer;
69 explicit StatementComposer_Data( const Reference< XConnection >& _rxConnection )
70 :xConnection( _rxConnection )
71 ,sCommand()
72 ,sFilter()
73 ,sOrder()
74 ,nCommandType( CommandType::COMMAND )
75 ,bEscapeProcessing( true )
76 ,bComposerDirty( true )
77 ,bDisposeComposer( true )
79 if ( !_rxConnection.is() )
80 throw NullPointerException();
85 namespace
88 void lcl_resetComposer( StatementComposer_Data& _rData )
90 if ( _rData.bDisposeComposer && _rData.xComposer.is() )
92 try
94 Reference< XComponent > xComposerComponent( _rData.xComposer, UNO_QUERY_THROW );
95 xComposerComponent->dispose();
97 catch( const Exception& )
99 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
102 _rData.xComposer.clear();
106 bool lcl_ensureUpToDateComposer_nothrow( StatementComposer_Data& _rData )
108 if ( !_rData.bComposerDirty )
109 return _rData.xComposer.is();
110 lcl_resetComposer( _rData );
114 OUString sStatement;
115 switch ( _rData.nCommandType )
117 case CommandType::COMMAND:
118 if ( _rData.bEscapeProcessing )
119 sStatement = _rData.sCommand;
120 // (in case of no escape processing we assume a not parseable statement)
121 break;
123 case CommandType::TABLE:
125 if ( _rData.sCommand.isEmpty() )
126 break;
128 sStatement = "SELECT * FROM ";
130 OUString sCatalog, sSchema, sTable;
131 qualifiedNameComponents( _rData.xConnection->getMetaData(), _rData.sCommand, sCatalog, sSchema, sTable, EComposeRule::InDataManipulation );
133 sStatement += composeTableNameForSelect( _rData.xConnection, sCatalog, sSchema, sTable );
135 break;
137 case CommandType::QUERY:
139 // ask the connection for the query
140 Reference< XQueriesSupplier > xSupplyQueries( _rData.xConnection, UNO_QUERY_THROW );
141 Reference< XNameAccess > xQueries( xSupplyQueries->getQueries(), css::uno::UNO_SET_THROW );
143 if ( !xQueries->hasByName( _rData.sCommand ) )
144 break;
146 Reference< XPropertySet > xQuery( xQueries->getByName( _rData.sCommand ), UNO_QUERY_THROW );
148 // a native query ?
149 bool bQueryEscapeProcessing = false;
150 xQuery->getPropertyValue("EscapeProcessing") >>= bQueryEscapeProcessing;
151 if ( !bQueryEscapeProcessing )
152 break;
154 // the command used by the query
155 xQuery->getPropertyValue("Command") >>= sStatement;
156 if ( sStatement.isEmpty() )
157 break;
159 // use a composer to build a statement from the query filter/order props
160 Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
161 ::utl::SharedUNOComponent< XSingleSelectQueryComposer > xComposer;
162 xComposer.set(
163 xFactory->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"),
164 UNO_QUERY_THROW
167 // the "basic" statement
168 xComposer->setElementaryQuery( sStatement );
170 // the sort order
171 const OUString sPropOrder( "Order" );
172 if ( ::comphelper::hasProperty( sPropOrder, xQuery ) )
174 OUString sOrder;
175 OSL_VERIFY( xQuery->getPropertyValue( sPropOrder ) >>= sOrder );
176 xComposer->setOrder( sOrder );
179 // the filter
180 bool bApplyFilter = true;
181 const OUString sPropApply( "ApplyFilter" );
182 if ( ::comphelper::hasProperty( sPropApply, xQuery ) )
184 OSL_VERIFY( xQuery->getPropertyValue( sPropApply ) >>= bApplyFilter );
187 if ( bApplyFilter )
189 OUString sFilter;
190 OSL_VERIFY( xQuery->getPropertyValue("Filter") >>= sFilter );
191 xComposer->setFilter( sFilter );
194 // the composed statement
195 sStatement = xComposer->getQuery();
197 break;
199 default:
200 OSL_FAIL("lcl_ensureUpToDateComposer_nothrow: no table, no query, no statement - what else ?!");
201 break;
204 if ( !sStatement.isEmpty() )
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 );
212 // append sort/filter
213 xComposer->setOrder( _rData.sOrder );
214 xComposer->setFilter( _rData.sFilter );
216 sStatement = xComposer->getQuery();
218 _rData.xComposer = xComposer;
219 _rData.bComposerDirty = false;
222 catch( const SQLException& )
224 // allowed to leave here
226 catch( const Exception& )
228 DBG_UNHANDLED_EXCEPTION("connectivity.commontools");
231 return _rData.xComposer.is();
235 StatementComposer::StatementComposer( const Reference< XConnection >& _rxConnection,
236 const OUString& _rCommand, const sal_Int32 _nCommandType, const bool _bEscapeProcessing )
237 :m_pData( new StatementComposer_Data( _rxConnection ) )
239 OSL_PRECOND( _rxConnection.is(), "StatementComposer::StatementComposer: illegal connection!" );
240 m_pData->sCommand = _rCommand;
241 m_pData->nCommandType = _nCommandType;
242 m_pData->bEscapeProcessing = _bEscapeProcessing;
246 StatementComposer::~StatementComposer()
248 lcl_resetComposer( *m_pData );
252 void StatementComposer::setDisposeComposer( bool _bDoDispose )
254 m_pData->bDisposeComposer = _bDoDispose;
258 void StatementComposer::setFilter( const OUString& _rFilter )
260 m_pData->sFilter = _rFilter;
261 m_pData->bComposerDirty = true;
265 void StatementComposer::setOrder( const OUString& _rOrder )
267 m_pData->sOrder = _rOrder;
268 m_pData->bComposerDirty = true;
272 Reference< XSingleSelectQueryComposer > const & StatementComposer::getComposer()
274 lcl_ensureUpToDateComposer_nothrow( *m_pData );
275 return m_pData->xComposer;
279 OUString StatementComposer::getQuery()
281 if ( lcl_ensureUpToDateComposer_nothrow( *m_pData ) )
283 return m_pData->xComposer->getQuery();
286 return OUString();
290 } // namespace dbtools
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */