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