PageRenderTime 82ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/connectivity/source/parse/sqlnode.cxx

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