LibreOffice Module connectivity (master) 1
sqlnode.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_SQLNODE_HXX
20#define INCLUDED_CONNECTIVITY_SQLNODE_HXX
21
24#include <com/sun/star/uno/Reference.hxx>
25#include <memory>
26#include <set>
27#include <string_view>
28#include <vector>
29#include <rtl/ustrbuf.hxx>
30
31namespace com::sun::star::lang { struct Locale; }
32namespace com::sun::star::sdbc { class SQLException; }
33namespace com::sun::star::sdbc { class XDatabaseMetaData; }
34
35namespace com::sun::star
36{
37 namespace beans
38 {
39 class XPropertySet;
40 }
41 namespace util
42 {
43 class XNumberFormatter;
44 }
45 namespace container
46 {
47 class XNameAccess;
48 }
49}
50
51#define ORDER_BY_CHILD_POS 5
52#define TABLE_EXPRESSION_CHILD_COUNT 9
53
54namespace connectivity
55{
56 class OSQLParser;
57 class IParseContext;
58
64
65 typedef ::std::set< OUString > QueryNameSet;
66
67 //= SQLParseNodeParameter
68
70 {
71 const css::lang::Locale& rLocale;
74 std::shared_ptr< QueryNameSet > pSubQueryHistory;
75 css::uno::Reference< css::util::XNumberFormatter > xFormatter;
76 css::uno::Reference< css::beans::XPropertySet > xField;
78 css::uno::Reference< css::container::XNameAccess > xQueries; // see bParseToSDBCLevel
80 OUString sDecSep;
81 bool bQuote : 1;
82 bool bInternational : 1;
83 bool bPredicate : 1;
85
87 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
88 const css::uno::Reference< css::util::XNumberFormatter >& _xFormatter,
89 const css::uno::Reference< css::beans::XPropertySet >& _xField,
90 OUString _sPredicateTableAlias,
91 const css::lang::Locale& _rLocale,
92 const IParseContext* _pContext,
93 bool _bIntl,
94 bool _bQuote,
95 OUString _sDecSep,
96 bool _bPredicate,
97 bool _bParseToSDBC
98 );
99 };
100
101 //= OSQLParseNode
102
104 {
105 friend class OSQLParser;
106
107 std::vector< std::unique_ptr<OSQLParseNode> >
109 OSQLParseNode* m_pParent; // pParent for reverse linkage in the tree
110 OUString m_aNodeValue; // token name, or empty in case of rules,
111 // or OUString in case of
112 // OUString, INT, etc.
114 sal_uInt32 m_nNodeID; // Rule ID (if IsRule())
115 // or Token ID (if !IsRule())
116 // Rule IDs and Token IDs can't
117 // be distinguished by their values,
118 // IsRule has to be used for that!
119 public:
120 enum Rule
121 {
122 UNKNOWN_RULE = 0, // ID indicating that a node is no rule with a matching Rule-enum value (see getKnownRuleID)
123 // we make sure it is 0 so that it is the default-constructor value of this enum
124 // and std::map<foo,Rule>::operator[](bar) default-inserts UNKNOWN_RULE rather than select_statement (!)
226 rule_count // last value
227 };
228
229 // must be ascii encoding for the value
230 OSQLParseNode(const char* _pValueStr,
231 SQLNodeType _eNodeType,
232 sal_uInt32 _nNodeID = 0);
233
234 OSQLParseNode(std::string_view _rValue,
235 SQLNodeType eNewNodeType,
236 sal_uInt32 nNewNodeID=0);
237
238 OSQLParseNode(OUString _sValue,
239 SQLNodeType _eNodeType,
240 sal_uInt32 _nNodeID = 0);
241
242 // copies the respective ParseNode
243 OSQLParseNode(const OSQLParseNode& rParseNode);
244 OSQLParseNode& operator=(const OSQLParseNode& rParseNode);
245
246 bool operator==(OSQLParseNode const & rParseNode) const;
247
248 // destructor destructs the tree recursively
249 virtual ~OSQLParseNode();
250
251 OSQLParseNode* getParent() const {return m_pParent;};
252
253 void setParent(OSQLParseNode* pParseNode) {m_pParent = pParseNode;};
254
255 size_t count() const {return m_aChildren.size();};
256 inline OSQLParseNode* getChild(sal_uInt32 nPos) const;
257
258 void append(OSQLParseNode* pNewSubTree);
259 void insert(sal_uInt32 nPos, OSQLParseNode* pNewSubTree);
260
261 void replaceAndDelete(OSQLParseNode* pOldSubTree, OSQLParseNode* pNewSubTree);
262
263 OSQLParseNode* removeAt(sal_uInt32 nPos);
264
265 void replaceNodeValue(const OUString& rTableAlias,const OUString& rColumnName);
266
303 bool parseNodeToExecutableStatement( OUString& _out_rString,
304 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
305 OSQLParser& _rParser,
306 css::sdbc::SQLException* _pErrorHolder ) const;
307
308 void parseNodeToStr(OUString& rString,
309 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
310 const IParseContext* pContext = nullptr,
311 bool _bIntl = false,
312 bool _bQuote= true) const;
313
314 // quoted and internationalised
315 void parseNodeToPredicateStr(OUString& rString,
316 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
317 const css::uno::Reference< css::util::XNumberFormatter > & xFormatter,
318 const css::lang::Locale& rIntl,
319 OUString _sDec,
320 const IParseContext* pContext = nullptr ) const;
321
322 void parseNodeToPredicateStr(OUString& rString,
323 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
324 const css::uno::Reference< css::util::XNumberFormatter > & xFormatter,
325 const css::uno::Reference< css::beans::XPropertySet > & _xField,
326 const OUString &_sTableAlias,
327 const css::lang::Locale& rIntl,
328 OUString strDec,
329 const IParseContext* pContext = nullptr ) const;
330
331 OSQLParseNode* getByRule(OSQLParseNode::Rule eRule) const;
332
333#if OSL_DEBUG_LEVEL > 1
334 // shows the ParseTree with tabs and linefeeds
335 void showParseTree( OUString& rString ) const;
336 void showParseTree( OUStringBuffer& _inout_rBuf, sal_uInt32 nLevel ) const;
337#endif
338
339 SQLNodeType getNodeType() const {return m_eNodeType;};
340
341 // RuleId returns the RuleID of the node's rule (only if IsRule())
342 sal_uInt32 getRuleID() const {return m_nNodeID;}
343
347 Rule getKnownRuleID() const;
348
349 // returns the TokenId of the node's token (only if !isRule())
350 sal_uInt32 getTokenID() const {return m_nNodeID;}
351
352 // IsRule tests whether a node is a rule (NonTerminal)
353 // ATTENTION: rules can be leaves, for example empty lists
354 bool isRule() const
355 { return (m_eNodeType == SQLNodeType::Rule) || (m_eNodeType == SQLNodeType::ListRule)
356 || (m_eNodeType == SQLNodeType::CommaListRule);}
357
358 // IsToken tests whether a Node is a Token (Terminal but not a rule)
359 bool isToken() const {return !isRule();}
360
361 const OUString& getTokenValue() const {return m_aNodeValue;}
362
363 bool isLeaf() const {return m_aChildren.empty();}
364
365 // negate only a searchcondition, any other rule could cause a gpf
366 static void negateSearchCondition(OSQLParseNode*& pSearchCondition, bool bNegate=false);
367
368 // normalize a logic form
369 // e.q. (a or b) and (c or d) <=> a and c or a and d or b and c or b and d
370 static void disjunctiveNormalForm(OSQLParseNode*& pSearchCondition);
371
372 // Simplifies logic expressions
373 // a and a = a
374 // a or a = a
375 // a and ( a + b) = a
376 // a or a and b = a
377 static void absorptions(OSQLParseNode*& pSearchCondition);
378
379 // erase unnecessary braces
380 static void eraseBraces(OSQLParseNode*& pSearchCondition);
381
382 // makes the logic formula a little smaller
383 static void compress(OSQLParseNode*& pSearchCondition);
384 // return the catalog, schema and tablename from this node
385 // _pTableNode must be a rule of that above or a SQL_TOKEN_NAME
386 static bool getTableComponents(const OSQLParseNode* _pTableNode,
387 css::uno::Any &_rCatalog,
388 OUString &_rSchema,
389 OUString &_rTable,
390 const css::uno::Reference< css::sdbc::XDatabaseMetaData >& _xMetaData);
391
392 // substitute all occurrences of :var or [name] into the dynamic parameter ?
393 // _pNode will be modified if parameters exists
394 static void substituteParameterNames(OSQLParseNode const * _pNode);
395
398 static OUString getTableRange(const OSQLParseNode* _pTableRef);
399
400 protected:
401 // ParseNodeToStr concatenates all Tokens (leaves) of the ParseNodes.
402 void parseNodeToStr(OUString& rString,
403 const css::uno::Reference< css::sdbc::XConnection >& _rxConnection,
404 const css::uno::Reference< css::util::XNumberFormatter > & xFormatter,
405 const css::uno::Reference< css::beans::XPropertySet > & _xField,
406 const OUString &_sPredicateTableAlias,
407 const css::lang::Locale& rIntl,
408 const IParseContext* pContext,
409 bool _bIntl,
410 bool _bQuote,
411 OUString _sDecSep,
412 bool _bPredicate) const;
413
414 private:
415 void impl_parseNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam, bool bSimple=true ) const;
416 void impl_parseLikeNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam, bool bSimple=true ) const;
417 void impl_parseTableRangeNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam ) const;
418
424 bool impl_parseTableNameNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam ) const;
425
426 bool addDateValue(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const;
427 static OUString convertDateTimeString(const SQLParseNodeParameter& rParam, const OUString& rString);
428 static OUString convertDateString(const SQLParseNodeParameter& rParam, std::u16string_view rString);
429 static OUString convertTimeString(const SQLParseNodeParameter& rParam, std::u16string_view rString);
430 void parseLeaf(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const;
431 };
432
433 inline OSQLParseNode* OSQLParseNode::getChild(sal_uInt32 nPos) const
434 {
435 return m_aChildren[nPos].get();
436 }
437
438 // utilities to query for a specific rule, token or punctuation
439 #define SQL_ISRULE(pParseNode, eRule) ((pParseNode)->isRule() && (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::eRule))
440 #define SQL_ISRULEOR2(pParseNode, e1, e2) ((pParseNode)->isRule() && ( \
441 (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e1) || \
442 (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e2)))
443 #define SQL_ISRULEOR3(pParseNode, e1, e2, e3) ((pParseNode)->isRule() && ( \
444 (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e1) || \
445 (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e2) || \
446 (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e3)))
447 #define SQL_ISTOKEN(pParseNode, token) ((pParseNode)->isToken() && (pParseNode)->getTokenID() == SQL_TOKEN_##token)
448 #define SQL_ISTOKENOR2(pParseNode, tok0, tok1) ((pParseNode)->isToken() && ( (pParseNode)->getTokenID() == SQL_TOKEN_##tok0 || (pParseNode)->getTokenID() == SQL_TOKEN_##tok1 ))
449 #define SQL_ISTOKENOR3(pParseNode, tok0, tok1, tok2) ((pParseNode)->isToken() && ( (pParseNode)->getTokenID() == SQL_TOKEN_##tok0 || (pParseNode)->getTokenID() == SQL_TOKEN_##tok1 || (pParseNode)->getTokenID() == SQL_TOKEN_##tok2 ))
450 #define SQL_ISPUNCTUATION(pParseNode, aString) ((pParseNode)->getNodeType() == SQLNodeType::Punctuation && (pParseNode)->getTokenValue() == (aString))
451}
452
453#endif // INCLUDED_CONNECTIVITY_SQLNODE_HXX
454
455/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void parseNodeToStr(OUString &rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter, const css::uno::Reference< css::beans::XPropertySet > &_xField, const OUString &_sPredicateTableAlias, const css::lang::Locale &rIntl, const IParseContext *pContext, bool _bIntl, bool _bQuote, OUString _sDecSep, bool _bPredicate) const
void parseNodeToStr(OUString &rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const IParseContext *pContext=nullptr, bool _bIntl=false, bool _bQuote=true) const
void setParent(OSQLParseNode *pParseNode)
Definition: sqlnode.hxx:253
void parseNodeToPredicateStr(OUString &rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter, const css::uno::Reference< css::beans::XPropertySet > &_xField, const OUString &_sTableAlias, const css::lang::Locale &rIntl, OUString strDec, const IParseContext *pContext=nullptr) const
std::vector< std::unique_ptr< OSQLParseNode > > m_aChildren
Definition: sqlnode.hxx:108
sal_uInt32 getTokenID() const
Definition: sqlnode.hxx:350
void parseNodeToPredicateStr(OUString &rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter, const css::lang::Locale &rIntl, OUString _sDec, const IParseContext *pContext=nullptr) const
const OUString & getTokenValue() const
Definition: sqlnode.hxx:361
sal_uInt32 getRuleID() const
Definition: sqlnode.hxx:342
OSQLParseNode * getParent() const
Definition: sqlnode.hxx:251
OSQLParseNode * getChild(sal_uInt32 nPos) const
Definition: sqlnode.hxx:433
OSQLParseNode * m_pParent
Definition: sqlnode.hxx:109
SQLNodeType getNodeType() const
Definition: sqlnode.hxx:339
Parser for SQL92.
Definition: sqlparse.hxx:110
encapsulates meta data about a database/connection which cannot be obtained from the usual XDatabaseM...
Definition: dbmetadata.hxx:50
#define OOO_DLLPUBLIC_DBTOOLS
sal_uInt16 nPos
class SAL_NO_VTABLE XPropertySet
::std::set< OUString > QueryNameSet
Definition: sqlnode.hxx:65
bool operator==(const OCharsetMap::CharsetIterator &lhs, const OCharsetMap::CharsetIterator &rhs)
Definition: dbcharset.cxx:182
SQLParseNodeParameter(const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::util::XNumberFormatter > &_xFormatter, const css::uno::Reference< css::beans::XPropertySet > &_xField, OUString _sPredicateTableAlias, const css::lang::Locale &_rLocale, const IParseContext *_pContext, bool _bIntl, bool _bQuote, OUString _sDecSep, bool _bPredicate, bool _bParseToSDBC)
should we create an SDBC-level statement (e.g. with substituted sub queries)?
Definition: sqlnode.cxx:180
::dbtools::DatabaseMetaData aMetaData
Definition: sqlnode.hxx:72
const IParseContext & m_rContext
Definition: sqlnode.hxx:79
const css::lang::Locale & rLocale
Definition: sqlnode.hxx:71
bool bPredicate
should we internationalize keywords and placeholders?
Definition: sqlnode.hxx:83
css::uno::Reference< css::beans::XPropertySet > xField
Definition: sqlnode.hxx:76
css::uno::Reference< css::util::XNumberFormatter > xFormatter
Definition: sqlnode.hxx:75
std::shared_ptr< QueryNameSet > pSubQueryHistory
Definition: sqlnode.hxx:74
css::uno::Reference< css::container::XNameAccess > xQueries
Definition: sqlnode.hxx:78
bool bParseToSDBCLevel
are we going to parse a mere predicate?
Definition: sqlnode.hxx:84
bool bInternational
should we quote identifiers?
Definition: sqlnode.hxx:82
OUString Name