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