1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
24 #include <com/sun/star/uno/Reference.h>
25 #include <osl/mutex.hxx>
26 #include <connectivity/sqlnode.hxx>
27 #include <connectivity/IParseContext.hxx>
28 #include <connectivity/dbtoolsdllapi.hxx>
29 #include <connectivity/sqlerror.hxx>
30 #include <salhelper/singletonref.hxx>
34 namespace com::sun::star::i18n
{ class XCharacterClassification
; }
35 namespace com::sun::star::i18n
{ class XLocaleData4
; }
49 class XNumberFormatter
;
55 namespace connectivity
61 class OOO_DLLPUBLIC_DBTOOLS OParseContext
: public IParseContext
66 virtual ~OParseContext();
67 // retrieves language specific error messages
68 virtual OUString
getErrorMessage(ErrorCode _eCodes
) const override
;
70 // retrieves language specific keyword strings (only ASCII allowed)
71 virtual OString
getIntlKeywordAscii(InternationalKeyCode _eKey
) const override
;
73 // finds out, if we have an international keyword (only ASCII allowed)
74 virtual InternationalKeyCode
getIntlKeyCode(const OString
& rToken
) const override
;
76 // determines the default international setting
77 static const css::lang::Locale
& getDefaultLocale();
79 /** gets a locale instance which should be used when parsing in the context specified by this instance
80 <p>if this is not overridden by derived classes, it returns the static default locale.</p>
82 virtual css::lang::Locale
getPreferredLocale( ) const override
;
85 // OSQLParseNodesContainer
86 // garbage collection of nodes
88 class OSQLParseNodesContainer
90 ::osl::Mutex m_aMutex
;
91 ::std::vector
< OSQLParseNode
* > m_aNodes
;
93 OSQLParseNodesContainer();
94 ~OSQLParseNodesContainer();
96 void push_back(OSQLParseNode
* _pNode
);
97 void erase(OSQLParseNode
* _pNode
);
99 void clearAndDelete();
102 typedef salhelper::SingletonRef
<OSQLParseNodesContainer
> OSQLParseNodesGarbageCollector
;
106 struct OSQLParser_Data
108 css::lang::Locale aLocale
;
109 ::connectivity::SQLError aErrors
;
114 class OOO_DLLPUBLIC_DBTOOLS OSQLParser
116 friend class OSQLParseNode
;
117 friend class OSQLInternalNode
;
118 friend struct SQLParseNodeParameter
;
121 typedef ::std::map
< sal_uInt32
, OSQLParseNode::Rule
> RuleIDMap
;
122 // static parts for parsers
123 static sal_uInt32 s_nRuleIDs
[OSQLParseNode::rule_count
+ 1];
124 static RuleIDMap s_aReverseRuleIDLookup
;
125 static OParseContext s_aDefaultContext
;
127 static OSQLScanner
* s_pScanner
;
128 static OSQLParseNodesGarbageCollector
* s_pGarbageCollector
;
129 static sal_Int32 s_nRefCount
;
131 // information on the current parse action
132 const IParseContext
* m_pContext
;
133 std::unique_ptr
<OSQLParseNode
> m_pParseTree
; // result from parsing
134 ::std::unique_ptr
< OSQLParser_Data
>
136 OUString m_sFieldName
; // current field name for a predicate
137 OUString m_sErrorMessage
;// current error msg
139 css::uno::Reference
< css::beans::XPropertySet
>
140 m_xField
; // current field
141 css::uno::Reference
< css::util::XNumberFormatter
>
142 m_xFormatter
; // current number formatter
143 sal_Int32 m_nFormatKey
; // numberformat, which should be used
144 sal_Int32 m_nDateFormatKey
;
145 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
146 css::uno::Reference
< css::i18n::XCharacterClassification
> m_xCharClass
;
147 static css::uno::Reference
< css::i18n::XLocaleData4
> s_xLocaleData
;
149 // convert a string into double trim it to scale of _nscale and than transform it back to string
150 OUString
stringToDouble(const OUString
& _rValue
,sal_Int16 _nScale
);
151 OSQLParseNode
* buildDate(sal_Int32 _nType
,OSQLParseNode
*& pLiteral
);
152 bool extractDate(OSQLParseNode
const * pLiteral
,double& _rfValue
);
153 void killThousandSeparator(OSQLParseNode
* pLiteral
);
154 OSQLParseNode
* convertNode(sal_Int32 nType
, OSQLParseNode
* pLiteral
);
155 // makes a string out of a number, pLiteral will be deleted
156 OSQLParseNode
* buildNode_STR_NUM(OSQLParseNode
*& pLiteral
);
157 OSQLParseNode
* buildNode_Date(const double& fValue
, sal_Int32 nType
);
159 static ::osl::Mutex
& getMutex();
162 // if NULL, a default context will be used
163 // the context must live as long as the parser
164 OSQLParser(const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
, const IParseContext
* _pContext
= nullptr);
167 // Parsing an SQLStatement
168 std::unique_ptr
<OSQLParseNode
> parseTree(OUString
& rErrorMessage
,
169 const OUString
& rStatement
,
170 bool bInternational
= false);
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);
180 // Access to the context
181 const IParseContext
& getContext() const {return *m_pContext
;}
183 /// access to the SQLError instance owned by this parser
184 const SQLError
& getErrorHelper() const;
186 // TokenIDToStr: token name belonging to a token number.
187 static OString
TokenIDToStr(sal_uInt32 nTokenID
, const IParseContext
* pContext
= nullptr);
189 #if OSL_DEBUG_LEVEL > 0
190 // (empty string if not found)
191 static OUString
RuleIDToStr(sal_uInt32 nRuleID
);
194 // StrToRuleID calculates the RuleID for a OUString (that is, css::sdbcx::Index in yytname)
195 // (0 if not found). The search for an ID based on a String is
196 // extremely inefficient (sequential search for OUString)!
197 static sal_uInt32
StrToRuleID(const OString
& rValue
);
199 static OSQLParseNode::Rule
RuleIDToRule( sal_uInt32 _nRule
);
201 // RuleId with enum, far more efficient
202 static sal_uInt32
RuleID(OSQLParseNode::Rule eRule
);
203 // compares the _sFunctionName with all known function names and return the DataType of the return value
204 static sal_Int32
getFunctionReturnType(const OUString
& _sFunctionName
, const IParseContext
* pContext
);
206 // returns the type for a parameter in a given function name
207 static sal_Int32
getFunctionParameterType(sal_uInt32 _nTokenId
,sal_uInt32 _nPos
);
209 void error(const sal_Char
*fmt
);
212 void setParseTree(OSQLParseNode
* pNewParseTree
);
214 // Is the parse in a special mode?
215 // Predicate check is used to check a condition for a field
216 bool inPredicateCheck() const {return m_xField
.is();}
217 const OUString
& getFieldName() const {return m_sFieldName
;}
219 static void reduceLiteral(OSQLParseNode
*& pLiteral
, bool bAppendBlank
);
220 // does not change the pLiteral argument
221 sal_Int16
buildNode(OSQLParseNode
*& pAppend
,OSQLParseNode
* pCompare
,OSQLParseNode
* pLiteral
,OSQLParseNode
* pLiteral2
);
223 sal_Int16
buildComparsionRule(OSQLParseNode
*& pAppend
,OSQLParseNode
* pLiteral
);
224 // pCompre will be deleted if it is not used
225 sal_Int16
buildPredicateRule(OSQLParseNode
*& pAppend
,OSQLParseNode
* const pLiteral
,OSQLParseNode
* pCompare
,OSQLParseNode
* pLiteral2
= nullptr);
227 sal_Int16
buildLikeRule(OSQLParseNode
* pAppend
, OSQLParseNode
*& pLiteral
, const OSQLParseNode
* pEscape
);
228 sal_Int16
buildStringNodes(OSQLParseNode
*& pLiteral
);
233 #endif // INCLUDED_CONNECTIVITY_SQLPARSE_HXX
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */