Tag: cws_src680_qiq User: fs Date: 06/05/15 06:34:21 Modified: /dba/connectivity/source/parse/ sqlnode.cxx
Log: #i51143# when parsing a query name to a sub select, don't append alias names if there already is an alias name File Changes: Directory: /dba/connectivity/source/parse/ ========================================== File [changed]: sqlnode.cxx Url: http://dba.openoffice.org/source/browse/dba/connectivity/source/parse/sqlnode.cxx?r1=1.38.62.2&r2=1.38.62.3 Delta lines: +126 -66 ---------------------- --- sqlnode.cxx 12 May 2006 14:11:40 -0000 1.38.62.2 +++ sqlnode.cxx 15 May 2006 13:34:18 -0000 1.38.62.3 @@ -4,9 +4,9 @@ * * $RCSfile: sqlnode.cxx,v $ * - * $Revision: 1.38.62.2 $ + * $Revision: 1.38.62.3 $ * - * last change: $Author: fs $ $Date: 2006/05/12 14:11:40 $ + * last change: $Author: fs $ $Date: 2006/05/15 13:34:18 $ * * The Contents of this file are made available subject to * the terms of GNU Lesser General Public License Version 2.1. @@ -319,6 +319,50 @@ } //----------------------------------------------------------------------------- +namespace +{ + bool lcl_isAliasNamePresent( const OSQLParseNode& _rTableNameNode ) + { + OSL_ENSURE( _rTableNameNode.getKnownRuleID() == OSQLParseNode::table_name, "lcl_isAliasNamePresent: Must be a 'table_name' rule!" ); + OSL_ENSURE( _rTableNameNode.getParent()->getKnownRuleID() == OSQLParseNode::table_ref, "lcl_isAliasNamePresent: parent must be a 'table_ref'!" ); + + const OSQLParseNode* pTableRef = _rTableNameNode.getParent(); + if ( pTableRef->count() == 4 ) + { + // table_ref := table_node as range_variable op_column_commalist + // table_node := table_name | ... + if ( pTableRef->getChild(1)->getKnownRuleID() == OSQLParseNode::as ) + return true; + + // table_ref := '{' SQL_TOKEN_OJ joined_table '}' + OSL_ENSURE( SQL_ISPUNCTUATION( pTableRef->getChild(0), "(" ), "lcl_isAliasNamePresent: grammar changed(2)!" ); + return false; + } + + if ( pTableRef->count() == 6 ) + { // '(' joined_table ')' as range_variable op_column_commalist + OSL_ENSURE( SQL_ISPUNCTUATION( pTableRef->getChild(2), "(" ) + && SQL_ISRULE( pTableRef->getChild(3), as ), + "lcl_isAliasNamePresent: grammar changed(3)!" ); + return true; + } + + if ( pTableRef->count() == 1 ) + return false; + +#if OSL_DEBUG_LEVEL > 0 + for ( size_t i=0; i<pTableRef->count(); ++i ) + { + const OSQLParseNode* pChildNode = pTableRef->getChild( i ); + OSQLParseNode::Rule eRuleID = pChildNode->getKnownRuleID(); + } +#endif + OSL_ENSURE( false, "lcl_isAliasNamePresent: unreachable code - except you extended the production rules for table_ref!" ); + return false; + } +} + +//----------------------------------------------------------------------------- void OSQLParseNode::parseNodeToStr(::rtl::OUString& rString, const SQLParseNodeParameter& rParam) const { if ( isToken() ) @@ -366,69 +410,9 @@ } break; - // table name - maybe a query name + // table name - might be a query name case table_name: - { - // if it's a query, maybe we need to substitute the SQL statement ... - - if ( !rParam.bParseToSDBCLevel ) - break; - - if ( !rParam.xQueries.is() ) - { - OSL_ENSURE( false, "OSQLParseNode::parseNodeToStr: cannot substitute a (possible) query name!" ); - break; - } - - try - { - ::rtl::OUString sTableOrQueryName( getChild(0)->getTokenValue() ); - bool bIsQuery = rParam.xQueries->hasByName( sTableOrQueryName ); - if ( !bIsQuery ) - break; - - Reference< XPropertySet > xQuery( rParam.xQueries->getByName( sTableOrQueryName ), UNO_QUERY_THROW ); - - // substitute the query name with the constituting command - ::rtl::OUString sCommand; - OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_COMMAND ) ) >>= sCommand ); - - sal_Bool bEscapeProcessing = sal_False; - OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ESCAPEPROCESSING ) ) >>= bEscapeProcessing ); - - // the query we found here might itself be based on another query, so parse it recursively - OSL_ENSURE( rParam.pParser, "OSQLParseNode::parseNodeToStr: cannot analyze sub queries without a parser!" ); - if ( bEscapeProcessing && rParam.pParser ) - { - ::rtl::OUString sError; - ::std::auto_ptr< OSQLParseNode > pSubQueryNode( rParam.pParser->parseTree( sError, sCommand, sal_False ) ); - if ( pSubQueryNode.get() ) - { - // parse the sub-select to SDBC level, too - ::rtl::OUString sSubSelect; - pSubQueryNode->parseNodeToStr( sSubSelect, rParam ); - if ( sSubSelect.getLength() ) - sCommand = sSubSelect; - } - } - - rString += ::rtl::OUString::createFromAscii( " ( " ); - rString += sCommand; - rString += ::rtl::OUString::createFromAscii( " )" ); - - // append the query name as table alias, since it might be referenced in other - // parts of the statement - rString += ::rtl::OUString::createFromAscii( " AS " ); - if ( rParam.bQuote ) - rString += SetQuotation( sTableOrQueryName, rParam.aIdentifierQuote, rParam.aIdentifierQuote ); - - bHandled = true; - } - catch( const Exception& ) - { - DBG_UNHANDLED_EXCEPTION(); - } - } + tableNameNodeToStr( rString, rParam ); break; case like_predicate: @@ -562,6 +546,79 @@ } } } + +//----------------------------------------------------------------------------- +bool OSQLParseNode::tableNameNodeToStr( ::rtl::OUString& rString, const SQLParseNodeParameter& rParam ) const +{ + // is the table_name part of a table_ref? + OSL_ENSURE( getParent(), "OSQLParseNode::tableNameNodeToStr: table_name without parent?" ); + if ( !getParent() || ( getParent()->getKnownRuleID() != table_ref ) ) + return false; + + // if it's a query, maybe we need to substitute the SQL statement ... + if ( !rParam.bParseToSDBCLevel ) + return false; + + if ( !rParam.xQueries.is() ) + { + OSL_ENSURE( false, "OSQLParseNode::tableNameNodeToStr: cannot substitute a (possible) query name!" ); + return false; + } + + try + { + ::rtl::OUString sTableOrQueryName( getChild(0)->getTokenValue() ); + bool bIsQuery = rParam.xQueries->hasByName( sTableOrQueryName ); + if ( !bIsQuery ) + return false; + + Reference< XPropertySet > xQuery( rParam.xQueries->getByName( sTableOrQueryName ), UNO_QUERY_THROW ); + + // substitute the query name with the constituting command + ::rtl::OUString sCommand; + OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_COMMAND ) ) >>= sCommand ); + + sal_Bool bEscapeProcessing = sal_False; + OSL_VERIFY( xQuery->getPropertyValue( OMetaConnection::getPropMap().getNameByIndex( PROPERTY_ID_ESCAPEPROCESSING ) ) >>= bEscapeProcessing ); + + // the query we found here might itself be based on another query, so parse it recursively + OSL_ENSURE( rParam.pParser, "OSQLParseNode::tableNameNodeToStr: cannot analyze sub queries without a parser!" ); + if ( bEscapeProcessing && rParam.pParser ) + { + ::rtl::OUString sError; + ::std::auto_ptr< OSQLParseNode > pSubQueryNode( rParam.pParser->parseTree( sError, sCommand, sal_False ) ); + if ( pSubQueryNode.get() ) + { + // parse the sub-select to SDBC level, too + ::rtl::OUString sSubSelect; + pSubQueryNode->parseNodeToStr( sSubSelect, rParam ); + if ( sSubSelect.getLength() ) + sCommand = sSubSelect; + } + } + + rString += ::rtl::OUString::createFromAscii( " ( " ); + rString += sCommand; + rString += ::rtl::OUString::createFromAscii( " )" ); + + // append the query name as table alias, since it might be referenced in other + // parts of the statement - but only if there's no other alias name present + if ( !lcl_isAliasNamePresent( *this ) ) + { + rString += ::rtl::OUString::createFromAscii( " AS " ); + if ( rParam.bQuote ) + rString += SetQuotation( sTableOrQueryName, rParam.aIdentifierQuote, rParam.aIdentifierQuote ); + } + + return true; + } + catch( const Exception& ) + { + DBG_UNHANDLED_EXCEPTION(); + } + return false; +} + //----------------------------------------------------------------------------- void OSQLParseNode::tableRangeNodeToStr(::rtl::OUString& rString, const SQLParseNodeParameter& rParam) const { @@ -1273,7 +1330,10 @@ { OSQLParseNode::base_table_def, "base_table_def" }, { OSQLParseNode::base_table_element_commalist, "base_table_element_commalist" }, { OSQLParseNode::data_type, "data_type" }, - { OSQLParseNode::column_def, "column_def" } + { OSQLParseNode::column_def, "column_def" }, + { OSQLParseNode::table_node, "table_node" }, + { OSQLParseNode::as, "as" }, + { OSQLParseNode::op_column_commalist, "op_column_commalist" } }; size_t nRuleMapCount = sizeof( aRuleDescriptions ) / sizeof( aRuleDescriptions[0] ); OSL_ENSURE( nRuleMapCount == size_t( OSQLParseNode::rule_count ), "OSQLParser::OSQLParser: added a new rule? Adjust this map!" ); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
