Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / connectivity / sqlparse.hxx
blob1bcfd272ad86d29a40cf86576317a63362696026
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #ifndef INCLUDED_CONNECTIVITY_SQLPARSE_HXX
20 #define INCLUDED_CONNECTIVITY_SQLPARSE_HXX
22 #include <com/sun/star/uno/Reference.h>
23 #include <connectivity/sqlnode.hxx>
24 #include <connectivity/IParseContext.hxx>
25 #include <connectivity/dbtoolsdllapi.hxx>
26 #include <connectivity/sqlerror.hxx>
27 #include <comphelper/singletonref.hxx>
29 #include <map>
30 #include <memory>
31 #include <mutex>
32 #include <string_view>
34 namespace com::sun::star::i18n { class XCharacterClassification; }
35 namespace com::sun::star::i18n { class XLocaleData4; }
37 namespace com::sun::star
39 namespace beans
41 class XPropertySet;
43 namespace util
45 class XNumberFormatter;
49 namespace connectivity
51 class OSQLScanner;
53 //= OParseContext
55 class OParseContext final : public IParseContext
57 public:
58 OParseContext();
60 virtual ~OParseContext();
61 // retrieves language specific error messages
62 virtual OUString getErrorMessage(ErrorCode _eCodes) const override;
64 // retrieves language specific keyword strings (only ASCII allowed)
65 virtual OString getIntlKeywordAscii(InternationalKeyCode _eKey) const override;
67 // finds out, if we have an international keyword (only ASCII allowed)
68 virtual InternationalKeyCode getIntlKeyCode(const OString& rToken) const override;
70 // determines the default international setting
71 static const css::lang::Locale& getDefaultLocale();
73 /** gets a locale instance which should be used when parsing in the context specified by this instance
74 <p>if this is not overridden by derived classes, it returns the static default locale.</p>
76 virtual css::lang::Locale getPreferredLocale( ) const override;
79 // OSQLParseNodesContainer
80 // garbage collection of nodes
82 class OSQLParseNodesContainer
84 std::mutex m_aMutex;
85 ::std::vector< OSQLParseNode* > m_aNodes;
86 public:
87 OSQLParseNodesContainer();
88 ~OSQLParseNodesContainer();
90 void push_back(OSQLParseNode* _pNode);
91 void erase(OSQLParseNode* _pNode);
92 void clear();
93 void clearAndDelete();
96 typedef comphelper::SingletonRef<OSQLParseNodesContainer> OSQLParseNodesGarbageCollector;
98 //= OSQLParser
100 struct OSQLParser_Data
102 css::lang::Locale aLocale;
103 ::connectivity::SQLError aErrors;
106 /** Parser for SQL92
108 class OOO_DLLPUBLIC_DBTOOLS OSQLParser
110 friend class OSQLParseNode;
111 friend class OSQLInternalNode;
112 friend struct SQLParseNodeParameter;
114 private:
115 typedef ::std::map< sal_uInt32, OSQLParseNode::Rule > RuleIDMap;
116 // static parts for parsers
117 static sal_uInt32 s_nRuleIDs[OSQLParseNode::rule_count + 1];
118 static RuleIDMap s_aReverseRuleIDLookup;
119 static OParseContext s_aDefaultContext;
121 static OSQLScanner* s_pScanner;
122 static OSQLParseNodesGarbageCollector* s_pGarbageCollector;
123 static sal_Int32 s_nRefCount;
125 // information on the current parse action
126 const IParseContext* m_pContext;
127 const IParseContext* m_pNeutral;
128 std::unique_ptr<OSQLParseNode> m_pParseTree; // result from parsing
129 ::std::unique_ptr< OSQLParser_Data >
130 m_pData;
131 OUString m_sFieldName; // current field name for a predicate
132 OUString m_sErrorMessage;// current error msg
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
139 sal_Int32 m_nDateFormatKey;
140 css::uno::Reference< css::uno::XComponentContext > m_xContext;
141 css::uno::Reference< css::i18n::XCharacterClassification> m_xCharClass;
142 static css::uno::Reference< css::i18n::XLocaleData4> s_xLocaleData;
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);
154 static std::mutex& getMutex();
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,
160 const IParseContext* _pContext = nullptr,
161 const IParseContext* _pNeutral = nullptr);
162 ~OSQLParser();
164 // Parsing an SQLStatement
165 std::unique_ptr<OSQLParseNode> parseTree(OUString& rErrorMessage,
166 const OUString& rStatement,
167 bool bInternational = false);
169 // Check a Predicate
170 // set bUseRealName to false if you pass a xField that comes from where you got that field,
171 // as opposed from to from yourself.
172 std::unique_ptr<OSQLParseNode> predicateTree(OUString& rErrorMessage, const OUString& rStatement,
173 const css::uno::Reference< css::util::XNumberFormatter > & xFormatter,
174 const css::uno::Reference< css::beans::XPropertySet > & xField,
175 bool bUseRealName = true);
177 // Access to the context
178 const IParseContext& getContext() const { return *m_pContext; }
179 const IParseContext* getNeutral() const { return m_pNeutral; }
181 /// access to the SQLError instance owned by this parser
182 const SQLError& getErrorHelper() const;
184 // TokenIDToStr: token name belonging to a token number.
185 static OString TokenIDToStr(sal_uInt32 nTokenID, const IParseContext* pContext = nullptr);
187 #if OSL_DEBUG_LEVEL > 0
188 // (empty string if not found)
189 static OUString RuleIDToStr(sal_uInt32 nRuleID);
190 #endif
192 // StrToRuleID calculates the RuleID for an OUString (that is, css::sdbcx::Index in yytname)
193 // (0 if not found). The search for an ID based on a String is
194 // extremely inefficient (sequential search for OUString)!
195 static sal_uInt32 StrToRuleID(const OString & rValue);
197 static OSQLParseNode::Rule RuleIDToRule( sal_uInt32 _nRule );
199 // RuleId with enum, far more efficient
200 static sal_uInt32 RuleID(OSQLParseNode::Rule eRule);
201 // compares the _sFunctionName with all known function names and return the DataType of the return value
202 static sal_Int32 getFunctionReturnType(std::u16string_view _sFunctionName, const IParseContext* pContext);
204 // returns the type for a parameter in a given function name
205 static sal_Int32 getFunctionParameterType(sal_uInt32 _nTokenId,sal_uInt32 _nPos);
207 void error(const char *fmt);
208 static int SQLlex();
209 #ifdef YYBISON
210 void setParseTree(OSQLParseNode * pNewParseTree);
212 // Is the parse in a special mode?
213 // Predicate check is used to check a condition for a field
214 bool inPredicateCheck() const {return m_xField.is();}
215 const OUString& getFieldName() const {return m_sFieldName;}
217 static void reduceLiteral(OSQLParseNode*& pLiteral, bool bAppendBlank);
218 // does not change the pLiteral argument
219 sal_Int16 buildNode(OSQLParseNode*& pAppend,OSQLParseNode* pCompare,OSQLParseNode* pLiteral,OSQLParseNode* pLiteral2);
221 sal_Int16 buildComparisonRule(OSQLParseNode*& pAppend,OSQLParseNode* pLiteral);
222 // pCompre will be deleted if it is not used
223 sal_Int16 buildPredicateRule(OSQLParseNode*& pAppend,OSQLParseNode* const pLiteral,OSQLParseNode* pCompare,OSQLParseNode* pLiteral2 = nullptr);
225 sal_Int16 buildLikeRule(OSQLParseNode* pAppend, OSQLParseNode*& pLiteral, const OSQLParseNode* pEscape);
226 sal_Int16 buildStringNodes(OSQLParseNode*& pLiteral);
227 #endif
231 #endif // INCLUDED_CONNECTIVITY_SQLPARSE_HXX
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */