LibreOffice Module connectivity (master) 1
sqliterator.hxx
Go to the documentation of this file.
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_CONNECTIVITY_SQLITERATOR_HXX
20#define INCLUDED_CONNECTIVITY_SQLITERATOR_HXX
21
24#include <com/sun/star/sdbc/DataType.hpp>
25#include <com/sun/star/sdbc/SQLException.hpp>
27#include <rtl/ref.hxx>
28
29#include <memory>
30#include <optional>
31#include <vector>
33
34namespace com::sun::star::sdbc { class XConnection; }
35namespace com::sun::star::beans { class XPropertySet; }
36
37namespace connectivity
38{
39 enum class TraversalParts
40 {
41 Parameters = 0x0001,
42 TableNames = 0x0002,
43 SelectColumns = 0x0006, // note that this includes TableNames. No SelectColumns without TableNames
44
45 // Those are not implemented currently
46 // GroupColumns = 0x0008,
47 // OrderColumns = 0x0010,
48 // SelectColumns = 0x0020,
49 // CreateColumns = 0x0040,
50
51 All = 0xFFFF
52 };
53}
54namespace o3tl
55{
56 template<> struct typed_flags<connectivity::TraversalParts> : is_typed_flags<connectivity::TraversalParts, 0xffff> {};
57}
58
59namespace connectivity
60{
61
62 class OSQLParseNode;
63 class OSQLParser;
64
65 typedef ::std::pair<const OSQLParseNode*,const OSQLParseNode* > TNodePair;
66
67 enum class OSQLStatementType {
68 Unknown,
69 Select,
70 Insert,
71 Update,
72 Delete,
75 };
76
77 struct OSQLParseTreeIteratorImpl;
78
80 {
81 private:
82 std::optional<css::sdbc::SQLException> m_xErrors; // contains the error while iterating through the statement
83 const OSQLParseNode* m_pParseTree; // current ParseTree
84 const OSQLParser& m_rParser; // if set used for general error messages from the context
86 ::rtl::Reference<OSQLColumns> m_aSelectColumns; // all columns from the Select clause
90 ::rtl::Reference<OSQLColumns> m_aCreateColumns; // the columns for Create table clause
91
92 ::std::unique_ptr< OSQLParseTreeIteratorImpl > m_pImpl;
93
94 void traverseParameter(const OSQLParseNode* _pParseNode,const OSQLParseNode* _pColumnRef,const OUString& _aColumnName, OUString& _aTableRange, const OUString& _rColumnAlias);
95 // inserts a table into the map
96 void traverseOneTableName( OSQLTables& _rTables,const OSQLParseNode * pTableName, const OUString & rTableRange );
97 void traverseSearchCondition(OSQLParseNode const * pSearchCondition);
98 void traverseOnePredicate(
99 OSQLParseNode const * pColumnRef,
100 OUString& aValue,
101 OSQLParseNode const * pParameter);
102 void traverseByColumnNames(const OSQLParseNode* pSelectNode, bool _bOrder);
103 void traverseParameters(const OSQLParseNode* pSelectNode);
104
105 const OSQLParseNode* getTableNode( OSQLTables& _rTables, const OSQLParseNode* pTableRef, OUString& aTableRange );
106 void getQualified_join( OSQLTables& _rTables, const OSQLParseNode *pTableRef, OUString& aTableRange );
107 void getSelect_statement(OSQLTables& _rTables,const OSQLParseNode* pSelect);
108 // get all the column names of m_aSelectColumns and return in a vector sorted by a UStringMixLess that's constructed from
109 // isCaseSensitive()
110 std::vector<OUString> getSelectColumnNames() const;
111 // rColumnNames is expected to be sorted as returned by getSelectColumnNames
112 OUString getUniqueColumnName(const std::vector<OUString>& rColumnNames, const OUString & rColumnName) const;
113
124 static css::uno::Reference< css::beans::XPropertySet > findColumn(
125 const OSQLTables& _rTables, const OUString & rColumnName, OUString & rTableRange );
126
137 css::uno::Reference< css::beans::XPropertySet > findColumn(
138 const OUString & rColumnName, OUString & rTableRange, bool _bLookInSubTables );
139
145 css::uno::Reference< css::beans::XPropertySet > findSelectColumn(
146 std::u16string_view rColumnName );
147
148 void setSelectColumnName(const OUString& rColumnName, const OUString& rColumnAlias, const OUString& rTableRange, bool bFkt = false, sal_Int32 _nType = css::sdbc::DataType::VARCHAR, bool bAggFkt = false);
149 void appendColumns(const OUString& _rTableAlias, const OSQLTable& _rTable);
150 // Other member variables that should be available in the "set" functions
151 // can be defined in the derived class. They can be initialized
152 // in its constructor and, after the "traverse" routines have been used,
153 // they can be queried using other functions.
154
156
157 public:
159 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
160 const css::uno::Reference< css::container::XNameAccess >& _rxTables,
161 const OSQLParser& _rParser );
163
164 void dispose();
165 bool isCaseSensitive() const;
166 // The parse tree to be analysed/traversed:
167 // If NULL is passed, the current parse tree will be deleted and the error status cleared.
168 void setParseTree(const OSQLParseNode * pNewParseTree);
169 const OSQLParseNode * getParseTree() const { return m_pParseTree; };
170
171 // subtrees in case of a select statement
172 const OSQLParseNode* getWhereTree() const;
173 const OSQLParseNode* getOrderTree() const;
174 const OSQLParseNode* getGroupByTree() const;
175 const OSQLParseNode* getHavingTree() const;
176
177 const OSQLParseNode* getSimpleWhereTree() const;
178 const OSQLParseNode* getSimpleOrderTree() const;
179 const OSQLParseNode* getSimpleGroupByTree() const;
180 const OSQLParseNode* getSimpleHavingTree() const;
181
186 const css::sdbc::SQLException& getErrors() const { return *m_xErrors; }
187 bool hasErrors() const { return bool(m_xErrors); }
188
189 // statement type (already set in setParseTree):
190 OSQLStatementType getStatementType() const { return m_eStatementType; }
191
198 void traverseAll();
199
200 // The TableRangeMap contains all tables associated with the range name found first.
201 const OSQLTables& getTables() const;
202
203 const ::rtl::Reference<OSQLColumns>& getSelectColumns() const { return m_aSelectColumns;}
204 const ::rtl::Reference<OSQLColumns>& getGroupColumns() const { return m_aGroupColumns;}
205 const ::rtl::Reference<OSQLColumns>& getOrderColumns() const { return m_aOrderColumns;}
206 const ::rtl::Reference<OSQLColumns>& getParameters() const { return m_aParameters; }
207
216 void getColumnRange( const OSQLParseNode* _pColumnRef,
217 OUString &_rColumnName,
218 OUString& _rTableRange) const;
219
232 void getColumnRange( const OSQLParseNode* _pColumnRef,
233 OUString& _out_rColumnName,
234 OUString& _out_rTableRange,
235 OUString& _out_rColumnAliasIfPresent
236 ) const;
237
244 static OUString getColumnAlias(const OSQLParseNode* _pDerivedColumn);
245
256 static void getColumnRange( const OSQLParseNode* _pColumnRef,
257 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
258 OUString &_rColumnName,
259 OUString& _rTableRange);
260
261 // return true when the tableNode is a rule like catalog_name, schema_name or table_name
262 static bool isTableNode(const OSQLParseNode* _pTableNode);
263
264 // tries to find the correct type of the function
265 sal_Int32 getFunctionReturnType(const OSQLParseNode* _pNode );
266
267 // returns a lis of all joined columns
268 ::std::vector< TNodePair >& getJoinConditions() const;
269
270 private:
271
274 bool traverseTableNames( OSQLTables& _rTables );
275
277 bool traverseSelectColumnNames(const OSQLParseNode* pSelectNode);
279 void traverseCreateColumns(const OSQLParseNode* pSelectNode);
280
281 bool traverseOrderByColumnNames(const OSQLParseNode* pSelectNode);
282 bool traverseGroupByColumnNames(const OSQLParseNode* pSelectNode);
283
284 bool traverseSelectionCriteria(const OSQLParseNode* pSelectNode);
285
289 const OSQLParseTreeIterator& _rParentIterator,
290 const OSQLParser& _rParser,
291 const OSQLParseNode* pRoot );
292
297 OSQLTable impl_createTableObject(
298 const OUString& rTableName, const OUString& rCatalogName, const OUString& rSchemaName );
299
302 OSQLTable impl_locateRecordSource(
303 const OUString& _rComposedName
304 );
305
308 void impl_traverse( TraversalParts _nIncludeMask );
309
312 void impl_getQueryParameterColumns( const OSQLTable& _rQuery );
313
314 void setOrderByColumnName(const OUString & rColumnName, OUString & rTableRange, bool bAscending);
315 void setGroupByColumnName(const OUString & rColumnName, OUString & rTableRange);
316
329 const OUString* _pReplaceToken1 = nullptr, const OUString* _pReplaceToken2 = nullptr );
330
333 void impl_appendError( const css::sdbc::SQLException& _rError );
334
335 void impl_fillJoinConditions(const OSQLParseNode* i_pJoinCondition);
336 };
337}
338
339#endif // INCLUDED_CONNECTIVITY_SQLITERATOR_HXX
340
341/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
::std::unique_ptr< OSQLParseTreeIteratorImpl > m_pImpl
Definition: sqliterator.hxx:92
void impl_appendError(IParseContext::ErrorCode _eError, const OUString *_pReplaceToken1=nullptr, const OUString *_pReplaceToken2=nullptr)
appends an SQLException corresponding to the given error code to our error collection
const ::rtl::Reference< OSQLColumns > & getParameters() const
OSQLParseTreeIterator(const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::container::XNameAccess > &_rxTables, const OSQLParser &_rParser)
::rtl::Reference< OSQLColumns > m_aSelectColumns
Definition: sqliterator.hxx:86
::rtl::Reference< OSQLColumns > m_aCreateColumns
Definition: sqliterator.hxx:90
static void getColumnRange(const OSQLParseNode *_pColumnRef, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, OUString &_rColumnName, OUString &_rTableRange)
return the columname and the table range
::rtl::Reference< OSQLColumns > m_aParameters
Definition: sqliterator.hxx:87
const css::sdbc::SQLException & getErrors() const
returns the errors which occurred during parsing.
OSQLParseTreeIterator(const OSQLParseTreeIterator &rIter)=delete
void impl_appendError(const css::sdbc::SQLException &_rError)
appends an SQLException corresponding to the given error code to our error collection
const ::rtl::Reference< OSQLColumns > & getSelectColumns() const
const ::rtl::Reference< OSQLColumns > & getGroupColumns() const
void getColumnRange(const OSQLParseNode *_pColumnRef, OUString &_out_rColumnName, OUString &_out_rTableRange, OUString &_out_rColumnAliasIfPresent) const
retrieves a column's name, table range, and alias
const OSQLParseNode * getParseTree() const
const ::rtl::Reference< OSQLColumns > & getOrderColumns() const
std::optional< css::sdbc::SQLException > m_xErrors
Definition: sqliterator.hxx:82
OSQLStatementType getStatementType() const
::rtl::Reference< OSQLColumns > m_aGroupColumns
Definition: sqliterator.hxx:88
const OSQLParseNode * m_pParseTree
Definition: sqliterator.hxx:83
::rtl::Reference< OSQLColumns > m_aOrderColumns
Definition: sqliterator.hxx:89
Parser for SQL92.
Definition: sqlparse.hxx:110
#define OOO_DLLPUBLIC_DBTOOLS
virtual void Insert(SotClipboardFormatId nFormat, const OUString &rFormatName) override
class SAL_NO_VTABLE XPropertySet
std::map< OUString, OSQLTable, comphelper::UStringMixLess > OSQLTables
Definition: CommonTools.hxx:56
css::uno::Reference< css::sdbcx::XColumnsSupplier > OSQLTable
Definition: CommonTools.hxx:54
::std::pair< const OSQLParseNode *, const OSQLParseNode * > TNodePair
Definition: sqliterator.hxx:63
Unknown
void dispose()