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>
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;
77 if ( !_rxConnection.is() )
78 throw NullPointerException();
86 void lcl_resetComposer( StatementComposer_Data& _rData )
88 if ( _rData.bDisposeComposer && _rData.xComposer.is() )
92 Reference< XComponent > xComposerComponent( _rData.xComposer, UNO_QUERY_THROW );
93 xComposerComponent->dispose();
95 catch(
const 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 );
113 switch ( _rData.nCommandType )
115 case CommandType::COMMAND:
116 if ( _rData.bEscapeProcessing )
117 sStatement = _rData.sCommand;
121 case CommandType::TABLE:
123 if ( _rData.sCommand.isEmpty() )
126 sStatement =
"SELECT * FROM ";
135 case CommandType::QUERY:
138 Reference< XQueriesSupplier > xSupplyQueries( _rData.xConnection, UNO_QUERY_THROW );
139 Reference< XNameAccess > xQueries( xSupplyQueries->getQueries(), css::uno::UNO_SET_THROW );
141 if ( !xQueries->hasByName( _rData.sCommand ) )
144 Reference< XPropertySet > xQuery( xQueries->getByName( _rData.sCommand ), UNO_QUERY_THROW );
147 bool bQueryEscapeProcessing =
false;
148 xQuery->getPropertyValue(
"EscapeProcessing") >>= bQueryEscapeProcessing;
149 if ( !bQueryEscapeProcessing )
153 xQuery->getPropertyValue(
"Command") >>= sStatement;
154 if ( sStatement.isEmpty() )
158 Reference< XMultiServiceFactory >
xFactory( _rData.xConnection, UNO_QUERY_THROW );
161 xFactory->createInstance(
"com.sun.star.sdb.SingleSelectQueryComposer"),
166 xComposer->setElementaryQuery( sStatement );
169 static constexpr OUStringLiteral sPropOrder( u
"Order" );
170 if ( ::comphelper::hasProperty( sPropOrder, xQuery ) )
173 OSL_VERIFY( xQuery->getPropertyValue( sPropOrder ) >>= sOrder );
174 xComposer->setOrder( sOrder );
178 bool bApplyFilter =
true;
179 static constexpr OUStringLiteral sPropApply( u
"ApplyFilter" );
180 if ( ::comphelper::hasProperty( sPropApply, xQuery ) )
182 OSL_VERIFY( xQuery->getPropertyValue( sPropApply ) >>= bApplyFilter );
188 OSL_VERIFY( xQuery->getPropertyValue(
"Filter") >>= sFilter );
189 xComposer->setFilter( sFilter );
190 OSL_VERIFY( xQuery->getPropertyValue(
"HavingClause") >>= sFilter );
191 xComposer->setHavingClause( sFilter );
195 sStatement = xComposer->getQuery();
200 OSL_FAIL(
"lcl_ensureUpToDateComposer_nothrow: no table, no query, no statement - what else ?!");
204 if ( !sStatement.isEmpty() )
207 Reference< XMultiServiceFactory >
xFactory( _rData.xConnection, UNO_QUERY_THROW );
208 Reference< XSingleSelectQueryComposer > xComposer(
xFactory->createInstance(
"com.sun.star.sdb.SingleSelectQueryComposer"),
210 xComposer->setElementaryQuery( sStatement );
213 xComposer->setOrder( _rData.sOrder );
214 xComposer->setFilter( _rData.sFilter );
215 xComposer->setHavingClause( _rData.sHavingClause );
217 sStatement = xComposer->getQuery();
219 _rData.xComposer = xComposer;
220 _rData.bComposerDirty =
false;
223 catch(
const SQLException& )
227 catch(
const Exception& )
232 return _rData.xComposer.is();
237 const OUString& _rCommand,
const sal_Int32 _nCommandType,
const bool _bEscapeProcessing )
238 :
m_pData( new StatementComposer_Data( _rxConnection ) )
240 OSL_PRECOND( _rxConnection.is(),
"StatementComposer::StatementComposer: illegal connection!" );
242 m_pData->nCommandType = _nCommandType;
243 m_pData->bEscapeProcessing = _bEscapeProcessing;
255 m_pData->bDisposeComposer = _bDoDispose;
262 m_pData->bComposerDirty =
true;
268 m_pData->sHavingClause = _rHavingClause;
269 m_pData->bComposerDirty =
true;
276 m_pData->bComposerDirty =
true;
282 lcl_ensureUpToDateComposer_nothrow( *
m_pData );
289 if ( lcl_ensureUpToDateComposer_nothrow( *
m_pData ) )
291 return m_pData->xComposer->getQuery();
bool set(const css::uno::BaseReference &_rRef, css::uno::UnoReference_Query _query)
#define DBG_UNHANDLED_EXCEPTION(...)
Reference< XSingleServiceFactory > xFactory