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
< css::sdb::XSingleSelectQueryComposer
,
46 css::sdb::XParametersSupplier
,
47 css::sdbcx::XColumnsSupplier
,
48 css::sdbcx::XTablesSupplier
,
49 css::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 css::uno::Reference
< css::sdbc::XConnection
> m_xConnection
;
89 css::uno::Reference
< css::sdbc::XDatabaseMetaData
> m_xMetaData
;
90 css::uno::Reference
< css::container::XNameAccess
> m_xConnectionTables
;
91 css::uno::Reference
< css::container::XNameAccess
> m_xConnectionQueries
;
92 css::uno::Reference
< css::util::XNumberFormatsSupplier
> m_xNumberFormatsSupplier
;
93 css::uno::Reference
< css::uno::XComponentContext
> m_aContext
;
94 css::uno::Reference
< css::script::XTypeConverter
> m_xTypeConverter
;
96 ::std::vector
<OPrivateColumns
*> m_aCurrentColumns
;
97 OPrivateTables
* m_pTables
; // currently used tables
99 OUString m_aPureSelectSQL
; // the pure select statement, without filter/order/groupby/having
100 OUString m_sDecimalSep
;
102 css::lang::Locale m_aLocale
;
103 sal_Int32 m_nBoolCompareMode
; // how to compare bool values
104 sal_Int32 m_nCommandType
;
111 bool setORCriteria(::connectivity::OSQLParseNode
* pCondition
, ::connectivity::OSQLParseTreeIterator
& _rIterator
,
112 ::std::vector
< ::std::vector
< css::beans::PropertyValue
> >& rFilters
, const css::uno::Reference
< css::util::XNumberFormatter
> & xFormatter
) const;
113 bool setANDCriteria(::connectivity::OSQLParseNode
* pCondition
, ::connectivity::OSQLParseTreeIterator
& _rIterator
,
114 ::std::vector
< css::beans::PropertyValue
> & rFilters
, const css::uno::Reference
< css::util::XNumberFormatter
> & xFormatter
) const;
115 bool setComparsionPredicate(::connectivity::OSQLParseNode
* pCondition
, ::connectivity::OSQLParseTreeIterator
& _rIterator
,
116 ::std::vector
< css::beans::PropertyValue
> & rFilters
, const css::uno::Reference
< css::util::XNumberFormatter
> & xFormatter
) const;
118 static OUString
getColumnName(::connectivity::OSQLParseNode
* pColumnRef
,::connectivity::OSQLParseTreeIterator
& _rIterator
);
119 OUString
getTableAlias(const css::uno::Reference
< css::beans::XPropertySet
>& column
) const;
120 static sal_Int32
getPredicateType(::connectivity::OSQLParseNode
* _pPredicate
);
121 // clears all Columns,Parameters and tables and insert it to their vectors
122 void clearCurrentCollections();
123 // clears the columns collection given by EColumnType
124 void clearColumns( const EColumnType _eType
);
126 /** retrieves a particular part of a statement
130 OUString
getStatementPart( TGetParseNode
& _aGetFunctor
, ::connectivity::OSQLParseTreeIterator
& _rIterator
);
131 void setQuery_Impl( const OUString
& command
);
133 void setConditionByColumn( const css::uno::Reference
< css::beans::XPropertySet
>& column
135 ,::std::mem_fun1_t
<bool,OSingleSelectQueryComposer
,const OUString
& >& _aSetFunctor
136 ,sal_Int32 filterOperator
);
138 /** getStructuredCondition returns the structured condition for the where or having clause
140 A member function to get the correct parse node.
143 The structured filter
145 css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> >
146 getStructuredCondition( TGetParseNode
& _aGetFunctor
);
148 css::uno::Reference
< css::container::XIndexAccess
>
149 setCurrentColumns( EColumnType _eType
, const ::rtl::Reference
< ::connectivity::OSQLColumns
>& _rCols
);
151 //helper methods for mem_fun_t
152 inline bool implSetFilter(const OUString
& _sFilter
) { setFilter(_sFilter
); return true;}
153 inline bool implSetHavingClause(const OUString
& _sFilter
) { setHavingClause(_sFilter
); return true;}
155 /** returns the part of the select statement
157 Which part should be returned.
159 If <TRUE/> the keyword will be added too. Otherwise not.
164 The part of the select statement.
166 OUString
getSQLPart( SQLPart _ePart
, ::connectivity::OSQLParseTreeIterator
& _rIterator
, bool _bWithKeyword
);
168 /** retrieves the keyword for the given SQLPart
170 static OUString
getKeyword( SQLPart _ePart
);
172 /** sets a single "additive" clause, means a filter/groupby/having/order clause
174 void setSingleAdditiveClause( SQLPart _ePart
, const OUString
& _rClause
);
176 /** composes a statement from m_aPureSelectSQL and the 4 usual clauses
178 OUString
composeStatementFromParts( const ::std::vector
< OUString
>& _rParts
);
180 /** return the name of the column in the *source* *table*.
182 That is, for (SELECT a AS b FROM t), it returns A or "t"."A", as appropriate.
184 Use e.g. for WHERE, GROUP BY and HAVING clauses.
186 @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 OUString
impl_getColumnRealName_throw(const css::uno::Reference
< css::beans::XPropertySet
>& column
, bool bGroupBy
);
190 /** return the name of the column in the *query* for ORDER BY clause.
192 That is, for (SELECT a AS b FROM t), it returns "b"
194 Throws exception if trying to use an unrelated column and the database does not support that.
196 OUString
impl_getColumnNameOrderBy_throw(const css::uno::Reference
< css::beans::XPropertySet
>& column
);
199 virtual ~OSingleSelectQueryComposer() override
;
202 OSingleSelectQueryComposer( const css::uno::Reference
< css::container::XNameAccess
>& _xTableSupplier
,
203 const css::uno::Reference
< css::sdbc::XConnection
>& _xConnection
,
204 const css::uno::Reference
< css::uno::XComponentContext
>& _rContext
);
207 void SAL_CALL
disposing() override
;
209 virtual css::uno::Sequence
<css::uno::Type
> SAL_CALL
getTypes()
210 throw (css::uno::RuntimeException
, std::exception
) override
;
211 virtual css::uno::Sequence
<sal_Int8
> SAL_CALL
getImplementationId()
212 throw (css::uno::RuntimeException
, std::exception
) override
;
214 // css::uno::XInterface
215 DECLARE_XINTERFACE( )
218 DECLARE_SERVICE_INFO();
220 DECLARE_PROPERTYCONTAINER_DEFAULTS();
222 // css::sdb::XSingleSelectQueryComposer
223 virtual OUString SAL_CALL
getElementaryQuery() throw (css::uno::RuntimeException
, std::exception
) override
;
224 virtual void SAL_CALL
setElementaryQuery( const OUString
& _rElementary
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
225 virtual void SAL_CALL
setFilter( const OUString
& filter
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
226 virtual void SAL_CALL
setStructuredFilter( const css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> >& filter
) throw (css::sdbc::SQLException
, css::lang::IllegalArgumentException
, css::uno::RuntimeException
, std::exception
) override
;
227 virtual void SAL_CALL
appendFilterByColumn( const css::uno::Reference
< css::beans::XPropertySet
>& column
, sal_Bool andCriteria
,sal_Int32 filterOperator
) throw (css::sdbc::SQLException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
228 virtual void SAL_CALL
appendGroupByColumn( const css::uno::Reference
< css::beans::XPropertySet
>& column
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
229 virtual void SAL_CALL
setGroup( const OUString
& group
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
230 virtual void SAL_CALL
setHavingClause( const OUString
& filter
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
231 virtual void SAL_CALL
setStructuredHavingClause( const css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> >& filter
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
232 virtual void SAL_CALL
appendHavingClauseByColumn( const css::uno::Reference
< css::beans::XPropertySet
>& column
, sal_Bool andCriteria
,sal_Int32 filterOperator
) throw (css::sdbc::SQLException
, css::lang::WrappedTargetException
, css::uno::RuntimeException
, std::exception
) override
;
233 virtual void SAL_CALL
appendOrderByColumn( const css::uno::Reference
< css::beans::XPropertySet
>& column
, sal_Bool ascending
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
234 virtual void SAL_CALL
setOrder( const OUString
& order
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
236 // XSingleSelectQueryAnalyzer
237 virtual OUString SAL_CALL
getQuery( ) throw (css::uno::RuntimeException
, std::exception
) override
;
238 virtual void SAL_CALL
setQuery( const OUString
& command
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
239 virtual void SAL_CALL
setCommand( const OUString
& command
,sal_Int32 CommandType
) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
240 virtual OUString SAL_CALL
getFilter( ) throw (css::uno::RuntimeException
, std::exception
) override
;
241 virtual css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> > SAL_CALL
getStructuredFilter( ) throw (css::uno::RuntimeException
, std::exception
) override
;
242 virtual OUString SAL_CALL
getGroup( ) throw (css::uno::RuntimeException
, std::exception
) override
;
243 virtual css::uno::Reference
< css::container::XIndexAccess
> SAL_CALL
getGroupColumns( ) throw (css::uno::RuntimeException
, std::exception
) override
;
244 virtual OUString SAL_CALL
getHavingClause( ) throw (css::uno::RuntimeException
, std::exception
) override
;
245 virtual css::uno::Sequence
< css::uno::Sequence
< css::beans::PropertyValue
> > SAL_CALL
getStructuredHavingClause( ) throw (css::uno::RuntimeException
, std::exception
) override
;
246 virtual OUString SAL_CALL
getOrder( ) throw (css::uno::RuntimeException
, std::exception
) override
;
247 virtual css::uno::Reference
< css::container::XIndexAccess
> SAL_CALL
getOrderColumns( ) throw (css::uno::RuntimeException
, std::exception
) override
;
248 virtual OUString SAL_CALL
getQueryWithSubstitution( ) throw (css::sdbc::SQLException
, css::uno::RuntimeException
, std::exception
) override
;
251 virtual css::uno::Reference
< css::container::XNameAccess
> SAL_CALL
getColumns( ) throw(css::uno::RuntimeException
, std::exception
) override
;
253 virtual css::uno::Reference
< css::container::XNameAccess
> SAL_CALL
getTables( ) throw(css::uno::RuntimeException
, std::exception
) override
;
254 // XParametersSupplier
255 virtual css::uno::Reference
< css::container::XIndexAccess
> SAL_CALL
getParameters( ) throw(css::uno::RuntimeException
, std::exception
) override
;
258 #endif // INCLUDED_DBACCESS_SOURCE_CORE_INC_SINGLESELECTQUERYCOMPOSER_HXX
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */