LibreOffice Module connectivity (master) 1
sqlparse.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_SQLPARSE_HXX
20#define INCLUDED_CONNECTIVITY_SQLPARSE_HXX
21
22#include <com/sun/star/uno/Reference.h>
28#include <vcl/lazydelete.hxx>
29
30#include <map>
31#include <memory>
32#include <mutex>
33#include <string_view>
34
35namespace com::sun::star::i18n { class XCharacterClassification; }
36namespace com::sun::star::i18n { class XLocaleData4; }
37
38namespace com::sun::star
39{
40 namespace beans
41 {
42 class XPropertySet;
43 }
44 namespace util
45 {
46 class XNumberFormatter;
47 }
48}
49
50namespace connectivity
51{
52 class OSQLScanner;
53
54 //= OParseContext
55
56 class OParseContext final : public IParseContext
57 {
58 public:
60
61 virtual ~OParseContext();
62 // retrieves language specific error messages
63 virtual OUString getErrorMessage(ErrorCode _eCodes) const override;
64
65 // retrieves language specific keyword strings (only ASCII allowed)
66 virtual OString getIntlKeywordAscii(InternationalKeyCode _eKey) const override;
67
68 // finds out, if we have an international keyword (only ASCII allowed)
69 virtual InternationalKeyCode getIntlKeyCode(const OString& rToken) const override;
70
71 // determines the default international setting
72 static const css::lang::Locale& getDefaultLocale();
73
77 virtual css::lang::Locale getPreferredLocale( ) const override;
78 };
79
80 // OSQLParseNodesContainer
81 // garbage collection of nodes
82
84 {
85 std::mutex m_aMutex;
86 ::std::vector< OSQLParseNode* > m_aNodes;
87 public:
90
91 void push_back(OSQLParseNode* _pNode);
92 void erase(OSQLParseNode* _pNode);
93 void clear();
94 void clearAndDelete();
95 };
96
98
99 //= OSQLParser
100
102 {
103 css::lang::Locale aLocale;
105 };
106
110 {
111 friend class OSQLParseNode;
112 friend class OSQLInternalNode;
114
115 private:
116 typedef ::std::map< sal_uInt32, OSQLParseNode::Rule > RuleIDMap;
117 // static parts for parsers
118 static sal_uInt32 s_nRuleIDs[OSQLParseNode::rule_count + 1];
121
124 static sal_Int32 s_nRefCount;
125
126 // information on the current parse action
128 std::unique_ptr<OSQLParseNode> m_pParseTree; // result from parsing
129 ::std::unique_ptr< OSQLParser_Data >
131 OUString m_sFieldName; // current field name for a predicate
132 OUString m_sErrorMessage;// current error msg
133
134 css::uno::Reference< css::beans::XPropertySet >
135 m_xField; // current field
136 css::uno::Reference< css::util::XNumberFormatter >
137 m_xFormatter; // current number formatter
138 sal_Int32 m_nFormatKey; // numberformat, which should be used
140 css::uno::Reference< css::uno::XComponentContext > m_xContext;
141 css::uno::Reference< css::i18n::XCharacterClassification> m_xCharClass;
143
144 // convert a string into double trim it to scale of _nscale and then transform it back to string
145 OUString stringToDouble(const OUString& _rValue,sal_Int16 _nScale);
146 OSQLParseNode* buildDate(sal_Int32 _nType,OSQLParseNode*& pLiteral);
147 bool extractDate(OSQLParseNode const * pLiteral,double& _rfValue);
148 void killThousandSeparator(OSQLParseNode* pLiteral);
149 OSQLParseNode* convertNode(sal_Int32 nType, OSQLParseNode* pLiteral);
150 // makes a string out of a number, pLiteral will be deleted
151 OSQLParseNode* buildNode_STR_NUM(OSQLParseNode*& pLiteral);
152 OSQLParseNode* buildNode_Date(const double& fValue, sal_Int32 nType);
153
154 static std::mutex& getMutex();
155
156 public:
157 // if NULL, a default context will be used
158 // the context must live as long as the parser
159 OSQLParser(css::uno::Reference< css::uno::XComponentContext > xContext, const IParseContext* _pContext = nullptr);
160 ~OSQLParser();
161
162 // Parsing an SQLStatement
163 std::unique_ptr<OSQLParseNode> parseTree(OUString& rErrorMessage,
164 const OUString& rStatement,
165 bool bInternational = false);
166
167 // Check a Predicate
168 // set bUseRealName to false if you pass a xField that comes from where you got that field,
169 // as opposed from to from yourself.
170 std::unique_ptr<OSQLParseNode> predicateTree(OUString& rErrorMessage, const OUString& rStatement,
171 const css::uno::Reference< css::util::XNumberFormatter > & xFormatter,
172 const css::uno::Reference< css::beans::XPropertySet > & xField,
173 bool bUseRealName = true);
174
175 // Access to the context
176 const IParseContext& getContext() const {return *m_pContext;}
177
179 const SQLError& getErrorHelper() const;
180
181 // TokenIDToStr: token name belonging to a token number.
182 static OString TokenIDToStr(sal_uInt32 nTokenID, const IParseContext* pContext = nullptr);
183
184#if OSL_DEBUG_LEVEL > 0
185 // (empty string if not found)
186 static OUString RuleIDToStr(sal_uInt32 nRuleID);
187#endif
188
189 // StrToRuleID calculates the RuleID for an OUString (that is, css::sdbcx::Index in yytname)
190 // (0 if not found). The search for an ID based on a String is
191 // extremely inefficient (sequential search for OUString)!
192 static sal_uInt32 StrToRuleID(const OString & rValue);
193
194 static OSQLParseNode::Rule RuleIDToRule( sal_uInt32 _nRule );
195
196 // RuleId with enum, far more efficient
197 static sal_uInt32 RuleID(OSQLParseNode::Rule eRule);
198 // compares the _sFunctionName with all known function names and return the DataType of the return value
199 static sal_Int32 getFunctionReturnType(std::u16string_view _sFunctionName, const IParseContext* pContext);
200
201 // returns the type for a parameter in a given function name
202 static sal_Int32 getFunctionParameterType(sal_uInt32 _nTokenId,sal_uInt32 _nPos);
203
204 void error(const char *fmt);
205 static int SQLlex();
206#ifdef YYBISON
207 void setParseTree(OSQLParseNode * pNewParseTree);
208
209 // Is the parse in a special mode?
210 // Predicate check is used to check a condition for a field
211 bool inPredicateCheck() const {return m_xField.is();}
212 const OUString& getFieldName() const {return m_sFieldName;}
213
214 static void reduceLiteral(OSQLParseNode*& pLiteral, bool bAppendBlank);
215 // does not change the pLiteral argument
216 sal_Int16 buildNode(OSQLParseNode*& pAppend,OSQLParseNode* pCompare,OSQLParseNode* pLiteral,OSQLParseNode* pLiteral2);
217
218 sal_Int16 buildComparisonRule(OSQLParseNode*& pAppend,OSQLParseNode* pLiteral);
219 // pCompre will be deleted if it is not used
220 sal_Int16 buildPredicateRule(OSQLParseNode*& pAppend,OSQLParseNode* const pLiteral,OSQLParseNode* pCompare,OSQLParseNode* pLiteral2 = nullptr);
221
222 sal_Int16 buildLikeRule(OSQLParseNode* pAppend, OSQLParseNode*& pLiteral, const OSQLParseNode* pEscape);
223 sal_Int16 buildStringNodes(OSQLParseNode*& pLiteral);
224#endif
225 };
226}
227
228#endif // INCLUDED_CONNECTIVITY_SQLPARSE_HXX
229
230/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::lang::Locale getPreferredLocale() const override
gets a locale instance which should be used when parsing in the context specified by this instance
virtual InternationalKeyCode getIntlKeyCode(const OString &rToken) const override
virtual OUString getErrorMessage(ErrorCode _eCodes) const override
static const css::lang::Locale & getDefaultLocale()
virtual OString getIntlKeywordAscii(InternationalKeyCode _eKey) const override
special node for avoiding memory leaks
::std::vector< OSQLParseNode * > m_aNodes
Definition: sqlparse.hxx:86
void push_back(OSQLParseNode *_pNode)
Definition: sqlnode.cxx:2731
void erase(OSQLParseNode *_pNode)
Definition: sqlnode.cxx:2737
Parser for SQL92.
Definition: sqlparse.hxx:110
void error(const char *fmt)
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: sqlparse.hxx:140
css::uno::Reference< css::util::XNumberFormatter > m_xFormatter
Definition: sqlparse.hxx:137
css::uno::Reference< css::beans::XPropertySet > m_xField
Definition: sqlparse.hxx:135
static OSQLScanner * s_pScanner
Definition: sqlparse.hxx:122
static OSQLParseNode::Rule RuleIDToRule(sal_uInt32 _nRule)
static OParseContext s_aDefaultContext
Definition: sqlparse.hxx:120
static sal_Int32 s_nRefCount
Definition: sqlparse.hxx:124
static OString TokenIDToStr(sal_uInt32 nTokenID, const IParseContext *pContext=nullptr)
static vcl::DeleteOnDeinit< css::uno::Reference< css::i18n::XLocaleData4 > > s_xLocaleData
Definition: sqlparse.hxx:142
::std::map< sal_uInt32, OSQLParseNode::Rule > RuleIDMap
Definition: sqlparse.hxx:116
static sal_uInt32 StrToRuleID(const OString &rValue)
std::unique_ptr< OSQLParseNode > parseTree(OUString &rErrorMessage, const OUString &rStatement, bool bInternational=false)
css::uno::Reference< css::i18n::XCharacterClassification > m_xCharClass
Definition: sqlparse.hxx:141
::std::unique_ptr< OSQLParser_Data > m_pData
Definition: sqlparse.hxx:130
static RuleIDMap s_aReverseRuleIDLookup
Definition: sqlparse.hxx:119
static OSQLParseNodesGarbageCollector * s_pGarbageCollector
Definition: sqlparse.hxx:123
const IParseContext * m_pContext
Definition: sqlparse.hxx:127
const IParseContext & getContext() const
Definition: sqlparse.hxx:176
std::unique_ptr< OSQLParseNode > m_pParseTree
Definition: sqlparse.hxx:128
static OUString RuleIDToStr(sal_uInt32 nRuleID)
static sal_uInt32 RuleID(OSQLParseNode::Rule eRule)
Scanner for SQL92.
Definition: sqlscan.hxx:31
a class which provides helpers for working with SQLErrors
Definition: sqlerror.hxx:59
#define OOO_DLLPUBLIC_DBTOOLS
class SAL_NO_VTABLE XPropertySet
OUString getFieldName(guint nCol)
comphelper::SingletonRef< OSQLParseNodesContainer > OSQLParseNodesGarbageCollector
Definition: sqlparse.hxx:97
::osl::Mutex & getMutex()
css::lang::Locale aLocale
Definition: sqlparse.hxx:103
::connectivity::SQLError aErrors
Definition: sqlparse.hxx:104