xref: /AOO41X/main/connectivity/source/commontools/statementcomposer.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include <connectivity/statementcomposer.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /** === begin UNO includes === **/
35*cdf0e10cSrcweir #include <com/sun/star/sdb/CommandType.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/NullPointerException.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/sdb/XQueriesSupplier.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40*cdf0e10cSrcweir /** === end UNO includes === **/
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <unotools/sharedunocomponent.hxx>
43*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
44*cdf0e10cSrcweir #include <comphelper/property.hxx>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir //........................................................................
47*cdf0e10cSrcweir namespace dbtools
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir //........................................................................
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 	/** === begin UNO using === **/
52*cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
53*cdf0e10cSrcweir     using ::com::sun::star::sdbc::XConnection;
54*cdf0e10cSrcweir     using ::com::sun::star::sdb::XSingleSelectQueryComposer;
55*cdf0e10cSrcweir     using ::com::sun::star::lang::NullPointerException;
56*cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
57*cdf0e10cSrcweir     using ::com::sun::star::lang::XComponent;
58*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY_THROW;
59*cdf0e10cSrcweir     using ::com::sun::star::sdb::XQueriesSupplier;
60*cdf0e10cSrcweir     using ::com::sun::star::container::XNameAccess;
61*cdf0e10cSrcweir     using ::com::sun::star::uno::UNO_QUERY;
62*cdf0e10cSrcweir     using ::com::sun::star::beans::XPropertySet;
63*cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
64*cdf0e10cSrcweir     using ::com::sun::star::sdbc::SQLException;
65*cdf0e10cSrcweir 	/** === end UNO using === **/
66*cdf0e10cSrcweir     namespace CommandType = ::com::sun::star::sdb::CommandType;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	//====================================================================
69*cdf0e10cSrcweir 	//= StatementComposer_Data
70*cdf0e10cSrcweir 	//====================================================================
71*cdf0e10cSrcweir     struct StatementComposer_Data
72*cdf0e10cSrcweir     {
73*cdf0e10cSrcweir         const Reference< XConnection >          xConnection;
74*cdf0e10cSrcweir         Reference< XSingleSelectQueryComposer > xComposer;
75*cdf0e10cSrcweir         ::rtl::OUString                         sCommand;
76*cdf0e10cSrcweir         ::rtl::OUString                         sFilter;
77*cdf0e10cSrcweir         ::rtl::OUString                         sOrder;
78*cdf0e10cSrcweir         sal_Int32                               nCommandType;
79*cdf0e10cSrcweir         sal_Bool                                bEscapeProcessing;
80*cdf0e10cSrcweir         bool                                    bComposerDirty;
81*cdf0e10cSrcweir         bool                                    bDisposeComposer;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         StatementComposer_Data( const Reference< XConnection >& _rxConnection )
84*cdf0e10cSrcweir             :xConnection( _rxConnection )
85*cdf0e10cSrcweir             ,sCommand()
86*cdf0e10cSrcweir             ,sFilter()
87*cdf0e10cSrcweir             ,sOrder()
88*cdf0e10cSrcweir             ,nCommandType( CommandType::COMMAND )
89*cdf0e10cSrcweir             ,bEscapeProcessing( sal_True )
90*cdf0e10cSrcweir             ,bComposerDirty( true )
91*cdf0e10cSrcweir             ,bDisposeComposer( true )
92*cdf0e10cSrcweir         {
93*cdf0e10cSrcweir             if ( !_rxConnection.is() )
94*cdf0e10cSrcweir                 throw NullPointerException();
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir     };
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 	//--------------------------------------------------------------------
99*cdf0e10cSrcweir     namespace
100*cdf0e10cSrcweir     {
101*cdf0e10cSrcweir         //----------------------------------------------------------------
102*cdf0e10cSrcweir         void    lcl_resetComposer( StatementComposer_Data& _rData )
103*cdf0e10cSrcweir         {
104*cdf0e10cSrcweir             if ( _rData.bDisposeComposer && _rData.xComposer.is() )
105*cdf0e10cSrcweir             {
106*cdf0e10cSrcweir                 try
107*cdf0e10cSrcweir                 {
108*cdf0e10cSrcweir                     Reference< XComponent > xComposerComponent( _rData.xComposer, UNO_QUERY_THROW );
109*cdf0e10cSrcweir                     xComposerComponent->dispose();
110*cdf0e10cSrcweir                 }
111*cdf0e10cSrcweir                 catch( const Exception& )
112*cdf0e10cSrcweir                 {
113*cdf0e10cSrcweir         	        DBG_UNHANDLED_EXCEPTION();
114*cdf0e10cSrcweir                 }
115*cdf0e10cSrcweir             }
116*cdf0e10cSrcweir             _rData.xComposer.clear();
117*cdf0e10cSrcweir         }
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         //----------------------------------------------------------------
120*cdf0e10cSrcweir         bool    lcl_ensureUpToDateComposer_nothrow( StatementComposer_Data& _rData )
121*cdf0e10cSrcweir         {
122*cdf0e10cSrcweir             if ( !_rData.bComposerDirty )
123*cdf0e10cSrcweir                 return _rData.xComposer.is();
124*cdf0e10cSrcweir             lcl_resetComposer( _rData );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir             try
127*cdf0e10cSrcweir             {
128*cdf0e10cSrcweir                 ::rtl::OUString sStatement;
129*cdf0e10cSrcweir                 switch ( _rData.nCommandType )
130*cdf0e10cSrcweir 	            {
131*cdf0e10cSrcweir 		            case CommandType::COMMAND:
132*cdf0e10cSrcweir 			            if ( _rData.bEscapeProcessing )
133*cdf0e10cSrcweir 				            sStatement = _rData.sCommand;
134*cdf0e10cSrcweir 			            // (in case of no escape processing  we assume a not parseable statement)
135*cdf0e10cSrcweir 			            break;
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir                     case CommandType::TABLE:
138*cdf0e10cSrcweir 		            {
139*cdf0e10cSrcweir 			            if ( !_rData.sCommand.getLength() )
140*cdf0e10cSrcweir                             break;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 			            sStatement = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "SELECT * FROM " ) );
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	                    ::rtl::OUString sCatalog, sSchema, sTable;
145*cdf0e10cSrcweir 	                    qualifiedNameComponents( _rData.xConnection->getMetaData(), _rData.sCommand, sCatalog, sSchema, sTable, eInDataManipulation );
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir                         sStatement += composeTableNameForSelect( _rData.xConnection, sCatalog, sSchema, sTable );
148*cdf0e10cSrcweir 		            }
149*cdf0e10cSrcweir 		            break;
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 		            case CommandType::QUERY:
152*cdf0e10cSrcweir 		            {
153*cdf0e10cSrcweir 			            // ask the connection for the query
154*cdf0e10cSrcweir 			            Reference< XQueriesSupplier > xSupplyQueries( _rData.xConnection, UNO_QUERY_THROW );
155*cdf0e10cSrcweir 			            Reference< XNameAccess >      xQueries( xSupplyQueries->getQueries(), UNO_QUERY_THROW );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 			            if ( !xQueries->hasByName( _rData.sCommand ) )
158*cdf0e10cSrcweir 				            break;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 			            Reference< XPropertySet > xQuery( xQueries->getByName( _rData.sCommand ), UNO_QUERY_THROW );
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir                         //  a native query ?
163*cdf0e10cSrcweir                         sal_Bool bQueryEscapeProcessing = sal_False;
164*cdf0e10cSrcweir                         xQuery->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "EscapeProcessing" ) ) ) >>= bQueryEscapeProcessing;
165*cdf0e10cSrcweir 			            if ( !bQueryEscapeProcessing )
166*cdf0e10cSrcweir 				            break;
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 			            // the command used by the query
169*cdf0e10cSrcweir 			            xQuery->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Command" ) ) ) >>= sStatement;
170*cdf0e10cSrcweir                         if ( !sStatement.getLength() )
171*cdf0e10cSrcweir                             break;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 			            // use a composer to build a statement from the query filter/order props
174*cdf0e10cSrcweir                         Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
175*cdf0e10cSrcweir                         ::utl::SharedUNOComponent< XSingleSelectQueryComposer > xComposer;
176*cdf0e10cSrcweir                         xComposer.set(
177*cdf0e10cSrcweir                             xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ),
178*cdf0e10cSrcweir                             UNO_QUERY_THROW
179*cdf0e10cSrcweir                         );
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir                         // the "basic" statement
182*cdf0e10cSrcweir 			            xComposer->setElementaryQuery( sStatement );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir                         // the sort order
185*cdf0e10cSrcweir                         const ::rtl::OUString sPropOrder( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Order" ) ) );
186*cdf0e10cSrcweir                         if ( ::comphelper::hasProperty( sPropOrder, xQuery ) )
187*cdf0e10cSrcweir                         {
188*cdf0e10cSrcweir                             ::rtl::OUString sOrder;
189*cdf0e10cSrcweir                             OSL_VERIFY( xQuery->getPropertyValue( sPropOrder ) >>= sOrder );
190*cdf0e10cSrcweir 				            xComposer->setOrder( sOrder );
191*cdf0e10cSrcweir                         }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir                         // the filter
194*cdf0e10cSrcweir 			            sal_Bool bApplyFilter = sal_True;
195*cdf0e10cSrcweir 			            const ::rtl::OUString sPropApply = ::rtl::OUString::createFromAscii( "ApplyFilter" );
196*cdf0e10cSrcweir                         if ( ::comphelper::hasProperty( sPropApply, xQuery ) )
197*cdf0e10cSrcweir                         {
198*cdf0e10cSrcweir                             OSL_VERIFY( xQuery->getPropertyValue( sPropApply ) >>= bApplyFilter );
199*cdf0e10cSrcweir                         }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 			            if ( bApplyFilter )
202*cdf0e10cSrcweir                         {
203*cdf0e10cSrcweir                             ::rtl::OUString sFilter;
204*cdf0e10cSrcweir                             OSL_VERIFY( xQuery->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Filter" ) ) ) >>= sFilter );
205*cdf0e10cSrcweir                             xComposer->setFilter( sFilter );
206*cdf0e10cSrcweir                         }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir                         // the composed statement
209*cdf0e10cSrcweir                         sStatement = xComposer->getQuery();
210*cdf0e10cSrcweir 		            }
211*cdf0e10cSrcweir 		            break;
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir                     default:
214*cdf0e10cSrcweir 			            OSL_ENSURE(sal_False, "lcl_ensureUpToDateComposer_nothrow: no table, no query, no statement - what else ?!");
215*cdf0e10cSrcweir 			            break;
216*cdf0e10cSrcweir 	            }
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 	            if ( sStatement.getLength() )
219*cdf0e10cSrcweir 	            {
220*cdf0e10cSrcweir 		            // create an composer
221*cdf0e10cSrcweir                     Reference< XMultiServiceFactory > xFactory( _rData.xConnection, UNO_QUERY_THROW );
222*cdf0e10cSrcweir                     Reference< XSingleSelectQueryComposer > xComposer( xFactory->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sdb.SingleSelectQueryComposer" ) ) ),
223*cdf0e10cSrcweir                         UNO_QUERY_THROW );
224*cdf0e10cSrcweir 			        xComposer->setElementaryQuery( sStatement );
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 			        // append sort/filter
227*cdf0e10cSrcweir 				    xComposer->setOrder( _rData.sOrder );
228*cdf0e10cSrcweir 					xComposer->setFilter( _rData.sFilter );
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir                     sStatement = xComposer->getQuery();
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir                     _rData.xComposer = xComposer;
233*cdf0e10cSrcweir                     _rData.bComposerDirty = false;
234*cdf0e10cSrcweir 		        }
235*cdf0e10cSrcweir             }
236*cdf0e10cSrcweir             catch( const SQLException& )
237*cdf0e10cSrcweir             {
238*cdf0e10cSrcweir                 // allowed to leave here
239*cdf0e10cSrcweir             }
240*cdf0e10cSrcweir             catch( const Exception& )
241*cdf0e10cSrcweir             {
242*cdf0e10cSrcweir             	DBG_UNHANDLED_EXCEPTION();
243*cdf0e10cSrcweir             }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir             return _rData.xComposer.is();
246*cdf0e10cSrcweir         }
247*cdf0e10cSrcweir     }
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 	//====================================================================
250*cdf0e10cSrcweir 	//= StatementComposer
251*cdf0e10cSrcweir 	//====================================================================
252*cdf0e10cSrcweir 	//--------------------------------------------------------------------
253*cdf0e10cSrcweir     StatementComposer::StatementComposer( const Reference< XConnection >& _rxConnection,
254*cdf0e10cSrcweir         const ::rtl::OUString&  _rCommand, const sal_Int32 _nCommandType, const sal_Bool _bEscapeProcessing )
255*cdf0e10cSrcweir         :m_pData( new StatementComposer_Data( _rxConnection ) )
256*cdf0e10cSrcweir     {
257*cdf0e10cSrcweir         OSL_PRECOND( _rxConnection.is(), "StatementComposer::StatementComposer: illegal connection!" );
258*cdf0e10cSrcweir         m_pData->sCommand = _rCommand;
259*cdf0e10cSrcweir         m_pData->nCommandType = _nCommandType;
260*cdf0e10cSrcweir         m_pData->bEscapeProcessing = _bEscapeProcessing;
261*cdf0e10cSrcweir     }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 	//--------------------------------------------------------------------
264*cdf0e10cSrcweir     StatementComposer::~StatementComposer()
265*cdf0e10cSrcweir     {
266*cdf0e10cSrcweir         lcl_resetComposer( *m_pData );
267*cdf0e10cSrcweir     }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir     //--------------------------------------------------------------------
270*cdf0e10cSrcweir     void StatementComposer::setDisposeComposer( bool _bDoDispose )
271*cdf0e10cSrcweir     {
272*cdf0e10cSrcweir         m_pData->bDisposeComposer = _bDoDispose;
273*cdf0e10cSrcweir     }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir     //--------------------------------------------------------------------
276*cdf0e10cSrcweir     bool StatementComposer::getDisposeComposer() const
277*cdf0e10cSrcweir     {
278*cdf0e10cSrcweir         return m_pData->bDisposeComposer;
279*cdf0e10cSrcweir     }
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     //--------------------------------------------------------------------
282*cdf0e10cSrcweir     void StatementComposer::setFilter( const ::rtl::OUString& _rFilter )
283*cdf0e10cSrcweir     {
284*cdf0e10cSrcweir         m_pData->sFilter = _rFilter;
285*cdf0e10cSrcweir         m_pData->bComposerDirty = true;
286*cdf0e10cSrcweir     }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 	//--------------------------------------------------------------------
289*cdf0e10cSrcweir     void StatementComposer::setOrder( const ::rtl::OUString& _rOrder )
290*cdf0e10cSrcweir     {
291*cdf0e10cSrcweir         m_pData->sOrder = _rOrder;
292*cdf0e10cSrcweir         m_pData->bComposerDirty = true;
293*cdf0e10cSrcweir     }
294*cdf0e10cSrcweir 
295*cdf0e10cSrcweir 	//--------------------------------------------------------------------
296*cdf0e10cSrcweir     Reference< XSingleSelectQueryComposer > StatementComposer::getComposer()
297*cdf0e10cSrcweir     {
298*cdf0e10cSrcweir         lcl_ensureUpToDateComposer_nothrow( *m_pData );
299*cdf0e10cSrcweir         return m_pData->xComposer;
300*cdf0e10cSrcweir     }
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 	//--------------------------------------------------------------------
303*cdf0e10cSrcweir     ::rtl::OUString StatementComposer::getQuery()
304*cdf0e10cSrcweir     {
305*cdf0e10cSrcweir         if ( lcl_ensureUpToDateComposer_nothrow( *m_pData ) )
306*cdf0e10cSrcweir         {
307*cdf0e10cSrcweir             return m_pData->xComposer->getQuery();
308*cdf0e10cSrcweir         }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir         return ::rtl::OUString();
311*cdf0e10cSrcweir     }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir //........................................................................
314*cdf0e10cSrcweir } // namespace dbtools
315*cdf0e10cSrcweir //........................................................................
316