Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #ifndef INCLUDED_DBACCESS_SOURCE_CORE_INC_SINGLESELECTQUERYCOMPOSER_HXX
20 : #define INCLUDED_DBACCESS_SOURCE_CORE_INC_SINGLESELECTQUERYCOMPOSER_HXX
21 :
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>
37 :
38 : namespace com { namespace sun { namespace star { namespace util {
39 : class XNumberFormatsSupplier;
40 : class XNumberFormatter;
41 : }}}}
42 :
43 : namespace dbaccess
44 : {
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;
50 :
51 : class OPrivateColumns;
52 : class OPrivateTables;
53 :
54 : class OSingleSelectQueryComposer : public ::comphelper::OMutexAndBroadcastHelper
55 : ,public OSubComponent
56 : ,public ::comphelper::OPropertyContainer
57 : ,public ::comphelper::OPropertyArrayUsageHelper < OSingleSelectQueryComposer >
58 : ,public OSingleSelectQueryComposer_BASE
59 : {
60 : enum SQLPart
61 : {
62 : Where = 0, // the 0 is important, as it will be used as index into arrays
63 : Group,
64 : Having,
65 : Order,
66 :
67 : SQLPartCount
68 : };
69 0 : inline void incSQLPart( SQLPart& e ) { e = (SQLPart)(1 + (size_t)e); }
70 : enum EColumnType
71 : {
72 : SelectColumns = 0,
73 : GroupByColumns = 1,
74 : OrderColumns = 2,
75 : ParameterColumns = 3
76 : };
77 : typedef ::std::const_mem_fun_t< const ::connectivity::OSQLParseNode*, ::connectivity::OSQLParseTreeIterator >
78 : TGetParseNode;
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;
85 :
86 : ::std::vector< OUString > m_aElementaryParts; // the filter/groupby/having/order of the elementary statement
87 :
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;
96 :
97 : ::std::vector<OPrivateColumns*> m_aCurrentColumns;
98 : OPrivateTables* m_pTables; // currently used tables
99 :
100 : OUString m_aPureSelectSQL; // the pure select statement, without filter/order/groupby/having
101 : OUString m_sDecimalSep;
102 : OUString m_sCommand;
103 : ::com::sun::star::lang::Locale m_aLocale;
104 : sal_Int32 m_nBoolCompareMode; // how to compare bool values
105 : sal_Int32 m_nCommandType;
106 :
107 : // <properties>
108 : OUString m_sOrignal;
109 : // </properties>
110 :
111 :
112 : sal_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 : sal_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 : sal_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;
118 :
119 : OUString getColumnName(::connectivity::OSQLParseNode* pColumnRef,::connectivity::OSQLParseTreeIterator& _rIterator) const;
120 : OUString getTableAlias(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column ) const;
121 : sal_Int32 getPredicateType(::connectivity::OSQLParseNode * _pPredicate) const;
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 );
126 :
127 : /** retrieves a particular part of a statement
128 : @param _rIterator
129 : the iterator to use.
130 : */
131 : OUString getStatementPart( TGetParseNode& _aGetFunctor, ::connectivity::OSQLParseTreeIterator& _rIterator );
132 : void setQuery_Impl( const OUString& command );
133 :
134 : void setConditionByColumn( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column
135 : , sal_Bool andCriteria
136 : ,::std::mem_fun1_t<bool,OSingleSelectQueryComposer,const OUString& >& _aSetFunctor
137 : ,sal_Int32 filterOperator);
138 :
139 : /** getStructuredCondition returns the structured condition for the where or having clause
140 : @param _aGetFunctor
141 : A member function to get the correct parse node.
142 :
143 : @return
144 : The structured filter
145 : */
146 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > >
147 : getStructuredCondition( TGetParseNode& _aGetFunctor );
148 :
149 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess >
150 : setCurrentColumns( EColumnType _eType, const ::rtl::Reference< ::connectivity::OSQLColumns >& _rCols );
151 :
152 : //helper methods for mem_fun_t
153 0 : inline bool implSetFilter(const OUString& _sFilter) { setFilter(_sFilter); return true;}
154 0 : inline bool implSetHavingClause(const OUString& _sFilter) { setHavingClause(_sFilter); return true;}
155 :
156 : /** returns the part of the seelect statement
157 : @param _ePart
158 : Which part should be returned.
159 : @param _bWithKeyword
160 : If <TRUE/> the keyword will be added too. Otherwise not.
161 : @param _rIterator
162 : The iterator to use.
163 :
164 : @return
165 : The part of the select statement.
166 : */
167 : OUString getSQLPart( SQLPart _ePart, ::connectivity::OSQLParseTreeIterator& _rIterator, sal_Bool _bWithKeyword );
168 :
169 : /** retrieves the keyword for the given SQLPart
170 : */
171 : OUString getKeyword( SQLPart _ePart ) const;
172 :
173 : /** sets a single "additive" clause, means a filter/groupby/having/order clause
174 : */
175 : void setSingleAdditiveClause( SQLPart _ePart, const OUString& _rClause );
176 :
177 : /** composes a statement from m_aPureSelectSQL and the 4 usual clauses
178 : */
179 : OUString composeStatementFromParts( const ::std::vector< OUString >& _rParts );
180 :
181 : /** return the name of the column in the *source* *table*.
182 :
183 : That is, for (SELECT a AS b FROM t), it returns A or "t"."A", as appropriate.
184 :
185 : Use e.g. for WHERE, GROUP BY and HAVING clauses.
186 :
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.
188 : */
189 : OUString impl_getColumnRealName_throw(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column, bool bGroupBy);
190 :
191 : /** return the name of the column in the *query*
192 :
193 : That is, for (SELECT a AS b FROM t), it returns "b"
194 :
195 : Use e.g. for ORDER BY clause.
196 :
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.
198 : */
199 : OUString impl_getColumnName_throw(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column, bool bOrderBy);
200 :
201 : protected:
202 : virtual ~OSingleSelectQueryComposer();
203 : public:
204 :
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);
208 :
209 :
210 : void SAL_CALL disposing(void) SAL_OVERRIDE;
211 :
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;
216 :
217 : // ::com::sun::star::uno::XInterface
218 : DECLARE_XINTERFACE( )
219 :
220 : // XServiceInfo
221 : DECLARE_SERVICE_INFO();
222 :
223 : DECLARE_PROPERTYCONTAINER_DEFAULTS();
224 :
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::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::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;
238 :
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;
252 :
253 : // XColumnsSupplier
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;
255 : // XTablesSupplier
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;
259 : };
260 : }
261 : #endif // INCLUDED_DBACCESS_SOURCE_CORE_INC_SINGLESELECTQUERYCOMPOSER_HXX
262 :
263 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|