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