bump product version to 4.1.6.2
[LibreOffice.git] / dbaccess / source / core / inc / SingleSelectQueryComposer.hxx
blob7031b4d3d0ad443a26240630abe7f4b770a91c99
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 .
19 #ifndef DBACCESS_CORE_API_SINGLESELECTQUERYCOMPOSER_HXX
20 #define DBACCESS_CORE_API_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 #include <memory>
40 namespace com { namespace sun { namespace star { namespace util {
41 class XNumberFormatsSupplier;
42 class XNumberFormatter;
43 }}}}
45 namespace dbaccess
47 typedef ::cppu::ImplHelper5< ::com::sun::star::sdb::XSingleSelectQueryComposer,
48 ::com::sun::star::sdb::XParametersSupplier,
49 ::com::sun::star::sdbcx::XColumnsSupplier,
50 ::com::sun::star::sdbcx::XTablesSupplier,
51 ::com::sun::star::lang::XServiceInfo > OSingleSelectQueryComposer_BASE;
53 class OPrivateColumns;
54 class OPrivateTables;
56 class OSingleSelectQueryComposer : public ::comphelper::OMutexAndBroadcastHelper
57 ,public OSubComponent
58 ,public ::comphelper::OPropertyContainer
59 ,public ::comphelper::OPropertyArrayUsageHelper < OSingleSelectQueryComposer >
60 ,public OSingleSelectQueryComposer_BASE
62 enum SQLPart
64 Where = 0, // the 0 is important, as it will be used as index into arrays
65 Group,
66 Having,
67 Order,
69 SQLPartCount
71 inline void incSQLPart( SQLPart& e ) { e = (SQLPart)(1 + (size_t)e); }
72 enum EColumnType
74 SelectColumns = 0,
75 GroupByColumns = 1,
76 OrderColumns = 2,
77 ParameterColumns = 3
79 typedef ::std::const_mem_fun_t< const ::connectivity::OSQLParseNode*, ::connectivity::OSQLParseTreeIterator >
80 TGetParseNode;
81 ::svxform::OSystemParseContext m_aParseContext;
82 ::connectivity::OSQLParser m_aSqlParser;
83 ::connectivity::OSQLParseTreeIterator m_aSqlIterator; // the iterator for the complete statement
84 ::connectivity::OSQLParseTreeIterator m_aAdditiveIterator; // the iterator for the "additive statement" (means without the clauses of the elementary statement)
85 ::std::vector<OPrivateColumns*> m_aColumnsCollection; // used for columns and parameters of old queries
86 ::std::vector<OPrivateTables*> m_aTablesCollection;
88 ::std::vector< OUString > m_aElementaryParts; // the filter/groupby/having/order of the elementary statement
90 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> m_xConnection;
91 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> m_xMetaData;
92 ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess> m_xConnectionTables;
93 ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess> m_xConnectionQueries;
94 ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier > m_xNumberFormatsSupplier;
95 ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess> m_xColumns;
96 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext> m_aContext;
97 ::com::sun::star::uno::Reference< ::com::sun::star::script::XTypeConverter > m_xTypeConverter;
99 ::std::vector<OPrivateColumns*> m_aCurrentColumns;
100 OPrivateTables* m_pTables; // currently used tables
102 OUString m_aPureSelectSQL; // the pure select statement, without filter/order/groupby/having
103 OUString m_sDecimalSep;
104 OUString m_sCommand;
105 ::com::sun::star::lang::Locale m_aLocale;
106 sal_Int32 m_nBoolCompareMode; // how to compare bool values
107 sal_Int32 m_nCommandType;
109 // <properties>
110 OUString m_sOrignal;
111 // </properties>
114 sal_Bool setORCriteria(::connectivity::OSQLParseNode* pCondition, ::connectivity::OSQLParseTreeIterator& _rIterator,
115 ::std::vector< ::std::vector < ::com::sun::star::beans::PropertyValue > >& rFilters, const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const;
116 sal_Bool setANDCriteria(::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;
118 sal_Bool setComparsionPredicate(::connectivity::OSQLParseNode* pCondition, ::connectivity::OSQLParseTreeIterator& _rIterator,
119 ::std::vector < ::com::sun::star::beans::PropertyValue > & rFilters, const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const;
121 OUString getColumnName(::connectivity::OSQLParseNode* pColumnRef,::connectivity::OSQLParseTreeIterator& _rIterator) const;
122 OUString getTableAlias(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column ) const;
123 sal_Int32 getPredicateType(::connectivity::OSQLParseNode * _pPredicate) const;
124 // clears all Columns,Parameters and tables and insert it to their vectors
125 void clearCurrentCollections();
126 // clears the columns collection given by EColumnType
127 void clearColumns( const EColumnType _eType );
129 /** retrieves a particular part of a statement
130 @param _rIterator
131 the iterator to use.
133 OUString getStatementPart( TGetParseNode& _aGetFunctor, ::connectivity::OSQLParseTreeIterator& _rIterator );
134 void setQuery_Impl( const OUString& command );
136 void setConditionByColumn( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column
137 , sal_Bool andCriteria
138 ,::std::mem_fun1_t<bool,OSingleSelectQueryComposer,OUString>& _aSetFunctor
139 ,sal_Int32 filterOperator);
141 /** getStructuredCondition returns the structured condition for the where or having clause
142 @param _aGetFunctor
143 A member function to get the correct parse node.
145 @return
146 The structured filter
148 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > >
149 getStructuredCondition( TGetParseNode& _aGetFunctor );
151 ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >
152 setCurrentColumns( EColumnType _eType, const ::rtl::Reference< ::connectivity::OSQLColumns >& _rCols );
154 //helper methods for mem_fun_t
155 inline bool implSetFilter(OUString _sFilter) { setFilter(_sFilter); return true;}
156 inline bool implSetHavingClause(OUString _sFilter) { setHavingClause(_sFilter); return true;}
158 /** returns the part of the seelect statement
159 @param _ePart
160 Which part should be returned.
161 @param _bWithKeyword
162 If <TRUE/> the keyword will be added too. Otherwise not.
163 @param _rIterator
164 The iterator to use.
166 @return
167 The part of the select statement.
169 OUString getSQLPart( SQLPart _ePart, ::connectivity::OSQLParseTreeIterator& _rIterator, sal_Bool _bWithKeyword );
171 /** retrieves the keyword for the given SQLPart
173 OUString getKeyword( SQLPart _ePart ) const;
175 /** sets a single "additive" clause, means a filter/groupby/having/order clause
177 void setSingleAdditiveClause( SQLPart _ePart, const OUString& _rClause );
179 /** composes a statement from m_aPureSelectSQL and the 4 usual clauses
181 OUString composeStatementFromParts( const ::std::vector< OUString >& _rParts );
183 /** return the name of the column in the *source* *table*.
185 That is, for (SELECT a AS b FROM t), it returns A or "t"."A", as appropriate.
187 Use e.g. for WHERE, GROUP BY and HAVING clauses.
189 @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.
191 OUString impl_getColumnRealName_throw(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column, bool bGroupBy);
193 /** return the name of the column in the *query*
195 That is, for (SELECT a AS b FROM t), it returns "b"
197 Use e.g. for ORDER BY clause.
199 @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.
201 OUString impl_getColumnName_throw(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column, bool bOrderBy);
203 protected:
204 virtual ~OSingleSelectQueryComposer();
205 public:
207 OSingleSelectQueryComposer( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess>& _xTableSupplier,
208 const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection,
209 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext>& _rContext);
212 void SAL_CALL disposing(void);
213 // ::com::sun::star::lang::XTypeProvider
214 DECLARE_TYPEPROVIDER( );
216 // com::sun::star::lang::XUnoTunnel
217 virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) throw(::com::sun::star::uno::RuntimeException);
218 // ::com::sun::star::uno::XInterface
219 DECLARE_XINTERFACE( )
221 // XServiceInfo
222 DECLARE_SERVICE_INFO();
224 DECLARE_PROPERTYCONTAINER_DEFAULTS();
226 // ::com::sun::star::sdb::XSingleSelectQueryComposer
227 virtual OUString SAL_CALL getElementaryQuery() throw (::com::sun::star::uno::RuntimeException);
228 virtual void SAL_CALL setElementaryQuery( const OUString& _rElementary ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
229 virtual void SAL_CALL setFilter( const OUString& filter ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
230 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);
231 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::uno::RuntimeException);
232 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);
233 virtual void SAL_CALL setGroup( const OUString& group ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
234 virtual void SAL_CALL setHavingClause( const OUString& filter ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
235 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);
236 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::uno::RuntimeException);
237 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);
238 virtual void SAL_CALL setOrder( const OUString& order ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
240 // XSingleSelectQueryAnalyzer
241 virtual OUString SAL_CALL getQuery( ) throw (::com::sun::star::uno::RuntimeException);
242 virtual void SAL_CALL setQuery( const OUString& command ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
243 virtual void SAL_CALL setCommand( const OUString& command,sal_Int32 CommandType ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
244 virtual OUString SAL_CALL getFilter( ) throw (::com::sun::star::uno::RuntimeException);
245 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);
246 virtual OUString SAL_CALL getGroup( ) throw (::com::sun::star::uno::RuntimeException);
247 virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess > SAL_CALL getGroupColumns( ) throw (::com::sun::star::uno::RuntimeException);
248 virtual OUString SAL_CALL getHavingClause( ) throw (::com::sun::star::uno::RuntimeException);
249 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);
250 virtual OUString SAL_CALL getOrder( ) throw (::com::sun::star::uno::RuntimeException);
251 virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess > SAL_CALL getOrderColumns( ) throw (::com::sun::star::uno::RuntimeException);
252 virtual OUString SAL_CALL getQueryWithSubstitution( ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException);
254 // XColumnsSupplier
255 virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getColumns( ) throw(::com::sun::star::uno::RuntimeException);
256 // XTablesSupplier
257 virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getTables( ) throw(::com::sun::star::uno::RuntimeException);
258 // XParametersSupplier
259 virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess > SAL_CALL getParameters( ) throw(::com::sun::star::uno::RuntimeException);
262 #endif // DBACCESS_CORE_API_SINGLESELECTQUERYCOMPOSER_HXX
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */