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 .
19 #ifndef INCLUDED_DBACCESS_SOURCE_CORE_INC_SINGLESELECTQUERYCOMPOSER_HXX
20 #define INCLUDED_DBACCESS_SOURCE_CORE_INC_SINGLESELECTQUERYCOMPOSER_HXX
22 #include <com/sun/star/sdb/XParametersSupplier.hpp>
23 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
25 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/script/XTypeConverter.hpp>
28 #include <cppuhelper/implbase5.hxx>
29 #include <connectivity/sqliterator.hxx>
30 #include <connectivity/sqlparse.hxx>
31 #include "apitools.hxx"
32 #include <comphelper/broadcasthelper.hxx>
33 #include <comphelper/uno3.hxx>
34 #include <comphelper/proparrhlp.hxx>
35 #include <comphelper/propertycontainer.hxx>
36 #include <svx/ParseContext.hxx>
38 namespace com
{ namespace sun
{ namespace star
{ namespace util
{
39 class XNumberFormatsSupplier
;
40 class XNumberFormatter
;
45 typedef ::cppu::ImplHelper5
< ::com::sun::star::sdb::XSingleSelectQueryComposer
,
46 ::com::sun::star::sdb::XParametersSupplier
,
47 ::com::sun::star::sdbcx::XColumnsSupplier
,
48 ::com::sun::star::sdbcx::XTablesSupplier
,
49 ::com::sun::star::lang::XServiceInfo
> OSingleSelectQueryComposer_BASE
;
51 class OPrivateColumns
;
54 class OSingleSelectQueryComposer
: public ::comphelper::OMutexAndBroadcastHelper
56 ,public ::comphelper::OPropertyContainer
57 ,public ::comphelper::OPropertyArrayUsageHelper
< OSingleSelectQueryComposer
>
58 ,public OSingleSelectQueryComposer_BASE
62 Where
= 0, // the 0 is important, as it will be used as index into arrays
69 static inline void incSQLPart( SQLPart
& e
) { e
= (SQLPart
)(1 + (size_t)e
); }
77 typedef ::std::const_mem_fun_t
< const ::connectivity::OSQLParseNode
*, ::connectivity::OSQLParseTreeIterator
>
79 ::svxform::OSystemParseContext m_aParseContext
;
80 ::connectivity::OSQLParser m_aSqlParser
;
81 ::connectivity::OSQLParseTreeIterator m_aSqlIterator
; // the iterator for the complete statement
82 ::connectivity::OSQLParseTreeIterator m_aAdditiveIterator
; // the iterator for the "additive statement" (means without the clauses of the elementary statement)
83 ::std::vector
<OPrivateColumns
*> m_aColumnsCollection
; // used for columns and parameters of old queries
84 ::std::vector
<OPrivateTables
*> m_aTablesCollection
;
86 ::std::vector
< OUString
> m_aElementaryParts
; // the filter/groupby/having/order of the elementary statement
88 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
> m_xConnection
;
89 ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XDatabaseMetaData
> m_xMetaData
;
90 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> m_xConnectionTables
;
91 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> m_xConnectionQueries
;
92 ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatsSupplier
> m_xNumberFormatsSupplier
;
93 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> m_xColumns
;
94 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
> m_aContext
;
95 ::com::sun::star::uno::Reference
< ::com::sun::star::script::XTypeConverter
> m_xTypeConverter
;
97 ::std::vector
<OPrivateColumns
*> m_aCurrentColumns
;
98 OPrivateTables
* m_pTables
; // currently used tables
100 OUString m_aPureSelectSQL
; // the pure select statement, without filter/order/groupby/having
101 OUString m_sDecimalSep
;
103 ::com::sun::star::lang::Locale m_aLocale
;
104 sal_Int32 m_nBoolCompareMode
; // how to compare bool values
105 sal_Int32 m_nCommandType
;
112 bool setORCriteria(::connectivity::OSQLParseNode
* pCondition
, ::connectivity::OSQLParseTreeIterator
& _rIterator
,
113 ::std::vector
< ::std::vector
< ::com::sun::star::beans::PropertyValue
> >& rFilters
, const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
> & xFormatter
) const;
114 bool setANDCriteria(::connectivity::OSQLParseNode
* pCondition
, ::connectivity::OSQLParseTreeIterator
& _rIterator
,
115 ::std::vector
< ::com::sun::star::beans::PropertyValue
> & rFilters
, const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
> & xFormatter
) const;
116 bool setComparsionPredicate(::connectivity::OSQLParseNode
* pCondition
, ::connectivity::OSQLParseTreeIterator
& _rIterator
,
117 ::std::vector
< ::com::sun::star::beans::PropertyValue
> & rFilters
, const ::com::sun::star::uno::Reference
< ::com::sun::star::util::XNumberFormatter
> & xFormatter
) const;
119 static OUString
getColumnName(::connectivity::OSQLParseNode
* pColumnRef
,::connectivity::OSQLParseTreeIterator
& _rIterator
);
120 OUString
getTableAlias(const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& column
) const;
121 static sal_Int32
getPredicateType(::connectivity::OSQLParseNode
* _pPredicate
);
122 // clears all Columns,Parameters and tables and insert it to their vectors
123 void clearCurrentCollections();
124 // clears the columns collection given by EColumnType
125 void clearColumns( const EColumnType _eType
);
127 /** retrieves a particular part of a statement
131 OUString
getStatementPart( TGetParseNode
& _aGetFunctor
, ::connectivity::OSQLParseTreeIterator
& _rIterator
);
132 void setQuery_Impl( const OUString
& command
);
134 void setConditionByColumn( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& column
136 ,::std::mem_fun1_t
<bool,OSingleSelectQueryComposer
,const OUString
& >& _aSetFunctor
137 ,sal_Int32 filterOperator
);
139 /** getStructuredCondition returns the structured condition for the where or having clause
141 A member function to get the correct parse node.
144 The structured filter
146 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> >
147 getStructuredCondition( TGetParseNode
& _aGetFunctor
);
149 ::com::sun::star::uno::Reference
< ::com::sun::star::container::XIndexAccess
>
150 setCurrentColumns( EColumnType _eType
, const ::rtl::Reference
< ::connectivity::OSQLColumns
>& _rCols
);
152 //helper methods for mem_fun_t
153 inline bool implSetFilter(const OUString
& _sFilter
) { setFilter(_sFilter
); return true;}
154 inline bool implSetHavingClause(const OUString
& _sFilter
) { setHavingClause(_sFilter
); return true;}
156 /** returns the part of the seelect statement
158 Which part should be returned.
160 If <TRUE/> the keyword will be added too. Otherwise not.
165 The part of the select statement.
167 OUString
getSQLPart( SQLPart _ePart
, ::connectivity::OSQLParseTreeIterator
& _rIterator
, bool _bWithKeyword
);
169 /** retrieves the keyword for the given SQLPart
171 static OUString
getKeyword( SQLPart _ePart
);
173 /** sets a single "additive" clause, means a filter/groupby/having/order clause
175 void setSingleAdditiveClause( SQLPart _ePart
, const OUString
& _rClause
);
177 /** composes a statement from m_aPureSelectSQL and the 4 usual clauses
179 OUString
composeStatementFromParts( const ::std::vector
< OUString
>& _rParts
);
181 /** return the name of the column in the *source* *table*.
183 That is, for (SELECT a AS b FROM t), it returns A or "t"."A", as appropriate.
185 Use e.g. for WHERE, GROUP BY and HAVING clauses.
187 @param bGroupBy: for GROUP BY clause? In that case, throw exception if trying to use an unrelated column and the database does not support that.
189 OUString
impl_getColumnRealName_throw(const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& column
, bool bGroupBy
);
191 /** return the name of the column in the *query*
193 That is, for (SELECT a AS b FROM t), it returns "b"
195 Use e.g. for ORDER BY clause.
197 @param bOrderBy: for ORDER BY clause? In that case, throw exception if trying to use an unrelated column and the database does not support that.
199 OUString
impl_getColumnName_throw(const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& column
, bool bOrderBy
);
202 virtual ~OSingleSelectQueryComposer();
205 OSingleSelectQueryComposer( const ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
>& _xTableSupplier
,
206 const ::com::sun::star::uno::Reference
< ::com::sun::star::sdbc::XConnection
>& _xConnection
,
207 const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XComponentContext
>& _rContext
);
210 void SAL_CALL
disposing() SAL_OVERRIDE
;
212 virtual css::uno::Sequence
<css::uno::Type
> SAL_CALL
getTypes()
213 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
214 virtual css::uno::Sequence
<sal_Int8
> SAL_CALL
getImplementationId()
215 throw (css::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
217 // ::com::sun::star::uno::XInterface
218 DECLARE_XINTERFACE( )
221 DECLARE_SERVICE_INFO();
223 DECLARE_PROPERTYCONTAINER_DEFAULTS();
225 // ::com::sun::star::sdb::XSingleSelectQueryComposer
226 virtual OUString SAL_CALL
getElementaryQuery() throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
227 virtual void SAL_CALL
setElementaryQuery( const OUString
& _rElementary
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
228 virtual void SAL_CALL
setFilter( const OUString
& filter
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
229 virtual void SAL_CALL
setStructuredFilter( const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> >& filter
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::lang::IllegalArgumentException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
230 virtual void SAL_CALL
appendFilterByColumn( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& column
, sal_Bool andCriteria
,sal_Int32 filterOperator
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
231 virtual void SAL_CALL
appendGroupByColumn( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& column
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
232 virtual void SAL_CALL
setGroup( const OUString
& group
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
233 virtual void SAL_CALL
setHavingClause( const OUString
& filter
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
234 virtual void SAL_CALL
setStructuredHavingClause( const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> >& filter
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
235 virtual void SAL_CALL
appendHavingClauseByColumn( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& column
, sal_Bool andCriteria
,sal_Int32 filterOperator
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::lang::WrappedTargetException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
236 virtual void SAL_CALL
appendOrderByColumn( const ::com::sun::star::uno::Reference
< ::com::sun::star::beans::XPropertySet
>& column
, sal_Bool ascending
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
237 virtual void SAL_CALL
setOrder( const OUString
& order
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
239 // XSingleSelectQueryAnalyzer
240 virtual OUString SAL_CALL
getQuery( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
241 virtual void SAL_CALL
setQuery( const OUString
& command
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
242 virtual void SAL_CALL
setCommand( const OUString
& command
,sal_Int32 CommandType
) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
243 virtual OUString SAL_CALL
getFilter( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
244 virtual ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> > SAL_CALL
getStructuredFilter( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
245 virtual OUString SAL_CALL
getGroup( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
246 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XIndexAccess
> SAL_CALL
getGroupColumns( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
247 virtual OUString SAL_CALL
getHavingClause( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
248 virtual ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> > SAL_CALL
getStructuredHavingClause( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
249 virtual OUString SAL_CALL
getOrder( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
250 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XIndexAccess
> SAL_CALL
getOrderColumns( ) throw (::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
251 virtual OUString SAL_CALL
getQueryWithSubstitution( ) throw (::com::sun::star::sdbc::SQLException
, ::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
254 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> SAL_CALL
getColumns( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
256 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XNameAccess
> SAL_CALL
getTables( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
257 // XParametersSupplier
258 virtual ::com::sun::star::uno::Reference
< ::com::sun::star::container::XIndexAccess
> SAL_CALL
getParameters( ) throw(::com::sun::star::uno::RuntimeException
, std::exception
) SAL_OVERRIDE
;
261 #endif // INCLUDED_DBACCESS_SOURCE_CORE_INC_SINGLESELECTQUERYCOMPOSER_HXX
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */