LibreOffice Module dbaccess (master) 1
SingleSelectQueryComposer.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <string.h>
21#include <sal/log.hxx>
22#include <composertools.hxx>
23#include <strings.hrc>
24#include <strings.hxx>
25#include <core_resource.hxx>
26#include <stringconstants.hxx>
27#include "HelperCollections.hxx"
29#include <sqlbison.hxx>
30#include <sdbcoretools.hxx>
31
32#include <com/sun/star/beans/PropertyAttribute.hpp>
33#include <com/sun/star/i18n/LocaleData.hpp>
34#include <com/sun/star/script/Converter.hpp>
35#include <com/sun/star/sdb/BooleanComparisonMode.hpp>
36#include <com/sun/star/sdb/SQLFilterOperator.hpp>
37#include <com/sun/star/sdb/XQueriesSupplier.hpp>
38#include <com/sun/star/sdb/CommandType.hpp>
39#include <com/sun/star/sdbc/ColumnSearch.hpp>
40#include <com/sun/star/sdbc/DataType.hpp>
41#include <com/sun/star/sdbc/XConnection.hpp>
42#include <com/sun/star/sdbc/XResultSetMetaData.hpp>
43#include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
44#include <com/sun/star/sdbc/XParameters.hpp>
45#include <com/sun/star/util/NumberFormatter.hpp>
46
47#include <comphelper/types.hxx>
53#include <osl/diagnose.h>
55
56#include <memory>
57#include <string_view>
58
59using namespace ::dbaccess;
60using namespace ::dbtools;
61using namespace ::comphelper;
62using namespace ::connectivity;
63using namespace ::com::sun::star::uno;
64using namespace ::com::sun::star::beans;
65using namespace ::com::sun::star::sdbc;
66using namespace ::com::sun::star::sdb;
67using namespace ::com::sun::star::sdbcx;
68using namespace ::com::sun::star::container;
69using namespace ::com::sun::star::i18n;
70using namespace ::com::sun::star::lang;
71using namespace ::com::sun::star::script;
72using namespace ::com::sun::star::util;
73using namespace ::cppu;
74using namespace ::osl;
75using namespace ::utl;
76
77namespace dbaccess {
78namespace BooleanComparisonMode = ::com::sun::star::sdb::BooleanComparisonMode;
79}
80
81constexpr OUStringLiteral STR_SELECT = u"SELECT ";
82constexpr OUStringLiteral STR_FROM = u" FROM ";
83constexpr OUStringLiteral STR_WHERE = u" WHERE ";
84constexpr OUStringLiteral STR_GROUP_BY = u" GROUP BY ";
85constexpr OUStringLiteral STR_HAVING = u" HAVING ";
86constexpr OUStringLiteral STR_ORDER_BY = u" ORDER BY ";
87constexpr OUStringLiteral STR_AND = u" AND ";
88constexpr OUStringLiteral STR_OR = u" OR ";
89constexpr OUStringLiteral STR_LIKE = u" LIKE ";
90constexpr OUStringLiteral L_BRACKET = u"(";
91constexpr OUStringLiteral R_BRACKET = u")";
92constexpr OUStringLiteral COMMA = u",";
93
94namespace
95{
101 std::unique_ptr<OSQLParseNode> parseStatement_throwError( OSQLParser& _rParser, const OUString& _rStatement, const Reference< XInterface >& _rxContext )
102 {
103 OUString aErrorMsg;
104 std::unique_ptr<OSQLParseNode> pNewSqlParseNode = _rParser.parseTree( aErrorMsg, _rStatement );
105 if ( !pNewSqlParseNode )
106 {
107 OUString sSQLStateGeneralError( getStandardSQLState( StandardSQLState::GENERAL_ERROR ) );
108 SQLException aError2( aErrorMsg, _rxContext, sSQLStateGeneralError, 1000, Any() );
109 SQLException aError1( _rStatement, _rxContext, sSQLStateGeneralError, 1000, Any( aError2 ) );
110 throw SQLException(_rParser.getContext().getErrorMessage(OParseContext::ErrorCode::General),_rxContext,sSQLStateGeneralError,1000,Any(aError1));
111 }
112 return pNewSqlParseNode;
113 }
114
118 void checkForSingleSelect_throwError( const OSQLParseNode* pStatementNode, OSQLParseTreeIterator& _rIterator,
119 const Reference< XInterface >& _rxContext, const OUString& _rOriginatingCommand )
120 {
121 const OSQLParseNode* pOldNode = _rIterator.getParseTree();
122
123 // determine the statement type
124 _rIterator.setParseTree( pStatementNode );
125 _rIterator.traverseAll();
126 bool bIsSingleSelect = ( _rIterator.getStatementType() == OSQLStatementType::Select );
127
128 // throw the error, if necessary
129 if ( !bIsSingleSelect || SQL_ISRULE( pStatementNode, union_statement ) ) // #i4229# OJ
130 {
131 // restore the old node before throwing the exception
132 _rIterator.setParseTree( pOldNode );
133 // and now really ...
134 SQLException aError1( _rOriginatingCommand, _rxContext, getStandardSQLState( StandardSQLState::GENERAL_ERROR ), 1000, Any() );
135 throw SQLException( DBA_RES( RID_STR_ONLY_QUERY ), _rxContext,
136 getStandardSQLState( StandardSQLState::GENERAL_ERROR ), 1000, Any( aError1 ) );
137 }
138
139 delete pOldNode;
140 }
141
144 void parseAndCheck_throwError( OSQLParser& _rParser, const OUString& _rStatement,
145 OSQLParseTreeIterator& _rIterator, const Reference< XInterface >& _rxContext )
146 {
147 std::unique_ptr<OSQLParseNode> pNode = parseStatement_throwError( _rParser, _rStatement, _rxContext );
148 checkForSingleSelect_throwError( pNode.release(), _rIterator, _rxContext, _rStatement );
149 }
150
154 OUString getPureSelectStatement( const OSQLParseNode* _pRootNode, const Reference< XConnection >& _rxConnection )
155 {
156 OUString sSQL = STR_SELECT;
157 _pRootNode->getChild(1)->parseNodeToStr( sSQL, _rxConnection );
158 _pRootNode->getChild(2)->parseNodeToStr( sSQL, _rxConnection );
159 sSQL += STR_FROM;
160 _pRootNode->getChild(3)->getChild(0)->getChild(1)->parseNodeToStr( sSQL, _rxConnection );
161 return sSQL;
162 }
163
166 void resetIterator( OSQLParseTreeIterator& _rIterator )
167 {
168 const OSQLParseNode* pSqlParseNode = _rIterator.getParseTree();
169 _rIterator.setParseTree(nullptr);
170 delete pSqlParseNode;
171 _rIterator.dispose();
172 }
173 void lcl_addFilterCriteria_throw(sal_Int32 i_nFilterOperator,std::u16string_view i_sValue,OUStringBuffer& o_sRet)
174 {
175 switch( i_nFilterOperator )
176 {
177 case SQLFilterOperator::EQUAL:
178 o_sRet.append(OUString::Concat(" = ") + i_sValue);
179 break;
180 case SQLFilterOperator::NOT_EQUAL:
181 o_sRet.append(OUString::Concat(" <> ") + i_sValue);
182 break;
183 case SQLFilterOperator::LESS:
184 o_sRet.append(OUString::Concat(" < ") + i_sValue);
185 break;
186 case SQLFilterOperator::GREATER:
187 o_sRet.append(OUString::Concat(" > ") + i_sValue);
188 break;
189 case SQLFilterOperator::LESS_EQUAL:
190 o_sRet.append(OUString::Concat(" <= ") + i_sValue);
191 break;
192 case SQLFilterOperator::GREATER_EQUAL:
193 o_sRet.append(OUString::Concat(" >= ") + i_sValue);
194 break;
195 case SQLFilterOperator::LIKE:
196 o_sRet.append(OUString::Concat(" LIKE ") + i_sValue);
197 break;
198 case SQLFilterOperator::NOT_LIKE:
199 o_sRet.append(OUString::Concat(" NOT LIKE ") + i_sValue);
200 break;
201 case SQLFilterOperator::SQLNULL:
202 o_sRet.append(" IS NULL");
203 break;
204 case SQLFilterOperator::NOT_SQLNULL:
205 o_sRet.append(" IS NOT NULL");
206 break;
207 default:
208 throw SQLException();
209 }
210 }
211
212}
213
214
215OSingleSelectQueryComposer::OSingleSelectQueryComposer(const Reference< XNameAccess>& _rxTables,
216 const Reference< XConnection>& _xConnection,
217 const Reference<XComponentContext>& _rContext )
218 :OSubComponent(m_aMutex,_xConnection)
219 ,OPropertyContainer(m_aBHelper)
220 ,m_aSqlParser( _rContext, &m_aParseContext )
221 ,m_aSqlIterator( _xConnection, _rxTables, m_aSqlParser )
222 ,m_aAdditiveIterator( _xConnection, _rxTables, m_aSqlParser )
223 ,m_aElementaryParts( size_t(SQLPartCount) )
224 ,m_xConnection(_xConnection)
225 ,m_xMetaData(_xConnection->getMetaData())
226 ,m_xConnectionTables( _rxTables )
227 ,m_aContext( _rContext )
228 ,m_nBoolCompareMode( BooleanComparisonMode::EQUAL_INTEGER )
230{
231 if ( !m_aContext.is() || !m_xConnection.is() || !m_xConnectionTables.is() )
232 throw IllegalArgumentException();
233
234 registerProperty(PROPERTY_ORIGINAL,PROPERTY_ID_ORIGINAL,PropertyAttribute::BOUND|PropertyAttribute::READONLY,&m_sOriginal,cppu::UnoType<decltype(m_sOriginal)>::get());
235
236 m_aCurrentColumns.resize(4);
237
240 Reference< XLocaleData4 > xLocaleData( LocaleData::create(m_aContext) );
241 LocaleDataItem aData = xLocaleData->getLocaleItem(m_aLocale);
242 m_sDecimalSep = aData.decimalSeparator;
243 OSL_ENSURE(m_sDecimalSep.getLength() == 1,"OSingleSelectQueryComposer::OSingleSelectQueryComposer decimal separator is not 1 length");
244 try
245 {
246 Any aValue;
247 Reference<XInterface> xDs = dbaccess::getDataSource(_xConnection);
248 if ( dbtools::getDataSourceSetting(xDs,static_cast <OUString> (PROPERTY_BOOLEANCOMPARISONMODE),aValue) )
249 {
250 OSL_VERIFY( aValue >>= m_nBoolCompareMode );
251 }
252 Reference< XQueriesSupplier > xQueriesAccess(m_xConnection, UNO_QUERY);
253 if (xQueriesAccess.is())
254 m_xConnectionQueries = xQueriesAccess->getQueries();
255 }
256 catch(Exception&)
257 {
258 }
259}
260
262{
263}
264
265// OComponentHelper
267{
268 OSubComponent::disposing();
269
270 MutexGuard aGuard(m_aMutex);
271
272 resetIterator( m_aSqlIterator );
273 resetIterator( m_aAdditiveIterator );
274
275 m_xConnectionTables = nullptr;
276 m_xConnection = nullptr;
277
279}
280
282OUString SAL_CALL OSingleSelectQueryComposer::getImplementationName()
283 {
284 return "org.openoffice.comp.dba.OSingleSelectQueryComposer";
285 }
286sal_Bool SAL_CALL OSingleSelectQueryComposer::supportsService(const OUString& _rServiceName)
287 {
288 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
289 for (const OUString& s : aSupported)
290 if (s == _rServiceName)
291 return true;
292
293 return false;
294 }
295css::uno::Sequence< OUString > SAL_CALL OSingleSelectQueryComposer::getSupportedServiceNames()
296{
298}
299
301{
302 return css::uno::Sequence<sal_Int8>();
303}
304
305css::uno::Sequence< css::uno::Type > OSingleSelectQueryComposer::getTypes()
306{
307 return ::comphelper::concatSequences(
308 OSubComponent::getTypes( ),
310 OPropertyContainer::getTypes( )
311 );
312}
313
314css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL OSingleSelectQueryComposer::getPropertySetInfo()
315{
316 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
317 return xInfo;
318}
320{
322}
324{
325 css::uno::Sequence< css::beans::Property > aProps;
326 describeProperties(aProps);
327 return new ::cppu::OPropertyArrayHelper(aProps);
328}
329
330
331// XSingleSelectQueryAnalyzer
333{
334 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
335 ::osl::MutexGuard aGuard( m_aMutex );
336
338 return getStatementPart(F_tmp,m_aSqlIterator);
339}
340
341void SAL_CALL OSingleSelectQueryComposer::setQuery( const OUString& command )
342{
343 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
344
345 ::osl::MutexGuard aGuard( m_aMutex );
346 m_nCommandType = CommandType::COMMAND;
347 // first clear the tables and columns
349 // now set the new one
350 setQuery_Impl(command);
351 m_sOriginal = command;
352
353 // reset the additive iterator to the same statement
354 parseAndCheck_throwError( m_aSqlParser, m_sOriginal, m_aAdditiveIterator, *this );
355
356 // we have no "elementary" parts anymore (means filter/groupby/having/order clauses)
357 for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
358 m_aElementaryParts[ eLoopParts ].clear();
359}
360
361void SAL_CALL OSingleSelectQueryComposer::setCommand( const OUString& Command,sal_Int32 _nCommandType )
362{
363 OUStringBuffer sSQL;
364 switch(_nCommandType)
365 {
366 case CommandType::COMMAND:
368 return;
370 if ( m_xConnectionTables->hasByName(Command) )
371 {
372 sSQL.append("SELECT * FROM ");
373 Reference< XPropertySet > xTable;
374 try
375 {
376 m_xConnectionTables->getByName( Command ) >>= xTable;
377 }
378 catch(const WrappedTargetException& e)
379 {
380 SQLException e2;
381 if ( e.TargetException >>= e2 )
382 throw e2;
383 }
384 catch(Exception&)
385 {
386 DBG_UNHANDLED_EXCEPTION("dbaccess");
387 }
388
390 }
391 else
392 {
393 OUString sMessage( DBA_RES( RID_STR_TABLE_DOES_NOT_EXIST ) );
394 throwGenericSQLException(sMessage.replaceAll( "$table$", Command ),*this);
395 }
396 break;
398 if ( m_xConnectionQueries->hasByName(Command) )
399 {
400
401 Reference<XPropertySet> xQuery(m_xConnectionQueries->getByName(Command),UNO_QUERY);
402 OUString sCommand;
403 xQuery->getPropertyValue(PROPERTY_COMMAND) >>= sCommand;
404 sSQL.append(sCommand);
405 }
406 else
407 {
408 OUString sMessage( DBA_RES( RID_STR_QUERY_DOES_NOT_EXIST ) );
409 throwGenericSQLException(sMessage.replaceAll( "$table$", Command ),*this);
410 }
411
412 break;
413 default:
414 break;
415 }
416 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
417
418 ::osl::MutexGuard aGuard( m_aMutex );
419 m_nCommandType = _nCommandType;
421 // first clear the tables and columns
423 // now set the new one
424 OUString sCommand = sSQL.makeStringAndClear();
425 setElementaryQuery(sCommand);
426 m_sOriginal = sCommand;
427}
428
429void OSingleSelectQueryComposer::setQuery_Impl( const OUString& command )
430{
431 // parse this
432 parseAndCheck_throwError( m_aSqlParser, command, m_aSqlIterator, *this );
433
434 // strip it from all clauses, to have the pure SELECT statement
435 m_aPureSelectSQL = getPureSelectStatement( m_aSqlIterator.getParseTree(), m_xConnection );
436
437 // update tables
438 getTables();
439}
440
441Sequence< Sequence< PropertyValue > > SAL_CALL OSingleSelectQueryComposer::getStructuredHavingClause( )
442{
444 return getStructuredCondition(F_tmp);
445}
446
447Sequence< Sequence< PropertyValue > > SAL_CALL OSingleSelectQueryComposer::getStructuredFilter( )
448{
450 return getStructuredCondition(F_tmp);
451}
452
453void SAL_CALL OSingleSelectQueryComposer::appendHavingClauseByColumn( const Reference< XPropertySet >& column, sal_Bool andCriteria,sal_Int32 filterOperator )
454{
455 auto F_tmp = std::mem_fn(&OSingleSelectQueryComposer::implSetHavingClause);
456 setConditionByColumn(column,andCriteria,F_tmp,filterOperator);
457}
458
459void SAL_CALL OSingleSelectQueryComposer::appendFilterByColumn( const Reference< XPropertySet >& column, sal_Bool andCriteria,sal_Int32 filterOperator )
460{
461 auto F_tmp = std::mem_fn(&OSingleSelectQueryComposer::implSetFilter);
462 setConditionByColumn(column,andCriteria,F_tmp,filterOperator);
463}
464
465OUString OSingleSelectQueryComposer::impl_getColumnRealName_throw(const Reference< XPropertySet >& column, bool bGroupBy)
466{
467 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
468
469 getColumns();
470 if ( !column.is()
472 || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_NAME)
473 )
474 {
475 OUString sError(DBA_RES(RID_STR_COLUMN_UNKNOWN_PROP));
476 SQLException aErr(sError.replaceAll("%value", PROPERTY_NAME),*this,SQLSTATE_GENERAL,1000,Any() );
477 throw SQLException(DBA_RES(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any(aErr) );
478 }
479
480 OUString aName, aNewName;
481 column->getPropertyValue(PROPERTY_NAME) >>= aName;
482
483 if ( bGroupBy &&
484 !m_xMetaData->supportsGroupByUnrelated() &&
487 {
488 OUString sError(DBA_RES(RID_STR_COLUMN_MUST_VISIBLE));
489 throw SQLException(sError.replaceAll("%name", aName),*this,SQLSTATE_GENERAL,1000,Any() );
490 }
491
492 OUString aQuote = m_xMetaData->getIdentifierQuoteString();
493 if ( m_aCurrentColumns[SelectColumns]->hasByName(aName) )
494 {
495 Reference<XPropertySet> xColumn;
497 OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME),"Property REALNAME not available!");
498 OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME),"Property TABLENAME not available!");
499 OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName("Function"),"Property FUNCTION not available!");
500
501 OUString sRealName, sTableName;
502 xColumn->getPropertyValue(PROPERTY_REALNAME) >>= sRealName;
503 xColumn->getPropertyValue(PROPERTY_TABLENAME) >>= sTableName;
504 bool bFunction = false;
505 xColumn->getPropertyValue("Function") >>= bFunction;
506 if ( sRealName == aName )
507 {
508 if ( bFunction )
509 aNewName = aName;
510 else
511 {
512 if(sTableName.indexOf('.') != -1)
513 {
514 OUString aCatalog,aSchema,aTable;
515 ::dbtools::qualifiedNameComponents(m_xMetaData,sTableName,aCatalog,aSchema,aTable,::dbtools::EComposeRule::InDataManipulation);
516 sTableName = ::dbtools::composeTableName( m_xMetaData, aCatalog, aSchema, aTable, true, ::dbtools::EComposeRule::InDataManipulation );
517 }
518 else if (!sTableName.isEmpty())
519 sTableName = ::dbtools::quoteName(aQuote,sTableName);
520
521 if(sTableName.isEmpty())
522 aNewName = ::dbtools::quoteName(aQuote,sRealName);
523 else
524 aNewName = sTableName + "." + ::dbtools::quoteName(aQuote,sRealName);
525 }
526 }
527 else
528 aNewName = ::dbtools::quoteName(aQuote,aName);
529 }
530 else
531 aNewName = getTableAlias(column) + ::dbtools::quoteName(aQuote,aName);
532 return aNewName;
533}
534
535OUString OSingleSelectQueryComposer::impl_getColumnNameOrderBy_throw(const Reference< XPropertySet >& column)
536{
537 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
538
539 getColumns();
540 if ( !column.is()
542 || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_NAME)
543 )
544 {
545 OUString sError(DBA_RES(RID_STR_COLUMN_UNKNOWN_PROP));
546 SQLException aErr(sError.replaceAll("%value", PROPERTY_NAME),*this,SQLSTATE_GENERAL,1000,Any() );
547 throw SQLException(DBA_RES(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any(aErr) );
548 }
549
550 OUString aName;
551 column->getPropertyValue(PROPERTY_NAME) >>= aName;
552
553 const OUString aQuote = m_xMetaData->getIdentifierQuoteString();
554
557 {
558 // It is a column from the SELECT list, use it as such.
559 return ::dbtools::quoteName(aQuote,aName);
560 }
561
562 // Nope, it is an unrelated column.
563 // Is that supported?
564 if ( !m_xMetaData->supportsOrderByUnrelated() )
565 {
566 OUString sError(DBA_RES(RID_STR_COLUMN_MUST_VISIBLE));
567 throw SQLException(sError.replaceAll("%name", aName),*this,SQLSTATE_GENERAL,1000,Any() );
568 }
569
570 // We need to refer to it by its "real" name, that is by schemaName.tableName.columnNameInTable
571 return impl_getColumnRealName_throw(column, false);
572}
573
574void SAL_CALL OSingleSelectQueryComposer::appendOrderByColumn( const Reference< XPropertySet >& column, sal_Bool ascending )
575{
576 ::osl::MutexGuard aGuard( m_aMutex );
577 OUString sColumnName( impl_getColumnNameOrderBy_throw(column) );
578 OUString sOrder = getOrder();
579 if ( !(sOrder.isEmpty() || sColumnName.isEmpty()) )
580 sOrder += COMMA;
581 sOrder += sColumnName;
582 if ( !(ascending || sColumnName.isEmpty()) )
583 sOrder += " DESC ";
584
585 setOrder(sOrder);
586}
587
588void SAL_CALL OSingleSelectQueryComposer::appendGroupByColumn( const Reference< XPropertySet >& column)
589{
590 ::osl::MutexGuard aGuard( m_aMutex );
591 OUString sColumnName( impl_getColumnRealName_throw(column, true) );
592 OrderCreator aComposer;
593 aComposer.append( getGroup() );
594 aComposer.append( sColumnName );
595 setGroup( aComposer.getComposedAndClear() );
596}
597
598OUString OSingleSelectQueryComposer::composeStatementFromParts( const std::vector< OUString >& _rParts )
599{
600 OSL_ENSURE( _rParts.size() == size_t(SQLPartCount), "OSingleSelectQueryComposer::composeStatementFromParts: invalid parts array!" );
601
602 OUStringBuffer aSql( m_aPureSelectSQL );
603 for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
604 if ( !_rParts[ eLoopParts ].isEmpty() )
605 {
606 aSql.append( getKeyword( eLoopParts ) );
607 aSql.append( _rParts[ eLoopParts ] );
608 }
609
610 return aSql.makeStringAndClear();
611}
612
614{
616}
617
618void SAL_CALL OSingleSelectQueryComposer::setElementaryQuery( const OUString& _rElementary )
619{
620 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
621 ::osl::MutexGuard aGuard( m_aMutex );
622
623 // remember the 4 current "additive" clauses
624 std::vector< OUString > aAdditiveClauses( SQLPartCount );
625 for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
626 aAdditiveClauses[ eLoopParts ] = getSQLPart( eLoopParts, m_aAdditiveIterator, false );
627
628 // clear the tables and columns
630 // set and parse the new query
631 setQuery_Impl( _rElementary );
632
633 // get the 4 elementary parts of the statement
634 for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
635 m_aElementaryParts[ eLoopParts ] = getSQLPart( eLoopParts, m_aSqlIterator, false );
636
637 // reset the AdditiveIterator: m_aPureSelectSQL may have changed
638 try
639 {
640 parseAndCheck_throwError( m_aSqlParser, composeStatementFromParts( aAdditiveClauses ), m_aAdditiveIterator, *this );
641 }
642 catch( const Exception& )
643 {
644 DBG_UNHANDLED_EXCEPTION("dbaccess");
645 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setElementaryQuery: there should be no error anymore for the additive statement!" );
646 // every part of the additive statement should have passed other tests already, and should not
647 // be able to cause any errors ... me thinks
648 }
649}
650
651namespace
652{
653 OUString getComposedClause( const OUString& _rElementaryClause, const OUString& _rAdditionalClause,
654 TokenComposer& _rComposer, std::u16string_view _rKeyword )
655 {
656 _rComposer.clear();
657 _rComposer.append( _rElementaryClause );
658 _rComposer.append( _rAdditionalClause );
659 OUString sComposed = _rComposer.getComposedAndClear();
660 if ( !sComposed.isEmpty() )
661 sComposed = _rKeyword + sComposed;
662 return sComposed;
663 }
664}
665
666void OSingleSelectQueryComposer::setSingleAdditiveClause( SQLPart _ePart, const OUString& _rClause )
667{
668 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
669 ::osl::MutexGuard aGuard( m_aMutex );
670
671 // if nothing is changed, do nothing
672 if ( getSQLPart( _ePart, m_aAdditiveIterator, false ) == _rClause )
673 return;
674
675 // collect the 4 single parts as they're currently set
676 std::vector< OUString > aClauses;
677 aClauses.reserve( size_t(SQLPartCount) );
678 for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
679 aClauses.push_back( getSQLPart( eLoopParts, m_aSqlIterator, true ) );
680
681 // overwrite the one part in question here
682 std::unique_ptr< TokenComposer > pComposer;
683 if ( ( _ePart == Where ) || ( _ePart == Having ) )
684 pComposer.reset( new FilterCreator );
685 else
686 pComposer.reset( new OrderCreator );
687 aClauses[ _ePart ] = getComposedClause( m_aElementaryParts[ _ePart ], _rClause,
688 *pComposer, getKeyword( _ePart ) );
689
690 // construct the complete statement
691 OUStringBuffer aSql(m_aPureSelectSQL);
692 for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
693 aSql.append(aClauses[ eLoopParts ]);
694
695 // set the query
696 setQuery_Impl(aSql.makeStringAndClear());
697
698 // clear column collections which (might) have changed
700 if ( _ePart == Order )
702 else if ( _ePart == Group )
704
705 // also, since the "additive filter" change, we need to rebuild our "additive" statement
706 aSql = m_aPureSelectSQL;
707 // again, first get all the old additive parts
708 for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
709 aClauses[ eLoopParts ] = getSQLPart( eLoopParts, m_aAdditiveIterator, true );
710 // then overwrite the one in question
711 aClauses[ _ePart ] = getComposedClause( OUString(), _rClause, *pComposer, getKeyword( _ePart ) );
712 // and parse it, so that m_aAdditiveIterator is up to date
713 for ( SQLPart eLoopParts = Where; eLoopParts != SQLPartCount; incSQLPart( eLoopParts ) )
714 aSql.append(aClauses[ eLoopParts ]);
715 try
716 {
717 parseAndCheck_throwError( m_aSqlParser, aSql.makeStringAndClear(), m_aAdditiveIterator, *this );
718 }
719 catch( const Exception& )
720 {
721 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setSingleAdditiveClause: there should be no error anymore for the additive statement!" );
722 // every part of the additive statement should have passed other tests already, and should not
723 // be able to cause any errors ... me thinks
724 }
725}
726
727void SAL_CALL OSingleSelectQueryComposer::setFilter( const OUString& filter )
728{
730}
731
732void SAL_CALL OSingleSelectQueryComposer::setOrder( const OUString& order )
733{
735}
736
737void SAL_CALL OSingleSelectQueryComposer::setGroup( const OUString& group )
738{
740}
741
743{
745}
746
747// XTablesSupplier
748Reference< XNameAccess > SAL_CALL OSingleSelectQueryComposer::getTables( )
749{
750 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
751
752 ::osl::MutexGuard aGuard( m_aMutex );
753 if ( !m_pTables )
754 {
755 const OSQLTables& aTables = m_aSqlIterator.getTables();
756 std::vector< OUString> aNames;
757 for (auto const& elem : aTables)
758 aNames.push_back(elem.first);
759
760 m_pTables.reset( new OPrivateTables(aTables,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,std::move(aNames)) );
761 }
762
763 return m_pTables.get();
764}
765
766// XColumnsSupplier
767Reference< XNameAccess > SAL_CALL OSingleSelectQueryComposer::getColumns( )
768{
769 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
770 ::osl::MutexGuard aGuard( m_aMutex );
772 return m_aCurrentColumns[SelectColumns].get();
773
774 std::vector< OUString> aNames;
775 ::rtl::Reference< OSQLColumns> aSelectColumns;
776 bool bCase = true;
777 Reference< XNameAccess> xQueryColumns;
779 {
780 Reference<XColumnsSupplier> xSup(m_xConnectionQueries->getByName(m_sCommand),UNO_QUERY);
781 if(xSup.is())
782 xQueryColumns = xSup->getColumns();
783 }
784
785 do {
786
787 try
788 {
789 SharedUNOComponent< XStatement, DisposableComponent > xStatement;
790 SharedUNOComponent< XPreparedStatement, DisposableComponent > xPreparedStatement;
791
792 bCase = m_xMetaData->supportsMixedCaseQuotedIdentifiers();
793 aSelectColumns = m_aSqlIterator.getSelectColumns();
794
795 OUStringBuffer aSQL( m_aPureSelectSQL + STR_WHERE + " ( 0 = 1 )");
796
797 // preserve the original WHERE clause
798 // #i102234#
799 OUString sOriginalWhereClause = getSQLPart( Where, m_aSqlIterator, false );
800 if ( !sOriginalWhereClause.isEmpty() )
801 {
802 aSQL.append( " AND ( " + sOriginalWhereClause + " ) " );
803 }
804
805 OUString sGroupBy = getSQLPart( Group, m_aSqlIterator, true );
806 if ( !sGroupBy.isEmpty() )
807 aSQL.append( sGroupBy );
808
809 OUString sSQL( aSQL.makeStringAndClear() );
810 // normalize the statement so that it doesn't contain any application-level features anymore
811 OUString sError;
812 const std::unique_ptr< OSQLParseNode > pStatementTree( m_aSqlParser.parseTree( sError, sSQL ) );
813 OSL_ENSURE(pStatementTree, "OSingleSelectQueryComposer::getColumns: could not parse the "
814 "column retrieval statement!");
815 if (pStatementTree)
816 if ( !pStatementTree->parseNodeToExecutableStatement( sSQL, m_xConnection, m_aSqlParser, nullptr ) )
817 break;
818
819 Reference< XResultSetMetaData > xResultSetMeta;
820 Reference< XResultSetMetaDataSupplier > xResMetaDataSup;
821 try
822 {
823 xPreparedStatement.set( m_xConnection->prepareStatement( sSQL ), UNO_QUERY_THROW );
824 xResMetaDataSup.set( xPreparedStatement, UNO_QUERY_THROW );
825 xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_SET_THROW );
826 }
827 catch( const Exception& ) { }
828
829 if ( !xResultSetMeta.is() && xPreparedStatement.is() )
830 {
831 try
832 {
833 //@see issue http://qa.openoffice.org/issues/show_bug.cgi?id=110111
834 // access returns a different order of column names when executing select * from
835 // and asking the columns from the metadata.
836 Reference< XParameters > xParameters( xPreparedStatement, UNO_QUERY_THROW );
837 Reference< XIndexAccess > xPara = getParameters();
838 for(sal_Int32 i = 1;i <= xPara->getCount();++i)
839 xParameters->setNull(i,DataType::VARCHAR);
840 xResMetaDataSup.set(xPreparedStatement->executeQuery(), UNO_QUERY_THROW );
841 xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_SET_THROW );
842 }
843 catch( const Exception& ) { }
844 }
845
846 if ( !xResultSetMeta.is() )
847 {
848 xStatement.reset( Reference< XStatement >( m_xConnection->createStatement(), UNO_SET_THROW ) );
849 Reference< XPropertySet > xStatementProps( xStatement, UNO_QUERY_THROW );
850 try { xStatementProps->setPropertyValue( PROPERTY_ESCAPE_PROCESSING, Any( false ) ); }
851 catch ( const Exception& ) { DBG_UNHANDLED_EXCEPTION("dbaccess"); }
852 xResMetaDataSup.set( xStatement->executeQuery( sSQL ), UNO_QUERY_THROW );
853 xResultSetMeta.set( xResMetaDataSup->getMetaData(), UNO_SET_THROW );
854
855 if (xResultSetMeta.is())
856 {
857 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::getColumns failed to get xResultSetMeta from executed PreparedStatement, but got it from 'no escape processing' statement. SQL command:\n\t" << sSQL );
858 }
859 }
860
861 if ( aSelectColumns->empty() )
862 {
863 // This is a valid case. If we can syntactically parse the query, but not semantically
864 // (e.g. because it is based on a table we do not know), then there will be no SelectColumns
865 aSelectColumns = ::connectivity::parse::OParseColumn::createColumnsForResultSet( xResultSetMeta, m_xMetaData ,xQueryColumns);
866 break;
867 }
868
869 const ::comphelper::UStringMixEqual aCaseCompare( bCase );
870 std::set< size_t > aUsedSelectColumns;
872
873 sal_Int32 nCount = xResultSetMeta->getColumnCount();
874 OSL_ENSURE( static_cast<size_t>(nCount) == aSelectColumns->size(), "OSingleSelectQueryComposer::getColumns: inconsistent column counts, this might result in wrong columns!" );
875 for(sal_Int32 i=1;i<=nCount;++i)
876 {
877 OUString sColumnName = xResultSetMeta->getColumnName(i);
878 OUString sColumnLabel;
879 if ( xQueryColumns.is() && xQueryColumns->hasByName(sColumnName) )
880 {
881 Reference<XPropertySet> xQueryColumn(xQueryColumns->getByName(sColumnName),UNO_QUERY_THROW);
882 xQueryColumn->getPropertyValue(PROPERTY_LABEL) >>= sColumnLabel;
883 }
884 else
885 sColumnLabel = xResultSetMeta->getColumnLabel(i);
886 bool bFound = false;
887 OSQLColumns::Vector::const_iterator aFind = ::connectivity::find(aSelectColumns->begin(),aSelectColumns->end(),sColumnLabel,aCaseCompare);
888 size_t nFoundSelectColumnPos = aFind - aSelectColumns->begin();
889 if ( aFind != aSelectColumns->end() )
890 {
891 if ( aUsedSelectColumns.find( nFoundSelectColumnPos ) != aUsedSelectColumns.end() )
892 { // we found a column name which exists twice
893 // so we start after the first found
894 do
895 {
896 aFind = ::connectivity::findRealName(++aFind,aSelectColumns->end(),sColumnName,aCaseCompare);
897 nFoundSelectColumnPos = aFind - aSelectColumns->begin();
898 }
899 while ( ( aUsedSelectColumns.find( nFoundSelectColumnPos ) != aUsedSelectColumns.end() )
900 && ( aFind != aSelectColumns->end() )
901 );
902 }
903 if ( aFind != aSelectColumns->end() )
904 {
905 (*aFind)->getPropertyValue(PROPERTY_NAME) >>= sColumnName;
906 aUsedSelectColumns.insert( nFoundSelectColumnPos );
907 aNames.push_back(sColumnName);
908 bFound = true;
909 }
910 }
911
912 if ( bFound )
913 continue;
914
915 OSQLColumns::Vector::const_iterator aRealFind = ::connectivity::findRealName(
916 aSelectColumns->begin(), aSelectColumns->end(), sColumnName, aCaseCompare );
917
918 if ( i > static_cast< sal_Int32>( aSelectColumns->size() ) )
919 {
920 aSelectColumns->emplace_back(::connectivity::parse::OParseColumn::createColumnForResultSet( xResultSetMeta, m_xMetaData, i ,aColumnNames)
921 );
922 OSL_ENSURE( aSelectColumns->size() == static_cast<size_t>(i), "OSingleSelectQueryComposer::getColumns: inconsistency!" );
923 }
924 else if ( aRealFind == aSelectColumns->end() )
925 {
926 // we can now only look if we found it under the realname property
927 // here we have to make the assumption that the position is correct
928 OSQLColumns::Vector::const_iterator aFind2 = aSelectColumns->begin() + i-1;
929 Reference<XPropertySet> xProp = *aFind2;
930 if ( !xProp.is() || !xProp->getPropertySetInfo()->hasPropertyByName( PROPERTY_REALNAME ) )
931 continue;
932
933 rtl::Reference<::connectivity::parse::OParseColumn> pColumn = new ::connectivity::parse::OParseColumn(xProp,bCase);
934 pColumn->setFunction(::comphelper::getBOOL(xProp->getPropertyValue("Function")));
935 pColumn->setAggregateFunction(::comphelper::getBOOL(xProp->getPropertyValue("AggregateFunction")));
936
937 OUString sRealName;
938 xProp->getPropertyValue(PROPERTY_REALNAME) >>= sRealName;
939 if ( sColumnName.isEmpty() )
940 xProp->getPropertyValue(PROPERTY_NAME) >>= sColumnName;
941
942 sal_Int32 j = 0;
943 while ( std::any_of(aNames.begin(),aNames.end(),
944 [&aCaseCompare, &sColumnName](const OUString& lhs)
945 { return aCaseCompare(lhs, sColumnName); } ) )
946 {
947 sColumnName += OUString::number(++j);
948 }
949
950 pColumn->setName(sColumnName);
951 pColumn->setRealName(sRealName);
952 pColumn->setTableName(::comphelper::getString(xProp->getPropertyValue(PROPERTY_TABLENAME)));
953
954 (*aSelectColumns)[i-1] = pColumn;
955 }
956 else
957 continue;
958
959 aUsedSelectColumns.insert( static_cast<size_t>(i - 1) );
960 aNames.push_back( sColumnName );
961 }
962 }
963 catch(const Exception&)
964 {
965 }
966
967 } while ( false );
968
969 bool bMissingSomeColumnLabels = !aNames.empty() && aNames.size() != aSelectColumns->size();
970 SAL_WARN_IF(bMissingSomeColumnLabels, "dbaccess", "We have column labels for *some* columns but not all");
971 //^^this happens in the evolution address book where we have real column names of e.g.
972 //first_name, second_name and city. On parsing via
973 //OSQLParseTreeIterator::appendColumns it creates some labels using those real names
974 //but the evo address book gives them proper labels of First Name, Second Name and City
975 //the munge means that here we have e.g. just "City" as a label because it matches
976
977 //This is all a horrible mess
978 if (bMissingSomeColumnLabels)
979 aNames.clear();
980
981 if ( aNames.empty() )
983 else
984 m_aCurrentColumns[ SelectColumns ].reset( new OPrivateColumns( aSelectColumns, bCase, *this, m_aMutex, aNames ) );
985
986 return m_aCurrentColumns[SelectColumns].get();
987}
988
990 std::vector< std::vector < PropertyValue > >& rFilters, const Reference< css::util::XNumberFormatter > & xFormatter) const
991{
992 // Round brackets around the expression
993 if (pCondition->count() == 3 &&
994 SQL_ISPUNCTUATION(pCondition->getChild(0),"(") &&
995 SQL_ISPUNCTUATION(pCondition->getChild(2),")"))
996 {
997 return setORCriteria(pCondition->getChild(1), _rIterator, rFilters, xFormatter);
998 }
999 // OR logic expression
1000 // a searchcondition can only look like this: search_condition SQL_TOKEN_OR boolean_term
1001 else if (SQL_ISRULE(pCondition,search_condition))
1002 {
1003 bool bResult = true;
1004 for (int i = 0; bResult && i < 3; i+=2)
1005 {
1006 // Is the first element a OR logic expression again?
1007 // Then descend recursively ...
1008 if (SQL_ISRULE(pCondition->getChild(i),search_condition))
1009 bResult = setORCriteria(pCondition->getChild(i), _rIterator, rFilters, xFormatter);
1010 else
1011 {
1012 rFilters.emplace_back();
1013 bResult = setANDCriteria(pCondition->getChild(i), _rIterator, rFilters[rFilters.size() - 1], xFormatter);
1014 }
1015 }
1016 return bResult;
1017 }
1018 else
1019 {
1020 rFilters.emplace_back();
1021 return setANDCriteria(pCondition, _rIterator, rFilters[rFilters.size() - 1], xFormatter);
1022 }
1023}
1024
1026 OSQLParseTreeIterator& _rIterator, std::vector < PropertyValue >& rFilter, const Reference< XNumberFormatter > & xFormatter) const
1027{
1028 // Round brackets
1029 if (SQL_ISRULE(pCondition,boolean_primary))
1030 {
1031 // this should not occur
1032 SAL_WARN("dbaccess","boolean_primary in And-Criteria");
1033 return false;
1034 }
1035 // The first element is an AND logical expression again
1036 else if ( SQL_ISRULE(pCondition,boolean_term) && pCondition->count() == 3 )
1037 {
1038 return setANDCriteria(pCondition->getChild(0), _rIterator, rFilter, xFormatter) &&
1039 setANDCriteria(pCondition->getChild(2), _rIterator, rFilter, xFormatter);
1040 }
1041 else if (SQL_ISRULE(pCondition, comparison_predicate))
1042 {
1043 return setComparisonPredicate(pCondition,_rIterator,rFilter,xFormatter);
1044 }
1045 else if (SQL_ISRULE(pCondition,like_predicate))
1046 {
1047 return setLikePredicate(pCondition,_rIterator,rFilter,xFormatter);
1048 }
1049 else if (SQL_ISRULE(pCondition,test_for_null) ||
1050 SQL_ISRULE(pCondition,in_predicate) ||
1051 SQL_ISRULE(pCondition,all_or_any_predicate) ||
1052 SQL_ISRULE(pCondition,between_predicate))
1053 {
1054 if (SQL_ISRULE(pCondition->getChild(0), column_ref))
1055 {
1056 PropertyValue aItem;
1057 OUString aValue;
1058 OUString aColumnName;
1059
1060 pCondition->parseNodeToStr( aValue, m_xConnection );
1061 pCondition->getChild(0)->parseNodeToStr( aColumnName, m_xConnection );
1062
1063 // don't display the column name
1064 aValue = aValue.copy(aColumnName.getLength());
1065 aValue = aValue.trim();
1066
1067 aItem.Name = getColumnName(pCondition->getChild(0),_rIterator);
1068 aItem.Value <<= aValue;
1069 aItem.Handle = 0; // just to know that this is not one the known ones
1070 if ( SQL_ISRULE(pCondition,like_predicate) )
1071 {
1072 if ( SQL_ISTOKEN(pCondition->getChild(1)->getChild(0),NOT) )
1073 aItem.Handle = SQLFilterOperator::NOT_LIKE;
1074 else
1075 aItem.Handle = SQLFilterOperator::LIKE;
1076 }
1077 else if (SQL_ISRULE(pCondition,test_for_null))
1078 {
1079 if (SQL_ISTOKEN(pCondition->getChild(1)->getChild(1),NOT) )
1080 aItem.Handle = SQLFilterOperator::NOT_SQLNULL;
1081 else
1082 aItem.Handle = SQLFilterOperator::SQLNULL;
1083 }
1084 else if (SQL_ISRULE(pCondition,in_predicate))
1085 {
1086 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setANDCriteria: in_predicate not implemented!" );
1087 }
1088 else if (SQL_ISRULE(pCondition,all_or_any_predicate))
1089 {
1090 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setANDCriteria: all_or_any_predicate not implemented!" );
1091 }
1092 else if (SQL_ISRULE(pCondition,between_predicate))
1093 {
1094 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::setANDCriteria: between_predicate not implemented!" );
1095 }
1096
1097 rFilter.push_back(aItem);
1098 }
1099 else
1100 return false;
1101 }
1102 else if (SQL_ISRULE(pCondition,existence_test) ||
1103 SQL_ISRULE(pCondition,unique_test))
1104 {
1105 // this couldn't be handled here, too complex
1106 // as we need a field name
1107 return false;
1108 }
1109 else
1110 return false;
1111
1112 return true;
1113}
1114
1116{
1117 sal_Int32 nPredicate = SQLFilterOperator::EQUAL;
1118 switch (_pPredicate->getNodeType())
1119 {
1120 case SQLNodeType::Equal:
1121 nPredicate = SQLFilterOperator::EQUAL;
1122 break;
1124 nPredicate = SQLFilterOperator::NOT_EQUAL;
1125 break;
1126 case SQLNodeType::Less:
1127 nPredicate = SQLFilterOperator::LESS;
1128 break;
1129 case SQLNodeType::LessEq:
1130 nPredicate = SQLFilterOperator::LESS_EQUAL;
1131 break;
1132 case SQLNodeType::Great:
1133 nPredicate = SQLFilterOperator::GREATER;
1134 break;
1135 case SQLNodeType::GreatEq:
1136 nPredicate = SQLFilterOperator::GREATER_EQUAL;
1137 break;
1138 default:
1139 SAL_WARN("dbaccess","Wrong NodeType!");
1140 }
1141 return nPredicate;
1142}
1143
1145 std::vector < PropertyValue >& rFilter, const Reference< css::util::XNumberFormatter > & xFormatter) const
1146{
1147 OSL_ENSURE(SQL_ISRULE(pCondition, like_predicate),"setLikePredicate: pCondition is not a LikePredicate");
1148
1149 assert(pCondition->count() == 2);
1150 OSQLParseNode const *pRowValue = pCondition->getChild(0);
1151 OSQLParseNode const *pPart2 = pCondition->getChild(1);
1152
1153 PropertyValue aItem;
1154 if ( SQL_ISTOKEN(pPart2->getChild(0),NOT) )
1155 aItem.Handle = SQLFilterOperator::NOT_LIKE;
1156 else
1157 aItem.Handle = SQLFilterOperator::LIKE;
1158
1159 if (SQL_ISRULE(pRowValue, column_ref))
1160 {
1161 OUString aValue;
1162
1163 // skip (optional "NOT") and "LIKE"
1164 for (size_t i=2; i < pPart2->count(); i++)
1165 {
1167 aValue, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1168 }
1169
1170 aItem.Name = getColumnName(pRowValue,_rIterator);
1171 aItem.Value <<= aValue;
1172 rFilter.push_back(aItem);
1173 }
1174 else if (SQL_ISRULE(pRowValue, set_fct_spec ) ||
1175 SQL_ISRULE(pRowValue, general_set_fct))
1176 {
1177 OUString aValue;
1178 OUString aColumnName;
1179
1180 pPart2->getChild(2)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1181 pPart2->getChild(3)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1182 pRowValue->parseNodeToPredicateStr( aColumnName, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1183
1184 aItem.Name = getColumnName(pRowValue,_rIterator);
1185 aItem.Value <<= aValue;
1186 rFilter.push_back(aItem);
1187 }
1188 else // Can only be an expression
1189 {
1190 OUString aName, aValue;
1191
1192 OSQLParseNode const *pValue = pPart2->getChild(2);
1193
1194 // Field names
1195 for (size_t i=0;i< pRowValue->count();i++)
1197
1198 // Criterion
1199 for(size_t i=0;i< pValue->count();i++)
1200 pValue->getChild(i)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1201 pPart2->getChild(3)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1202
1203 aItem.Name = aName;
1204 aItem.Value <<= aValue;
1205 rFilter.push_back(aItem);
1206 }
1207 return true;
1208}
1209
1211 std::vector < PropertyValue >& rFilter, const Reference< css::util::XNumberFormatter > & xFormatter) const
1212{
1213 OSL_ENSURE(SQL_ISRULE(pCondition, comparison_predicate),"setComparisonPredicate: pCondition is not a ComparisonPredicate");
1214 if (SQL_ISRULE(pCondition->getChild(0), column_ref) ||
1215 SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref))
1216 {
1217 PropertyValue aItem;
1218 OUString aValue;
1219 sal_uInt32 nPos;
1220 if (SQL_ISRULE(pCondition->getChild(0), column_ref))
1221 {
1222 nPos = 0;
1223 size_t i=1;
1224
1225 aItem.Handle = getPredicateType(pCondition->getChild(i));
1226
1227 // go forward - don't display the operator
1228 for (i++;i < pCondition->count();i++)
1229 pCondition->getChild(i)->parseNodeToPredicateStr(
1230 aValue, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1231 }
1232 else if (SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref))
1233 {
1234 nPos = pCondition->count()-1;
1235
1236 sal_Int32 i = pCondition->count() - 2;
1237 switch (pCondition->getChild(i)->getNodeType())
1238 {
1239 case SQLNodeType::Equal:
1240 aItem.Handle = SQLFilterOperator::EQUAL;
1241 break;
1243 aItem.Handle = SQLFilterOperator::NOT_EQUAL;
1244 break;
1245 case SQLNodeType::Less:
1246 // take the opposite as we change the order
1247 aItem.Handle = SQLFilterOperator::GREATER_EQUAL;
1248 break;
1249 case SQLNodeType::LessEq:
1250 // take the opposite as we change the order
1251 aItem.Handle = SQLFilterOperator::GREATER;
1252 break;
1253 case SQLNodeType::Great:
1254 // take the opposite as we change the order
1255 aItem.Handle = SQLFilterOperator::LESS_EQUAL;
1256 break;
1257 case SQLNodeType::GreatEq:
1258 // take the opposite as we change the order
1259 aItem.Handle = SQLFilterOperator::LESS;
1260 break;
1261 default:
1262 break;
1263 }
1264
1265 // go backward - don't display the operator
1266 for (i--; i >= 0; i--)
1267 pCondition->getChild(i)->parseNodeToPredicateStr(
1268 aValue, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1269 }
1270 else
1271 return false;
1272
1273 aItem.Name = getColumnName(pCondition->getChild(nPos),_rIterator);
1274 aItem.Value <<= aValue;
1275 rFilter.push_back(aItem);
1276 }
1277 else if (SQL_ISRULE(pCondition->getChild(0), set_fct_spec ) ||
1278 SQL_ISRULE(pCondition->getChild(0), general_set_fct))
1279 {
1280 PropertyValue aItem;
1281 OUString aValue;
1282 OUString aColumnName;
1283
1284 pCondition->getChild(2)->parseNodeToPredicateStr(aValue, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1285 pCondition->getChild(0)->parseNodeToPredicateStr( aColumnName, m_xConnection, xFormatter, m_aLocale, m_sDecimalSep );
1286
1287 aItem.Name = getColumnName(pCondition->getChild(0),_rIterator);
1288 aItem.Value <<= aValue;
1289 aItem.Handle = getPredicateType(pCondition->getChild(1));
1290 rFilter.push_back(aItem);
1291 }
1292 else // Can only be an expression
1293 {
1294 PropertyValue aItem;
1295 OUString aName, aValue;
1296
1297 OSQLParseNode *pLhs = pCondition->getChild(0);
1298 OSQLParseNode *pRhs = pCondition->getChild(2);
1299
1300 // Field names
1301 for (size_t i=0;i< pLhs->count();i++)
1303
1304 // Criterion
1305 aItem.Handle = getPredicateType(pCondition->getChild(1));
1306 for(size_t i=0;i< pRhs->count();i++)
1308
1309 aItem.Name = aName;
1310 aItem.Value <<= aValue;
1311 rFilter.push_back(aItem);
1312 }
1313 return true;
1314}
1315
1316// Functions for analysing SQL
1318{
1319 OUString aTableRange, aColumnName;
1320 _rIterator.getColumnRange(pColumnRef,aColumnName,aTableRange);
1321 return aColumnName;
1322}
1323
1325{
1326 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1327 ::osl::MutexGuard aGuard( m_aMutex );
1328 return getSQLPart(Where,m_aAdditiveIterator,false);
1329}
1330
1332{
1333 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1334 ::osl::MutexGuard aGuard( m_aMutex );
1335 return getSQLPart(Order,m_aAdditiveIterator,false);
1336}
1337
1339{
1340 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1341 ::osl::MutexGuard aGuard( m_aMutex );
1342 return getSQLPart(Group,m_aAdditiveIterator,false);
1343}
1344
1346{
1347 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1348 ::osl::MutexGuard aGuard( m_aMutex );
1350}
1351
1352OUString OSingleSelectQueryComposer::getTableAlias(const Reference< XPropertySet >& column) const
1353{
1354 OUString sReturn;
1355 if(m_pTables && m_pTables->getCount() > 1)
1356 {
1357 OUString aCatalog,aSchema,aTable,aColumnName;
1358 if(column->getPropertySetInfo()->hasPropertyByName(PROPERTY_CATALOGNAME))
1359 column->getPropertyValue(PROPERTY_CATALOGNAME) >>= aCatalog;
1360 if(column->getPropertySetInfo()->hasPropertyByName(PROPERTY_SCHEMANAME))
1361 column->getPropertyValue(PROPERTY_SCHEMANAME) >>= aSchema;
1362 if(column->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME))
1363 column->getPropertyValue(PROPERTY_TABLENAME) >>= aTable;
1364 column->getPropertyValue(PROPERTY_NAME) >>= aColumnName;
1365
1366 Sequence< OUString> aNames(m_pTables->getElementNames());
1367 const OUString* pBegin = aNames.getConstArray();
1368 const OUString* const pEnd = pBegin + aNames.getLength();
1369
1370 if(aTable.isEmpty())
1371 { // we haven't found a table name, now we must search every table for this column
1372 for(;pBegin != pEnd;++pBegin)
1373 {
1374 Reference<XColumnsSupplier> xColumnsSupp;
1375 m_pTables->getByName(*pBegin) >>= xColumnsSupp;
1376
1377 if(xColumnsSupp.is() && xColumnsSupp->getColumns()->hasByName(aColumnName))
1378 {
1379 aTable = *pBegin;
1380 break;
1381 }
1382 }
1383 }
1384 else
1385 {
1386 OUString aComposedName = ::dbtools::composeTableName( m_xMetaData, aCatalog, aSchema, aTable, false, ::dbtools::EComposeRule::InDataManipulation );
1387
1388 // Is this the right case for the table name?
1389 // Else, look for it with different case, if applicable.
1390
1391 if(!m_pTables->hasByName(aComposedName))
1392 {
1395 for(;pBegin != pEnd;++pBegin)
1396 {
1397 Reference<XPropertySet> xTableProp;
1398 m_pTables->getByName(*pBegin) >>= xTableProp;
1399 OSL_ENSURE(xTableProp.is(),"Table isn't a propertyset!");
1400 if(xTableProp.is())
1401 {
1402 OUString aCatalog2,aSchema2,aTable2;
1403 xTableProp->getPropertyValue(PROPERTY_CATALOGNAME) >>= aCatalog2;
1404 xTableProp->getPropertyValue(PROPERTY_SCHEMANAME) >>= aSchema2;
1405 xTableProp->getPropertyValue(PROPERTY_NAME) >>= aTable2;
1406 if(aComp(aCatalog,aCatalog2) && aComp(aSchema,aSchema2) && aComp(aTable,aTable2))
1407 {
1408 aCatalog = aCatalog2;
1409 aSchema = aSchema2;
1410 aTable = aTable2;
1411 break;
1412 }
1413 }
1414 }
1415 }
1416 }
1417 if(pBegin != pEnd)
1418 {
1419 sReturn = ::dbtools::composeTableName( m_xMetaData, aCatalog, aSchema, aTable, true, ::dbtools::EComposeRule::InDataManipulation ) + ".";
1420 }
1421 }
1422 return sReturn;
1423}
1424
1425Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getParameters( )
1426{
1427 // now set the Parameters
1429 {
1431 std::vector< OUString> aNames;
1432 for (auto const& elem : *aCols)
1433 aNames.push_back(getString(elem->getPropertyValue(PROPERTY_NAME)));
1434 m_aCurrentColumns[ParameterColumns].reset( new OPrivateColumns(aCols,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,aNames,true) );
1435 }
1436
1437 return m_aCurrentColumns[ParameterColumns].get();
1438}
1439
1441{
1442 OPrivateColumns* pColumns = m_aCurrentColumns[ _eType ].get();
1443 if ( pColumns != nullptr )
1444 {
1445 pColumns->disposing();
1446 m_aColumnsCollection.push_back( std::move(m_aCurrentColumns[ _eType ]) );
1447 }
1448}
1449
1451{
1452 for (auto & currentColumn : m_aCurrentColumns)
1453 {
1454 if (currentColumn)
1455 {
1456 currentColumn->disposing();
1457 m_aColumnsCollection.push_back(std::move(currentColumn));
1458 }
1459 }
1460
1461 if(m_pTables)
1462 {
1463 m_pTables->disposing();
1464 m_aTablesCollection.push_back(std::move(m_pTables));
1465 }
1466}
1467
1469 const ::rtl::Reference< OSQLColumns >& _rCols )
1470{
1471 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1472
1473 ::osl::MutexGuard aGuard( m_aMutex );
1474 // now set the group columns
1475 if ( !m_aCurrentColumns[_eType] )
1476 {
1477 std::vector< OUString> aNames;
1478 for (auto const& elem : *_rCols)
1479 aNames.push_back(getString(elem->getPropertyValue(PROPERTY_NAME)));
1480 m_aCurrentColumns[_eType].reset( new OPrivateColumns(_rCols,m_xMetaData->supportsMixedCaseQuotedIdentifiers(),*this,m_aMutex,aNames,true) );
1481 }
1482
1483 return m_aCurrentColumns[_eType].get();
1484}
1485
1486Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getGroupColumns( )
1487{
1489}
1490
1491Reference< XIndexAccess > SAL_CALL OSingleSelectQueryComposer::getOrderColumns( )
1492{
1494}
1495
1497{
1498 ::osl::MutexGuard aGuard( m_aMutex );
1499 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1500
1501 OUString sSqlStatement( getQuery() );
1502
1503 const OSQLParseNode* pStatementNode = m_aSqlIterator.getParseTree();
1504 if ( pStatementNode )
1505 {
1506 SQLException aError;
1507 if ( !pStatementNode->parseNodeToExecutableStatement( sSqlStatement, m_xConnection, m_aSqlParser, &aError ) )
1508 throw aError;
1509 }
1510
1511 return sSqlStatement;
1512}
1513
1515{
1516 OUString sResult;
1517
1518 const OSQLParseNode* pNode = _aGetFunctor( &_rIterator );
1519 if ( pNode )
1520 pNode->parseNodeToStr( sResult, m_xConnection );
1521
1522 return sResult;
1523}
1524
1525namespace
1526{
1527 OUString lcl_getDecomposedColumnName(const OUString& rComposedName, std::u16string_view rQuoteString)
1528 {
1529 const size_t nQuoteLength = rQuoteString.size();
1530 OUString sName = rComposedName.trim();
1531 OUString sColumnName;
1532 sal_Int32 nPos, nRPos = 0;
1533
1534 for (;;)
1535 {
1536 nPos = sName.indexOf( rQuoteString, nRPos );
1537 if ( nPos >= 0 )
1538 {
1539 nRPos = sName.indexOf( rQuoteString, nPos + nQuoteLength );
1540 if ( nRPos > nPos )
1541 {
1542 if ( static_cast<sal_Int32>(nRPos + nQuoteLength) < sName.getLength() )
1543 {
1544 nRPos += nQuoteLength; // -1 + 1 skip dot
1545 }
1546 else
1547 {
1548 sColumnName = sName.copy( nPos + nQuoteLength, nRPos - nPos - nQuoteLength );
1549 break;
1550 }
1551 }
1552 else
1553 break;
1554 }
1555 else
1556 break;
1557 }
1558 return sColumnName.isEmpty() ? rComposedName : sColumnName;
1559 }
1560
1561 OUString lcl_getCondition(const Sequence< Sequence< PropertyValue > >& filter,
1562 const OPredicateInputController& i_aPredicateInputController,
1563 const Reference< XNameAccess >& i_xSelectColumns,
1564 std::u16string_view rQuoteString)
1565 {
1566 OUStringBuffer sRet;
1567 const Sequence< PropertyValue >* pOrIter = filter.getConstArray();
1568 const Sequence< PropertyValue >* pOrEnd = pOrIter + filter.getLength();
1569 while ( pOrIter != pOrEnd )
1570 {
1571 if ( pOrIter->hasElements() )
1572 {
1573 sRet.append(L_BRACKET);
1574 const PropertyValue* pAndIter = pOrIter->getConstArray();
1575 const PropertyValue* pAndEnd = pAndIter + pOrIter->getLength();
1576 while ( pAndIter != pAndEnd )
1577 {
1578 sRet.append(pAndIter->Name);
1579 OUString sValue;
1580 pAndIter->Value >>= sValue;
1581 const OUString sColumnName = lcl_getDecomposedColumnName( pAndIter->Name, rQuoteString );
1582 if ( i_xSelectColumns.is() && i_xSelectColumns->hasByName(sColumnName) )
1583 {
1584 Reference<XPropertySet> xColumn(i_xSelectColumns->getByName(sColumnName),UNO_QUERY);
1585 sValue = i_aPredicateInputController.getPredicateValueStr(sValue,xColumn);
1586 }
1587 else
1588 {
1589 sValue = i_aPredicateInputController.getPredicateValueStr(pAndIter->Name,sValue);
1590 }
1591 lcl_addFilterCriteria_throw(pAndIter->Handle,sValue,sRet);
1592 ++pAndIter;
1593 if ( pAndIter != pAndEnd )
1594 sRet.append(STR_AND);
1595 }
1596 sRet.append(R_BRACKET);
1597 }
1598 ++pOrIter;
1599 if ( pOrIter != pOrEnd && !sRet.isEmpty() )
1600 sRet.append(STR_OR);
1601 }
1602 return sRet.makeStringAndClear();
1603 }
1604}
1605
1606void SAL_CALL OSingleSelectQueryComposer::setStructuredFilter( const Sequence< Sequence< PropertyValue > >& filter )
1607{
1609 setFilter(lcl_getCondition(filter, aPredicateInput, getColumns(), m_xMetaData->getIdentifierQuoteString()));
1610}
1611
1612void SAL_CALL OSingleSelectQueryComposer::setStructuredHavingClause( const Sequence< Sequence< PropertyValue > >& filter )
1613{
1615 setHavingClause(lcl_getCondition(filter, aPredicateInput, getColumns(), m_xMetaData->getIdentifierQuoteString()));
1616}
1617
1618void OSingleSelectQueryComposer::setConditionByColumn( const Reference< XPropertySet >& column, bool andCriteria, std::function<bool(OSingleSelectQueryComposer *, const OUString&)> const & _aSetFunctor, sal_Int32 filterOperator)
1619{
1620 try
1621 {
1622 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1623
1624 if ( !column.is()
1625 || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_VALUE)
1626 || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_NAME)
1627 || !column->getPropertySetInfo()->hasPropertyByName(PROPERTY_TYPE))
1628 throw SQLException(DBA_RES(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any() );
1629
1630 sal_Int32 nType = 0;
1631 column->getPropertyValue(PROPERTY_TYPE) >>= nType;
1632 sal_Int32 nSearchable = dbtools::getSearchColumnFlag(m_xConnection,nType);
1633 if(nSearchable == ColumnSearch::NONE)
1634 throw SQLException(DBA_RES(RID_STR_COLUMN_NOT_SEARCHABLE),*this,SQLSTATE_GENERAL,1000,Any() );
1635
1636 ::osl::MutexGuard aGuard( m_aMutex );
1637
1638 OUString aName;
1639 column->getPropertyValue(PROPERTY_NAME) >>= aName;
1640
1641 const Any aValue = column->getPropertyValue(PROPERTY_VALUE);
1642
1643 OUStringBuffer aSQL;
1644 const OUString aQuote = m_xMetaData->getIdentifierQuoteString();
1645 getColumns();
1646
1647 // TODO: if this is called for HAVING, check that the column is a GROUP BY column
1648 // or that it is an aggregate function
1649
1651 {
1652 Reference<XPropertySet> xColumn;
1654 OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME),"Property REALNAME not available!");
1655 OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName(PROPERTY_TABLENAME),"Property TABLENAME not available!");
1656 OSL_ENSURE(xColumn->getPropertySetInfo()->hasPropertyByName("AggregateFunction"),"Property AggregateFunction not available!");
1657
1658 OUString sRealName,sTableName;
1659 xColumn->getPropertyValue(PROPERTY_REALNAME) >>= sRealName;
1660 xColumn->getPropertyValue(PROPERTY_TABLENAME) >>= sTableName;
1661 if(sTableName.indexOf('.') != -1)
1662 {
1663 OUString aCatalog,aSchema,aTable;
1664 ::dbtools::qualifiedNameComponents(m_xMetaData,sTableName,aCatalog,aSchema,aTable,::dbtools::EComposeRule::InDataManipulation);
1665 sTableName = ::dbtools::composeTableName( m_xMetaData, aCatalog, aSchema, aTable, true, ::dbtools::EComposeRule::InDataManipulation );
1666 }
1667 else
1668 sTableName = ::dbtools::quoteName(aQuote,sTableName);
1669
1670 if ( !::comphelper::getBOOL(xColumn->getPropertyValue("Function")) )
1671 {
1672 aSQL = sTableName + "." + ::dbtools::quoteName( aQuote, sRealName );
1673 }
1674 else
1675 aSQL = sRealName;
1676 }
1677 else
1678 {
1679 aSQL = getTableAlias( column ) + ::dbtools::quoteName( aQuote, aName );
1680 }
1681
1682 if ( aValue.hasValue() )
1683 {
1684 if( !m_xTypeConverter.is() )
1685 m_xTypeConverter.set( Converter::create(m_aContext) );
1686 OSL_ENSURE(m_xTypeConverter.is(),"NO typeconverter!");
1687
1688 if ( nType != DataType::BOOLEAN && DataType::BIT != nType )
1689 {
1690 lcl_addFilterCriteria_throw(filterOperator,u"",aSQL);
1691 }
1692
1693 switch(nType)
1694 {
1695 case DataType::VARCHAR:
1696 case DataType::CHAR:
1697 case DataType::LONGVARCHAR:
1698 aSQL.append( DBTypeConversion::toSQLString( nType, aValue, m_xTypeConverter ) );
1699 break;
1700 case DataType::CLOB:
1701 {
1702 Reference< XClob > xClob(aValue,UNO_QUERY);
1703 if ( xClob.is() )
1704 {
1705 const ::sal_Int64 nLength = xClob->length();
1706 if ( sal_Int64(nLength + aSQL.getLength() + STR_LIKE.getLength() ) < sal_Int64(SAL_MAX_INT32) )
1707 {
1708 aSQL.append("'" + xClob->getSubString(1,static_cast<sal_Int32>(nLength)) + "'");
1709 }
1710 }
1711 else
1712 {
1713 aSQL.append( DBTypeConversion::toSQLString( nType, aValue, m_xTypeConverter ) );
1714 }
1715 }
1716 break;
1717 case DataType::VARBINARY:
1718 case DataType::BINARY:
1719 case DataType::LONGVARBINARY:
1720 {
1721 Sequence<sal_Int8> aSeq;
1722 if(!(aValue >>= aSeq))
1723 throw SQLException(DBA_RES(RID_STR_NOT_SEQUENCE_INT8),*this,SQLSTATE_GENERAL,1000,Any() );
1724 if(nSearchable == ColumnSearch::CHAR)
1725 {
1726 aSQL.append( "\'" );
1727 }
1728 aSQL.append( "0x" );
1729 const sal_Int8* pBegin = aSeq.getConstArray();
1730 const sal_Int8* pEnd = pBegin + aSeq.getLength();
1731 for(;pBegin != pEnd;++pBegin)
1732 {
1733 aSQL.append( static_cast<sal_Int32>(*pBegin), 16 );
1734 }
1735 if(nSearchable == ColumnSearch::CHAR)
1736 aSQL.append( "\'" );
1737 }
1738 break;
1739 case DataType::BIT:
1740 case DataType::BOOLEAN:
1741 {
1742 bool bValue = false;
1743 m_xTypeConverter->convertToSimpleType(aValue, TypeClass_BOOLEAN) >>= bValue;
1744
1745 OUString sColumnExp = aSQL.makeStringAndClear();
1746 getBooleanComparisonPredicate( sColumnExp, bValue, m_nBoolCompareMode, aSQL );
1747 }
1748 break;
1749 default:
1750 aSQL.append( DBTypeConversion::toSQLString( nType, aValue, m_xTypeConverter ) );
1751 break;
1752 }
1753 }
1754 else
1755 {
1756 sal_Int32 nFilterOp = filterOperator;
1757 if ( filterOperator != SQLFilterOperator::SQLNULL && filterOperator != SQLFilterOperator::NOT_SQLNULL )
1758 nFilterOp = SQLFilterOperator::SQLNULL;
1759 lcl_addFilterCriteria_throw(nFilterOp,u"",aSQL);
1760 }
1761
1762 // Attach filter
1763 // Construct SELECT without WHERE and ORDER BY
1764 OUString sFilter = getFilter();
1765
1766 if ( !sFilter.isEmpty() && !aSQL.isEmpty() )
1767 {
1768 sFilter = L_BRACKET + sFilter + R_BRACKET +
1769 (andCriteria ? std::u16string_view(STR_AND) : std::u16string_view(STR_OR));
1770 }
1771 sFilter += aSQL;
1772
1773 // add the filter and the sort order
1774 _aSetFunctor(this,sFilter);
1775 }
1776 catch (css::lang::WrappedTargetException & e)
1777 {
1778 if (e.TargetException.isExtractableTo(
1780 {
1781 cppu::throwException(e.TargetException);
1782 }
1783 else
1784 {
1785 throw;
1786 }
1787 }
1788}
1789
1790Sequence< Sequence< PropertyValue > > OSingleSelectQueryComposer::getStructuredCondition( TGetParseNode const & _aGetFunctor )
1791{
1792 ::connectivity::checkDisposed(OSubComponent::rBHelper.bDisposed);
1793
1794 MutexGuard aGuard(m_aMutex);
1795
1796 Sequence< Sequence< PropertyValue > > aFilterSeq;
1797 OUString sFilter = getStatementPart( _aGetFunctor, m_aAdditiveIterator );
1798
1799
1800 if ( !sFilter.isEmpty() )
1801 {
1802 OUString aSql(m_aPureSelectSQL + STR_WHERE + sFilter);
1803 // build a temporary parse node
1804 const OSQLParseNode* pTempNode = m_aAdditiveIterator.getParseTree();
1805
1806 OUString aErrorMsg;
1807 std::unique_ptr<OSQLParseNode> pSqlParseNode( m_aSqlParser.parseTree(aErrorMsg,aSql));
1808 if (pSqlParseNode)
1809 {
1810 m_aAdditiveIterator.setParseTree(pSqlParseNode.get());
1811 // normalize the filter
1812 OSQLParseNode* pWhereNode = const_cast<OSQLParseNode*>(m_aAdditiveIterator.getWhereTree());
1813
1814 OSQLParseNode* pCondition = pWhereNode->getChild(1);
1815 #if OSL_DEBUG_LEVEL > 0
1816 OUString sCondition;
1817 pCondition->parseNodeToStr( sCondition, m_xConnection );
1818 #endif
1820
1821 pCondition = pWhereNode->getChild(1);
1822 #if OSL_DEBUG_LEVEL > 0
1823 sCondition.clear();
1824 pCondition->parseNodeToStr( sCondition, m_xConnection );
1825 #endif
1827
1828 pCondition = pWhereNode->getChild(1);
1829 #if OSL_DEBUG_LEVEL > 0
1830 sCondition.clear();
1831 pCondition->parseNodeToStr( sCondition, m_xConnection );
1832 #endif
1833 OSQLParseNode::absorptions(pCondition);
1834
1835 pCondition = pWhereNode->getChild(1);
1836 #if OSL_DEBUG_LEVEL > 0
1837 sCondition.clear();
1838 pCondition->parseNodeToStr( sCondition, m_xConnection );
1839 #endif
1840 if ( pCondition )
1841 {
1842 std::vector< std::vector < PropertyValue > > aFilters;
1843 Reference< XNumberFormatter > xFormatter( NumberFormatter::create(m_aContext), UNO_QUERY_THROW );
1844 xFormatter->attachNumberFormatsSupplier( m_xNumberFormatsSupplier );
1845
1846 if (setORCriteria(pCondition, m_aAdditiveIterator, aFilters, xFormatter))
1847 {
1848 aFilterSeq.realloc(aFilters.size());
1849 Sequence<PropertyValue>* pFilters = aFilterSeq.getArray();
1850 for (auto const& filter : aFilters)
1851 {
1852 pFilters->realloc(filter.size());
1853 PropertyValue* pFilter = pFilters->getArray();
1854 for (auto const& elem : filter)
1855 {
1856 *pFilter = elem;
1857 ++pFilter;
1858 }
1859 ++pFilters;
1860 }
1861 }
1862 }
1863 // restore
1865 }
1866 }
1867 return aFilterSeq;
1868}
1869
1871{
1872 OUString sKeyword;
1873 switch(_ePart)
1874 {
1875 default:
1876 SAL_WARN("dbaccess", "OSingleSelectQueryComposer::getKeyWord: Invalid enum value!" );
1877 [[fallthrough]]; // fallback to WHERE
1878 case Where:
1879 sKeyword = STR_WHERE;
1880 break;
1881 case Group:
1882 sKeyword = STR_GROUP_BY;
1883 break;
1884 case Having:
1885 sKeyword = STR_HAVING;
1886 break;
1887 case Order:
1888 sKeyword = STR_ORDER_BY;
1889 break;
1890 }
1891 return sKeyword;
1892}
1893
1894OUString OSingleSelectQueryComposer::getSQLPart( SQLPart _ePart, OSQLParseTreeIterator& _rIterator, bool _bWithKeyword )
1895{
1897 OUString sKeyword( getKeyword( _ePart ) );
1898 switch(_ePart)
1899 {
1900 case Where:
1902 break;
1903 case Group:
1905 break;
1906 case Having:
1908 break;
1909 case Order:
1911 break;
1912 default:
1913 SAL_WARN("dbaccess","Invalid enum value!");
1914 }
1915
1916 OUString sRet = getStatementPart( F_tmp, _rIterator );
1917 if ( _bWithKeyword && !sRet.isEmpty() )
1918 sRet = sKeyword + sRet;
1919 return sRet;
1920}
1921
1922/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOGroup Group
OptionalString sName
constexpr OUStringLiteral COMMA
constexpr OUStringLiteral STR_ORDER_BY
constexpr OUStringLiteral STR_GROUP_BY
constexpr OUStringLiteral STR_OR
constexpr OUStringLiteral STR_LIKE
constexpr OUStringLiteral R_BRACKET
constexpr OUStringLiteral STR_WHERE
constexpr OUStringLiteral STR_HAVING
constexpr OUStringLiteral STR_SELECT
constexpr OUStringLiteral STR_FROM
constexpr OUStringLiteral STR_AND
constexpr OUStringLiteral L_BRACKET
void describeProperties(css::uno::Sequence< css::beans::Property > &_rProps) const
void registerProperty(const OUString &_rName, sal_Int32 _nHandle, sal_Int32 _nAttributes, void *_pPointerToMember, const css::uno::Type &_rMemberType)
static void absorptions(OSQLParseNode *&pSearchCondition)
bool parseNodeToExecutableStatement(OUString &_out_rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, OSQLParser &_rParser, css::sdbc::SQLException *_pErrorHolder) const
static void negateSearchCondition(OSQLParseNode *&pSearchCondition, bool bNegate=false)
static void disjunctiveNormalForm(OSQLParseNode *&pSearchCondition)
void parseNodeToStr(OUString &rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const IParseContext *pContext=nullptr, bool _bIntl=false, bool _bQuote=true) const
void parseNodeToPredicateStr(OUString &rString, const css::uno::Reference< css::sdbc::XConnection > &_rxConnection, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter, const css::lang::Locale &rIntl, OUString _sDec, const IParseContext *pContext=nullptr) const
OSQLParseNode * getChild(sal_uInt32 nPos) const
SQLNodeType getNodeType() const
const ::rtl::Reference< OSQLColumns > & getParameters() const
const OSQLParseNode * getSimpleGroupByTree() const
const ::rtl::Reference< OSQLColumns > & getSelectColumns() const
const OSQLTables & getTables() const
void getColumnRange(const OSQLParseNode *_pColumnRef, OUString &_rColumnName, OUString &_rTableRange) const
void setParseTree(const OSQLParseNode *pNewParseTree)
const ::rtl::Reference< OSQLColumns > & getGroupColumns() const
const OSQLParseNode * getParseTree() const
const OSQLParseNode * getSimpleHavingTree() const
const ::rtl::Reference< OSQLColumns > & getOrderColumns() const
const OSQLParseNode * getSimpleWhereTree() const
const OSQLParseNode * getWhereTree() const
const OSQLParseNode * getSimpleOrderTree() const
std::unique_ptr< OSQLParseNode > parseTree(OUString &rErrorMessage, const OUString &rStatement, bool bInternational=false)
static rtl::Reference< OParseColumn > createColumnForResultSet(const css::uno::Reference< css::sdbc::XResultSetMetaData > &_rxResMetaData, const css::uno::Reference< css::sdbc::XDatabaseMetaData > &_rxDBMetaData, sal_Int32 _nColumnPos, StringMap &_rColumns)
static ::rtl::Reference< OSQLColumns > createColumnsForResultSet(const css::uno::Reference< css::sdbc::XResultSetMetaData > &_rxResMetaData, const css::uno::Reference< css::sdbc::XDatabaseMetaData > &_rxDBMetaData, const css::uno::Reference< css::container::XNameAccess > &i_xQueryColumns)
std::map< OUString, int > StringMap
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() SAL_OVERRIDE
virtual void disposing() override
static std::unique_ptr< OPrivateColumns > createWithIntrinsicNames(const ::rtl::Reference< ::connectivity::OSQLColumns > &_rColumns, bool _bCase, ::cppu::OWeakObject &_rParent, ::osl::Mutex &_rMutex)
creates a columns instance as above, but taking the names from the columns itself
void setConditionByColumn(const css::uno::Reference< css::beans::XPropertySet > &column, bool andCriteria, std::function< bool(OSingleSelectQueryComposer *, const OUString &)> const &_aSetFunctor, sal_Int32 filterOperator)
OUString getTableAlias(const css::uno::Reference< css::beans::XPropertySet > &column) const
std::unique_ptr< OPrivateTables > m_pTables
OUString impl_getColumnNameOrderBy_throw(const css::uno::Reference< css::beans::XPropertySet > &column)
return the name of the column in the query for ORDER BY clause.
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
virtual OUString SAL_CALL getGroup() override
std::vector< std::unique_ptr< OPrivateColumns > > m_aColumnsCollection
bool implSetFilter(const OUString &_sFilter)
OUString getStatementPart(TGetParseNode const &_aGetFunctor, ::connectivity::OSQLParseTreeIterator &_rIterator)
retrieves a particular part of a statement
css::uno::Reference< css::script::XTypeConverter > m_xTypeConverter
::connectivity::OSQLParseTreeIterator m_aAdditiveIterator
css::uno::Reference< css::container::XNameAccess > m_xConnectionTables
css::uno::Reference< css::sdbc::XDatabaseMetaData > m_xMetaData
OUString composeStatementFromParts(const std::vector< OUString > &_rParts)
composes a statement from m_aPureSelectSQL and the 4 usual clauses
virtual void SAL_CALL appendOrderByColumn(const css::uno::Reference< css::beans::XPropertySet > &column, sal_Bool ascending) override
::connectivity::OSQLParseTreeIterator m_aSqlIterator
virtual void SAL_CALL setElementaryQuery(const OUString &_rElementary) override
bool setANDCriteria(::connectivity::OSQLParseNode const *pCondition, ::connectivity::OSQLParseTreeIterator &_rIterator, std::vector< css::beans::PropertyValue > &rFilters, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter) const
std::function< const ::connectivity::OSQLParseNode *(::connectivity::OSQLParseTreeIterator *)> TGetParseNode
bool setORCriteria(::connectivity::OSQLParseNode const *pCondition, ::connectivity::OSQLParseTreeIterator &_rIterator, std::vector< std::vector< css::beans::PropertyValue > > &rFilters, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter) const
virtual void SAL_CALL setStructuredHavingClause(const css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > &filter) override
virtual OUString SAL_CALL getHavingClause() override
std::vector< std::unique_ptr< OPrivateTables > > m_aTablesCollection
virtual OUString SAL_CALL getFilter() override
virtual void SAL_CALL setOrder(const OUString &order) override
css::uno::Reference< css::sdbc::XConnection > m_xConnection
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getTables() override
virtual css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > SAL_CALL getStructuredFilter() override
virtual OUString SAL_CALL getOrder() override
bool setComparisonPredicate(::connectivity::OSQLParseNode const *pCondition, ::connectivity::OSQLParseTreeIterator const &_rIterator, std::vector< css::beans::PropertyValue > &rFilters, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter) const
css::uno::Reference< css::container::XNameAccess > m_xConnectionQueries
virtual void SAL_CALL appendFilterByColumn(const css::uno::Reference< css::beans::XPropertySet > &column, sal_Bool andCriteria, sal_Int32 filterOperator) override
virtual void SAL_CALL setFilter(const OUString &filter) override
css::uno::Reference< css::container::XIndexAccess > setCurrentColumns(EColumnType _eType, const ::rtl::Reference< ::connectivity::OSQLColumns > &_rCols)
virtual void SAL_CALL setHavingClause(const OUString &filter) override
virtual void SAL_CALL setQuery(const OUString &command) override
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
virtual OUString SAL_CALL getElementaryQuery() override
::svxform::OSystemParseContext m_aParseContext
virtual void SAL_CALL appendGroupByColumn(const css::uno::Reference< css::beans::XPropertySet > &column) override
virtual OUString SAL_CALL getQueryWithSubstitution() override
virtual void SAL_CALL setGroup(const OUString &group) override
virtual css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > SAL_CALL getStructuredHavingClause() override
virtual css::uno::Reference< css::container::XIndexAccess > SAL_CALL getGroupColumns() override
virtual void SAL_CALL setCommand(const OUString &command, sal_Int32 CommandType) override
OUString getSQLPart(SQLPart _ePart, ::connectivity::OSQLParseTreeIterator &_rIterator, bool _bWithKeyword)
returns the part of the select statement
css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > getStructuredCondition(TGetParseNode const &_aGetFunctor)
getStructuredCondition returns the structured condition for the where or having clause
bool setLikePredicate(::connectivity::OSQLParseNode const *pCondition, ::connectivity::OSQLParseTreeIterator const &_rIterator, std::vector< css::beans::PropertyValue > &rFilters, const css::uno::Reference< css::util::XNumberFormatter > &xFormatter) const
static OUString getKeyword(SQLPart _ePart)
retrieves the keyword for the given SQLPart
css::uno::Reference< css::uno::XComponentContext > m_aContext
void setSingleAdditiveClause(SQLPart _ePart, const OUString &_rClause)
sets a single "additive" clause, means a filter/groupby/having/order clause
css::uno::Reference< css::util::XNumberFormatsSupplier > m_xNumberFormatsSupplier
OUString impl_getColumnRealName_throw(const css::uno::Reference< css::beans::XPropertySet > &column, bool bGroupBy)
return the name of the column in the source table.
virtual OUString SAL_CALL getQuery() override
virtual css::uno::Reference< css::container::XIndexAccess > SAL_CALL getParameters() override
bool implSetHavingClause(const OUString &_sFilter)
std::vector< std::unique_ptr< OPrivateColumns > > m_aCurrentColumns
virtual css::uno::Reference< css::container::XNameAccess > SAL_CALL getColumns() override
virtual css::uno::Sequence< css::uno::Type > SAL_CALL getTypes() override
virtual css::uno::Reference< css::container::XIndexAccess > SAL_CALL getOrderColumns() override
static OUString getColumnName(::connectivity::OSQLParseNode const *pColumnRef, ::connectivity::OSQLParseTreeIterator const &_rIterator)
virtual void SAL_CALL setStructuredFilter(const css::uno::Sequence< css::uno::Sequence< css::beans::PropertyValue > > &filter) override
virtual css::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId() override
static sal_Int32 getPredicateType(::connectivity::OSQLParseNode const *_pPredicate)
virtual ::cppu::IPropertyArrayHelper * createArrayHelper() const override
virtual void SAL_CALL appendHavingClauseByColumn(const css::uno::Reference< css::beans::XPropertySet > &column, sal_Bool andCriteria, sal_Int32 filterOperator) override
virtual SVXCORE_DLLPUBLIC css::lang::Locale getPreferredLocale() const override
sal_Int32 m_nCommandType
#define DBA_RES(id)
int nCount
Reference< XComponentContext > m_aContext
#define DBG_UNHANDLED_EXCEPTION(...)
float u
Reference< XColumn > xColumn
std::mutex m_aMutex
OUString aName
sal_uInt16 nPos
Sequence< sal_Int8 > aSeq
#define SAL_WARN_IF(condition, area, stream)
#define SAL_WARN(area, stream)
constexpr OUStringLiteral aData
@ Exception
OUString getString(const Any &_rAny)
css::uno::Sequence< OUString > getSupportedServiceNames()
std::map< OUString, OSQLTable, comphelper::UStringMixLess > OSQLTables
void SAL_CALL throwException(Any const &exc)
css::uno::Reference< css::uno::XInterface > getDataSource(const css::uno::Reference< css::uno::XInterface > &_rxDependentObject)
bool getDataSourceSetting(const Reference< XInterface > &_xChild, const OUString &_sAsciiSettingsName, Any &_rSettingsValue)
sal_Int32 getSearchColumnFlag(const Reference< XConnection > &_rxConn, sal_Int32 _nDataType)
OUString composeTableNameForSelect(const Reference< XConnection > &_rxConnection, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName)
Reference< XNumberFormatsSupplier > getNumberFormats(const Reference< XConnection > &_rxConn, bool _bAlloweDefault, const Reference< XComponentContext > &_rxContext)
void getBooleanComparisonPredicate(std::u16string_view _rExpression, const bool _bValue, const sal_Int32 _nBooleanComparisonMode, OUStringBuffer &_out_rSQLPredicate)
OUString getStandardSQLState(StandardSQLState _eState)
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
Order
int i
group
Reference< XConnection > m_xConnection
Definition: objectnames.cxx:79
QPRO_FUNC_TYPE nType
OUString sMessage
Definition: sqlmessage.cxx:159
#define SQL_ISRULE(pParseNode, eRule)
#define SQL_ISPUNCTUATION(pParseNode, aString)
#define SQL_ISTOKEN(pParseNode, token)
#define PROPERTY_ID_ORIGINAL
constexpr OUStringLiteral PROPERTY_COMMAND(u"Command")
constexpr OUStringLiteral PROPERTY_LABEL(u"Label")
constexpr OUStringLiteral PROPERTY_ORIGINAL(u"Original")
constexpr OUStringLiteral PROPERTY_SCHEMANAME(u"SchemaName")
constexpr OUStringLiteral PROPERTY_REALNAME(u"RealName")
constexpr OUStringLiteral SERVICE_NAME_SINGLESELECTQUERYCOMPOSER
Definition: strings.hxx:201
constexpr OUStringLiteral PROPERTY_BOOLEANCOMPARISONMODE(u"BooleanComparisonMode")
constexpr OUStringLiteral PROPERTY_TABLENAME(u"TableName")
constexpr OUStringLiteral PROPERTY_ESCAPE_PROCESSING(u"EscapeProcessing")
constexpr OUStringLiteral SQLSTATE_GENERAL
Definition: strings.hxx:267
constexpr OUStringLiteral PROPERTY_CATALOGNAME(u"CatalogName")
constexpr OUStringLiteral PROPERTY_TYPE(u"Type")
constexpr OUStringLiteral PROPERTY_NAME(u"Name")
constexpr OUStringLiteral PROPERTY_VALUE(u"Value")
void append(const OUString &lhs)
NOT
#define SAL_MAX_INT32
unsigned char sal_Bool
signed char sal_Int8
#define IMPLEMENT_FORWARD_XINTERFACE3(classname, refcountbase, baseclass2, baseclass3)
sal_Int32 nLength