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