1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
;
64 sal_Int32 nCommandType
;
65 bool bEscapeProcessing
;
67 bool bDisposeComposer
;
69 explicit StatementComposer_Data( const Reference
< XConnection
>& _rxConnection
)
70 :xConnection( _rxConnection
)
74 ,nCommandType( CommandType::COMMAND
)
75 ,bEscapeProcessing( true )
76 ,bComposerDirty( true )
77 ,bDisposeComposer( true )
79 if ( !_rxConnection
.is() )
80 throw NullPointerException();
88 void lcl_resetComposer( StatementComposer_Data
& _rData
)
90 if ( _rData
.bDisposeComposer
&& _rData
.xComposer
.is() )
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
);
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)
123 case CommandType::TABLE
:
125 if ( _rData
.sCommand
.isEmpty() )
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
);
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
) )
146 Reference
< XPropertySet
> xQuery( xQueries
->getByName( _rData
.sCommand
), UNO_QUERY_THROW
);
149 bool bQueryEscapeProcessing
= false;
150 xQuery
->getPropertyValue("EscapeProcessing") >>= bQueryEscapeProcessing
;
151 if ( !bQueryEscapeProcessing
)
154 // the command used by the query
155 xQuery
->getPropertyValue("Command") >>= sStatement
;
156 if ( sStatement
.isEmpty() )
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
;
163 xFactory
->createInstance("com.sun.star.sdb.SingleSelectQueryComposer"),
167 // the "basic" statement
168 xComposer
->setElementaryQuery( sStatement
);
171 const OUString
sPropOrder( "Order" );
172 if ( ::comphelper::hasProperty( sPropOrder
, xQuery
) )
175 OSL_VERIFY( xQuery
->getPropertyValue( sPropOrder
) >>= sOrder
);
176 xComposer
->setOrder( sOrder
);
180 bool bApplyFilter
= true;
181 const OUString
sPropApply( "ApplyFilter" );
182 if ( ::comphelper::hasProperty( sPropApply
, xQuery
) )
184 OSL_VERIFY( xQuery
->getPropertyValue( sPropApply
) >>= bApplyFilter
);
190 OSL_VERIFY( xQuery
->getPropertyValue("Filter") >>= sFilter
);
191 xComposer
->setFilter( sFilter
);
194 // the composed statement
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
);
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();
290 } // namespace dbtools
293 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */