LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/include/connectivity - sqliterator.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 12 15 80.0 %
Date: 2013-07-09 Functions: 9 11 81.8 %
Legend: Lines: hit not hit

          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 _CONNECTIVITY_PARSE_SQLITERATOR_HXX_
      20             : #define _CONNECTIVITY_PARSE_SQLITERATOR_HXX_
      21             : 
      22             : #include "connectivity/dbtoolsdllapi.hxx"
      23             : #include "connectivity/sqlnode.hxx"
      24             : #include <connectivity/IParseContext.hxx>
      25             : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
      26             : #include <com/sun/star/sdbc/DataType.hpp>
      27             : #include <com/sun/star/sdbc/SQLWarning.hpp>
      28             : #include <com/sun/star/beans/XPropertySet.hpp>
      29             : #include "connectivity/CommonTools.hxx"
      30             : #include <rtl/ref.hxx>
      31             : #include <cppuhelper/weak.hxx>
      32             : 
      33             : #include <map>
      34             : #include <memory>
      35             : #include <vector>
      36             : 
      37             : namespace connectivity
      38             : {
      39             : 
      40             :     class OSQLParseNode;
      41             :     class OSQLParser;
      42             : 
      43             :     typedef ::std::pair<const OSQLParseNode*,const OSQLParseNode* > TNodePair;
      44             : 
      45             :     enum OSQLStatementType {
      46             :         SQL_STATEMENT_UNKNOWN,
      47             :         SQL_STATEMENT_SELECT,
      48             :         SQL_STATEMENT_INSERT,
      49             :         SQL_STATEMENT_UPDATE,
      50             :         SQL_STATEMENT_DELETE,
      51             :         SQL_STATEMENT_ODBC_CALL,
      52             :         SQL_STATEMENT_CREATE_TABLE
      53             :     };
      54             : 
      55             :     struct OSQLParseTreeIteratorImpl;
      56             : 
      57             :     class OOO_DLLPUBLIC_DBTOOLS OSQLParseTreeIterator
      58             :     {
      59             :     private:
      60             :         ::com::sun::star::sdbc::SQLException            m_aErrors;          // conatins the error while iterating through the statement
      61             :         const OSQLParseNode*                            m_pParseTree;       // current ParseTree
      62             :         const OSQLParser&                               m_rParser;          // if set used for general error messages from the context
      63             :         OSQLStatementType                               m_eStatementType;
      64             :         ::rtl::Reference<OSQLColumns>                       m_aSelectColumns;   // all columns from the Select clause
      65             :         ::rtl::Reference<OSQLColumns>                       m_aParameters;      // all parameters
      66             :         ::rtl::Reference<OSQLColumns>                       m_aGroupColumns;    // the group by columns
      67             :         ::rtl::Reference<OSQLColumns>                       m_aOrderColumns;    // the order by columns
      68             :         ::rtl::Reference<OSQLColumns>                       m_aCreateColumns;   // the columns for Create table clause
      69             : 
      70             :         ::std::auto_ptr< OSQLParseTreeIteratorImpl >    m_pImpl;
      71             : 
      72             :         void                traverseParameter(const OSQLParseNode* _pParseNode,const OSQLParseNode* _pColumnRef,const OUString& _aColumnName, OUString& _aTableRange, const OUString& _rColumnAlias);
      73             :         // inserts a table into the map
      74             :         void                traverseOneTableName( OSQLTables& _rTables,const OSQLParseNode * pTableName, const OUString & rTableRange );
      75             :         void                traverseSearchCondition(OSQLParseNode * pSearchCondition);
      76             :         void                traverseOnePredicate(
      77             :                                                 OSQLParseNode * pColumnRef,
      78             :                                                 OUString& aValue,
      79             :                                                 OSQLParseNode * pParameter);
      80             :         void traverseByColumnNames(const OSQLParseNode* pSelectNode,sal_Bool _bOrder);
      81             :         void                traverseParameters(const OSQLParseNode* pSelectNode);
      82             : 
      83             :         const OSQLParseNode*    getTableNode( OSQLTables& _rTables, const OSQLParseNode* pTableRef, OUString& aTableRange );
      84             :         void                    getQualified_join( OSQLTables& _rTables, const OSQLParseNode *pTableRef, OUString& aTableRange );
      85             :         void                    getSelect_statement(OSQLTables& _rTables,const OSQLParseNode* pSelect);
      86             :         OUString         getUniqueColumnName(const OUString & rColumnName)    const;
      87             : 
      88             :         /** finds the column with a given name, belonging to a given table, in a given tables collection
      89             :             @param  _rTables
      90             :                 the tables collection to look in
      91             :             @param  rColumnName
      92             :                 the column name to look for
      93             :             @param  rTableRange
      94             :                 the table alias name; if empty, look in all tables
      95             :             @return
      96             :                 the desired column object, or <NULL/> if no such column could be found
      97             :         */
      98             :         static ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > findColumn(
      99             :             const OSQLTables& _rTables, const OUString & rColumnName, OUString & rTableRange );
     100             : 
     101             :         /** finds a column with a given name, belonging to a given table
     102             :             @param  rColumnName
     103             :                 the column name to look for
     104             :             @param  rTableRange
     105             :                     the table alias name; if empty, look in all tables
     106             :             @param  _bLookInSubTables
     107             :                 <TRUE/> if and only if not only our direct tables, but also our sub tables (from sub selects)
     108             :                 should be searched
     109             :             @return
     110             :         */
     111             :         ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > findColumn(
     112             :             const OUString & rColumnName, OUString & rTableRange, bool _bLookInSubTables );
     113             : 
     114             :       protected:
     115             :         void setSelectColumnName(::rtl::Reference<OSQLColumns>& _rColumns,const OUString & rColumnName,const OUString & rColumnAlias, const OUString & rTableRange,sal_Bool bFkt=sal_False,sal_Int32 _nType = com::sun::star::sdbc::DataType::VARCHAR,sal_Bool bAggFkt=sal_False);
     116             :         void appendColumns(::rtl::Reference<OSQLColumns>& _rColumns,const OUString& _rTableAlias,const OSQLTable& _rTable);
     117             :         // Other member variables that should be available in the "set" functions
     118             :         // can be defined in the derived class. They can be initialized
     119             :         // in its constructor and, after the "traverse" routines have been used,
     120             :         // they can be queried using other functions.
     121             : 
     122             : 
     123             :       private:
     124             :         OSQLParseTreeIterator();                                        // never implemented
     125             :         OSQLParseTreeIterator(const OSQLParseTreeIterator & rIter);     // never implemented
     126             : 
     127             :       public:
     128             :         OSQLParseTreeIterator(
     129             :             const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
     130             :             const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess >& _rxTables,
     131             :             const OSQLParser& _rParser,
     132             :             const OSQLParseNode* pRoot = NULL );
     133             :         ~OSQLParseTreeIterator();
     134             : 
     135           2 :         inline static void * SAL_CALL operator new( size_t nSize ) SAL_THROW(())
     136           2 :             { return ::rtl_allocateMemory( nSize ); }
     137             :         inline static void * SAL_CALL operator new( size_t,void* _pHint ) SAL_THROW(())
     138             :             { return _pHint; }
     139           0 :         inline static void SAL_CALL operator delete( void * pMem ) SAL_THROW(())
     140           0 :             { ::rtl_freeMemory( pMem ); }
     141             :         inline static void SAL_CALL operator delete( void *,void* ) SAL_THROW(())
     142             :             {  }
     143             : 
     144             :         void dispose();
     145             :         bool isCaseSensitive() const;
     146             :         // The parse tree to be analysed/traversed:
     147             :         // If NULL is passed, the current parse tree will be deleted and the error status cleared.
     148             :         void setParseTree(const OSQLParseNode * pNewParseTree);
     149             : //      void setParser(const OSQLParser* _pParser) { m_pParser = _pParser; }
     150         399 :         const OSQLParseNode * getParseTree() const { return m_pParseTree; };
     151             : 
     152             :         // subtrees in case of a select statement
     153             :         const OSQLParseNode* getWhereTree() const;
     154             :         const OSQLParseNode* getOrderTree() const;
     155             :         const OSQLParseNode* getGroupByTree() const;
     156             :         const OSQLParseNode* getHavingTree() const;
     157             : 
     158             :         const OSQLParseNode* getSimpleWhereTree() const;
     159             :         const OSQLParseNode* getSimpleOrderTree() const;
     160             :         const OSQLParseNode* getSimpleGroupByTree() const;
     161             :         const OSQLParseNode* getSimpleHavingTree() const;
     162             : 
     163             :         /** returns the errors which occurred during parsing.
     164             : 
     165             :             The returned object contains a chain (via SQLException::NextException) of SQLExceptions.
     166             :         */
     167           0 :         inline const ::com::sun::star::sdbc::SQLException&   getErrors() const { return m_aErrors; }
     168         868 :         inline bool hasErrors() const { return !m_aErrors.Message.isEmpty(); }
     169             : 
     170             :         // statement type (already set in setParseTree):
     171        7280 :         OSQLStatementType getStatementType() const { return m_eStatementType; }
     172             : 
     173             :         /** traverses the complete statement tree, and fills all our data with
     174             :             the information obatined during traversal.
     175             : 
     176             :             Implemented by calling the single traverse* methods in the proper
     177             :             order (depending on the statement type).
     178             :         */
     179             :         void traverseAll();
     180             : 
     181             :         enum TraversalParts
     182             :         {
     183             :             Parameters      = 0x0001,
     184             :             TableNames      = 0x0002,
     185             :             SelectColumns   = 0x0006,   // note that this includes TableNames. No SelectColumns without TableNames
     186             : 
     187             :             // Those are not implemented currently
     188             :             // GroupColumns    = 0x0008,
     189             :             // OrderColumns    = 0x0010,
     190             :             // SelectColumns   = 0x0020,
     191             :             // CreateColumns   = 0x0040,
     192             : 
     193             :             All             = 0xFFFF
     194             :         };
     195             :         /** traverses selected parts of the statement tree, and fills our data with
     196             :             the information obtained during traversal
     197             : 
     198             :             @param _nIncludeMask
     199             :                 set of TraversalParts bits, specifying which information is to be collected.
     200             :                 Note TraversalParts is currently not
     201             :         */
     202             :         void traverseSome( sal_uInt32 _nIncludeMask );
     203             : 
     204             :         // The TableRangeMap contains all tables associated with the range name found first.
     205             :         const OSQLTables& getTables() const;
     206             : 
     207         246 :         ::rtl::Reference<OSQLColumns> getSelectColumns() const { return m_aSelectColumns;}
     208           1 :         ::rtl::Reference<OSQLColumns> getGroupColumns() const { return m_aGroupColumns;}
     209           2 :         ::rtl::Reference<OSQLColumns> getOrderColumns() const { return m_aOrderColumns;}
     210          87 :         ::rtl::Reference<OSQLColumns> getParameters()   const { return m_aParameters; }
     211             :         ::rtl::Reference<OSQLColumns> getCreateColumns() const { return m_aCreateColumns;}
     212             : 
     213             :         /** return the columname and the table range
     214             :             @param  _pColumnRef
     215             :                 The column ref parse node.
     216             :             @param  _rColumnName
     217             :                 The column name to be set.
     218             :             @param  _rTableRange
     219             :                 The table range to be set.
     220             :         */
     221             :         void getColumnRange(    const OSQLParseNode* _pColumnRef,
     222             :                                 OUString &_rColumnName,
     223             :                                 OUString& _rTableRange) const;
     224             : 
     225             :         /** retrieves a column's name, table range, and alias
     226             : 
     227             :             @param  _pColumnRef
     228             :                 The column_ref parse node.
     229             :             @param  _out_rColumnName
     230             :                 The column name to be set.
     231             :             @param  _out_rTableRange
     232             :                 The table range to be set.
     233             :             @param _out_rColumnAliasIfPresent
     234             :                 If the column specified by _pColumnRef is part of the select columns, and contains a column alias there,
     235             :                 this alias is returned here.
     236             :         */
     237             :         void getColumnRange(    const OSQLParseNode* _pColumnRef,
     238             :                                 OUString& _out_rColumnName,
     239             :                                 OUString& _out_rTableRange,
     240             :                                 OUString& _out_rColumnAliasIfPresent
     241             :                                 ) const;
     242             : 
     243             :         /** return the alias name of a column
     244             :             @param  _pDerivedColumn
     245             :                 The parse node where SQL_ISRULE(_pDerivedColumn,derived_column) must be true
     246             :             @return
     247             :                 The alias name of the column or an empty string.
     248             :         */
     249             :         static OUString getColumnAlias(const OSQLParseNode* _pDerivedColumn);
     250             : 
     251             :         /** return the columname and the table range
     252             :             @param  _pColumnRef
     253             :                 The column ref parse node.
     254             :             @param  _xMetaData
     255             :                 The database meta data.
     256             :             @param  _rColumnName
     257             :                 The column name to be set.
     258             :             @param  _rTableRange
     259             :                 The table range to be set.
     260             :         */
     261             :         static void getColumnRange( const OSQLParseNode* _pColumnRef,
     262             :                                     const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
     263             :                                     OUString &_rColumnName,
     264             :                                     OUString& _rTableRange);
     265             : 
     266             :         // empty if ambiguous
     267             :         sal_Bool getColumnTableRange(const OSQLParseNode* pNode, OUString &rTableRange) const;
     268             : 
     269             :         // return true when the tableNode is a rule like catalog_name, schema_name or table_name
     270             :         sal_Bool isTableNode(const OSQLParseNode* _pTableNode) const;
     271             : 
     272             :         // tries to find the correct type of the function
     273             :         sal_Int32 getFunctionReturnType(const OSQLParseNode* _pNode );
     274             : 
     275             :         // returns a lis of all joined columns
     276             :         ::std::vector< TNodePair >& getJoinConditions() const;
     277             : 
     278             :     private:
     279             :         /** traverses the list of table names, and filles _rTables
     280             :         */
     281             :         bool traverseTableNames( OSQLTables& _rTables );
     282             : 
     283             :         /// traverses columns in a SELECT statement
     284             :         bool traverseSelectColumnNames(const OSQLParseNode* pSelectNode);
     285             :         /// traverses columns in a CREATE TABLE statement
     286             :         void traverseCreateColumns(const OSQLParseNode* pSelectNode);
     287             : 
     288             :         bool traverseOrderByColumnNames(const OSQLParseNode* pSelectNode);
     289             :         bool traverseGroupByColumnNames(const OSQLParseNode* pSelectNode);
     290             : 
     291             :         bool traverseSelectionCriteria(const OSQLParseNode* pSelectNode);
     292             : 
     293             :     private:
     294             :         /** constructs a new iterator, which inherits some of the settings from a parent iterator
     295             :         */
     296             :         OSQLParseTreeIterator(
     297             :             const OSQLParseTreeIterator& _rParentIterator,
     298             :             const OSQLParser& _rParser,
     299             :             const OSQLParseNode* pRoot );
     300             : 
     301             :         /** creates a table object and inserts it into our tables collection
     302             : 
     303             :             only used when we're iterating through a CREATE TABLE statement
     304             :         */
     305             :         OSQLTable   impl_createTableObject(
     306             :             const OUString& rTableName, const OUString& rCatalogName, const OUString& rSchemaName );
     307             : 
     308             :         /** locates a record source (a table or query) with the given name
     309             :         */
     310             :         OSQLTable   impl_locateRecordSource(
     311             :             const OUString& _rComposedName
     312             :         );
     313             : 
     314             :         /** implementation for both traverseAll and traverseSome
     315             :         */
     316             :         void    impl_traverse( sal_uInt32 _nIncludeMask );
     317             : 
     318             :         /** retrieves the parameter columns of the given query
     319             :         */
     320             :         void    impl_getQueryParameterColumns( const OSQLTable& _rQuery );
     321             : 
     322             :         void setOrderByColumnName(const OUString & rColumnName, OUString & rTableRange, sal_Bool bAscending);
     323             :         void setGroupByColumnName(const OUString & rColumnName, OUString & rTableRange);
     324             : 
     325             :     private:
     326             :         /** appends an SQLException corresponding to the given error code to our error collection
     327             : 
     328             :             @param  _eError
     329             :                 the code of the error which occurred
     330             :             @param  _pReplaceToken1
     331             :                 if not <NULL/>, the first occurrence of '#' in the error message will be replaced
     332             :                 with the given token
     333             :             @param  _pReplaceToken2
     334             :                 if not <NULL/>, and if _rReplaceToken1 is not <NULL/>, the second occurrence of '#'
     335             :                 in the error message will be replaced with _rReplaceToken2
     336             :         */
     337             :         void impl_appendError( IParseContext::ErrorCode _eError,
     338             :             const OUString* _pReplaceToken1 = NULL, const OUString* _pReplaceToken2 = NULL );
     339             : 
     340             :         /** appends an SQLException corresponding to the given error code to our error collection
     341             :         */
     342             :         void impl_appendError( const ::com::sun::star::sdbc::SQLException& _rError );
     343             : 
     344             :         /** resets our errors
     345             :         */
     346         212 :         inline void impl_resetErrors()
     347             :         {
     348         212 :             m_aErrors = ::com::sun::star::sdbc::SQLException();
     349         212 :         }
     350             :         void impl_fillJoinConditions(const OSQLParseNode* i_pJoinCondition);
     351             :     };
     352             : }
     353             : 
     354             : #endif // _CONNECTIVITY_PARSE_SQLITERATOR_HXX_
     355             : 
     356             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10