LibreOffice Module connectivity (master) 1
sqlnode.cxx
Go to the documentation of this file.
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
20#include <sal/macros.h>
25#define YYBISON 1
26#include <sqlbison.hxx>
29#include <com/sun/star/lang/Locale.hpp>
30#include <com/sun/star/util/XNumberFormatter.hpp>
31#include <com/sun/star/util/XNumberFormatTypes.hpp>
32#include <com/sun/star/i18n/LocaleData.hpp>
33#include <com/sun/star/i18n/NumberFormatIndex.hpp>
34#include <com/sun/star/beans/XPropertySet.hpp>
35#include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
36#include <com/sun/star/sdbc/DataType.hpp>
37#include <com/sun/star/sdb/XQueriesSupplier.hpp>
38#include <com/sun/star/sdb/ErrorCondition.hpp>
39#include <com/sun/star/util/XNumberFormatsSupplier.hpp>
40#include <com/sun/star/util/XNumberFormats.hpp>
41#include <com/sun/star/util/NumberFormat.hpp>
42#include <com/sun/star/i18n/KParseType.hpp>
43#include <com/sun/star/i18n/KParseTokens.hpp>
44#include <com/sun/star/i18n/CharacterClassification.hpp>
46#include <com/sun/star/util/DateTime.hpp>
47#include <com/sun/star/util/Time.hpp>
48#include <com/sun/star/util/Date.hpp>
49#include <TConnection.hxx>
54#include <string.h>
55#include <algorithm>
56#include <functional>
57#include <memory>
58#include <string_view>
59
60#include <rtl/ustrbuf.hxx>
61#include <sal/log.hxx>
62#include <utility>
63
64using namespace ::com::sun::star::sdbc;
65using namespace ::com::sun::star::util;
66using namespace ::com::sun::star::beans;
67using namespace ::com::sun::star::sdb;
68using namespace ::com::sun::star::uno;
69using namespace ::com::sun::star::lang;
70using namespace ::com::sun::star::i18n;
71using namespace ::com::sun::star;
72using namespace ::osl;
73using namespace ::dbtools;
74using namespace ::comphelper;
75
76namespace
77{
78
79 bool lcl_saveConvertToNumber(const Reference< XNumberFormatter > & _xFormatter,sal_Int32 _nKey,const OUString& _sValue,double& _nrValue)
80 {
81 bool bRet = false;
82 try
83 {
84 _nrValue = _xFormatter->convertStringToNumber(_nKey, _sValue);
85 bRet = true;
86 }
87 catch(Exception&)
88 {
89 }
90 return bRet;
91 }
92
93 void replaceAndReset(connectivity::OSQLParseNode*& _pResetNode,connectivity::OSQLParseNode* _pNewNode)
94 {
95 _pResetNode->getParent()->replaceAndDelete(_pResetNode, _pNewNode);
96 _pResetNode = _pNewNode;
97 }
98
109 OUString SetQuotation(const OUString& rValue, std::u16string_view rQuote, std::u16string_view rQuoteToReplace)
110 {
111 // Replace quotes with double quotes or the parser gets into problems
112 if (!rQuote.empty())
113 return rQuote + rValue.replaceAll(rQuote, rQuoteToReplace) + rQuote;
114 return rValue;
115 }
116
117 bool columnMatchP(const connectivity::OSQLParseNode* pSubTree, const connectivity::SQLParseNodeParameter& rParam)
118 {
119 using namespace connectivity;
120 assert(SQL_ISRULE(pSubTree,column_ref));
121
122 if(!rParam.xField.is())
123 return false;
124
125 // retrieve the field's name & table range
126 OUString aFieldName;
127 try
128 {
129 sal_Int32 nNamePropertyId = PROPERTY_ID_NAME;
130 if ( rParam.xField->getPropertySetInfo()->hasPropertyByName( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_REALNAME ) ) )
131 nNamePropertyId = PROPERTY_ID_REALNAME;
132 rParam.xField->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( nNamePropertyId ) ) >>= aFieldName;
133 }
134 catch ( Exception& )
135 {
136 }
137
138 if(!pSubTree->count())
139 return false;
140
141 const OSQLParseNode* pCol = pSubTree->getChild(pSubTree->count()-1);
142 if (SQL_ISRULE(pCol,column_val))
143 {
144 assert(pCol->count() == 1);
145 pCol = pCol->getChild(0);
146 }
147 const OSQLParseNode* pTable(nullptr);
148 switch (pSubTree->count())
149 {
150 case 1:
151 break;
152 case 3:
153 pTable = pSubTree->getChild(0);
154 break;
155 case 5:
156 case 7:
157 SAL_WARN("connectivity.parse", "SQL: catalog and/or schema in column_ref in predicate");
158 break;
159 default:
160 SAL_WARN("connectivity.parse", "columnMatchP: SQL grammar changed; column_ref has " << pSubTree->count() << " children");
161 assert(false);
162 break;
163 }
164 // TODO: not all DBMS match column names case-insensitively...
165 // see XDatabaseMetaData::supportsMixedCaseIdentifiers()
166 // and XDatabaseMetaData::supportsMixedCaseQuotedIdentifiers()
167 if ( // table name matches (or no table name)?
168 ( !pTable || pTable->getTokenValue().equalsIgnoreAsciiCase(rParam.sPredicateTableAlias) )
169 && // column name matches?
170 pCol->getTokenValue().equalsIgnoreAsciiCase(aFieldName)
171 )
172 return true;
173 return false;
174 }
175}
176
177namespace connectivity
178{
179
180SQLParseNodeParameter::SQLParseNodeParameter( const Reference< XConnection >& _rxConnection,
181 const Reference< XNumberFormatter >& _xFormatter, const Reference< XPropertySet >& _xField,
182 OUString _sPredicateTableAlias,
183 const Locale& _rLocale, const IParseContext* _pContext,
184 bool _bIntl, bool _bQuote, OUString _sDecSep, bool _bPredicate, bool _bParseToSDBC )
185 :rLocale(_rLocale)
186 ,aMetaData( _rxConnection )
187 ,pParser( nullptr )
188 ,pSubQueryHistory( std::make_shared<QueryNameSet>() )
189 ,xFormatter(_xFormatter)
190 ,xField(_xField)
191 ,sPredicateTableAlias(std::move(_sPredicateTableAlias))
192 ,m_rContext( _pContext ? *_pContext : OSQLParser::s_aDefaultContext )
193 ,sDecSep(std::move(_sDecSep))
194 ,bQuote(_bQuote)
195 ,bInternational(_bIntl)
196 ,bPredicate(_bPredicate)
197 ,bParseToSDBCLevel( _bParseToSDBC )
198{
199}
200
201OUString OSQLParseNode::convertDateString(const SQLParseNodeParameter& rParam, std::u16string_view rString)
202{
203 Date aDate = DBTypeConversion::toDate(rString);
204 Reference< XNumberFormatsSupplier > xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
205 Reference< XNumberFormatTypes > xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
206
207 double fDate = DBTypeConversion::toDouble(aDate,DBTypeConversion::getNULLDate(xSupplier));
208 sal_Int32 nKey = xTypes->getFormatIndex(NumberFormatIndex::DATE_SYS_DDMMYYYY, rParam.rLocale);
209 return rParam.xFormatter->convertNumberToString(nKey, fDate);
210}
211
212
213OUString OSQLParseNode::convertDateTimeString(const SQLParseNodeParameter& rParam, const OUString& rString)
214{
215 DateTime aDate = DBTypeConversion::toDateTime(rString);
216 Reference< XNumberFormatsSupplier > xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
217 Reference< XNumberFormatTypes > xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
218
219 double fDateTime = DBTypeConversion::toDouble(aDate,DBTypeConversion::getNULLDate(xSupplier));
220 sal_Int32 nKey = xTypes->getFormatIndex(NumberFormatIndex::DATETIME_SYS_DDMMYYYY_HHMMSS, rParam.rLocale);
221 return rParam.xFormatter->convertNumberToString(nKey, fDateTime);
222}
223
224
225OUString OSQLParseNode::convertTimeString(const SQLParseNodeParameter& rParam, std::u16string_view rString)
226{
227 css::util::Time aTime = DBTypeConversion::toTime(rString);
228 Reference< XNumberFormatsSupplier > xSupplier(rParam.xFormatter->getNumberFormatsSupplier());
229
230 Reference< XNumberFormatTypes > xTypes(xSupplier->getNumberFormats(), UNO_QUERY);
231
232 double fTime = DBTypeConversion::toDouble(aTime);
233 sal_Int32 nKey = xTypes->getFormatIndex(NumberFormatIndex::TIME_HHMMSS, rParam.rLocale);
234 return rParam.xFormatter->convertNumberToString(nKey, fTime);
235}
236
237
238void OSQLParseNode::parseNodeToStr(OUString& rString,
239 const Reference< XConnection >& _rxConnection,
240 const IParseContext* pContext,
241 bool _bIntl,
242 bool _bQuote) const
243{
245 rString, _rxConnection, nullptr, nullptr, OUString(),
246 pContext ? pContext->getPreferredLocale() : OParseContext::getDefaultLocale(),
247 pContext, _bIntl, _bQuote, OUString("."), false );
248}
249
250
251void OSQLParseNode::parseNodeToPredicateStr(OUString& rString,
252 const Reference< XConnection >& _rxConnection,
253 const Reference< XNumberFormatter > & xFormatter,
254 const css::lang::Locale& rIntl,
255 OUString _sDec,
256 const IParseContext* pContext ) const
257{
258 OSL_ENSURE(xFormatter.is(), "OSQLParseNode::parseNodeToPredicateStr:: no formatter!");
259
260 if (xFormatter.is())
261 parseNodeToStr(rString, _rxConnection, xFormatter, nullptr, OUString(), rIntl, pContext, true, true, _sDec, true);
262}
263
264
265void OSQLParseNode::parseNodeToPredicateStr(OUString& rString,
266 const Reference< XConnection > & _rxConnection,
267 const Reference< XNumberFormatter > & xFormatter,
268 const Reference< XPropertySet > & _xField,
269 const OUString &_sPredicateTableAlias,
270 const css::lang::Locale& rIntl,
271 OUString _sDec,
272 const IParseContext* pContext ) const
273{
274 OSL_ENSURE(xFormatter.is(), "OSQLParseNode::parseNodeToPredicateStr:: no formatter!");
275
276 if (xFormatter.is())
277 parseNodeToStr( rString, _rxConnection, xFormatter, _xField, _sPredicateTableAlias, rIntl, pContext, true, true, _sDec, true );
278}
279
280
281void OSQLParseNode::parseNodeToStr(OUString& rString,
282 const Reference< XConnection > & _rxConnection,
283 const Reference< XNumberFormatter > & xFormatter,
284 const Reference< XPropertySet > & _xField,
285 const OUString &_sPredicateTableAlias,
286 const css::lang::Locale& rIntl,
287 const IParseContext* pContext,
288 bool _bIntl,
289 bool _bQuote,
290 OUString _sDecSep,
291 bool _bPredicate) const
292{
293 OSL_ENSURE( _rxConnection.is(), "OSQLParseNode::parseNodeToStr: invalid connection!" );
294
295 if ( !_rxConnection.is() )
296 return;
297
298 OUStringBuffer sBuffer(rString);
299 try
300 {
303 _rxConnection, xFormatter, _xField, _sPredicateTableAlias, rIntl, pContext,
304 _bIntl, _bQuote, _sDecSep, _bPredicate, false
305 ) );
306 }
307 catch( const SQLException& )
308 {
309 SAL_WARN( "connectivity.parse", "OSQLParseNode::parseNodeToStr: this should not throw!" );
310 // our callers don't expect this method to throw anything. The only known situation
311 // where impl_parseNodeToString_throw can throw is when there is a cyclic reference
312 // in the sub queries, but this cannot be the case here, as we do not parse to
313 // SDBC level.
314 }
315 rString = sBuffer.makeStringAndClear();
316}
317
318bool OSQLParseNode::parseNodeToExecutableStatement( OUString& _out_rString, const Reference< XConnection >& _rxConnection,
319 OSQLParser& _rParser, css::sdbc::SQLException* _pErrorHolder ) const
320{
321 OSL_PRECOND( _rxConnection.is(), "OSQLParseNode::parseNodeToExecutableStatement: invalid connection!" );
322 SQLParseNodeParameter aParseParam( _rxConnection,
323 nullptr, nullptr, OUString(), OParseContext::getDefaultLocale(), nullptr, false, true, OUString("."), false, true );
324
325 if ( aParseParam.aMetaData.supportsSubqueriesInFrom() )
326 {
327 Reference< XQueriesSupplier > xSuppQueries( _rxConnection, UNO_QUERY );
328 OSL_ENSURE( xSuppQueries.is(), "OSQLParseNode::parseNodeToExecutableStatement: cannot substitute everything without a QueriesSupplier!" );
329 if ( xSuppQueries.is() )
330 aParseParam.xQueries = xSuppQueries->getQueries();
331 }
332
333 aParseParam.pParser = &_rParser;
334
335 // LIMIT keyword differs in Firebird
336 OSQLParseNode* pTableExp = getChild(3);
337 Reference< XDatabaseMetaData > xMeta( _rxConnection->getMetaData() );
338 OUString sLimitValue;
339 if( pTableExp->getChild(6)->count() >= 2 && pTableExp->getChild(6)->getChild(1)
340 && (xMeta->getURL().equalsIgnoreAsciiCase("sdbc:embedded:firebird")
341 || xMeta->getURL().startsWithIgnoreAsciiCase("sdbc:firebird:")))
342 {
343 sLimitValue = pTableExp->getChild(6)->getChild(1)->getTokenValue();
344 delete pTableExp->removeAt(6);
345 }
346
347 _out_rString.clear();
348 OUStringBuffer sBuffer;
349 bool bSuccess = false;
350 try
351 {
352 impl_parseNodeToString_throw( sBuffer, aParseParam );
353 bSuccess = true;
354 }
355 catch( const SQLException& e )
356 {
357 if ( _pErrorHolder )
358 *_pErrorHolder = e;
359 }
360
361 if(sLimitValue.getLength() > 0)
362 {
363 constexpr char SELECT_KEYWORD[] = "SELECT";
364 sBuffer.insert(sBuffer.indexOf(SELECT_KEYWORD) + strlen(SELECT_KEYWORD),
365 Concat2View(" FIRST " + sLimitValue));
366 }
367
368 _out_rString = sBuffer.makeStringAndClear();
369 return bSuccess;
370}
371
372
373namespace
374{
375 bool lcl_isAliasNamePresent( const OSQLParseNode& _rTableNameNode )
376 {
377 return !OSQLParseNode::getTableRange(_rTableNameNode.getParent()).isEmpty();
378 }
379}
380
381
382void OSQLParseNode::impl_parseNodeToString_throw(OUStringBuffer& rString, const SQLParseNodeParameter& rParam, bool bSimple) const
383{
384 if ( isToken() )
385 {
386 parseLeaf(rString,rParam);
387 return;
388 }
389
390 // Lets see how many nodes this subtree has
391 sal_uInt32 nCount = count();
392
393 bool bHandled = false;
394 switch ( getKnownRuleID() )
395 {
396 // special handling for parameters
397 case parameter:
398 {
399 bSimple=false;
400 if(!rString.isEmpty())
401 rString.append(" ");
402 if (nCount == 1) // ?
403 m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam, false );
405 {
406 rString.append("?");
407 }
408 else if (nCount == 2) // :Name
409 {
410 m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam, false );
411 rString.append(m_aChildren[1]->m_aNodeValue);
412 } // [Name]
413 else
414 {
415 assert (nCount == 3);
416 m_aChildren[0]->impl_parseNodeToString_throw( rString, rParam, false );
417 rString.append(m_aChildren[1]->m_aNodeValue);
418 rString.append(m_aChildren[2]->m_aNodeValue);
419 }
420 bHandled = true;
421 }
422 break;
423
424 // table refs
425 case table_ref:
426 bSimple=false;
427 if ( ( nCount == 2 ) || ( nCount == 3 ) || ( nCount == 5 ) )
428 {
430 bHandled = true;
431 }
432 break;
433
434 // table name - might be a query name
435 case table_name:
436 bSimple=false;
437 bHandled = impl_parseTableNameNodeToString_throw( rString, rParam );
438 break;
439
440 case as_clause:
441 bSimple=false;
442 assert(nCount == 0 || nCount == 2);
443 if (nCount == 2)
444 {
446 rString.append(" AS ");
447 m_aChildren[1]->impl_parseNodeToString_throw( rString, rParam, false );
448 }
449 bHandled = true;
450 break;
451
452 case opt_as:
453 assert(nCount == 0);
454 bHandled = true;
455 break;
456
457 case like_predicate:
458 // Depending on whether international is given, LIKE is treated differently
459 // international: *, ? are placeholders
460 // else SQL92 conform: %, _
461 impl_parseLikeNodeToString_throw( rString, rParam, bSimple );
462 bHandled = true;
463 break;
464
465 case general_set_fct:
466 case set_fct_spec:
467 case position_exp:
468 case extract_exp:
469 case length_exp:
470 case char_value_fct:
471 bSimple=false;
472 if (!addDateValue(rString, rParam))
473 {
474 // Do not quote function name
475 SQLParseNodeParameter aNewParam(rParam);
476 aNewParam.bQuote = ( SQL_ISRULE(this,length_exp) || SQL_ISRULE(this,char_value_fct) );
477
478 m_aChildren[0]->impl_parseNodeToString_throw( rString, aNewParam, false );
479 aNewParam.bQuote = rParam.bQuote;
480 //aNewParam.bPredicate = sal_False; // disable [ ] around names // look at i73215
481 OUStringBuffer aStringPara;
482 for (sal_uInt32 i=1; i<nCount; i++)
483 {
484 const OSQLParseNode * pSubTree = m_aChildren[i].get();
485 if (pSubTree)
486 {
487 pSubTree->impl_parseNodeToString_throw( aStringPara, aNewParam, false );
488
489 // In the comma lists, put commas in-between all subtrees
490 if ((m_eNodeType == SQLNodeType::CommaListRule) && (i < (nCount - 1)))
491 aStringPara.append(",");
492 }
493 else
494 i++;
495 }
496 rString.append(aStringPara);
497 }
498 bHandled = true;
499 break;
500 case odbc_call_spec:
501 case subquery:
502 case term:
503 case factor:
504 case window_function:
505 case cast_spec:
506 case num_value_exp:
507 bSimple = false;
508 break;
509 default:
510 break;
511 } // switch ( getKnownRuleID() )
512
513 if ( bHandled )
514 return;
515
516 for (auto i = m_aChildren.begin(); i != m_aChildren.end();)
517 {
518 const OSQLParseNode* pSubTree = i->get();
519 if ( !pSubTree )
520 {
521 ++i;
522 continue;
523 }
524
525 SQLParseNodeParameter aNewParam(rParam);
526
527 // don't replace the field for subqueries
528 if (rParam.xField.is() && SQL_ISRULE(pSubTree,subquery))
529 aNewParam.xField = nullptr;
530
531 // When we are building a criterion inside a query view,
532 // simplify criterion display by removing:
533 // "currentFieldName"
534 // "currentFieldName" =
535 // but only in simple expressions.
536 // This means anything that is made of:
537 // (see the rules conditionalised by inPredicateCheck() in sqlbison.y).
538 // - parentheses
539 // - logical operators (and, or, not)
540 // - comparison operators (IS, =, >, <, BETWEEN, LIKE, ...)
541 // but *not* e.g. in function arguments
542 if (bSimple && rParam.bPredicate && rParam.xField.is() && SQL_ISRULE(pSubTree,column_ref))
543 {
544 if (columnMatchP(pSubTree, rParam))
545 {
546 // skip field
547 ++i;
548 // if the following node is the comparison operator'=',
549 // we filter it as well
551 {
552 if(i != m_aChildren.end())
553 {
554 pSubTree = i->get();
555 if (pSubTree && pSubTree->getNodeType() == SQLNodeType::Equal)
556 ++i;
557 }
558 }
559 }
560 else
561 {
562 pSubTree->impl_parseNodeToString_throw( rString, aNewParam, bSimple );
563 ++i;
564
565 // In the comma lists, put commas in-between all subtrees
567 rString.append(",");
568 }
569 }
570 else
571 {
572 pSubTree->impl_parseNodeToString_throw( rString, aNewParam, bSimple );
573 ++i;
574
575 // In the comma lists, put commas in-between all subtrees
577 {
578 if (SQL_ISRULE(this,value_exp_commalist) && rParam.bPredicate)
579 rString.append(";");
580 else
581 rString.append(",");
582 }
583 }
584 // The right hand-side of these operators is not simple
585 switch ( getKnownRuleID() )
586 {
587 case general_set_fct:
588 case set_fct_spec:
589 case position_exp:
590 case extract_exp:
591 case length_exp:
592 case char_value_fct:
593 case odbc_call_spec:
594 case subquery:
597 case like_predicate:
598 case test_for_null:
599 case in_predicate:
600 case existence_test:
601 case unique_test:
603 case join_condition:
608 bSimple=false;
609 break;
610 default:
611 break;
612 }
613 }
614}
615
616
617bool OSQLParseNode::impl_parseTableNameNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam ) const
618{
619 // is the table_name part of a table_ref?
620 OSL_ENSURE( getParent(), "OSQLParseNode::impl_parseTableNameNodeToString_throw: table_name without parent?" );
621 if ( !getParent() || ( getParent()->getKnownRuleID() != table_ref ) )
622 return false;
623
624 // if it's a query, maybe we need to substitute the SQL statement ...
625 if ( !rParam.bParseToSDBCLevel )
626 return false;
627
628 if ( !rParam.xQueries.is() )
629 // connection does not support queries in queries, or was no query supplier
630 return false;
631
632 try
633 {
634 OUString sTableOrQueryName( getChild(0)->getTokenValue() );
635 bool bIsQuery = rParam.xQueries->hasByName( sTableOrQueryName );
636 if ( !bIsQuery )
637 return false;
638
639 // avoid recursion (e.g. "foo" defined as "SELECT * FROM bar" and "bar" defined as "SELECT * FROM foo".
640 if ( rParam.pSubQueryHistory->find( sTableOrQueryName ) != rParam.pSubQueryHistory->end() )
641 {
642 OSL_ENSURE( rParam.pParser, "OSQLParseNode::impl_parseTableNameNodeToString_throw: no parser?" );
643 if ( rParam.pParser )
644 {
645 const SQLError& rErrors( rParam.pParser->getErrorHelper() );
646 rErrors.raiseException( sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES );
647 }
648 else
649 {
650 SQLError aErrors;
651 aErrors.raiseException( sdb::ErrorCondition::PARSER_CYCLIC_SUB_QUERIES );
652 }
653 }
654 rParam.pSubQueryHistory->insert( sTableOrQueryName );
655
656 Reference< XPropertySet > xQuery( rParam.xQueries->getByName( sTableOrQueryName ), UNO_QUERY_THROW );
657
658 // substitute the query name with the constituting command
659 OUString sCommand;
660 OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_COMMAND ) ) >>= sCommand );
661
662 bool bEscapeProcessing = false;
663 OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ESCAPEPROCESSING ) ) >>= bEscapeProcessing );
664
665 // the query we found here might itself be based on another query, so parse it recursively
666 OSL_ENSURE( rParam.pParser, "OSQLParseNode::impl_parseTableNameNodeToString_throw: cannot analyze sub queries without a parser!" );
667 if ( bEscapeProcessing && rParam.pParser )
668 {
669 OUString sError;
670 std::unique_ptr< OSQLParseNode > pSubQueryNode( rParam.pParser->parseTree( sError, sCommand ) );
671 if (pSubQueryNode)
672 {
673 // parse the sub-select to SDBC level, too
674 OUStringBuffer sSubSelect;
675 pSubQueryNode->impl_parseNodeToString_throw( sSubSelect, rParam, false );
676 if ( !sSubSelect.isEmpty() )
677 sCommand = sSubSelect.makeStringAndClear();
678 }
679 }
680
681 rString.append( " ( " );
682 rString.append(sCommand);
683 rString.append( " )" );
684
685 // append the query name as table alias, since it might be referenced in other
686 // parts of the statement - but only if there's no other alias name present
687 if ( !lcl_isAliasNamePresent( *this ) )
688 {
689 rString.append( " AS " );
690 if ( rParam.bQuote )
691 rString.append(SetQuotation( sTableOrQueryName,
693 }
694
695 // don't forget to remove the query name from the history, else multiple inclusions
696 // won't work
697 // #i69227# / 2006-10-10 / frank.schoenheit@sun.com
698 rParam.pSubQueryHistory->erase( sTableOrQueryName );
699
700 return true;
701 }
702 catch( const SQLException& )
703 {
704 throw;
705 }
706 catch( const Exception& )
707 {
708 DBG_UNHANDLED_EXCEPTION("connectivity.parse");
709 }
710 return false;
711}
712
713
714void OSQLParseNode::impl_parseTableRangeNodeToString_throw(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
715{
716 OSL_PRECOND( ( count() == 2 ) || ( count() == 3 ) || ( count() == 5 ) ,"Illegal count");
717
718 // rString += " ";
719 std::for_each(m_aChildren.begin(),m_aChildren.end(),
720 [&] (std::unique_ptr<OSQLParseNode> const & pNode) { pNode->impl_parseNodeToString_throw(rString, rParam, false); });
721}
722
723
724void OSQLParseNode::impl_parseLikeNodeToString_throw( OUStringBuffer& rString, const SQLParseNodeParameter& rParam, bool bSimple ) const
725{
726 assert(SQL_ISRULE(this,like_predicate));
727 OSL_ENSURE(count() == 2,"count != 2: Prepare for GPF");
728
729 const OSQLParseNode* pEscNode = nullptr;
730 const OSQLParseNode* pParaNode = nullptr;
731
732 SQLParseNodeParameter aNewParam(rParam);
733 //aNewParam.bQuote = sal_True; // why setting this to true? @see https://bz.apache.org/ooo/show_bug.cgi?id=75557
734
735 if ( !(bSimple && rParam.bPredicate && rParam.xField.is() && SQL_ISRULE(m_aChildren[0],column_ref) && columnMatchP(m_aChildren[0].get(), rParam)) )
736 m_aChildren[0]->impl_parseNodeToString_throw( rString, aNewParam, bSimple );
737
738 const OSQLParseNode* pPart2 = m_aChildren[1].get();
739 pPart2->getChild(0)->impl_parseNodeToString_throw( rString, aNewParam, false );
740 pPart2->getChild(1)->impl_parseNodeToString_throw( rString, aNewParam, false );
741 pParaNode = pPart2->getChild(2);
742 pEscNode = pPart2->getChild(3);
743
744 if (pParaNode->isToken())
745 {
746 OUString aStr = ConvertLikeToken(pParaNode, pEscNode, rParam.bInternational);
747 rString.append(" ");
748 rString.append(SetQuotation(aStr, u"\'", u"\'\'"));
749 }
750 else
751 pParaNode->impl_parseNodeToString_throw( rString, aNewParam, false );
752
753 pEscNode->impl_parseNodeToString_throw( rString, aNewParam, false );
754}
755
756
758 css::uno::Any &_rCatalog,
759 OUString &_rSchema,
760 OUString &_rTable,
761 const Reference< XDatabaseMetaData >& _xMetaData)
762{
763 OSL_ENSURE(_pTableNode,"Wrong use of getTableComponents! _pTableNode is not allowed to be null!");
764 if(_pTableNode)
765 {
766 const bool bSupportsCatalog = _xMetaData.is() && _xMetaData->supportsCatalogsInDataManipulation();
767 const bool bSupportsSchema = _xMetaData.is() && _xMetaData->supportsSchemasInDataManipulation();
768 const OSQLParseNode* pTableNode = _pTableNode;
769 // clear the parameter given
770 _rCatalog = Any();
771 _rSchema.clear();
772 _rTable.clear();
773 // see rule catalog_name: in sqlbison.y
774 if (SQL_ISRULE(pTableNode,catalog_name))
775 {
776 OSL_ENSURE(pTableNode->getChild(0) && pTableNode->getChild(0)->isToken(),"Invalid parsenode!");
777 _rCatalog <<= pTableNode->getChild(0)->getTokenValue();
778 pTableNode = pTableNode->getChild(2);
779 }
780 // check if we have schema_name rule
781 if(SQL_ISRULE(pTableNode,schema_name))
782 {
783 if ( bSupportsCatalog && !bSupportsSchema )
784 _rCatalog <<= pTableNode->getChild(0)->getTokenValue();
785 else
786 _rSchema = pTableNode->getChild(0)->getTokenValue();
787 pTableNode = pTableNode->getChild(2);
788 }
789 // check if we have table_name rule
790 if(SQL_ISRULE(pTableNode,table_name))
791 {
792 _rTable = pTableNode->getChild(0)->getTokenValue();
793 }
794 else
795 {
796 SAL_WARN( "connectivity.parse","Error in parse tree!");
797 }
798 }
799 return !_rTable.isEmpty();
800}
801
803{
804 if ( pLiteral )
805 {
806 if ( s_xLocaleData.get()->get()->getLocaleItem( m_pData->aLocale ).decimalSeparator.toChar() == ',' )
807 {
808 pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace('.', sal_Unicode());
809 // and replace decimal
810 pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace(',', '.');
811 }
812 else
813 pLiteral->m_aNodeValue = pLiteral->m_aNodeValue.replace(',', sal_Unicode());
814 }
815}
816
818{
819 if ( !pLiteral )
820 return nullptr;
821
822 OSQLParseNode* pReturn = pLiteral;
823
824 if ( ( pLiteral->isRule() && !SQL_ISRULE(pLiteral,value_exp) ) || SQL_ISTOKEN(pLiteral,FALSE) || SQL_ISTOKEN(pLiteral,TRUE) )
825 {
826 switch(nType)
827 {
828 case DataType::CHAR:
829 case DataType::VARCHAR:
830 case DataType::LONGVARCHAR:
831 case DataType::CLOB:
832 if ( !SQL_ISRULE(pReturn,char_value_exp) && !buildStringNodes(pReturn) )
833 pReturn = nullptr;
834 break;
835 default:
836 break;
837 }
838 }
839 else
840 {
841 switch(pLiteral->getNodeType())
842 {
844 switch(nType)
845 {
846 case DataType::CHAR:
847 case DataType::VARCHAR:
848 case DataType::LONGVARCHAR:
849 case DataType::CLOB:
850 break;
851 case DataType::DATE:
852 case DataType::TIME:
853 case DataType::TIMESTAMP:
854 if (m_xFormatter.is())
855 pReturn = buildDate( nType, pReturn);
856 break;
857 default:
859 break;
860 }
861 break;
863 switch(nType)
864 {
865 case DataType::DATE:
866 case DataType::TIME:
867 case DataType::TIMESTAMP:
868 if ( m_xFormatter.is() )
869 pReturn = buildDate( nType, pReturn);
870 else
872 break;
873 default:
875 break;
876 }
877 break;
879 switch(nType)
880 {
881 case DataType::BIT:
882 case DataType::BOOLEAN:
883 case DataType::DECIMAL:
884 case DataType::NUMERIC:
885 case DataType::TINYINT:
886 case DataType::SMALLINT:
887 case DataType::INTEGER:
888 case DataType::BIGINT:
889 case DataType::FLOAT:
890 case DataType::REAL:
891 case DataType::DOUBLE:
892 // kill thousand separators if any
893 killThousandSeparator(pReturn);
894 break;
895 case DataType::CHAR:
896 case DataType::VARCHAR:
897 case DataType::LONGVARCHAR:
898 case DataType::CLOB:
899 pReturn = buildNode_STR_NUM(pReturn);
900 break;
901 default:
903 break;
904 }
905 break;
907 switch(nType)
908 {
909 case DataType::DECIMAL:
910 case DataType::NUMERIC:
911 case DataType::FLOAT:
912 case DataType::REAL:
913 case DataType::DOUBLE:
914 // kill thousand separators if any
915 killThousandSeparator(pReturn);
916 break;
917 case DataType::CHAR:
918 case DataType::VARCHAR:
919 case DataType::LONGVARCHAR:
920 case DataType::CLOB:
921 pReturn = buildNode_STR_NUM(pReturn);
922 break;
923 case DataType::INTEGER:
924 default:
926 break;
927 }
928 break;
929 default:
930 ;
931 }
932 }
933 return pReturn;
934}
935
936sal_Int16 OSQLParser::buildPredicateRule(OSQLParseNode*& pAppend, OSQLParseNode* pLiteral, OSQLParseNode* pCompare, OSQLParseNode* pLiteral2)
937{
938 OSL_ENSURE(inPredicateCheck(),"Only in predicate check allowed!");
939 sal_Int16 nErg = 0;
940 if ( m_xField.is() )
941 {
942 sal_Int32 nType = 0;
943 try
944 {
945 m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
946 }
947 catch( Exception& )
948 {
949 return nErg;
950 }
951
952 OSQLParseNode* pNode1 = convertNode(nType,pLiteral);
953 if ( pNode1 )
954 {
955 OSQLParseNode* pNode2 = convertNode(nType,pLiteral2);
956 if ( m_sErrorMessage.isEmpty() )
957 nErg = buildNode(pAppend,pCompare,pNode1,pNode2);
958 }
959 }
960 if (!pCompare->getParent()) // I have no parent so I was not used and I must die :-)
961 delete pCompare;
962 return nErg;
963}
964
965sal_Int16 OSQLParser::buildLikeRule(OSQLParseNode* pAppend, OSQLParseNode*& pLiteral, const OSQLParseNode* pEscape)
966{
967 sal_Int16 nErg = 0;
968 sal_Int32 nType = 0;
969
970 if (!m_xField.is())
971 return nErg;
972 try
973 {
974 Any aValue;
975 {
976 aValue = m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE));
977 aValue >>= nType;
978 }
979 }
980 catch( Exception& )
981 {
982 return nErg;
983 }
984
985 switch (nType)
986 {
987 case DataType::CHAR:
988 case DataType::VARCHAR:
989 case DataType::LONGVARCHAR:
990 case DataType::CLOB:
991 if(pLiteral->isRule())
992 {
993 pAppend->append(pLiteral);
994 nErg = 1;
995 }
996 else
997 {
998 switch(pLiteral->getNodeType())
999 {
1001 pLiteral->m_aNodeValue = ConvertLikeToken(pLiteral, pEscape, false);
1002 pAppend->append(pLiteral);
1003 nErg = 1;
1004 break;
1006 if (m_xFormatter.is() && m_nFormatKey)
1007 {
1008 sal_Int16 nScale = 0;
1009 try
1010 {
1011 Any aValue = getNumberFormatProperty( m_xFormatter, m_nFormatKey, "Decimals" );
1012 aValue >>= nScale;
1013 }
1014 catch( Exception& )
1015 {
1016 }
1017
1018 pAppend->append(new OSQLInternalNode(stringToDouble(pLiteral->getTokenValue(),nScale),SQLNodeType::String));
1019 }
1020 else
1021 pAppend->append(new OSQLInternalNode(pLiteral->getTokenValue(),SQLNodeType::String));
1022
1023 delete pLiteral;
1024 nErg = 1;
1025 break;
1026 default:
1028 m_sErrorMessage = m_sErrorMessage.replaceAt(m_sErrorMessage.indexOf("#1"),2,pLiteral->getTokenValue());
1029 break;
1030 }
1031 }
1032 break;
1033 default:
1035 break;
1036 }
1037 return nErg;
1038}
1039
1040OSQLParseNode* OSQLParser::buildNode_Date(const double& fValue, sal_Int32 nType)
1041{
1045 pNewNode->append(pDateNode);
1047
1048 switch (nType)
1049 {
1050 case DataType::DATE:
1051 {
1052 Date aDate = DBTypeConversion::toDate(fValue,DBTypeConversion::getNULLDate(m_xFormatter->getNumberFormatsSupplier()));
1053 OUString aString = DBTypeConversion::toDateString(aDate);
1054 pDateNode->append(new OSQLInternalNode("", SQLNodeType::Keyword, SQL_TOKEN_D));
1055 pDateNode->append(new OSQLInternalNode(aString, SQLNodeType::String));
1056 break;
1057 }
1058 case DataType::TIME:
1059 {
1060 css::util::Time aTime = DBTypeConversion::toTime(fValue);
1061 OUString aString = DBTypeConversion::toTimeString(aTime);
1062 pDateNode->append(new OSQLInternalNode("", SQLNodeType::Keyword, SQL_TOKEN_T));
1063 pDateNode->append(new OSQLInternalNode(aString, SQLNodeType::String));
1064 break;
1065 }
1066 case DataType::TIMESTAMP:
1067 {
1068 DateTime aDateTime = DBTypeConversion::toDateTime(fValue,DBTypeConversion::getNULLDate(m_xFormatter->getNumberFormatsSupplier()));
1069 if (aDateTime.Seconds || aDateTime.Minutes || aDateTime.Hours)
1070 {
1071 OUString aString = DBTypeConversion::toDateTimeString(aDateTime);
1072 pDateNode->append(new OSQLInternalNode("", SQLNodeType::Keyword, SQL_TOKEN_TS));
1073 pDateNode->append(new OSQLInternalNode(aString, SQLNodeType::String));
1074 }
1075 else
1076 {
1077 Date aDate(aDateTime.Day,aDateTime.Month,aDateTime.Year);
1078 pDateNode->append(new OSQLInternalNode("", SQLNodeType::Keyword, SQL_TOKEN_D));
1080 }
1081 break;
1082 }
1083 }
1084
1085 return pNewNode;
1086}
1087
1089{
1090 OSQLParseNode* pReturn = nullptr;
1091 if ( _pLiteral )
1092 {
1093 if (m_nFormatKey)
1094 {
1095 sal_Int16 nScale = 0;
1096 try
1097 {
1098 Any aValue = getNumberFormatProperty( m_xFormatter, m_nFormatKey, "Decimals" );
1099 aValue >>= nScale;
1100 }
1101 catch( Exception& )
1102 {
1103 }
1104
1105 pReturn = new OSQLInternalNode(stringToDouble(_pLiteral->getTokenValue(),nScale),SQLNodeType::String);
1106 }
1107 else
1108 pReturn = new OSQLInternalNode(_pLiteral->getTokenValue(),SQLNodeType::String);
1109
1110 delete _pLiteral;
1111 _pLiteral = nullptr;
1112 }
1113 return pReturn;
1114}
1115
1116OUString OSQLParser::stringToDouble(const OUString& _rValue,sal_Int16 _nScale)
1117{
1118 OUString aValue;
1119 if(!m_xCharClass.is())
1120 m_xCharClass = CharacterClassification::create( m_xContext );
1121 if( s_xLocaleData.get() )
1122 {
1123 try
1124 {
1125 ParseResult aResult = m_xCharClass->parsePredefinedToken(KParseType::ANY_NUMBER,_rValue,0,m_pData->aLocale,0,OUString(),KParseType::ANY_NUMBER,OUString());
1126 if((aResult.TokenType & KParseType::IDENTNAME) && aResult.EndPos == _rValue.getLength())
1127 {
1128 aValue = OUString::number(aResult.Value);
1129 sal_Int32 nPos = aValue.lastIndexOf('.');
1130 if((nPos+_nScale) < aValue.getLength())
1131 aValue = aValue.replaceAt(nPos+_nScale,aValue.getLength()-nPos-_nScale, u"");
1132 OUString sDecimalSeparator = s_xLocaleData.get()->get()->getLocaleItem(m_pData->aLocale).decimalSeparator;
1133 aValue = aValue.replaceAt(aValue.lastIndexOf('.'), 1, sDecimalSeparator);
1134 return aValue;
1135 }
1136 }
1137 catch(Exception&)
1138 {
1139 }
1140 }
1141 return aValue;
1142}
1143
1144
1146{
1147 static std::mutex aMutex;
1148 return aMutex;
1149}
1150
1151
1152std::unique_ptr<OSQLParseNode> OSQLParser::predicateTree(OUString& rErrorMessage, const OUString& rStatement,
1153 const Reference< css::util::XNumberFormatter > & xFormatter,
1154 const Reference< XPropertySet > & xField,
1155 bool bUseRealName)
1156{
1157 // Guard the parsing
1158 std::unique_lock aGuard(getMutex());
1159 // must be reset
1160 setParser(this);
1161
1162
1163 // reset the parser
1164 m_xField = xField;
1165 m_xFormatter = xFormatter;
1166
1167 if (m_xField.is())
1168 {
1169 sal_Int32 nType=0;
1170 try
1171 {
1172 // get the field name
1173 OUString aString;
1174
1175 // retrieve the fields name
1176 // #75243# use the RealName of the column if there is any otherwise the name which could be the alias
1177 // of the field
1178 Reference< XPropertySetInfo> xInfo = m_xField->getPropertySetInfo();
1179 if ( bUseRealName && xInfo->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME)))
1180 m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REALNAME)) >>= aString;
1181 else
1182 m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)) >>= aString;
1183
1184 m_sFieldName = aString;
1185
1186 // get the field format key
1187 if ( xInfo->hasPropertyByName(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)))
1188 m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_FORMATKEY)) >>= m_nFormatKey;
1189 else
1190 m_nFormatKey = 0;
1191
1192 // get the field type
1193 m_xField->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)) >>= nType;
1194 }
1195 catch ( Exception& )
1196 {
1197 OSL_ASSERT(false);
1198 }
1199
1200 if (m_nFormatKey && m_xFormatter.is())
1201 {
1203 OSL_ENSURE(aValue.getValueType() == cppu::UnoType<css::lang::Locale>::get(), "OSQLParser::PredicateTree : invalid language property !");
1204
1205 if (aValue.getValueType() == cppu::UnoType<css::lang::Locale>::get())
1206 aValue >>= m_pData->aLocale;
1207 }
1208 else
1210
1211 if ( m_xFormatter.is() )
1212 {
1213 try
1214 {
1215 Reference< css::util::XNumberFormatsSupplier > xFormatSup = m_xFormatter->getNumberFormatsSupplier();
1216 if ( xFormatSup.is() )
1217 {
1218 Reference< css::util::XNumberFormats > xFormats = xFormatSup->getNumberFormats();
1219 if ( xFormats.is() )
1220 {
1221 css::lang::Locale aLocale;
1222 aLocale.Language = "en";
1223 aLocale.Country = "US";
1224 OUString sFormat("YYYY-MM-DD");
1225 m_nDateFormatKey = xFormats->queryKey(sFormat,aLocale,false);
1226 if ( m_nDateFormatKey == sal_Int32(-1) )
1227 m_nDateFormatKey = xFormats->addNew(sFormat, aLocale);
1228 }
1229 }
1230 }
1231 catch ( Exception& )
1232 {
1233 SAL_WARN( "connectivity.parse","DateFormatKey");
1234 }
1235 }
1236
1237 switch (nType)
1238 {
1239 case DataType::DATE:
1240 case DataType::TIME:
1241 case DataType::TIMESTAMP:
1243 break;
1244 case DataType::CHAR:
1245 case DataType::VARCHAR:
1246 case DataType::LONGVARCHAR:
1247 case DataType::CLOB:
1249 break;
1250 default:
1251 if ( s_xLocaleData.get()->get()->getLocaleItem( m_pData->aLocale ).decimalSeparator.toChar() == ',' )
1253 else
1255 }
1256
1257 }
1258 else
1260
1261 s_pScanner->prepareScan(rStatement, m_pContext, true);
1262
1263 SQLyylval.pParseNode = nullptr;
1264 // SQLyypvt = NULL;
1265 m_pParseTree = nullptr;
1266 m_sErrorMessage.clear();
1267
1268 // Start the parser
1269 if (SQLyyparse() != 0)
1270 {
1271 m_sFieldName.clear();
1272 m_xField.clear();
1273 m_xFormatter.clear();
1274 m_nFormatKey = 0;
1275 m_nDateFormatKey = 0;
1276
1277 if (m_sErrorMessage.isEmpty())
1279 if (m_sErrorMessage.isEmpty())
1281
1282 rErrorMessage = m_sErrorMessage;
1283
1284 // clear the garbage collector
1285 (*s_pGarbageCollector)->clearAndDelete();
1286 // coverity[leaked_storage : FALSE] - because the garbage collector deleted it
1287 m_pParseTree.release();
1288 return nullptr;
1289 }
1290 else
1291 {
1292 (*s_pGarbageCollector)->clear();
1293
1294 m_sFieldName.clear();
1295 m_xField.clear();
1296 m_xFormatter.clear();
1297 m_nFormatKey = 0;
1298 m_nDateFormatKey = 0;
1299
1300 // Return the result (the root parse node):
1301
1302 // Instead, the parse method sets the member pParseTree and simply returns that
1303 OSL_ENSURE(m_pParseTree != nullptr,"OSQLParser: Parser did not return a ParseTree!");
1304 return std::move(m_pParseTree);
1305 }
1306}
1307
1308
1309OSQLParser::OSQLParser(css::uno::Reference< css::uno::XComponentContext > xContext, const IParseContext* _pContext)
1310 :m_pContext(_pContext)
1311 ,m_pData( new OSQLParser_Data )
1312 ,m_nFormatKey(0)
1313 ,m_nDateFormatKey(0)
1314 ,m_xContext(std::move(xContext))
1315{
1316
1317
1318 setParser(this);
1319
1320#ifdef SQLYYDEBUG
1321#ifdef SQLYYDEBUG_ON
1322 SQLyydebug = 1;
1323#endif
1324#endif
1325
1326 std::unique_lock aGuard(getMutex());
1327 // Do we have to initialize the data?
1328 if (s_nRefCount == 0)
1329 {
1330 s_pScanner = new OSQLScanner();
1333
1334 if(!s_xLocaleData.get())
1335 s_xLocaleData.set(LocaleData::create(m_xContext));
1336
1337 // reset to UNKNOWN_RULE
1338 static_assert(OSQLParseNode::UNKNOWN_RULE==0, "UNKNOWN_RULE must be 0 for memset to 0 to work");
1340
1341 const struct
1342 {
1343 OSQLParseNode::Rule eRule; // the parse node's ID for the rule
1344 OString sRuleName; // the name of the rule ("select_statement")
1345 } aRuleDescriptions[] =
1346 {
1347 { OSQLParseNode::select_statement, "select_statement" },
1348 { OSQLParseNode::table_exp, "table_exp" },
1349 { OSQLParseNode::table_ref_commalist, "table_ref_commalist" },
1350 { OSQLParseNode::table_ref, "table_ref" },
1351 { OSQLParseNode::catalog_name, "catalog_name" },
1352 { OSQLParseNode::schema_name, "schema_name" },
1353 { OSQLParseNode::table_name, "table_name" },
1354 { OSQLParseNode::opt_column_commalist, "opt_column_commalist" },
1355 { OSQLParseNode::column_commalist, "column_commalist" },
1356 { OSQLParseNode::column_ref_commalist, "column_ref_commalist" },
1357 { OSQLParseNode::column_ref, "column_ref" },
1358 { OSQLParseNode::opt_order_by_clause, "opt_order_by_clause" },
1359 { OSQLParseNode::ordering_spec_commalist, "ordering_spec_commalist" },
1360 { OSQLParseNode::ordering_spec, "ordering_spec" },
1361 { OSQLParseNode::opt_asc_desc, "opt_asc_desc" },
1362 { OSQLParseNode::where_clause, "where_clause" },
1363 { OSQLParseNode::opt_where_clause, "opt_where_clause" },
1364 { OSQLParseNode::search_condition, "search_condition" },
1365 { OSQLParseNode::comparison, "comparison" },
1366 { OSQLParseNode::comparison_predicate, "comparison_predicate" },
1367 { OSQLParseNode::between_predicate, "between_predicate" },
1368 { OSQLParseNode::like_predicate, "like_predicate" },
1369 { OSQLParseNode::opt_escape, "opt_escape" },
1370 { OSQLParseNode::test_for_null, "test_for_null" },
1371 { OSQLParseNode::scalar_exp_commalist, "scalar_exp_commalist" },
1372 { OSQLParseNode::scalar_exp, "scalar_exp" },
1373 { OSQLParseNode::parameter_ref, "parameter_ref" },
1374 { OSQLParseNode::parameter, "parameter" },
1375 { OSQLParseNode::general_set_fct, "general_set_fct" },
1376 { OSQLParseNode::range_variable, "range_variable" },
1377 { OSQLParseNode::column, "column" },
1378 { OSQLParseNode::delete_statement_positioned, "delete_statement_positioned" },
1379 { OSQLParseNode::delete_statement_searched, "delete_statement_searched" },
1380 { OSQLParseNode::update_statement_positioned, "update_statement_positioned" },
1381 { OSQLParseNode::update_statement_searched, "update_statement_searched" },
1382 { OSQLParseNode::assignment_commalist, "assignment_commalist" },
1383 { OSQLParseNode::assignment, "assignment" },
1384 { OSQLParseNode::values_or_query_spec, "values_or_query_spec" },
1385 { OSQLParseNode::insert_statement, "insert_statement" },
1386 { OSQLParseNode::insert_atom_commalist, "insert_atom_commalist" },
1387 { OSQLParseNode::insert_atom, "insert_atom" },
1388 { OSQLParseNode::from_clause, "from_clause" },
1389 { OSQLParseNode::qualified_join, "qualified_join" },
1390 { OSQLParseNode::cross_union, "cross_union" },
1391 { OSQLParseNode::select_sublist, "select_sublist" },
1392 { OSQLParseNode::derived_column, "derived_column" },
1393 { OSQLParseNode::column_val, "column_val" },
1394 { OSQLParseNode::set_fct_spec, "set_fct_spec" },
1395 { OSQLParseNode::boolean_term, "boolean_term" },
1396 { OSQLParseNode::boolean_primary, "boolean_primary" },
1397 { OSQLParseNode::num_value_exp, "num_value_exp" },
1398 { OSQLParseNode::join_type, "join_type" },
1399 { OSQLParseNode::position_exp, "position_exp" },
1400 { OSQLParseNode::extract_exp, "extract_exp" },
1401 { OSQLParseNode::length_exp, "length_exp" },
1402 { OSQLParseNode::char_value_fct, "char_value_fct" },
1403 { OSQLParseNode::odbc_call_spec, "odbc_call_spec" },
1404 { OSQLParseNode::in_predicate, "in_predicate" },
1405 { OSQLParseNode::existence_test, "existence_test" },
1406 { OSQLParseNode::unique_test, "unique_test" },
1407 { OSQLParseNode::all_or_any_predicate, "all_or_any_predicate" },
1408 { OSQLParseNode::named_columns_join, "named_columns_join" },
1409 { OSQLParseNode::join_condition, "join_condition" },
1410 { OSQLParseNode::joined_table, "joined_table" },
1411 { OSQLParseNode::boolean_factor, "boolean_factor" },
1412 { OSQLParseNode::sql_not, "sql_not" },
1413 { OSQLParseNode::manipulative_statement, "manipulative_statement" },
1414 { OSQLParseNode::subquery, "subquery" },
1415 { OSQLParseNode::value_exp_commalist, "value_exp_commalist" },
1416 { OSQLParseNode::odbc_fct_spec, "odbc_fct_spec" },
1417 { OSQLParseNode::union_statement, "union_statement" },
1418 { OSQLParseNode::outer_join_type, "outer_join_type" },
1419 { OSQLParseNode::char_value_exp, "char_value_exp" },
1420 { OSQLParseNode::term, "term" },
1421 { OSQLParseNode::value_exp_primary, "value_exp_primary" },
1422 { OSQLParseNode::value_exp, "value_exp" },
1423 { OSQLParseNode::selection, "selection" },
1424 { OSQLParseNode::fold, "fold" },
1425 { OSQLParseNode::char_substring_fct, "char_substring_fct" },
1426 { OSQLParseNode::factor, "factor" },
1427 { OSQLParseNode::base_table_def, "base_table_def" },
1428 { OSQLParseNode::base_table_element_commalist, "base_table_element_commalist" },
1429 { OSQLParseNode::data_type, "data_type" },
1430 { OSQLParseNode::column_def, "column_def" },
1431 { OSQLParseNode::table_node, "table_node" },
1432 { OSQLParseNode::as_clause, "as_clause" },
1433 { OSQLParseNode::opt_as, "opt_as" },
1434 { OSQLParseNode::op_column_commalist, "op_column_commalist" },
1435 { OSQLParseNode::table_primary_as_range_column, "table_primary_as_range_column" },
1436 { OSQLParseNode::datetime_primary, "datetime_primary" },
1437 { OSQLParseNode::concatenation, "concatenation" },
1438 { OSQLParseNode::char_factor, "char_factor" },
1439 { OSQLParseNode::bit_value_fct, "bit_value_fct" },
1440 { OSQLParseNode::comparison_predicate_part_2, "comparison_predicate_part_2" },
1441 { OSQLParseNode::parenthesized_boolean_value_expression, "parenthesized_boolean_value_expression" },
1442 { OSQLParseNode::character_string_type, "character_string_type" },
1443 { OSQLParseNode::other_like_predicate_part_2, "other_like_predicate_part_2" },
1444 { OSQLParseNode::between_predicate_part_2, "between_predicate_part_2" },
1445 { OSQLParseNode::null_predicate_part_2, "null_predicate_part_2" },
1446 { OSQLParseNode::cast_spec, "cast_spec" },
1447 { OSQLParseNode::window_function, "window_function" }
1448 };
1449 const size_t nRuleMapCount = std::size( aRuleDescriptions );
1450 // added a new rule? Adjust this map!
1451 // +1 for UNKNOWN_RULE
1452 static_assert(nRuleMapCount + 1 == static_cast<size_t>(OSQLParseNode::rule_count), "must be equal");
1453
1454 for (const auto & aRuleDescription : aRuleDescriptions)
1455 {
1456 // look up the rule description in the our identifier map
1457 sal_uInt32 nParserRuleID = StrToRuleID( aRuleDescription.sRuleName );
1458 // map the parser's rule ID to the OSQLParseNode::Rule
1459 s_aReverseRuleIDLookup[ nParserRuleID ] = aRuleDescription.eRule;
1460 // and map the OSQLParseNode::Rule to the parser's rule ID
1461 s_nRuleIDs[ aRuleDescription.eRule ] = nParserRuleID;
1462 }
1463 }
1464 ++s_nRefCount;
1465
1466 if (m_pContext == nullptr)
1467 // take the default context
1469
1471}
1472
1473
1475{
1476 std::unique_lock aGuard(getMutex());
1477 OSL_ENSURE(s_nRefCount > 0, "OSQLParser::~OSQLParser() : suspicious call : has a refcount of 0 !");
1478 if (!--s_nRefCount)
1479 {
1480 s_pScanner->setScanner(true);
1481 delete s_pScanner;
1482 s_pScanner = nullptr;
1483
1484 delete s_pGarbageCollector;
1485 s_pGarbageCollector = nullptr;
1486
1488 }
1489 m_pParseTree = nullptr;
1490}
1491
1493{
1494 sal_Int32 nCount = _pNode->count();
1495 for(sal_Int32 i=0;i < nCount;++i)
1496 {
1497 OSQLParseNode* pChildNode = _pNode->getChild(i);
1498 if(SQL_ISRULE(pChildNode,parameter) && pChildNode->count() > 1)
1499 {
1501 pChildNode->replaceAndDelete(pChildNode->getChild(0), pNewNode);
1502 sal_Int32 nChildCount = pChildNode->count();
1503 for(sal_Int32 j=1;j < nChildCount;++j)
1504 delete pChildNode->removeAt(1);
1505 }
1506 else
1507 substituteParameterNames(pChildNode);
1508
1509 }
1510}
1511
1512bool OSQLParser::extractDate(OSQLParseNode const * pLiteral,double& _rfValue)
1513{
1514 Reference< XNumberFormatsSupplier > xFormatSup = m_xFormatter->getNumberFormatsSupplier();
1515 Reference< XNumberFormatTypes > xFormatTypes;
1516 if ( xFormatSup.is() )
1517 xFormatTypes.set(xFormatSup->getNumberFormats(), css::uno::UNO_QUERY);
1518
1519 // if there is no format key, yet, make sure we have a feasible one for our locale
1520 try
1521 {
1522 if ( !m_nFormatKey && xFormatTypes.is() )
1524 }
1525 catch( Exception& ) { }
1526 const OUString& sValue = pLiteral->getTokenValue();
1527 sal_Int32 nTryFormat = m_nFormatKey;
1528 bool bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
1529
1530 // If our format key didn't do, try the default date format for our locale.
1531 if ( !bSuccess && xFormatTypes.is() )
1532 {
1533 try
1534 {
1535 nTryFormat = xFormatTypes->getStandardFormat( NumberFormat::DATE, m_pData->aLocale );
1536 }
1537 catch( Exception& ) { }
1538 bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
1539 }
1540
1541 // if this also didn't do, try ISO format
1542 if ( !bSuccess && xFormatTypes.is() )
1543 {
1544 try
1545 {
1546 nTryFormat = xFormatTypes->getFormatIndex( NumberFormatIndex::DATE_DIN_YYYYMMDD, m_pData->aLocale );
1547 }
1548 catch( Exception& ) { }
1549 bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
1550 }
1551
1552 // if this also didn't do, try fallback date format (en-US)
1553 if ( !bSuccess )
1554 {
1555 nTryFormat = m_nDateFormatKey;
1556 bSuccess = lcl_saveConvertToNumber( m_xFormatter, nTryFormat, sValue, _rfValue );
1557 }
1558 return bSuccess;
1559}
1560
1562{
1563 // try converting the string into a date, according to our format key
1564 double fValue = 0.0;
1565 OSQLParseNode* pFCTNode = nullptr;
1566
1567 if ( extractDate(pLiteral,fValue) )
1568 pFCTNode = buildNode_Date( fValue, _nType);
1569
1570 delete pLiteral;
1571 pLiteral = nullptr;
1572
1573 if ( !pFCTNode )
1575
1576 return pFCTNode;
1577}
1578
1579
1580OSQLParseNode::OSQLParseNode(const char * pNewValue,
1581 SQLNodeType eNewNodeType,
1582 sal_uInt32 nNewNodeID)
1583 :m_pParent(nullptr)
1584 ,m_aNodeValue(pNewValue,strlen(pNewValue),RTL_TEXTENCODING_UTF8)
1585 ,m_eNodeType(eNewNodeType)
1586 ,m_nNodeID(nNewNodeID)
1587{
1588 OSL_ENSURE(m_eNodeType >= SQLNodeType::Rule && m_eNodeType <= SQLNodeType::Concat,"OSQLParseNode: created with invalid NodeType");
1589}
1590
1591OSQLParseNode::OSQLParseNode(std::string_view _rNewValue,
1592 SQLNodeType eNewNodeType,
1593 sal_uInt32 nNewNodeID)
1594 :m_pParent(nullptr)
1595 ,m_aNodeValue(OStringToOUString(_rNewValue,RTL_TEXTENCODING_UTF8))
1596 ,m_eNodeType(eNewNodeType)
1597 ,m_nNodeID(nNewNodeID)
1598{
1599 OSL_ENSURE(m_eNodeType >= SQLNodeType::Rule && m_eNodeType <= SQLNodeType::Concat,"OSQLParseNode: created with invalid NodeType");
1600}
1601
1603 SQLNodeType eNewNodeType,
1604 sal_uInt32 nNewNodeID)
1605 :m_pParent(nullptr)
1606 ,m_aNodeValue(std::move(_aNewValue))
1607 ,m_eNodeType(eNewNodeType)
1608 ,m_nNodeID(nNewNodeID)
1609{
1610 OSL_ENSURE(m_eNodeType >= SQLNodeType::Rule && m_eNodeType <= SQLNodeType::Concat,"OSQLParseNode: created with invalid NodeType");
1611}
1612
1614{
1615 // Set the getParent to NULL
1616 m_pParent = nullptr;
1617
1618 // Copy the members
1619 m_aNodeValue = rParseNode.m_aNodeValue;
1620 m_eNodeType = rParseNode.m_eNodeType;
1621 m_nNodeID = rParseNode.m_nNodeID;
1622
1623
1624 // Remember that we derived from Container. According to SV-Help the Container's
1625 // copy ctor creates a new Container with the same pointers for content.
1626 // This means after copying the Container, for all non-NULL pointers a copy is
1627 // created and reattached instead of the old pointer.
1628
1629 // If not a leaf, then process SubTrees
1630 for (auto const& child : rParseNode.m_aChildren)
1631 append(new OSQLParseNode(*child));
1632}
1633
1634
1636{
1637 if (this != &rParseNode)
1638 {
1639 // Copy the members - pParent remains the same
1640 m_aNodeValue = rParseNode.m_aNodeValue;
1641 m_eNodeType = rParseNode.m_eNodeType;
1642 m_nNodeID = rParseNode.m_nNodeID;
1643
1644 m_aChildren.clear();
1645
1646 for (auto const& child : rParseNode.m_aChildren)
1647 append(new OSQLParseNode(*child));
1648 }
1649 return *this;
1650}
1651
1652
1653bool OSQLParseNode::operator==(OSQLParseNode const & rParseNode) const
1654{
1655 // The members must be equal
1656 bool bResult = (m_nNodeID == rParseNode.m_nNodeID) &&
1657 (m_eNodeType == rParseNode.m_eNodeType) &&
1658 (m_aNodeValue == rParseNode.m_aNodeValue) &&
1659 count() == rParseNode.count();
1660
1661 // Parameters are not equal!
1662 bResult = bResult && !SQL_ISRULE(this, parameter);
1663
1664 // compare children
1665 for (size_t i=0; bResult && i < count(); i++)
1666 bResult = *getChild(i) == *rParseNode.getChild(i);
1667
1668 return bResult;
1669}
1670
1671
1673{
1674}
1675
1676
1678{
1679 OSL_ENSURE(pNewNode != nullptr, "OSQLParseNode: invalid NewSubTree");
1680 OSL_ENSURE(pNewNode->getParent() == nullptr, "OSQLParseNode: Node is not an orphan");
1681 OSL_ENSURE(std::none_of(m_aChildren.begin(), m_aChildren.end(),
1682 [&] (std::unique_ptr<OSQLParseNode> const & r) { return r.get() == pNewNode; }),
1683 "OSQLParseNode::append() Node already element of parent");
1684
1685 // Create connection to getParent
1686 pNewNode->setParent( this );
1687 // and attach the SubTree at the end
1688 m_aChildren.emplace_back(pNewNode);
1689}
1690
1691bool OSQLParseNode::addDateValue(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
1692{
1693 // special display for date/time values
1695 return false;
1696
1697 const OSQLParseNode* pODBCNode = m_aChildren[1].get();
1698 const OSQLParseNode* pODBCNodeChild = pODBCNode->m_aChildren[0].get();
1699
1700 if (pODBCNodeChild->getNodeType() != SQLNodeType::Keyword || !(
1701 SQL_ISTOKEN(pODBCNodeChild, D) ||
1702 SQL_ISTOKEN(pODBCNodeChild, T) ||
1703 SQL_ISTOKEN(pODBCNodeChild, TS) ))
1704 return false;
1705
1706 OUString suQuote("'");
1707 if (rParam.bPredicate)
1708 {
1709 if (rParam.aMetaData.shouldEscapeDateTime())
1710 {
1711 suQuote = "#";
1712 }
1713 }
1714 else
1715 {
1716 if (rParam.aMetaData.shouldEscapeDateTime())
1717 {
1718 // suQuote = "'";
1719 return false;
1720 }
1721 }
1722
1723 if (!rString.isEmpty())
1724 rString.append(" ");
1725 rString.append(suQuote);
1726 const OUString sTokenValue = pODBCNode->m_aChildren[1]->getTokenValue();
1727 if (SQL_ISTOKEN(pODBCNodeChild, D))
1728 {
1729 rString.append(rParam.bPredicate ? convertDateString(rParam, sTokenValue) : sTokenValue);
1730 }
1731 else if (SQL_ISTOKEN(pODBCNodeChild, T))
1732 {
1733 rString.append(rParam.bPredicate ? convertTimeString(rParam, sTokenValue) : sTokenValue);
1734 }
1735 else
1736 {
1737 rString.append(rParam.bPredicate ? convertDateTimeString(rParam, sTokenValue) : sTokenValue);
1738 }
1739 rString.append(suQuote);
1740 return true;
1741}
1742
1743void OSQLParseNode::replaceNodeValue(const OUString& rTableAlias, const OUString& rColumnName)
1744{
1745 for (size_t i=0;i<count();++i)
1746 {
1747 if (SQL_ISRULE(this,column_ref) && count() == 1 && getChild(0)->getTokenValue() == rColumnName)
1748 {
1749 OSQLParseNode * pCol = removeAt(sal_uInt32(0));
1750 append(new OSQLParseNode(rTableAlias,SQLNodeType::Name));
1752 append(pCol);
1753 }
1754 else
1755 getChild(i)->replaceNodeValue(rTableAlias,rColumnName);
1756 }
1757}
1758
1760{
1761 OSQLParseNode* pRetNode = nullptr;
1762 if (isRule() && OSQLParser::RuleID(eRule) == getRuleID())
1763 pRetNode = const_cast<OSQLParseNode*>(this);
1764 else
1765 {
1766 for (auto const& child : m_aChildren)
1767 {
1768 pRetNode = child->getByRule(eRule);
1769 if (pRetNode)
1770 break;
1771 }
1772 }
1773 return pRetNode;
1774}
1775
1777{
1779 pNewNode->append(pLeftLeaf);
1780 pNewNode->append(new OSQLParseNode("AND",SQLNodeType::Keyword,SQL_TOKEN_AND));
1781 pNewNode->append(pRightLeaf);
1782 return pNewNode;
1783}
1784
1786{
1788 pNewNode->append(pLeftLeaf);
1789 pNewNode->append(new OSQLParseNode("OR",SQLNodeType::Keyword,SQL_TOKEN_OR));
1790 pNewNode->append(pRightLeaf);
1791 return pNewNode;
1792}
1793
1795{
1796 if(!pSearchCondition) // no where condition at entry point
1797 return;
1798
1799 OSQLParseNode::absorptions(pSearchCondition);
1800 // '(' search_condition ')'
1801 if (SQL_ISRULE(pSearchCondition,boolean_primary))
1802 {
1803 OSQLParseNode* pLeft = pSearchCondition->getChild(1);
1804 disjunctiveNormalForm(pLeft);
1805 }
1806 // search_condition SQL_TOKEN_OR boolean_term
1807 else if (SQL_ISRULE(pSearchCondition,search_condition))
1808 {
1809 OSQLParseNode* pLeft = pSearchCondition->getChild(0);
1810 disjunctiveNormalForm(pLeft);
1811
1812 OSQLParseNode* pRight = pSearchCondition->getChild(2);
1813 disjunctiveNormalForm(pRight);
1814 }
1815 // boolean_term SQL_TOKEN_AND boolean_factor
1816 else if (SQL_ISRULE(pSearchCondition,boolean_term))
1817 {
1818 OSQLParseNode* pLeft = pSearchCondition->getChild(0);
1819 disjunctiveNormalForm(pLeft);
1820
1821 OSQLParseNode* pRight = pSearchCondition->getChild(2);
1822 disjunctiveNormalForm(pRight);
1823
1824 OSQLParseNode* pNewNode = nullptr;
1825 // '(' search_condition ')' on left side
1826 if(pLeft->count() == 3 && SQL_ISRULE(pLeft,boolean_primary) && SQL_ISRULE(pLeft->getChild(1),search_condition))
1827 {
1828 // and-or tree on left side
1829 OSQLParseNode* pOr = pLeft->getChild(1);
1830 OSQLParseNode* pNewLeft = nullptr;
1831 OSQLParseNode* pNewRight = nullptr;
1832
1833 // cut right from parent
1834 OSQLParseNode* pOldRight = pSearchCondition->removeAt(2);
1835 assert(pOldRight == pRight);
1836
1837 pNewRight = MakeANDNode(pOr->removeAt(2), pOldRight);
1838 pNewLeft = MakeANDNode(pOr->removeAt(sal_uInt32(0)), new OSQLParseNode(*pOldRight));
1839 pNewNode = MakeORNode(pNewLeft,pNewRight);
1840 // and append new Node
1841 replaceAndReset(pSearchCondition,pNewNode);
1842
1843 disjunctiveNormalForm(pSearchCondition);
1844 }
1845 else if(pRight->count() == 3 && SQL_ISRULE(pRight,boolean_primary) && SQL_ISRULE(pRight->getChild(1),search_condition))
1846 { // '(' search_condition ')' on right side
1847 // and-or tree on right side
1848 // a and (b or c)
1849 OSQLParseNode* pOr = pRight->getChild(1);
1850 OSQLParseNode* pNewLeft = nullptr;
1851 OSQLParseNode* pNewRight = nullptr;
1852
1853 // cut left from parent
1854 OSQLParseNode* pOldLeft = pSearchCondition->removeAt(sal_uInt32(0));
1855 assert(pOldLeft == pLeft);
1856
1857 pNewRight = MakeANDNode(pOldLeft, pOr->removeAt(2));
1858 pNewLeft = MakeANDNode(new OSQLParseNode(*pOldLeft), pOr->removeAt(sal_uInt32(0)));
1859 pNewNode = MakeORNode(pNewLeft,pNewRight);
1860
1861 // and append new Node
1862 replaceAndReset(pSearchCondition,pNewNode);
1863 disjunctiveNormalForm(pSearchCondition);
1864 }
1865 else if(SQL_ISRULE(pLeft,boolean_primary) && (!SQL_ISRULE(pLeft->getChild(1),search_condition) || !SQL_ISRULE(pLeft->getChild(1),boolean_term)))
1866 pSearchCondition->replaceAndDelete(pLeft, pLeft->removeAt(1));
1867 else if(SQL_ISRULE(pRight,boolean_primary) && (!SQL_ISRULE(pRight->getChild(1),search_condition) || !SQL_ISRULE(pRight->getChild(1),boolean_term)))
1868 pSearchCondition->replaceAndDelete(pRight, pRight->removeAt(1));
1869 }
1870}
1871
1872void OSQLParseNode::negateSearchCondition(OSQLParseNode*& pSearchCondition, bool bNegate)
1873{
1874 if(!pSearchCondition) // no where condition at entry point
1875 return;
1876 // '(' search_condition ')'
1877 if (pSearchCondition->count() == 3 && SQL_ISRULE(pSearchCondition,boolean_primary))
1878 {
1879 OSQLParseNode* pRight = pSearchCondition->getChild(1);
1880 negateSearchCondition(pRight,bNegate);
1881 }
1882 // search_condition SQL_TOKEN_OR boolean_term
1883 else if (SQL_ISRULE(pSearchCondition,search_condition))
1884 {
1885 OSQLParseNode* pLeft = pSearchCondition->getChild(0);
1886 OSQLParseNode* pRight = pSearchCondition->getChild(2);
1887 if(bNegate)
1888 {
1890 pNewNode->append(pSearchCondition->removeAt(sal_uInt32(0)));
1891 pNewNode->append(new OSQLParseNode("AND",SQLNodeType::Keyword,SQL_TOKEN_AND));
1892 pNewNode->append(pSearchCondition->removeAt(sal_uInt32(1)));
1893 replaceAndReset(pSearchCondition,pNewNode);
1894
1895 pLeft = pNewNode->getChild(0);
1896 pRight = pNewNode->getChild(2);
1897 }
1898
1899 negateSearchCondition(pLeft,bNegate);
1900 negateSearchCondition(pRight,bNegate);
1901 }
1902 // boolean_term SQL_TOKEN_AND boolean_factor
1903 else if (SQL_ISRULE(pSearchCondition,boolean_term))
1904 {
1905 OSQLParseNode* pLeft = pSearchCondition->getChild(0);
1906 OSQLParseNode* pRight = pSearchCondition->getChild(2);
1907 if(bNegate)
1908 {
1910 pNewNode->append(pSearchCondition->removeAt(sal_uInt32(0)));
1911 pNewNode->append(new OSQLParseNode("OR",SQLNodeType::Keyword,SQL_TOKEN_OR));
1912 pNewNode->append(pSearchCondition->removeAt(sal_uInt32(1)));
1913 replaceAndReset(pSearchCondition,pNewNode);
1914
1915 pLeft = pNewNode->getChild(0);
1916 pRight = pNewNode->getChild(2);
1917 }
1918
1919 negateSearchCondition(pLeft,bNegate);
1920 negateSearchCondition(pRight,bNegate);
1921 }
1922 // SQL_TOKEN_NOT ( boolean_primary )
1923 else if (SQL_ISRULE(pSearchCondition,boolean_factor))
1924 {
1925 OSQLParseNode *pNot = pSearchCondition->removeAt(sal_uInt32(0));
1926 delete pNot;
1927 OSQLParseNode *pBooleanTest = pSearchCondition->removeAt(sal_uInt32(0));
1928 // TODO is this needed // pBooleanTest->setParent(NULL);
1929 replaceAndReset(pSearchCondition,pBooleanTest);
1930
1931 if (!bNegate)
1932 negateSearchCondition(pSearchCondition, true); // negate all deeper values
1933 }
1934 // row_value_constructor comparison row_value_constructor
1935 // row_value_constructor comparison any_all_some subquery
1936 else if(bNegate && (SQL_ISRULE(pSearchCondition,comparison_predicate) || SQL_ISRULE(pSearchCondition,all_or_any_predicate)))
1937 {
1938 assert(pSearchCondition->count() == 3);
1939 OSQLParseNode* pComparison = pSearchCondition->getChild(1);
1940 if(SQL_ISRULE(pComparison, comparison))
1941 {
1942 assert(pComparison->count() == 2 ||
1943 pComparison->count() == 4);
1944 assert(SQL_ISTOKEN(pComparison->getChild(0), IS));
1945
1946 OSQLParseNode* pNot = pComparison->getChild(1);
1947 OSQLParseNode* pNotNot = nullptr;
1948 if(pNot->isRule()) // no NOT token (empty rule)
1949 pNotNot = new OSQLParseNode("NOT",SQLNodeType::Keyword,SQL_TOKEN_NOT);
1950 else
1951 {
1952 assert(SQL_ISTOKEN(pNot,NOT));
1954 }
1955 pComparison->replaceAndDelete(pNot, pNotNot);
1956 }
1957 else
1958 {
1959 OSQLParseNode* pNewComparison;
1960 switch(pComparison->getNodeType())
1961 {
1962 default:
1963 case SQLNodeType::Equal:
1964 assert(pComparison->getNodeType() == SQLNodeType::Equal &&
1965 "OSQLParseNode::negateSearchCondition: unexpected node type!");
1966 pNewComparison = new OSQLParseNode("<>",SQLNodeType::NotEqual,SQL_NOTEQUAL);
1967 break;
1968 case SQLNodeType::Less:
1969 pNewComparison = new OSQLParseNode(">=",SQLNodeType::GreatEq,SQL_GREATEQ);
1970 break;
1971 case SQLNodeType::Great:
1972 pNewComparison = new OSQLParseNode("<=",SQLNodeType::LessEq,SQL_LESSEQ);
1973 break;
1975 pNewComparison = new OSQLParseNode(">",SQLNodeType::Great,SQL_GREAT);
1976 break;
1978 pNewComparison = new OSQLParseNode("<",SQLNodeType::Less,SQL_LESS);
1979 break;
1981 pNewComparison = new OSQLParseNode("=",SQLNodeType::Equal,SQL_EQUAL);
1982 break;
1983 }
1984 pSearchCondition->replaceAndDelete(pComparison, pNewComparison);
1985 }
1986 }
1987
1988 else if(bNegate && (SQL_ISRULE(pSearchCondition,test_for_null) ||
1989 SQL_ISRULE(pSearchCondition,in_predicate) ||
1990 SQL_ISRULE(pSearchCondition,between_predicate) ))
1991 {
1992 OSQLParseNode* pPart2 = pSearchCondition->getChild(1);
1993 sal_uInt32 nNotPos = 0;
1994 if ( SQL_ISRULE( pSearchCondition, test_for_null ) )
1995 nNotPos = 1;
1996
1997 OSQLParseNode* pNot = pPart2->getChild(nNotPos);
1998 OSQLParseNode* pNotNot = nullptr;
1999 if(pNot->isRule()) // no NOT token (empty rule)
2000 pNotNot = new OSQLParseNode("NOT",SQLNodeType::Keyword,SQL_TOKEN_NOT);
2001 else
2002 {
2003 assert(SQL_ISTOKEN(pNot,NOT));
2005 }
2006 pPart2->replaceAndDelete(pNot, pNotNot);
2007 }
2008 else if(bNegate && SQL_ISRULE(pSearchCondition,like_predicate))
2009 {
2010 OSQLParseNode* pNot = pSearchCondition->getChild( 1 )->getChild( 0 );
2011 OSQLParseNode* pNotNot = nullptr;
2012 if(pNot->isRule())
2013 pNotNot = new OSQLParseNode("NOT",SQLNodeType::Keyword,SQL_TOKEN_NOT);
2014 else
2016 pSearchCondition->getChild( 1 )->replaceAndDelete(pNot, pNotNot);
2017 }
2018}
2019
2021{
2022 if (!(pSearchCondition && (SQL_ISRULE(pSearchCondition,boolean_primary) || (pSearchCondition->count() == 3 && SQL_ISPUNCTUATION(pSearchCondition->getChild(0),"(") &&
2023 SQL_ISPUNCTUATION(pSearchCondition->getChild(2),")")))))
2024 return;
2025
2026 OSQLParseNode* pRight = pSearchCondition->getChild(1);
2027 absorptions(pRight);
2028 // if child is not an or and tree then delete () around child
2029 if(!(SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || SQL_ISRULE(pSearchCondition->getChild(1),search_condition)) ||
2030 SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || // and can always stand without ()
2031 (SQL_ISRULE(pSearchCondition->getChild(1),search_condition) && SQL_ISRULE(pSearchCondition->getParent(),search_condition)))
2032 {
2033 OSQLParseNode* pNode = pSearchCondition->removeAt(1);
2034 replaceAndReset(pSearchCondition,pNode);
2035 }
2036}
2037
2039{
2040 if(!pSearchCondition) // no where condition at entry point
2041 return;
2042
2043 eraseBraces(pSearchCondition);
2044
2045 if(SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
2046 {
2047 OSQLParseNode* pLeft = pSearchCondition->getChild(0);
2048 absorptions(pLeft);
2049 OSQLParseNode* pRight = pSearchCondition->getChild(2);
2050 absorptions(pRight);
2051 }
2052
2053 sal_uInt32 nPos = 0;
2054 // a and a || a or a
2055 OSQLParseNode* pNewNode = nullptr;
2056 if(( SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
2057 && *pSearchCondition->getChild(0) == *pSearchCondition->getChild(2))
2058 {
2059 pNewNode = pSearchCondition->removeAt(sal_uInt32(0));
2060 replaceAndReset(pSearchCondition,pNewNode);
2061 }
2062 // ( a or b ) and a || ( b or c ) and a
2063 // a and ( a or b ) || a and ( b or c )
2064 else if ( SQL_ISRULE(pSearchCondition,boolean_term)
2065 && (
2066 ( SQL_ISRULE(pSearchCondition->getChild(nPos = 0),boolean_primary)
2067 || SQL_ISRULE(pSearchCondition->getChild(nPos),search_condition)
2068 )
2069 || ( SQL_ISRULE(pSearchCondition->getChild(nPos = 2),boolean_primary)
2070 || SQL_ISRULE(pSearchCondition->getChild(nPos),search_condition)
2071 )
2072 )
2073 )
2074 {
2075 OSQLParseNode* p2ndSearch = pSearchCondition->getChild(nPos);
2076 if ( SQL_ISRULE(p2ndSearch,boolean_primary) )
2077 p2ndSearch = p2ndSearch->getChild(1);
2078
2079 if ( *p2ndSearch->getChild(0) == *pSearchCondition->getChild(2-nPos) ) // a and ( a or b) -> a or b
2080 {
2081 pNewNode = pSearchCondition->removeAt(sal_uInt32(0));
2082 replaceAndReset(pSearchCondition,pNewNode);
2083
2084 }
2085 else if ( *p2ndSearch->getChild(2) == *pSearchCondition->getChild(2-nPos) ) // a and ( b or a) -> a or b
2086 {
2087 pNewNode = pSearchCondition->removeAt(sal_uInt32(2));
2088 replaceAndReset(pSearchCondition,pNewNode);
2089 }
2090 else if ( p2ndSearch->getByRule(OSQLParseNode::search_condition) )
2091 {
2092 // a and ( b or c ) -> ( a and b ) or ( a and c )
2093 // ( b or c ) and a -> ( a and b ) or ( a and c )
2094 OSQLParseNode* pC = p2ndSearch->removeAt(sal_uInt32(2));
2095 OSQLParseNode* pB = p2ndSearch->removeAt(sal_uInt32(0));
2096 OSQLParseNode* pA = pSearchCondition->removeAt(sal_uInt32(2)-nPos);
2097
2098 OSQLParseNode* p1stAnd = MakeANDNode(pA,pB);
2099 OSQLParseNode* p2ndAnd = MakeANDNode(new OSQLParseNode(*pA),pC);
2100 pNewNode = MakeORNode(p1stAnd,p2ndAnd);
2103 pNode->append(pNewNode);
2107 replaceAndReset(pSearchCondition,pNode);
2108 }
2109 }
2110 // a or a and b || a or b and a
2111 else if(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(2),boolean_term))
2112 {
2113 if(*pSearchCondition->getChild(2)->getChild(0) == *pSearchCondition->getChild(0))
2114 {
2115 pNewNode = pSearchCondition->removeAt(sal_uInt32(0));
2116 replaceAndReset(pSearchCondition,pNewNode);
2117 }
2118 else if(*pSearchCondition->getChild(2)->getChild(2) == *pSearchCondition->getChild(0))
2119 {
2120 pNewNode = pSearchCondition->removeAt(sal_uInt32(0));
2121 replaceAndReset(pSearchCondition,pNewNode);
2122 }
2123 }
2124 // a and b or a || b and a or a
2125 else if(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(0),boolean_term))
2126 {
2127 if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2))
2128 {
2129 pNewNode = pSearchCondition->removeAt(sal_uInt32(2));
2130 replaceAndReset(pSearchCondition,pNewNode);
2131 }
2132 else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2))
2133 {
2134 pNewNode = pSearchCondition->removeAt(sal_uInt32(2));
2135 replaceAndReset(pSearchCondition,pNewNode);
2136 }
2137 }
2138 eraseBraces(pSearchCondition);
2139}
2140
2142{
2143 if(!pSearchCondition) // no WHERE condition at entry point
2144 return;
2145
2146 OSQLParseNode::eraseBraces(pSearchCondition);
2147
2148 if(SQL_ISRULE(pSearchCondition,boolean_term) || SQL_ISRULE(pSearchCondition,search_condition))
2149 {
2150 OSQLParseNode* pLeft = pSearchCondition->getChild(0);
2151 compress(pLeft);
2152
2153 OSQLParseNode* pRight = pSearchCondition->getChild(2);
2154 compress(pRight);
2155 }
2156 else if( SQL_ISRULE(pSearchCondition,boolean_primary) || (pSearchCondition->count() == 3 && SQL_ISPUNCTUATION(pSearchCondition->getChild(0),"(") &&
2157 SQL_ISPUNCTUATION(pSearchCondition->getChild(2),")")))
2158 {
2159 OSQLParseNode* pRight = pSearchCondition->getChild(1);
2160 compress(pRight);
2161 // if child is not an or and tree then delete () around child
2162 if(!(SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) || SQL_ISRULE(pSearchCondition->getChild(1),search_condition)) ||
2163 (SQL_ISRULE(pSearchCondition->getChild(1),boolean_term) && SQL_ISRULE(pSearchCondition->getParent(),boolean_term)) ||
2164 (SQL_ISRULE(pSearchCondition->getChild(1),search_condition) && SQL_ISRULE(pSearchCondition->getParent(),search_condition)))
2165 {
2166 OSQLParseNode* pNode = pSearchCondition->removeAt(1);
2167 replaceAndReset(pSearchCondition,pNode);
2168 }
2169 }
2170
2171 // or with two and trees where one element of the and trees are equal
2172 if(!(SQL_ISRULE(pSearchCondition,search_condition) && SQL_ISRULE(pSearchCondition->getChild(0),boolean_term) && SQL_ISRULE(pSearchCondition->getChild(2),boolean_term)))
2173 return;
2174
2175 if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2)->getChild(0))
2176 {
2177 OSQLParseNode* pLeft = pSearchCondition->getChild(0)->removeAt(2);
2178 OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt(2);
2179 OSQLParseNode* pNode = MakeORNode(pLeft,pRight);
2180
2182 pNewRule->append(new OSQLParseNode("(",SQLNodeType::Punctuation));
2183 pNewRule->append(pNode);
2184 pNewRule->append(new OSQLParseNode(")",SQLNodeType::Punctuation));
2185
2188
2189 pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt(sal_uInt32(0)),pNewRule);
2190 replaceAndReset(pSearchCondition,pNode);
2191 }
2192 else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2)->getChild(0))
2193 {
2194 OSQLParseNode* pLeft = pSearchCondition->getChild(0)->removeAt(sal_uInt32(0));
2195 OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt(2);
2196 OSQLParseNode* pNode = MakeORNode(pLeft,pRight);
2197
2199 pNewRule->append(new OSQLParseNode("(",SQLNodeType::Punctuation));
2200 pNewRule->append(pNode);
2201 pNewRule->append(new OSQLParseNode(")",SQLNodeType::Punctuation));
2202
2205
2206 pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt(1),pNewRule);
2207 replaceAndReset(pSearchCondition,pNode);
2208 }
2209 else if(*pSearchCondition->getChild(0)->getChild(0) == *pSearchCondition->getChild(2)->getChild(2))
2210 {
2211 OSQLParseNode* pLeft = pSearchCondition->getChild(0)->removeAt(2);
2212 OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt(sal_uInt32(0));
2213 OSQLParseNode* pNode = MakeORNode(pLeft,pRight);
2214
2216 pNewRule->append(new OSQLParseNode("(",SQLNodeType::Punctuation));
2217 pNewRule->append(pNode);
2218 pNewRule->append(new OSQLParseNode(")",SQLNodeType::Punctuation));
2219
2222
2223 pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt(sal_uInt32(0)),pNewRule);
2224 replaceAndReset(pSearchCondition,pNode);
2225 }
2226 else if(*pSearchCondition->getChild(0)->getChild(2) == *pSearchCondition->getChild(2)->getChild(2))
2227 {
2228 OSQLParseNode* pLeft = pSearchCondition->getChild(0)->removeAt(sal_uInt32(0));
2229 OSQLParseNode* pRight = pSearchCondition->getChild(2)->removeAt(sal_uInt32(0));
2230 OSQLParseNode* pNode = MakeORNode(pLeft,pRight);
2231
2233 pNewRule->append(new OSQLParseNode("(",SQLNodeType::Punctuation));
2234 pNewRule->append(pNode);
2235 pNewRule->append(new OSQLParseNode(")",SQLNodeType::Punctuation));
2236
2239
2240 pNode = MakeANDNode(pSearchCondition->getChild(0)->removeAt(1),pNewRule);
2241 replaceAndReset(pSearchCondition,pNode);
2242 }
2243}
2244#if OSL_DEBUG_LEVEL > 1
2245
2246void OSQLParseNode::showParseTree( OUString& rString ) const
2247{
2248 OUStringBuffer aBuf;
2249 showParseTree( aBuf, 0 );
2250 rString = aBuf.makeStringAndClear();
2251}
2252
2253
2254void OSQLParseNode::showParseTree( OUStringBuffer& _inout_rBuffer, sal_uInt32 nLevel ) const
2255{
2256 for ( sal_uInt32 j=0; j<nLevel; ++j)
2257 _inout_rBuffer.appendAscii( " " );
2258
2259 if ( !isToken() )
2260 {
2261 // Rule name as rule
2262 _inout_rBuffer.appendAscii( "RULE_ID: " );
2263 _inout_rBuffer.append( (sal_Int32)getRuleID() );
2264 _inout_rBuffer.append( '(' );
2265 _inout_rBuffer.append( OSQLParser::RuleIDToStr( getRuleID() ) );
2266 _inout_rBuffer.append( ')' );
2267 _inout_rBuffer.append( '\n' );
2268
2269 // Get the first sub tree
2270 for (auto const& child : m_aChildren)
2271 child->showParseTree( _inout_rBuffer, nLevel+1 );
2272 }
2273 else
2274 {
2275 // Found a token
2276 switch (m_eNodeType)
2277 {
2278
2280 _inout_rBuffer.appendAscii( "SQL_KEYWORD: " );
2281 _inout_rBuffer.append( OStringToOUString( OSQLParser::TokenIDToStr( getTokenID() ), RTL_TEXTENCODING_UTF8 ) );
2282 _inout_rBuffer.append( '\n' );
2283 break;
2284
2285 case SQLNodeType::Name:
2286 _inout_rBuffer.appendAscii( "SQL_NAME: " );
2287 _inout_rBuffer.append( '"' );
2288 _inout_rBuffer.append( m_aNodeValue );
2289 _inout_rBuffer.append( '"' );
2290 _inout_rBuffer.append( '\n' );
2291 break;
2292
2294 _inout_rBuffer.appendAscii( "SQL_STRING: " );
2295 _inout_rBuffer.append( '\'' );
2296 _inout_rBuffer.append( m_aNodeValue );
2297 _inout_rBuffer.append( '\'' );
2298 _inout_rBuffer.append( '\n' );
2299 break;
2300
2302 _inout_rBuffer.appendAscii( "SQL_INTNUM: " );
2303 _inout_rBuffer.append( m_aNodeValue );
2304 _inout_rBuffer.append( '\n' );
2305 break;
2306
2308 _inout_rBuffer.appendAscii( "SQL_APPROXNUM: " );
2309 _inout_rBuffer.append( m_aNodeValue );
2310 _inout_rBuffer.append( '\n' );
2311 break;
2312
2314 _inout_rBuffer.appendAscii( "SQL_PUNCTUATION: " );
2315 _inout_rBuffer.append( m_aNodeValue );
2316 _inout_rBuffer.append( '\n' );
2317 break;
2318
2319 case SQLNodeType::Equal:
2320 case SQLNodeType::Less:
2321 case SQLNodeType::Great:
2325 _inout_rBuffer.append( m_aNodeValue );
2326 _inout_rBuffer.append( '\n' );
2327 break;
2328
2330 _inout_rBuffer.appendAscii( "SQL_ACCESS_DATE: " );
2331 _inout_rBuffer.append( m_aNodeValue );
2332 _inout_rBuffer.append( '\n' );
2333 break;
2334
2336 _inout_rBuffer.appendAscii( "||" );
2337 _inout_rBuffer.append( '\n' );
2338 break;
2339
2340 default:
2341 SAL_INFO( "connectivity.parse", "-- " << int( m_eNodeType ) );
2342 SAL_WARN( "connectivity.parse", "OSQLParser::ShowParseTree: unzulaessiger NodeType" );
2343 }
2344 }
2345}
2346#endif // OSL_DEBUG_LEVEL > 0
2347
2348// Insert methods
2349
2350void OSQLParseNode::insert(sal_uInt32 nPos, OSQLParseNode* pNewSubTree)
2351{
2352 assert(pNewSubTree != nullptr && "OSQLParseNode: invalid NewSubTree");
2353 OSL_ENSURE(pNewSubTree->getParent() == nullptr, "OSQLParseNode: Node is not an orphan");
2354
2355 // Create connection to getParent
2356 pNewSubTree->setParent( this );
2357 m_aChildren.emplace(m_aChildren.begin() + nPos, pNewSubTree);
2358}
2359
2360// removeAt methods
2361
2363{
2364 assert(nPos < m_aChildren.size() && "Illegal position for removeAt");
2365 auto aPos(m_aChildren.begin() + nPos);
2366 auto pNode = std::move(*aPos);
2367
2368 // Set the getParent of the removed node to NULL
2369 pNode->setParent( nullptr );
2370
2371 m_aChildren.erase(aPos);
2372 return pNode.release();
2373}
2374
2375// Replace methods
2376
2378{
2379 assert(pOldSubNode != nullptr && pNewSubNode != nullptr && "OSQLParseNode: invalid nodes");
2380 assert(pOldSubNode != pNewSubNode && "OSQLParseNode: same node");
2381 assert(pNewSubNode->getParent() == nullptr && "OSQLParseNode: node already has getParent");
2382 assert(std::any_of(m_aChildren.begin(), m_aChildren.end(),
2383 [&] (std::unique_ptr<OSQLParseNode> const & r) { return r.get() == pOldSubNode; })
2384 && "OSQLParseNode::Replace() Node not element of parent");
2385 assert(std::none_of(m_aChildren.begin(), m_aChildren.end(),
2386 [&] (std::unique_ptr<OSQLParseNode> const & r) { return r.get() == pNewSubNode; })
2387 && "OSQLParseNode::Replace() Node already element of parent");
2388
2389 pOldSubNode->setParent( nullptr );
2390 pNewSubNode->setParent( this );
2391 auto it = std::find_if(m_aChildren.begin(), m_aChildren.end(),
2392 [&pOldSubNode](const std::unique_ptr<OSQLParseNode>& rxChild) { return rxChild.get() == pOldSubNode; });
2393 assert(it != m_aChildren.end());
2394 it->reset(pNewSubNode);
2395}
2396
2397void OSQLParseNode::parseLeaf(OUStringBuffer& rString, const SQLParseNodeParameter& rParam) const
2398{
2399 // Found a leaf
2400 // Append content to the output string
2401 switch (m_eNodeType)
2402 {
2404 {
2405 if (!rString.isEmpty())
2406 rString.append(" ");
2407
2408 const OString sT = OSQLParser::TokenIDToStr(m_nNodeID, rParam.bInternational ? &rParam.m_rContext : nullptr);
2409 rString.append(OStringToOUString(sT,RTL_TEXTENCODING_UTF8));
2410 } break;
2412 if (!rString.isEmpty())
2413 rString.append(" ");
2414 rString.append(SetQuotation(m_aNodeValue, u"\'", u"\'\'"));
2415 break;
2416 case SQLNodeType::Name:
2417 if (!rString.isEmpty())
2418 {
2419 switch(rString[rString.getLength()-1])
2420 {
2421 case ' ' :
2422 case '.' : break;
2423 default :
2424 if ( rParam.aMetaData.getCatalogSeparator().isEmpty()
2425 || rString[rString.getLength() - 1] != rParam.aMetaData.getCatalogSeparator().toChar()
2426 )
2427 rString.append(" ");
2428 break;
2429 }
2430 }
2431 if (rParam.bQuote)
2432 {
2433 if (rParam.bPredicate)
2434 {
2435 rString.append("[");
2436 rString.append(m_aNodeValue);
2437 rString.append("]");
2438 }
2439 else
2440 rString.append(SetQuotation(m_aNodeValue,
2442 }
2443 else
2444 rString.append(m_aNodeValue);
2445 break;
2447 if (!rString.isEmpty())
2448 rString.append(" ");
2449 rString.append("#");
2450 rString.append(m_aNodeValue);
2451 rString.append("#");
2452 break;
2453
2456 {
2457 OUString aTmp = m_aNodeValue;
2458 static constexpr OUStringLiteral strPoint(u".");
2459 if (rParam.bInternational && rParam.bPredicate && rParam.sDecSep != strPoint)
2460 aTmp = aTmp.replaceAll(strPoint, rParam.sDecSep);
2461
2462 if (!rString.isEmpty())
2463 rString.append(" ");
2464 rString.append(aTmp);
2465
2466 } break;
2468 if ( getParent() && SQL_ISRULE(getParent(),cast_spec) && m_aNodeValue.toChar() == '(' ) // no spaces in front of '('
2469 {
2470 rString.append(m_aNodeValue);
2471 break;
2472 }
2473 [[fallthrough]];
2474 default:
2475 if (!rString.isEmpty() && m_aNodeValue.toChar() != '.' && m_aNodeValue.toChar() != ':' )
2476 {
2477 switch( rString[rString.getLength() - 1] )
2478 {
2479 case ' ' :
2480 case '.' : break;
2481 default :
2482 if ( rParam.aMetaData.getCatalogSeparator().isEmpty()
2483 || rString[rString.getLength() - 1] != rParam.aMetaData.getCatalogSeparator().toChar()
2484 )
2485 rString.append(" ");
2486 break;
2487 }
2488 }
2489 rString.append(m_aNodeValue);
2490 }
2491}
2492
2493
2494sal_Int32 OSQLParser::getFunctionReturnType(std::u16string_view _sFunctionName, const IParseContext* pContext)
2495{
2496 sal_Int32 nType = DataType::VARCHAR;
2497 OString sFunctionName(OUStringToOString(_sFunctionName,RTL_TEXTENCODING_UTF8));
2498
2499 if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ASCII,pContext))) nType = DataType::INTEGER;
2500 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_BIT_LENGTH,pContext))) nType = DataType::INTEGER;
2501 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHAR,pContext))) nType = DataType::VARCHAR;
2502 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CHAR_LENGTH,pContext))) nType = DataType::INTEGER;
2503 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CONCAT,pContext))) nType = DataType::VARCHAR;
2504 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DIFFERENCE,pContext))) nType = DataType::VARCHAR;
2505 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_INSERT,pContext))) nType = DataType::VARCHAR;
2506 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LCASE,pContext))) nType = DataType::VARCHAR;
2507 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LEFT,pContext))) nType = DataType::VARCHAR;
2508 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LENGTH,pContext))) nType = DataType::INTEGER;
2509 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOCATE,pContext))) nType = DataType::VARCHAR;
2510 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOCATE_2,pContext))) nType = DataType::VARCHAR;
2511 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LTRIM,pContext))) nType = DataType::VARCHAR;
2512 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_OCTET_LENGTH,pContext))) nType = DataType::INTEGER;
2513 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_POSITION,pContext))) nType = DataType::INTEGER;
2514 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_REPEAT,pContext))) nType = DataType::VARCHAR;
2515 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_REPLACE,pContext))) nType = DataType::VARCHAR;
2516 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RIGHT,pContext))) nType = DataType::VARCHAR;
2517 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RTRIM,pContext))) nType = DataType::VARCHAR;
2518 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SOUNDEX,pContext))) nType = DataType::VARCHAR;
2519 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SPACE,pContext))) nType = DataType::VARCHAR;
2520 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SUBSTRING,pContext))) nType = DataType::VARCHAR;
2521 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_UCASE,pContext))) nType = DataType::VARCHAR;
2522 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_DATE,pContext))) nType = DataType::DATE;
2523 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_TIME,pContext))) nType = DataType::TIME;
2524 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURRENT_TIMESTAMP,pContext))) nType = DataType::TIMESTAMP;
2525 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURDATE,pContext))) nType = DataType::DATE;
2526 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DATEDIFF,pContext))) nType = DataType::INTEGER;
2527 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DATEVALUE,pContext))) nType = DataType::DATE;
2528 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CURTIME,pContext))) nType = DataType::TIME;
2529 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYNAME,pContext))) nType = DataType::VARCHAR;
2530 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFMONTH,pContext))) nType = DataType::INTEGER;
2531 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFWEEK,pContext))) nType = DataType::INTEGER;
2532 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DAYOFYEAR,pContext))) nType = DataType::INTEGER;
2533 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_EXTRACT,pContext))) nType = DataType::VARCHAR;
2534 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_HOUR,pContext))) nType = DataType::INTEGER;
2535 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MINUTE,pContext))) nType = DataType::INTEGER;
2536 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MONTH,pContext))) nType = DataType::INTEGER;
2537 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MONTHNAME,pContext))) nType = DataType::VARCHAR;
2538 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_NOW,pContext))) nType = DataType::TIMESTAMP;
2539 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_QUARTER,pContext))) nType = DataType::INTEGER;
2540 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SECOND,pContext))) nType = DataType::INTEGER;
2541 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMESTAMPADD,pContext))) nType = DataType::TIMESTAMP;
2542 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMESTAMPDIFF,pContext))) nType = DataType::TIMESTAMP;
2543 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TIMEVALUE,pContext))) nType = DataType::TIMESTAMP;
2544 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_WEEK,pContext))) nType = DataType::INTEGER;
2545 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_YEAR,pContext))) nType = DataType::INTEGER;
2546 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ABS,pContext))) nType = DataType::DOUBLE;
2547 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ACOS,pContext))) nType = DataType::DOUBLE;
2548 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ASIN,pContext))) nType = DataType::DOUBLE;
2549 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ATAN,pContext))) nType = DataType::DOUBLE;
2550 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ATAN2,pContext))) nType = DataType::DOUBLE;
2551 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_CEILING,pContext))) nType = DataType::DOUBLE;
2552 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COS,pContext))) nType = DataType::DOUBLE;
2553 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COT,pContext))) nType = DataType::DOUBLE;
2554 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_DEGREES,pContext))) nType = DataType::DOUBLE;
2555 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_EXP,pContext))) nType = DataType::DOUBLE;
2556 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_FLOOR,pContext))) nType = DataType::DOUBLE;
2557 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOGF,pContext))) nType = DataType::DOUBLE;
2558 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOG,pContext))) nType = DataType::DOUBLE;
2559 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOG10,pContext))) nType = DataType::DOUBLE;
2560 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LN,pContext))) nType = DataType::DOUBLE;
2561 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MOD,pContext))) nType = DataType::DOUBLE;
2562 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_PI,pContext))) nType = DataType::DOUBLE;
2563 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_POWER,pContext))) nType = DataType::DOUBLE;
2564 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RADIANS,pContext))) nType = DataType::DOUBLE;
2565 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_RAND,pContext))) nType = DataType::DOUBLE;
2566 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ROUND,pContext))) nType = DataType::DOUBLE;
2567 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_ROUNDMAGIC,pContext))) nType = DataType::DOUBLE;
2568 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SIGN,pContext))) nType = DataType::DOUBLE;
2569 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SIN,pContext))) nType = DataType::DOUBLE;
2570 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SQRT,pContext))) nType = DataType::DOUBLE;
2571 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TAN,pContext))) nType = DataType::DOUBLE;
2572 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_TRUNCATE,pContext))) nType = DataType::DOUBLE;
2573 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_COUNT,pContext))) nType = DataType::INTEGER;
2574 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MAX,pContext))) nType = DataType::DOUBLE;
2575 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_MIN,pContext))) nType = DataType::DOUBLE;
2576 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_AVG,pContext))) nType = DataType::DOUBLE;
2577 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_SUM,pContext))) nType = DataType::DOUBLE;
2578 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_LOWER,pContext))) nType = DataType::VARCHAR;
2579 else if(sFunctionName.equalsIgnoreAsciiCase(TokenIDToStr(SQL_TOKEN_UPPER,pContext))) nType = DataType::VARCHAR;
2580
2581 return nType;
2582}
2583
2584sal_Int32 OSQLParser::getFunctionParameterType(sal_uInt32 _nTokenId, sal_uInt32 _nPos)
2585{
2586 sal_Int32 nType = DataType::VARCHAR;
2587
2588 if(_nTokenId == SQL_TOKEN_CHAR) nType = DataType::INTEGER;
2589 else if(_nTokenId == SQL_TOKEN_INSERT)
2590 {
2591 if ( _nPos == 2 || _nPos == 3 )
2592 nType = DataType::INTEGER;
2593 }
2594 else if(_nTokenId == SQL_TOKEN_LEFT)
2595 {
2596 if ( _nPos == 2 )
2597 nType = DataType::INTEGER;
2598 }
2599 else if(_nTokenId == SQL_TOKEN_LOCATE)
2600 {
2601 if ( _nPos == 3 )
2602 nType = DataType::INTEGER;
2603 }
2604 else if(_nTokenId == SQL_TOKEN_LOCATE_2)
2605 {
2606 if ( _nPos == 3 )
2607 nType = DataType::INTEGER;
2608 }
2609 else if( _nTokenId == SQL_TOKEN_REPEAT || _nTokenId == SQL_TOKEN_RIGHT )
2610 {
2611 if ( _nPos == 2 )
2612 nType = DataType::INTEGER;
2613 }
2614 else if(_nTokenId == SQL_TOKEN_SPACE )
2615 {
2616 nType = DataType::INTEGER;
2617 }
2618 else if(_nTokenId == SQL_TOKEN_SUBSTRING)
2619 {
2620 if ( _nPos != 1 )
2621 nType = DataType::INTEGER;
2622 }
2623 else if(_nTokenId == SQL_TOKEN_DATEDIFF)
2624 {
2625 if ( _nPos != 1 )
2626 nType = DataType::TIMESTAMP;
2627 }
2628 else if(_nTokenId == SQL_TOKEN_DATEVALUE)
2629 nType = DataType::DATE;
2630 else if(_nTokenId == SQL_TOKEN_DAYNAME)
2631 nType = DataType::DATE;
2632 else if(_nTokenId == SQL_TOKEN_DAYOFMONTH)
2633 nType = DataType::DATE;
2634 else if(_nTokenId == SQL_TOKEN_DAYOFWEEK)
2635 nType = DataType::DATE;
2636 else if(_nTokenId == SQL_TOKEN_DAYOFYEAR)
2637 nType = DataType::DATE;
2638 else if(_nTokenId == SQL_TOKEN_EXTRACT) nType = DataType::VARCHAR;
2639 else if(_nTokenId == SQL_TOKEN_HOUR) nType = DataType::TIME;
2640 else if(_nTokenId == SQL_TOKEN_MINUTE) nType = DataType::TIME;
2641 else if(_nTokenId == SQL_TOKEN_MONTH) nType = DataType::DATE;
2642 else if(_nTokenId == SQL_TOKEN_MONTHNAME) nType = DataType::DATE;
2643 else if(_nTokenId == SQL_TOKEN_NOW) nType = DataType::TIMESTAMP;
2644 else if(_nTokenId == SQL_TOKEN_QUARTER) nType = DataType::DATE;
2645 else if(_nTokenId == SQL_TOKEN_SECOND) nType = DataType::TIME;
2646 else if(_nTokenId == SQL_TOKEN_TIMESTAMPADD) nType = DataType::TIMESTAMP;
2647 else if(_nTokenId == SQL_TOKEN_TIMESTAMPDIFF) nType = DataType::TIMESTAMP;
2648 else if(_nTokenId == SQL_TOKEN_TIMEVALUE) nType = DataType::TIMESTAMP;
2649 else if(_nTokenId == SQL_TOKEN_WEEK) nType = DataType::DATE;
2650 else if(_nTokenId == SQL_TOKEN_YEAR) nType = DataType::DATE;
2651
2652 else if(_nTokenId == SQL_TOKEN_ABS) nType = DataType::DOUBLE;
2653 else if(_nTokenId == SQL_TOKEN_ACOS) nType = DataType::DOUBLE;
2654 else if(_nTokenId == SQL_TOKEN_ASIN) nType = DataType::DOUBLE;
2655 else if(_nTokenId == SQL_TOKEN_ATAN) nType = DataType::DOUBLE;
2656 else if(_nTokenId == SQL_TOKEN_ATAN2) nType = DataType::DOUBLE;
2657 else if(_nTokenId == SQL_TOKEN_CEILING) nType = DataType::DOUBLE;
2658 else if(_nTokenId == SQL_TOKEN_COS) nType = DataType::DOUBLE;
2659 else if(_nTokenId == SQL_TOKEN_COT) nType = DataType::DOUBLE;
2660 else if(_nTokenId == SQL_TOKEN_DEGREES) nType = DataType::DOUBLE;
2661 else if(_nTokenId == SQL_TOKEN_EXP) nType = DataType::DOUBLE;
2662 else if(_nTokenId == SQL_TOKEN_FLOOR) nType = DataType::DOUBLE;
2663 else if(_nTokenId == SQL_TOKEN_LOGF) nType = DataType::DOUBLE;
2664 else if(_nTokenId == SQL_TOKEN_LOG) nType = DataType::DOUBLE;
2665 else if(_nTokenId == SQL_TOKEN_LOG10) nType = DataType::DOUBLE;
2666 else if(_nTokenId == SQL_TOKEN_LN) nType = DataType::DOUBLE;
2667 else if(_nTokenId == SQL_TOKEN_MOD) nType = DataType::DOUBLE;
2668 else if(_nTokenId == SQL_TOKEN_PI) nType = DataType::DOUBLE;
2669 else if(_nTokenId == SQL_TOKEN_POWER) nType = DataType::DOUBLE;
2670 else if(_nTokenId == SQL_TOKEN_RADIANS) nType = DataType::DOUBLE;
2671 else if(_nTokenId == SQL_TOKEN_RAND) nType = DataType::DOUBLE;
2672 else if(_nTokenId == SQL_TOKEN_ROUND) nType = DataType::DOUBLE;
2673 else if(_nTokenId == SQL_TOKEN_ROUNDMAGIC) nType = DataType::DOUBLE;
2674 else if(_nTokenId == SQL_TOKEN_SIGN) nType = DataType::DOUBLE;
2675 else if(_nTokenId == SQL_TOKEN_SIN) nType = DataType::DOUBLE;
2676 else if(_nTokenId == SQL_TOKEN_SQRT) nType = DataType::DOUBLE;
2677 else if(_nTokenId == SQL_TOKEN_TAN) nType = DataType::DOUBLE;
2678 else if(_nTokenId == SQL_TOKEN_TRUNCATE) nType = DataType::DOUBLE;
2679 else if(_nTokenId == SQL_TOKEN_COUNT) nType = DataType::INTEGER;
2680 else if(_nTokenId == SQL_TOKEN_MAX) nType = DataType::DOUBLE;
2681 else if(_nTokenId == SQL_TOKEN_MIN) nType = DataType::DOUBLE;
2682 else if(_nTokenId == SQL_TOKEN_AVG) nType = DataType::DOUBLE;
2683 else if(_nTokenId == SQL_TOKEN_SUM) nType = DataType::DOUBLE;
2684
2685 else if(_nTokenId == SQL_TOKEN_LOWER) nType = DataType::VARCHAR;
2686 else if(_nTokenId == SQL_TOKEN_UPPER) nType = DataType::VARCHAR;
2687
2688 return nType;
2689}
2690
2691
2693{
2694 return m_pData->aErrors;
2695}
2696
2697
2699{
2700 if ( !isRule() )
2701 return UNKNOWN_RULE;
2703}
2704
2706{
2707 OSL_ENSURE(_pTableRef && _pTableRef->count() > 1 && _pTableRef->getKnownRuleID() == OSQLParseNode::table_ref,"Invalid node give, only table ref is allowed!");
2708 const sal_uInt32 nCount = _pTableRef->count();
2709 OUString sTableRange;
2710 if ( nCount == 2 || (nCount == 3 && !_pTableRef->getChild(0)->isToken()) )
2711 {
2712 const OSQLParseNode* pNode = _pTableRef->getChild(nCount - (nCount == 2 ? 1 : 2));
2713 OSL_ENSURE(pNode && (pNode->getKnownRuleID() == OSQLParseNode::table_primary_as_range_column
2715 ,"SQL grammar changed!");
2716 if ( !pNode->isLeaf() )
2717 sTableRange = pNode->getChild(1)->getTokenValue();
2718 } // if ( nCount == 2 || nCount == 3 )
2719
2720 return sTableRange;
2721}
2722
2724{
2725}
2726
2728{
2729}
2730
2732{
2733 std::unique_lock aGuard(m_aMutex);
2734 m_aNodes.push_back(_pNode);
2735}
2736
2738{
2739 std::unique_lock aGuard(m_aMutex);
2740 if ( !m_aNodes.empty() )
2741 {
2742 std::vector< OSQLParseNode* >::iterator aFind = std::find(m_aNodes.begin(), m_aNodes.end(),_pNode);
2743 if ( aFind != m_aNodes.end() )
2744 m_aNodes.erase(aFind);
2745 }
2746}
2747
2749{
2750 std::unique_lock aGuard(m_aMutex);
2751 m_aNodes.clear();
2752}
2753
2755{
2756 std::unique_lock aGuard(m_aMutex);
2757 // clear the garbage collector
2758 while ( !m_aNodes.empty() )
2759 {
2760 OSQLParseNode* pNode = m_aNodes[0];
2761 while ( pNode->getParent() )
2762 {
2763 pNode = pNode->getParent();
2764 }
2765 aGuard.unlock(); // can call back into this object during destruction
2766 delete pNode;
2767 aGuard.lock();
2768 }
2769}
2770} // namespace connectivity
2771
2772/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static double toDouble(std::string_view rString)
Definition: DTable.cxx:1635
Reference< XComponentContext > m_xContext
virtual OUString getErrorMessage(ErrorCode _eCodes) const =0
virtual css::lang::Locale getPreferredLocale() const =0
gets a locale instance which should be used when parsing in the context specified by this instance
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
static const css::lang::Locale & getDefaultLocale()
void impl_parseTableRangeNodeToString_throw(OUStringBuffer &rString, const SQLParseNodeParameter &rParam) const
Definition: sqlnode.cxx:714
static void absorptions(OSQLParseNode *&pSearchCondition)
Definition: sqlnode.cxx:2038
static void eraseBraces(OSQLParseNode *&pSearchCondition)
Definition: sqlnode.cxx:2020
void impl_parseLikeNodeToString_throw(OUStringBuffer &rString, const SQLParseNodeParameter &rParam, bool bSimple=true) const
Definition: sqlnode.cxx:724
bool parseNodeToExecutableStatement(OUString &_out_rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, OSQLParser &_rParser, css::sdbc::SQLException *_pErrorHolder) const
parses the node to a string which can be passed to a driver's connection for execution
Definition: sqlnode.cxx:318
static void negateSearchCondition(OSQLParseNode *&pSearchCondition, bool bNegate=false)
Definition: sqlnode.cxx:1872
static void disjunctiveNormalForm(OSQLParseNode *&pSearchCondition)
Definition: sqlnode.cxx:1794
void parseNodeToStr(OUString &rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const IParseContext *pContext=nullptr, bool _bIntl=false, bool _bQuote=true) const
void setParent(OSQLParseNode *pParseNode)
Definition: sqlnode.hxx:253
OSQLParseNode(const char *_pValueStr, SQLNodeType _eNodeType, sal_uInt32 _nNodeID=0)
Definition: sqlnode.cxx:1580
OSQLParseNode & operator=(const OSQLParseNode &rParseNode)
Definition: sqlnode.cxx:1635
void append(OSQLParseNode *pNewSubTree)
Definition: sqlnode.cxx:1677
bool addDateValue(OUStringBuffer &rString, const SQLParseNodeParameter &rParam) const
Definition: sqlnode.cxx:1691
std::vector< std::unique_ptr< OSQLParseNode > > m_aChildren
Definition: sqlnode.hxx:108
void showParseTree(OUString &rString) const
Definition: sqlnode.cxx:2246
static OUString convertDateTimeString(const SQLParseNodeParameter &rParam, const OUString &rString)
Definition: sqlnode.cxx:213
bool impl_parseTableNameNodeToString_throw(OUStringBuffer &rString, const SQLParseNodeParameter &rParam) const
parses a table_name node into a SQL statement particle.
Definition: sqlnode.cxx:617
sal_uInt32 getTokenID() const
Definition: sqlnode.hxx:350
bool operator==(OSQLParseNode const &rParseNode) const
Definition: sqlnode.cxx:1653
static OUString getTableRange(const OSQLParseNode *_pTableRef)
return a table range when it exists.
Definition: sqlnode.cxx:2705
static OUString convertDateString(const SQLParseNodeParameter &rParam, std::u16string_view rString)
Definition: sqlnode.cxx:201
static void substituteParameterNames(OSQLParseNode const *_pNode)
Definition: sqlnode.cxx:1492
void replaceAndDelete(OSQLParseNode *pOldSubTree, OSQLParseNode *pNewSubTree)
Definition: sqlnode.cxx:2377
Rule getKnownRuleID() const
returns the ID of the rule represented by the node If the node does not represent a rule,...
Definition: sqlnode.cxx:2698
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 impl_parseNodeToString_throw(OUStringBuffer &rString, const SQLParseNodeParameter &rParam, bool bSimple=true) const
Definition: sqlnode.cxx:382
OSQLParseNode * removeAt(sal_uInt32 nPos)
Definition: sqlnode.cxx:2362
static bool getTableComponents(const OSQLParseNode *_pTableNode, css::uno::Any &_rCatalog, OUString &_rSchema, OUString &_rTable, const css::uno::Reference< css::sdbc::XDatabaseMetaData > &_xMetaData)
Definition: sqlnode.cxx:757
const OUString & getTokenValue() const
Definition: sqlnode.hxx:361
sal_uInt32 getRuleID() const
Definition: sqlnode.hxx:342
OSQLParseNode * getParent() const
Definition: sqlnode.hxx:251
void insert(sal_uInt32 nPos, OSQLParseNode *pNewSubTree)
Definition: sqlnode.cxx:2350
static OUString convertTimeString(const SQLParseNodeParameter &rParam, std::u16string_view rString)
Definition: sqlnode.cxx:225
OSQLParseNode * getChild(sal_uInt32 nPos) const
Definition: sqlnode.hxx:433
static void compress(OSQLParseNode *&pSearchCondition)
Definition: sqlnode.cxx:2141
OSQLParseNode * getByRule(OSQLParseNode::Rule eRule) const
Definition: sqlnode.cxx:1759
OSQLParseNode * m_pParent
Definition: sqlnode.hxx:109
void parseLeaf(OUStringBuffer &rString, const SQLParseNodeParameter &rParam) const
Definition: sqlnode.cxx:2397
SQLNodeType getNodeType() const
Definition: sqlnode.hxx:339
void replaceNodeValue(const OUString &rTableAlias, const OUString &rColumnName)
Definition: sqlnode.cxx:1743
::std::vector< OSQLParseNode * > m_aNodes
Definition: sqlparse.hxx:86
void push_back(OSQLParseNode *_pNode)
Definition: sqlnode.cxx:2731
void erase(OSQLParseNode *_pNode)
Definition: sqlnode.cxx:2737
Parser for SQL92.
Definition: sqlparse.hxx:110
void killThousandSeparator(OSQLParseNode *pLiteral)
Definition: sqlnode.cxx:802
bool extractDate(OSQLParseNode const *pLiteral, double &_rfValue)
Definition: sqlnode.cxx:1512
friend class OSQLInternalNode
Definition: sqlparse.hxx:112
static sal_uInt32 s_nRuleIDs[OSQLParseNode::rule_count+1]
Definition: sqlparse.hxx:118
OUString stringToDouble(const OUString &_rValue, sal_Int16 _nScale)
Definition: sqlnode.cxx:1116
static std::mutex & getMutex()
Definition: sqlnode.cxx:1145
OSQLParseNode * buildNode_Date(const double &fValue, sal_Int32 nType)
Definition: sqlnode.cxx:1040
css::uno::Reference< css::uno::XComponentContext > m_xContext
Definition: sqlparse.hxx:140
static sal_Int32 getFunctionParameterType(sal_uInt32 _nTokenId, sal_uInt32 _nPos)
Definition: sqlnode.cxx:2584
css::uno::Reference< css::util::XNumberFormatter > m_xFormatter
Definition: sqlparse.hxx:137
css::uno::Reference< css::beans::XPropertySet > m_xField
Definition: sqlparse.hxx:135
static OSQLScanner * s_pScanner
Definition: sqlparse.hxx:122
static OSQLParseNode::Rule RuleIDToRule(sal_uInt32 _nRule)
static OParseContext s_aDefaultContext
Definition: sqlparse.hxx:120
static sal_Int32 s_nRefCount
Definition: sqlparse.hxx:124
static sal_Int32 getFunctionReturnType(std::u16string_view _sFunctionName, const IParseContext *pContext)
Definition: sqlnode.cxx:2494
const SQLError & getErrorHelper() const
access to the SQLError instance owned by this parser
Definition: sqlnode.cxx:2692
static OString TokenIDToStr(sal_uInt32 nTokenID, const IParseContext *pContext=nullptr)
static vcl::DeleteOnDeinit< css::uno::Reference< css::i18n::XLocaleData4 > > s_xLocaleData
Definition: sqlparse.hxx:142
::std::map< sal_uInt32, OSQLParseNode::Rule > RuleIDMap
Definition: sqlparse.hxx:116
std::unique_ptr< OSQLParseNode > predicateTree(OUString &rErrorMessage, const OUString &rStatement, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter, const css::uno::Reference< css::beans::XPropertySet > &xField, bool bUseRealName=true)
Definition: sqlnode.cxx:1152
static sal_uInt32 StrToRuleID(const OString &rValue)
OSQLParseNode * convertNode(sal_Int32 nType, OSQLParseNode *pLiteral)
Definition: sqlnode.cxx:817
OSQLParseNode * buildNode_STR_NUM(OSQLParseNode *&pLiteral)
Definition: sqlnode.cxx:1088
std::unique_ptr< OSQLParseNode > parseTree(OUString &rErrorMessage, const OUString &rStatement, bool bInternational=false)
css::uno::Reference< css::i18n::XCharacterClassification > m_xCharClass
Definition: sqlparse.hxx:141
OSQLParseNode * buildDate(sal_Int32 _nType, OSQLParseNode *&pLiteral)
Definition: sqlnode.cxx:1561
::std::unique_ptr< OSQLParser_Data > m_pData
Definition: sqlparse.hxx:130
static RuleIDMap s_aReverseRuleIDLookup
Definition: sqlparse.hxx:119
static OSQLParseNodesGarbageCollector * s_pGarbageCollector
Definition: sqlparse.hxx:123
OSQLParser(css::uno::Reference< css::uno::XComponentContext > xContext, const IParseContext *_pContext=nullptr)
Definition: sqlnode.cxx:1309
const IParseContext * m_pContext
Definition: sqlparse.hxx:127
std::unique_ptr< OSQLParseNode > m_pParseTree
Definition: sqlparse.hxx:128
static OUString RuleIDToStr(sal_uInt32 nRuleID)
static sal_uInt32 RuleID(OSQLParseNode::Rule eRule)
Scanner for SQL92.
Definition: sqlscan.hxx:31
static sal_Int32 GetENGRule()
Definition: sqlflex.l:795
static sal_Int32 GetGERRule()
Definition: sqlflex.l:794
static sal_Int32 GetSTRINGRule()
Definition: sqlflex.l:798
void prepareScan(const OUString &rNewStatement, const IParseContext *pContext, bool bInternational)
Definition: sqlflex.l:768
static sal_Int32 GetDATERule()
Definition: sqlflex.l:797
void setScanner(bool _bNull=false)
Definition: sqlflex.l:799
static sal_Int32 GetSQLRule()
Definition: sqlflex.l:796
void SetRule(sal_Int32 nRule)
Definition: sqlscan.hxx:57
const OUString & getErrorMessage() const
Definition: sqlscan.hxx:50
a class which provides helpers for working with SQLErrors
Definition: sqlerror.hxx:59
void raiseException(const ErrorCondition _eCondition, const css::uno::Reference< css::uno::XInterface > &_rxContext, const std::optional< OUString > &_rParamValue1=std::nullopt, const std::optional< OUString > &_rParamValue2=std::nullopt, const std::optional< OUString > &_rParamValue3=std::nullopt) const
throws an SQLException describing the given error condition
const OUString & getIdentifierQuoteString() const
wraps XDatabaseMetaData::getIdentifierQuoteString
Definition: dbmetadata.cxx:257
bool generateASBeforeCorrelationName() const
determines whether when generating SQL statements, an AS keyword should be generated before a correla...
Definition: dbmetadata.cxx:282
bool shouldSubstituteParameterNames() const
should named parameters (:foo, [foo]) be replaced by unnamed parameters (?)
Definition: dbmetadata.cxx:302
bool shouldEscapeDateTime() const
should date time be escaped like '2001-01-01' => {D '2001-01-01' }
Definition: dbmetadata.cxx:292
const OUString & getCatalogSeparator() const
wraps XDatabaseMetaData::getCatalogSeparator
Definition: dbmetadata.cxx:263
bool supportsSubqueriesInFrom() const
determines whether the database supports sub queries in the FROM part of a SELECT clause are supporte...
Definition: dbmetadata.cxx:215
const OUString & getNameByIndex(sal_Int32 _nIndex) const
Definition: propertyids.cxx:95
std::optional< T > set(Args &&... args)
int nCount
#define DBG_UNHANDLED_EXCEPTION(...)
float u
FmFilterData * m_pData
#define TRUE
#define FALSE
sal_uInt16 nPos
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
aStr
aBuf
@ Exception
Any getNumberFormatProperty(const Reference< XNumberFormatter > &_rxFormatter, sal_Int32 _nKey, const OUString &_rPropertyName)
comphelper::SingletonRef< OSQLParseNodesContainer > OSQLParseNodesGarbageCollector
Definition: sqlparse.hxx:97
::std::set< OUString > QueryNameSet
Definition: sqlnode.hxx:65
static OSQLParseNode * MakeANDNode(OSQLParseNode *pLeftLeaf, OSQLParseNode *pRightLeaf)
Definition: sqlnode.cxx:1776
static OSQLParseNode * MakeORNode(OSQLParseNode *pLeftLeaf, OSQLParseNode *pRightLeaf)
Definition: sqlnode.cxx:1785
OOO_DLLPUBLIC_DBTOOLS OUString toTimeString(const css::util::Time &rTime)
OOO_DLLPUBLIC_DBTOOLS OUString toDateString(const css::util::Date &rDate)
OOO_DLLPUBLIC_DBTOOLS css::util::Date toDate(double dVal, const css::util::Date &_rNullDate=getStandardDate())
OOO_DLLPUBLIC_DBTOOLS OUString toDateTimeString(const css::util::DateTime &_rDateTime)
OOO_DLLPUBLIC_DBTOOLS css::util::Date getNULLDate(const css::uno::Reference< css::util::XNumberFormatsSupplier > &xSupplier)
OOO_DLLPUBLIC_DBTOOLS css::util::Time toTime(double dVal, short nDigits=9)
OOO_DLLPUBLIC_DBTOOLS css::util::DateTime toDateTime(double dVal, const css::util::Date &_rNullDate=getStandardDate())
sal_Int32 getDefaultNumberFormat(const Reference< XPropertySet > &_xColumn, const Reference< XNumberFormatTypes > &_xTypes, const Locale &_rLocale)
Definition: dbtools.cxx:115
int i
std::shared_ptr< T > make_shared(Args &&... args)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Definition: pq_tools.cxx:100
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
std::mutex aMutex
#define PROPERTY_ID_NAME
Definition: propertyids.hxx:50
#define PROPERTY_ID_FORMATKEY
Definition: propertyids.hxx:88
#define PROPERTY_ID_TYPE
Definition: propertyids.hxx:51
#define PROPERTY_ID_LOCALE
Definition: propertyids.hxx:89
#define PROPERTY_ID_COMMAND
Definition: propertyids.hxx:72
#define PROPERTY_ID_REALNAME
Definition: propertyids.hxx:79
#define PROPERTY_ID_ESCAPEPROCESSING
Definition: propertyids.hxx:47
QPRO_FUNC_TYPE nType
OUString ConvertLikeToken(const ::connectivity::OSQLParseNode *pTokenNode, const ::connectivity::OSQLParseNode *pEscapeNode, bool bInternational)
void setParser(::connectivity::OSQLParser *)
int SQLyyparse()
#define SQL_ISRULE(pParseNode, eRule)
Definition: sqlnode.hxx:439
#define SQL_ISPUNCTUATION(pParseNode, aString)
Definition: sqlnode.hxx:450
#define SQL_ISTOKEN(pParseNode, token)
Definition: sqlnode.hxx:447
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, OUString _sPredicateTableAlias, const css::lang::Locale &_rLocale, const IParseContext *_pContext, bool _bIntl, bool _bQuote, OUString _sDecSep, bool _bPredicate, bool _bParseToSDBC)
should we create an SDBC-level statement (e.g. with substituted sub queries)?
Definition: sqlnode.cxx:180
::dbtools::DatabaseMetaData aMetaData
Definition: sqlnode.hxx:72
const IParseContext & m_rContext
Definition: sqlnode.hxx:79
const css::lang::Locale & rLocale
Definition: sqlnode.hxx:71
bool bPredicate
should we internationalize keywords and placeholders?
Definition: sqlnode.hxx:83
css::uno::Reference< css::beans::XPropertySet > xField
Definition: sqlnode.hxx:76
css::uno::Reference< css::util::XNumberFormatter > xFormatter
Definition: sqlnode.hxx:75
std::shared_ptr< QueryNameSet > pSubQueryHistory
Definition: sqlnode.hxx:74
css::uno::Reference< css::container::XNameAccess > xQueries
Definition: sqlnode.hxx:78
bool bParseToSDBCLevel
are we going to parse a mere predicate?
Definition: sqlnode.hxx:84
bool bInternational
should we quote identifiers?
Definition: sqlnode.hxx:82
const Reference< XComponentContext > & m_rContext
NOT
IS
sal_uInt16 sal_Unicode
sal_Int32 _nPos