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_SQLNODE_HXX
20 : #define INCLUDED_CONNECTIVITY_SQLNODE_HXX
21 :
22 : #include <connectivity/dbtoolsdllapi.hxx>
23 : #include <connectivity/dbmetadata.hxx>
24 : #include <com/sun/star/uno/Reference.hxx>
25 : #include <com/sun/star/util/XNumberFormatTypes.hpp>
26 : #include <com/sun/star/beans/XPropertySet.hpp>
27 : #include <functional>
28 : #include <memory>
29 : #include <set>
30 : #include <vector>
31 : #include <rtl/ustrbuf.hxx>
32 : #include <osl/diagnose.h>
33 :
34 : namespace com
35 : {
36 : namespace sun
37 : {
38 : namespace star
39 : {
40 : namespace beans
41 : {
42 : class XPropertySet;
43 : }
44 : namespace util
45 : {
46 : class XNumberFormatter;
47 : }
48 : namespace container
49 : {
50 : class XNameAccess;
51 : }
52 : }
53 : }
54 : }
55 :
56 : #define ORDER_BY_CHILD_POS 5
57 : #define TABLE_EXPRESSION_CHILD_COUNT 9
58 :
59 : namespace connectivity
60 : {
61 : class OSQLParser;
62 : class OSQLParseNode;
63 : class IParseContext;
64 :
65 : typedef ::std::vector< OSQLParseNode* > OSQLParseNodes;
66 :
67 : enum SQLNodeType {SQL_NODE_RULE, SQL_NODE_LISTRULE, SQL_NODE_COMMALISTRULE,
68 : SQL_NODE_KEYWORD, SQL_NODE_COMPARISON, SQL_NODE_NAME,
69 : SQL_NODE_STRING, SQL_NODE_INTNUM, SQL_NODE_APPROXNUM,
70 : SQL_NODE_EQUAL,SQL_NODE_LESS,SQL_NODE_GREAT,SQL_NODE_LESSEQ,SQL_NODE_GREATEQ,SQL_NODE_NOTEQUAL,
71 : SQL_NODE_PUNCTUATION, SQL_NODE_AMMSC, SQL_NODE_ACCESS_DATE,SQL_NODE_DATE,SQL_NODE_CONCAT};
72 :
73 : typedef ::std::set< OUString > QueryNameSet;
74 :
75 : //= SQLParseNodeParameter
76 :
77 12506 : struct OOO_DLLPUBLIC_DBTOOLS SQLParseNodeParameter
78 : {
79 : const ::com::sun::star::lang::Locale& rLocale;
80 : ::dbtools::DatabaseMetaData aMetaData;
81 : OSQLParser* pParser;
82 : std::shared_ptr< QueryNameSet > pSubQueryHistory;
83 : ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > xFormatter;
84 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > xField;
85 : OUString sPredicateTableAlias;
86 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameAccess > xQueries; // see bParseToSDBCLevel
87 : const IParseContext& m_rContext;
88 : sal_Char cDecSep;
89 : bool bQuote : 1; /// should we quote identifiers?
90 : bool bInternational : 1; /// should we internationalize keywords and placeholders?
91 : bool bPredicate : 1; /// are we going to parse a mere predicate?
92 : bool bParseToSDBCLevel : 1; /// should we create an SDBC-level statement (e.g. with substituted sub queries)?
93 :
94 : SQLParseNodeParameter(
95 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
96 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter >& _xFormatter,
97 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet >& _xField,
98 : const OUString &_sPredicateTableAlias,
99 : const ::com::sun::star::lang::Locale& _rLocale,
100 : const IParseContext* _pContext,
101 : bool _bIntl,
102 : bool _bQuote,
103 : sal_Char _cDecSep,
104 : bool _bPredicate,
105 : bool _bParseToSDBC
106 : );
107 : ~SQLParseNodeParameter();
108 : };
109 :
110 : //= OSQLParseNode
111 :
112 : class OOO_DLLPUBLIC_DBTOOLS OSQLParseNode
113 : {
114 : friend class OSQLParser;
115 :
116 : OSQLParseNodes m_aChildren;
117 : OSQLParseNode* m_pParent; // pParent for reverse linkage in the tree
118 : OUString m_aNodeValue; // token name, or empty in case of rules,
119 : // or OUString in case of
120 : // OUString, INT, etc.
121 : SQLNodeType m_eNodeType; // see above
122 : sal_uInt32 m_nNodeID; // Rule ID (if IsRule())
123 : // or Token ID (if !IsRule())
124 : // Rule IDs and Token IDs can't
125 : // be distinguished by their values,
126 : // IsRule has to be used for that!
127 : public:
128 : enum Rule
129 : {
130 : UNKNOWN_RULE = 0, // ID indicating that a node is no rule with a matching Rule-enum value (see getKnownRuleID)
131 : // we make sure it is 0 so that it is the default-constructor value of this enum
132 : // and std::map<foo,Rule>::operator[](bar) default-inserts UNKNOWN_RULE rather than select_statement (!)
133 : select_statement,
134 : table_exp,
135 : table_ref_commalist,
136 : table_ref,
137 : catalog_name,
138 : schema_name,
139 : table_name,
140 : opt_column_commalist,
141 : column_commalist,
142 : column_ref_commalist,
143 : column_ref,
144 : opt_order_by_clause,
145 : ordering_spec_commalist,
146 : ordering_spec,
147 : opt_asc_desc,
148 : where_clause,
149 : opt_where_clause,
150 : search_condition,
151 : comparison,
152 : comparison_predicate,
153 : between_predicate,
154 : like_predicate,
155 : opt_escape,
156 : test_for_null,
157 : scalar_exp_commalist,
158 : scalar_exp,
159 : parameter_ref,
160 : parameter,
161 : general_set_fct,
162 : range_variable,
163 : column,
164 : delete_statement_positioned,
165 : delete_statement_searched,
166 : update_statement_positioned,
167 : update_statement_searched,
168 : assignment_commalist,
169 : assignment,
170 : values_or_query_spec,
171 : insert_statement,
172 : insert_atom_commalist,
173 : insert_atom,
174 : from_clause,
175 : qualified_join,
176 : cross_union,
177 : select_sublist,
178 : derived_column,
179 : column_val,
180 : set_fct_spec,
181 : boolean_term,
182 : boolean_primary,
183 : num_value_exp,
184 : join_type,
185 : position_exp,
186 : extract_exp,
187 : length_exp,
188 : char_value_fct,
189 : odbc_call_spec,
190 : in_predicate,
191 : existence_test,
192 : unique_test,
193 : all_or_any_predicate,
194 : named_columns_join,
195 : join_condition,
196 : joined_table,
197 : boolean_factor,
198 : sql_not,
199 : manipulative_statement,
200 : subquery,
201 : value_exp_commalist,
202 : odbc_fct_spec,
203 : union_statement,
204 : outer_join_type,
205 : char_value_exp,
206 : term,
207 : value_exp_primary,
208 : value_exp,
209 : selection,
210 : fold,
211 : char_substring_fct,
212 : factor,
213 : base_table_def,
214 : base_table_element_commalist,
215 : data_type,
216 : column_def,
217 : table_node,
218 : as_clause,
219 : opt_as,
220 : op_column_commalist,
221 : table_primary_as_range_column,
222 : datetime_primary,
223 : concatenation,
224 : char_factor,
225 : bit_value_fct,
226 : comparison_predicate_part_2,
227 : parenthesized_boolean_value_expression,
228 : character_string_type,
229 : other_like_predicate_part_2,
230 : between_predicate_part_2,
231 : null_predicate_part_2,
232 : cast_spec,
233 : window_function,
234 : rule_count // last value
235 : };
236 :
237 : // must be ascii encoding for the value
238 : OSQLParseNode(const sal_Char* _pValueStr,
239 : SQLNodeType _eNodeType,
240 : sal_uInt32 _nNodeID = 0);
241 :
242 : OSQLParseNode(const OString& _rValue,
243 : SQLNodeType eNewNodeType,
244 : sal_uInt32 nNewNodeID=0);
245 :
246 : OSQLParseNode(const OUString& _rValue,
247 : SQLNodeType _eNodeType,
248 : sal_uInt32 _nNodeID = 0);
249 :
250 : // copies the respective ParseNode
251 : OSQLParseNode(const OSQLParseNode& rParseNode);
252 : OSQLParseNode& operator=(const OSQLParseNode& rParseNode);
253 :
254 : bool operator==(OSQLParseNode& rParseNode) const;
255 :
256 : // destructor destructs the tree recursively
257 : virtual ~OSQLParseNode();
258 :
259 17614 : OSQLParseNode* getParent() const {return m_pParent;};
260 :
261 17025 : void setParent(OSQLParseNode* pParseNode) {m_pParent = pParseNode;};
262 :
263 30046 : size_t count() const {return m_aChildren.size();};
264 : inline OSQLParseNode* getChild(sal_uInt32 nPos) const;
265 :
266 : void append(OSQLParseNode* pNewSubTree);
267 : void insert(sal_uInt32 nPos, OSQLParseNode* pNewSubTree);
268 :
269 : OSQLParseNode* replace(OSQLParseNode* pOldSubTree, OSQLParseNode* pNewSubTree);
270 :
271 : OSQLParseNode* removeAt(sal_uInt32 nPos);
272 :
273 : void replaceNodeValue(const OUString& rTableAlias,const OUString& rColumnName);
274 :
275 : /** parses the node to a string which can be passed to a driver's connection for execution
276 :
277 : Any particles of the parse tree which represent application-level features - such
278 : as queries appearing in the FROM part - are substituted, so that the resulting statement can
279 : be executed at an SDBC-level connection.
280 :
281 : @param _out_rString
282 : is an output parameter taking the resulting SQL statement
283 :
284 : @param _rxConnection
285 : the connection relative to which to parse. This must be an SDB-level connection (e.g.
286 : support the XQueriesSupplier interface) for the method to be able to do all necessary
287 : substitutions.
288 :
289 : @param _rParser
290 : the SQLParser used to create the node. This is needed in case we need to parse
291 : sub queries which are present in the SQL statement - those sub queries need to be parsed,
292 : too, to check whether they contain nested sub queries.
293 :
294 : @param _pErrorHolder
295 : takes the error which occurred while generating the statement, if any. Might be <NULL/>,
296 : in this case the error is not reported back, and can only be recognized by examing the
297 : return value.
298 :
299 : @return
300 : <TRUE/> if and only if the parsing was successful.<br/>
301 :
302 : Currently, there's only one condition how this method can fail: If it contains a nested
303 : query which causes a cycle. E.g., consider a statement <code>SELECT * from "foo"</code>,
304 : where <code>foo</code> is a query defined as <code>SELECT * FROM "bar"</code>, where
305 : <code>bar</code> is defined as <code>SELECT * FROM "foo"</code>. This statement obviously
306 : cannot be parsed to an executable statement.
307 :
308 : If this method returns <FALSE/>, you're encouraged to check and handle the error in
309 : <arg>_pErrorHolder</arg>.
310 : */
311 : bool parseNodeToExecutableStatement( OUString& _out_rString,
312 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
313 : OSQLParser& _rParser,
314 : ::com::sun::star::sdbc::SQLException* _pErrorHolder ) const;
315 :
316 : void parseNodeToStr(OUString& rString,
317 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
318 : const IParseContext* pContext = NULL,
319 : bool _bIntl = false,
320 : bool _bQuote= true) const;
321 :
322 : // quoted and internationalised
323 : void parseNodeToPredicateStr(OUString& rString,
324 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
325 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter,
326 : const ::com::sun::star::lang::Locale& rIntl,
327 : sal_Char _cDec,
328 : const IParseContext* pContext = NULL ) const;
329 :
330 : void parseNodeToPredicateStr(OUString& rString,
331 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
332 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter,
333 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > & _xField,
334 : const OUString &_sTableAlias,
335 : const ::com::sun::star::lang::Locale& rIntl,
336 : sal_Char _cDec,
337 : const IParseContext* pContext = NULL ) const;
338 :
339 : OSQLParseNode* getByRule(OSQLParseNode::Rule eRule) const;
340 :
341 : #if OSL_DEBUG_LEVEL > 1
342 : // shows the ParseTree with tabs and linefeeds
343 : void showParseTree( OUString& rString ) const;
344 : void showParseTree( OUStringBuffer& _inout_rBuf, sal_uInt32 nLevel ) const;
345 : #endif
346 :
347 12785 : SQLNodeType getNodeType() const {return m_eNodeType;};
348 :
349 : // RuleId returns the RuleID of the node's rule (only if IsRule())
350 83771 : sal_uInt32 getRuleID() const {return m_nNodeID;}
351 :
352 : /** returns the ID of the rule represented by the node
353 : If the node does not represent a rule, UNKNOWN_RULE is returned
354 : */
355 : Rule getKnownRuleID() const;
356 :
357 : // returns the TokenId of the node's token (only if !isRule())
358 181 : sal_uInt32 getTokenID() const {return m_nNodeID;}
359 :
360 : // IsRule tests whether a node is a rule (NonTerminal)
361 : // ATTENTION: rules can be leaves, for example empty lists
362 110426 : bool isRule() const
363 132453 : { return (m_eNodeType == SQL_NODE_RULE) || (m_eNodeType == SQL_NODE_LISTRULE)
364 132453 : || (m_eNodeType == SQL_NODE_COMMALISTRULE);}
365 :
366 : // IsToken tests whether a Node is a Token (Terminal but not a rule)
367 15625 : bool isToken() const {return !isRule();}
368 :
369 3401 : const OUString& getTokenValue() const {return m_aNodeValue;}
370 :
371 : void setTokenValue(const OUString& rString) { if (isToken()) m_aNodeValue = rString;}
372 :
373 306 : bool isLeaf() const {return m_aChildren.empty();}
374 :
375 : // negate only a searchcondition, any other rule could cause a gpf
376 : static void negateSearchCondition(OSQLParseNode*& pSearchCondition, bool bNegate=false);
377 :
378 : // normalize a logic form
379 : // e.q. (a or b) and (c or d) <=> a and c or a and d or b and c or b and d
380 : static void disjunctiveNormalForm(OSQLParseNode*& pSearchCondition);
381 :
382 : // Simplifies logic expressions
383 : // a and a = a
384 : // a or a = a
385 : // a and ( a + b) = a
386 : // a or a and b = a
387 : static void absorptions(OSQLParseNode*& pSearchCondition);
388 :
389 : // erase unnecessary braces
390 : static void eraseBraces(OSQLParseNode*& pSearchCondition);
391 :
392 : // makes the logic formula a little smaller
393 : static void compress(OSQLParseNode*& pSearchCondition);
394 : // return the catalog, schema and tablename form this node
395 : // _pTableNode must be a rule of that above or a SQL_TOKEN_NAME
396 : static bool getTableComponents(const OSQLParseNode* _pTableNode,
397 : ::com::sun::star::uno::Any &_rCatalog,
398 : OUString &_rSchema,
399 : OUString &_rTable
400 : ,const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData >& _xMetaData);
401 :
402 : // substitute all occurrences of :var or [name] into the dynamic parameter ?
403 : // _pNode will be modified if parameters exists
404 : static void substituteParameterNames(OSQLParseNode* _pNode);
405 :
406 : /** return a table range when it exists.
407 : */
408 : static OUString getTableRange(const OSQLParseNode* _pTableRef);
409 :
410 : protected:
411 : // ParseNodeToStr concatenates all Tokens (leaves) of the ParseNodes.
412 : void parseNodeToStr(OUString& rString,
413 : const ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection >& _rxConnection,
414 : const ::com::sun::star::uno::Reference< ::com::sun::star::util::XNumberFormatter > & xFormatter,
415 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > & _xField,
416 : const OUString &_sPredicateTableAlias,
417 : const ::com::sun::star::lang::Locale& rIntl,
418 : const IParseContext* pContext,
419 : bool _bIntl,
420 : bool _bQuote,
421 : sal_Char _cDecSep,
422 : bool _bPredicate,
423 : bool _bSubstitute) const;
424 :
425 : private:
426 : void impl_parseNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam, bool bSimple=true ) const;
427 : void impl_parseLikeNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam, bool bSimple=true ) const;
428 : void impl_parseTableRangeNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam ) const;
429 :
430 : /** parses a table_name node into a SQL statement particle.
431 : @return
432 : <TRUE/> if and only if parsing was successful, <FALSE/> if default handling should
433 : be applied.
434 : */
435 : bool impl_parseTableNameNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam ) const;
436 :
437 : bool addDateValue(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const;
438 : static OUString convertDateTimeString(const SQLParseNodeParameter& rParam, const OUString& rString);
439 : static OUString convertDateString(const SQLParseNodeParameter& rParam, const OUString& rString);
440 : static OUString convertTimeString(const SQLParseNodeParameter& rParam, const OUString& rString);
441 : void parseLeaf(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const;
442 : };
443 :
444 38735 : inline OSQLParseNode* OSQLParseNode::getChild(sal_uInt32 nPos) const
445 : {
446 : assert(nPos < m_aChildren.size());
447 :
448 38735 : return m_aChildren[nPos];
449 : }
450 :
451 : // utilities to query for a specific rule, token or punctuation
452 : #define SQL_ISRULE(pParseNode, eRule) ((pParseNode)->isRule() && (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::eRule))
453 : #define SQL_ISRULEOR2(pParseNode, e1, e2) ((pParseNode)->isRule() && ( \
454 : (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e1) || \
455 : (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e2)))
456 : #define SQL_ISRULEOR3(pParseNode, e1, e2, e3) ((pParseNode)->isRule() && ( \
457 : (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e1) || \
458 : (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e2) || \
459 : (pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e3)))
460 : #define SQL_ISTOKEN(pParseNode, token) ((pParseNode)->isToken() && (pParseNode)->getTokenID() == SQL_TOKEN_##token)
461 : #define SQL_ISTOKENOR2(pParseNode, tok0, tok1) ((pParseNode)->isToken() && ( (pParseNode)->getTokenID() == SQL_TOKEN_##tok0 || (pParseNode)->getTokenID() == SQL_TOKEN_##tok1 ))
462 : #define SQL_ISTOKENOR3(pParseNode, tok0, tok1, tok2) ((pParseNode)->isToken() && ( (pParseNode)->getTokenID() == SQL_TOKEN_##tok0 || (pParseNode)->getTokenID() == SQL_TOKEN_##tok1 || (pParseNode)->getTokenID() == SQL_TOKEN_##tok2 ))
463 : #define SQL_ISPUNCTUATION(pParseNode, aString) ((pParseNode)->getNodeType() == SQL_NODE_PUNCTUATION && (pParseNode)->getTokenValue() == (aString))
464 : }
465 :
466 : #endif // INCLUDED_CONNECTIVITY_SQLNODE_HXX
467 :
468 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|