1*2e2212a7SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*2e2212a7SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*2e2212a7SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*2e2212a7SAndrew Rist * distributed with this work for additional information 6*2e2212a7SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*2e2212a7SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*2e2212a7SAndrew Rist * "License"); you may not use this file except in compliance 9*2e2212a7SAndrew Rist * with the License. You may obtain a copy of the License at 10*2e2212a7SAndrew Rist * 11*2e2212a7SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*2e2212a7SAndrew Rist * 13*2e2212a7SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*2e2212a7SAndrew Rist * software distributed under the License is distributed on an 15*2e2212a7SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2e2212a7SAndrew Rist * KIND, either express or implied. See the License for the 17*2e2212a7SAndrew Rist * specific language governing permissions and limitations 18*2e2212a7SAndrew Rist * under the License. 19*2e2212a7SAndrew Rist * 20*2e2212a7SAndrew Rist *************************************************************/ 21*2e2212a7SAndrew Rist 22*2e2212a7SAndrew Rist 23cdf0e10cSrcweir #ifndef DBACCESS_CORE_API_SINGLESELECTQUERYCOMPOSER_HXX 24cdf0e10cSrcweir #define DBACCESS_CORE_API_SINGLESELECTQUERYCOMPOSER_HXX 25cdf0e10cSrcweir 26cdf0e10cSrcweir #include <com/sun/star/sdb/XParametersSupplier.hpp> 27cdf0e10cSrcweir #include <com/sun/star/sdbcx/XTablesSupplier.hpp> 28cdf0e10cSrcweir #include <com/sun/star/sdbcx/XColumnsSupplier.hpp> 29cdf0e10cSrcweir #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp> 30cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 31cdf0e10cSrcweir #include <com/sun/star/script/XTypeConverter.hpp> 32cdf0e10cSrcweir #include <cppuhelper/implbase5.hxx> 33cdf0e10cSrcweir #include <connectivity/sqliterator.hxx> 34cdf0e10cSrcweir #include <connectivity/sqlparse.hxx> 35cdf0e10cSrcweir #include "apitools.hxx" 36cdf0e10cSrcweir #include <comphelper/broadcasthelper.hxx> 37cdf0e10cSrcweir #include <comphelper/uno3.hxx> 38cdf0e10cSrcweir #include <comphelper/proparrhlp.hxx> 39cdf0e10cSrcweir #include <comphelper/propertycontainer.hxx> 40cdf0e10cSrcweir #include <comphelper/componentcontext.hxx> 41cdf0e10cSrcweir 42cdf0e10cSrcweir #include <memory> 43cdf0e10cSrcweir 44cdf0e10cSrcweir namespace com { namespace sun { namespace star { namespace util { 45cdf0e10cSrcweir class XNumberFormatsSupplier; 46cdf0e10cSrcweir class XNumberFormatter; 47cdf0e10cSrcweir }}}} 48cdf0e10cSrcweir 49cdf0e10cSrcweir namespace dbaccess 50cdf0e10cSrcweir { 51cdf0e10cSrcweir typedef ::cppu::ImplHelper5< ::com::sun::star::sdb::XSingleSelectQueryComposer, 52cdf0e10cSrcweir ::com::sun::star::sdb::XParametersSupplier, 53cdf0e10cSrcweir ::com::sun::star::sdbcx::XColumnsSupplier, 54cdf0e10cSrcweir ::com::sun::star::sdbcx::XTablesSupplier, 55cdf0e10cSrcweir ::com::sun::star::lang::XServiceInfo > OSingleSelectQueryComposer_BASE; 56cdf0e10cSrcweir 57cdf0e10cSrcweir class OPrivateColumns; 58cdf0e10cSrcweir class OPrivateTables; 59cdf0e10cSrcweir 60cdf0e10cSrcweir class OSingleSelectQueryComposer : public ::comphelper::OMutexAndBroadcastHelper 61cdf0e10cSrcweir ,public OSubComponent 62cdf0e10cSrcweir ,public ::comphelper::OPropertyContainer 63cdf0e10cSrcweir ,public ::comphelper::OPropertyArrayUsageHelper < OSingleSelectQueryComposer > 64cdf0e10cSrcweir ,public OSingleSelectQueryComposer_BASE 65cdf0e10cSrcweir { 66cdf0e10cSrcweir enum SQLPart 67cdf0e10cSrcweir { 68cdf0e10cSrcweir Where = 0, // the 0 is important, as it will be used as index into arrays 69cdf0e10cSrcweir Group, 70cdf0e10cSrcweir Having, 71cdf0e10cSrcweir Order, 72cdf0e10cSrcweir 73cdf0e10cSrcweir SQLPartCount 74cdf0e10cSrcweir }; incSQLPart(SQLPart & e)75cdf0e10cSrcweir inline void incSQLPart( SQLPart& e ) { e = (SQLPart)(1 + (size_t)e); } 76cdf0e10cSrcweir enum EColumnType 77cdf0e10cSrcweir { 78cdf0e10cSrcweir SelectColumns = 0, 79cdf0e10cSrcweir GroupByColumns = 1, 80cdf0e10cSrcweir OrderColumns = 2, 81cdf0e10cSrcweir ParameterColumns = 3 82cdf0e10cSrcweir }; 83cdf0e10cSrcweir typedef ::std::const_mem_fun_t< const ::connectivity::OSQLParseNode*, ::connectivity::OSQLParseTreeIterator > 84cdf0e10cSrcweir TGetParseNode; 85cdf0e10cSrcweir ::connectivity::OSQLParser m_aSqlParser; 86cdf0e10cSrcweir ::connectivity::OSQLParseTreeIterator m_aSqlIterator; // the iterator for the complete statement 87cdf0e10cSrcweir ::connectivity::OSQLParseTreeIterator m_aAdditiveIterator; // the iterator for the "additive statement" (means without the clauses of the elementary statement) 88cdf0e10cSrcweir ::std::vector<OPrivateColumns*> m_aColumnsCollection; // used for columns and parameters of old queries 89cdf0e10cSrcweir ::std::vector<OPrivateTables*> m_aTablesCollection; 90cdf0e10cSrcweir 91cdf0e10cSrcweir ::std::vector< ::rtl::OUString > m_aElementaryParts; // the filter/groupby/having/order of the elementary statement 92cdf0e10cSrcweir 93cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> m_xConnection; 94cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> m_xMetaData; 95cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess> m_xConnectionTables; 96cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess> m_xConnectionQueries; 97cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatsSupplier > m_xNumberFormatsSupplier; 98cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess> m_xColumns; 99cdf0e10cSrcweir ::comphelper::ComponentContext m_aContext; 100cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::script::XTypeConverter > m_xTypeConverter; 101cdf0e10cSrcweir 102cdf0e10cSrcweir ::std::vector<OPrivateColumns*> m_aCurrentColumns; 103cdf0e10cSrcweir OPrivateTables* m_pTables; // currently used tables 104cdf0e10cSrcweir 105cdf0e10cSrcweir ::rtl::OUString m_aPureSelectSQL; // the pure select statement, without filter/order/groupby/having 106cdf0e10cSrcweir ::rtl::OUString m_sDecimalSep; 107cdf0e10cSrcweir ::rtl::OUString m_sCommand; 108cdf0e10cSrcweir ::com::sun::star::lang::Locale m_aLocale; 109cdf0e10cSrcweir sal_Int32 m_nBoolCompareMode; // how to compare bool values 110cdf0e10cSrcweir sal_Int32 m_nCommandType; 111cdf0e10cSrcweir 112cdf0e10cSrcweir // <properties> 113cdf0e10cSrcweir ::rtl::OUString m_sOrignal; 114cdf0e10cSrcweir // </properties> 115cdf0e10cSrcweir 116cdf0e10cSrcweir 117cdf0e10cSrcweir sal_Bool setORCriteria(::connectivity::OSQLParseNode* pCondition, ::connectivity::OSQLParseTreeIterator& _rIterator, 118cdf0e10cSrcweir ::std::vector< ::std::vector < ::com::sun::star::beans::PropertyValue > >& rFilters, const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const; 119cdf0e10cSrcweir sal_Bool setANDCriteria(::connectivity::OSQLParseNode* pCondition, ::connectivity::OSQLParseTreeIterator& _rIterator, 120cdf0e10cSrcweir ::std::vector < ::com::sun::star::beans::PropertyValue > & rFilters, const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const; 121cdf0e10cSrcweir sal_Bool setComparsionPredicate(::connectivity::OSQLParseNode* pCondition, ::connectivity::OSQLParseTreeIterator& _rIterator, 122cdf0e10cSrcweir ::std::vector < ::com::sun::star::beans::PropertyValue > & rFilters, const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter) const; 123cdf0e10cSrcweir 124cdf0e10cSrcweir ::rtl::OUString getColumnName(::connectivity::OSQLParseNode* pColumnRef,::connectivity::OSQLParseTreeIterator& _rIterator) const; 125cdf0e10cSrcweir ::rtl::OUString getTableAlias(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column ) const; 126cdf0e10cSrcweir sal_Int32 getPredicateType(::connectivity::OSQLParseNode * _pPredicate) const; 127cdf0e10cSrcweir // clears all Columns,Parameters and tables and insert it to their vectors 128cdf0e10cSrcweir void clearCurrentCollections(); 129cdf0e10cSrcweir // clears the columns collection given by EColumnType 130cdf0e10cSrcweir void clearColumns( const EColumnType _eType ); 131cdf0e10cSrcweir 132cdf0e10cSrcweir /** retrieves a particular part of a statement 133cdf0e10cSrcweir @param _rIterator 134cdf0e10cSrcweir the iterator to use. 135cdf0e10cSrcweir */ 136cdf0e10cSrcweir ::rtl::OUString getStatementPart( TGetParseNode& _aGetFunctor, ::connectivity::OSQLParseTreeIterator& _rIterator ); 137cdf0e10cSrcweir void setQuery_Impl( const ::rtl::OUString& command ); 138cdf0e10cSrcweir 139cdf0e10cSrcweir void setConditionByColumn( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column 140cdf0e10cSrcweir , sal_Bool andCriteria 141cdf0e10cSrcweir ,::std::mem_fun1_t<bool,OSingleSelectQueryComposer,::rtl::OUString>& _aSetFunctor 142cdf0e10cSrcweir ,sal_Int32 filterOperator); 143cdf0e10cSrcweir 144cdf0e10cSrcweir /** getStructuredCondition returns the structured condition for the where or having clause 145cdf0e10cSrcweir @param _aGetFunctor 146cdf0e10cSrcweir A member function to get the correct parse node. 147cdf0e10cSrcweir 148cdf0e10cSrcweir @return 149cdf0e10cSrcweir The structured filter 150cdf0e10cSrcweir */ 151cdf0e10cSrcweir ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue > > 152cdf0e10cSrcweir getStructuredCondition( TGetParseNode& _aGetFunctor ); 153cdf0e10cSrcweir 154cdf0e10cSrcweir ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess > 155cdf0e10cSrcweir setCurrentColumns( EColumnType _eType, const ::vos::ORef< ::connectivity::OSQLColumns >& _rCols ); 156cdf0e10cSrcweir 157cdf0e10cSrcweir //helper methods for mem_fun_t implSetFilter(::rtl::OUString _sFilter)158cdf0e10cSrcweir inline bool implSetFilter(::rtl::OUString _sFilter) { setFilter(_sFilter); return true;} implSetHavingClause(::rtl::OUString _sFilter)159cdf0e10cSrcweir inline bool implSetHavingClause(::rtl::OUString _sFilter) { setHavingClause(_sFilter); return true;} 160cdf0e10cSrcweir 161cdf0e10cSrcweir /** returns the part of the seelect statement 162cdf0e10cSrcweir @param _ePart 163cdf0e10cSrcweir Which part should be returned. 164cdf0e10cSrcweir @param _bWithKeyword 165cdf0e10cSrcweir If <TRUE/> the keyword will be added too. Otherwise not. 166cdf0e10cSrcweir @param _rIterator 167cdf0e10cSrcweir The iterator to use. 168cdf0e10cSrcweir 169cdf0e10cSrcweir @return 170cdf0e10cSrcweir The part of the select statement. 171cdf0e10cSrcweir */ 172cdf0e10cSrcweir ::rtl::OUString getSQLPart( SQLPart _ePart, ::connectivity::OSQLParseTreeIterator& _rIterator, sal_Bool _bWithKeyword ); 173cdf0e10cSrcweir 174cdf0e10cSrcweir /** retrieves the keyword for the given SQLPart 175cdf0e10cSrcweir */ 176cdf0e10cSrcweir ::rtl::OUString getKeyword( SQLPart _ePart ) const; 177cdf0e10cSrcweir 178cdf0e10cSrcweir /** sets a single "additive" clause, means a filter/groupby/having/order clause 179cdf0e10cSrcweir */ 180cdf0e10cSrcweir void setSingleAdditiveClause( SQLPart _ePart, const ::rtl::OUString& _rClause ); 181cdf0e10cSrcweir 182cdf0e10cSrcweir /** composes a statement from m_aPureSelectSQL and the 4 usual clauses 183cdf0e10cSrcweir */ 184cdf0e10cSrcweir ::rtl::OUString composeStatementFromParts( const ::std::vector< ::rtl::OUString >& _rParts ); 185cdf0e10cSrcweir 186cdf0e10cSrcweir /** return the name of the column. 187cdf0e10cSrcweir */ 188cdf0e10cSrcweir ::rtl::OUString impl_getColumnName_throw(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& column); 189cdf0e10cSrcweir 190cdf0e10cSrcweir protected: 191cdf0e10cSrcweir virtual ~OSingleSelectQueryComposer(); 192cdf0e10cSrcweir public: 193cdf0e10cSrcweir 194cdf0e10cSrcweir OSingleSelectQueryComposer( const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess>& _xTableSupplier, 195cdf0e10cSrcweir const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection>& _xConnection, 196cdf0e10cSrcweir const ::comphelper::ComponentContext& _rContext); 197cdf0e10cSrcweir 198cdf0e10cSrcweir 199cdf0e10cSrcweir void SAL_CALL disposing(void); 200cdf0e10cSrcweir // ::com::sun::star::lang::XTypeProvider 201cdf0e10cSrcweir DECLARE_TYPEPROVIDER( ); 202cdf0e10cSrcweir 203cdf0e10cSrcweir // com::sun::star::lang::XUnoTunnel 204cdf0e10cSrcweir virtual sal_Int64 SAL_CALL getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& aIdentifier ) throw(::com::sun::star::uno::RuntimeException); 205cdf0e10cSrcweir // ::com::sun::star::uno::XInterface 206cdf0e10cSrcweir DECLARE_XINTERFACE( ) 207cdf0e10cSrcweir 208cdf0e10cSrcweir // XServiceInfo 209cdf0e10cSrcweir DECLARE_SERVICE_INFO(); 210cdf0e10cSrcweir 211cdf0e10cSrcweir DECLARE_PROPERTYCONTAINER_DEFAULTS(); 212cdf0e10cSrcweir 213cdf0e10cSrcweir // ::com::sun::star::sdb::XSingleSelectQueryComposer 214cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getElementaryQuery() throw (::com::sun::star::uno::RuntimeException); 215cdf0e10cSrcweir virtual void SAL_CALL setElementaryQuery( const ::rtl::OUString& _rElementary ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); 216cdf0e10cSrcweir virtual void SAL_CALL setFilter( const ::rtl::OUString& filter ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); 217cdf0e10cSrcweir 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); 218cdf0e10cSrcweir 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); 219cdf0e10cSrcweir 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); 220cdf0e10cSrcweir virtual void SAL_CALL setGroup( const ::rtl::OUString& group ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); 221cdf0e10cSrcweir virtual void SAL_CALL setHavingClause( const ::rtl::OUString& filter ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); 222cdf0e10cSrcweir 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); 223cdf0e10cSrcweir 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); 224cdf0e10cSrcweir 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); 225cdf0e10cSrcweir virtual void SAL_CALL setOrder( const ::rtl::OUString& order ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); 226cdf0e10cSrcweir 227cdf0e10cSrcweir // XSingleSelectQueryAnalyzer 228cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getQuery( ) throw (::com::sun::star::uno::RuntimeException); 229cdf0e10cSrcweir virtual void SAL_CALL setQuery( const ::rtl::OUString& command ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); 230cdf0e10cSrcweir virtual void SAL_CALL setCommand( const ::rtl::OUString& command,sal_Int32 CommandType ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); 231cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getFilter( ) throw (::com::sun::star::uno::RuntimeException); 232cdf0e10cSrcweir 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); 233cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getGroup( ) throw (::com::sun::star::uno::RuntimeException); 234cdf0e10cSrcweir virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess > SAL_CALL getGroupColumns( ) throw (::com::sun::star::uno::RuntimeException); 235cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getHavingClause( ) throw (::com::sun::star::uno::RuntimeException); 236cdf0e10cSrcweir 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); 237cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getOrder( ) throw (::com::sun::star::uno::RuntimeException); 238cdf0e10cSrcweir virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess > SAL_CALL getOrderColumns( ) throw (::com::sun::star::uno::RuntimeException); 239cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getQueryWithSubstitution( ) throw (::com::sun::star::sdbc::SQLException, ::com::sun::star::uno::RuntimeException); 240cdf0e10cSrcweir 241cdf0e10cSrcweir // XColumnsSupplier 242cdf0e10cSrcweir virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getColumns( ) throw(::com::sun::star::uno::RuntimeException); 243cdf0e10cSrcweir // XTablesSupplier 244cdf0e10cSrcweir virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > SAL_CALL getTables( ) throw(::com::sun::star::uno::RuntimeException); 245cdf0e10cSrcweir // XParametersSupplier 246cdf0e10cSrcweir virtual ::com::sun::star::uno::Reference< ::com::sun::star::container::XIndexAccess > SAL_CALL getParameters( ) throw(::com::sun::star::uno::RuntimeException); 247cdf0e10cSrcweir }; 248cdf0e10cSrcweir } 249cdf0e10cSrcweir #endif // DBACCESS_CORE_API_SINGLESELECTQUERYCOMPOSER_HXX 250