Tag: cws_src680_qiq
User: fs      
Date: 06/05/10 04:05:37

Modified:
 /dba/dbaccess/source/ui/querydesign/
  QueryDesignView.cxx

Log:
 #i51143# also allow for queries in queries
 (while I was here: SELECT "a" * "b" FROM <table> previously showed wrong
 table information in the graphical view - fixed this)

File Changes:

Directory: /dba/dbaccess/source/ui/querydesign/
===============================================

File [changed]: QueryDesignView.cxx
Url: 
http://dba.openoffice.org/source/browse/dba/dbaccess/source/ui/querydesign/QueryDesignView.cxx?r1=1.78&r2=1.78.16.1
Delta lines:  +284 -226
-----------------------
--- QueryDesignView.cxx 7 Apr 2006 14:13:23 -0000       1.78
+++ QueryDesignView.cxx 10 May 2006 11:05:35 -0000      1.78.16.1
@@ -4,9 +4,9 @@
  *
  *  $RCSfile: QueryDesignView.cxx,v $
  *
- *  $Revision: 1.78 $
+ *  $Revision: 1.78.16.1 $
  *
- *  last change: $Author: vg $ $Date: 2006/04/07 14:13:23 $
+ *  last change: $Author: fs $ $Date: 2006/05/10 11:05:35 $
  *
  *  The Contents of this file are made available subject to
  *  the terms of GNU Lesser General Public License Version 2.1.
@@ -751,7 +751,7 @@
                                                                rFieldName = 
aInfo->GetField();
                                                        }
                                                }
-                                               if ( rFieldName.toChar() != '*' 
)
+                                               if ( ( rFieldName.toChar() != 
'*' ) && ( rFieldName.indexOf( aQuote ) == -1 ) )
                                                {
                                                        
OSL_ENSURE(pEntryField->GetTable().getLength(),"No table field name!");
                                                        aTmpStr += 
::dbtools::quoteName(aQuote, rFieldName);
@@ -1721,6 +1721,23 @@
                }
                return eErrorCode;
        }
+
+    
//------------------------------------------------------------------------------
+    namespace
+    {
+        OQueryTableWindow* lcl_findColumnInTables( const ::rtl::OUString& 
_rColumName, const OJoinTableView::OTableWindowMap& _rTabList, 
OTableFieldDescRef& _rInfo )
+        {
+                       OJoinTableView::OTableWindowMap::const_iterator aIter = 
_rTabList.begin();
+                       for ( ; aIter != _rTabList.end(); ++aIter )
+                       {
+                               OQueryTableWindow* pTabWin = static_cast< 
OQueryTableWindow* >( aIter->second );
+                               if ( pTabWin && pTabWin->ExistsField( 
_rColumName, _rInfo ) )
+                                       return pTabWin;
+                       }
+            return NULL;
+        }
+    }
+
        
//------------------------------------------------------------------------------
        void InsertColumnRef(const OQueryDesignView* _pView,
                                                const 
::connectivity::OSQLParseNode * pColumnRef,
@@ -1740,18 +1757,9 @@
                if (!aTableRange.getLength())
                {
                        // SELECT column, ...
-                       OJoinTableView::OTableWindowMap::iterator aIter = 
pTabList->begin();
-                       for(;aIter != pTabList->end();++aIter)
-                       {
-                               OQueryTableWindow* pTabWin = 
static_cast<OQueryTableWindow*>(aIter->second);
-                               if (pTabWin && pTabWin->ExistsField( 
aColumnName, _raInfo ) )
-                               {
-                                       if(aColumnName.toChar() != '*')
+            bFound = NULL != lcl_findColumnInTables( aColumnName, *pTabList, 
_raInfo );
+                       if ( bFound && ( aColumnName.toChar() != '*' ) )
                                                
_raInfo->SetFieldAlias(aColumnAlias);
-                                       bFound = sal_True;
-                                       break;
-                               }
-                       }
                }
                else
                {
@@ -1864,29 +1872,50 @@
                const ::connectivity::OSQLParseNode* pParseTree = 
aIterator.getParseTree();
                const ::connectivity::OSQLParseNode* pTableRefCommaList = 0;
 
-               if (pParseTree)
+        do
+        {
+            if ( !pParseTree )
+            {
+                           // now we have to insert the fields which aren't in 
the statement
+                           insertUnUsedFields(_pView,_pSelectionBrw);
+                break;
+            }
+
+            if ( !pController->isEsacpeProcessing() ) // not allowed in this 
mode
                {
-                       if (!pController->isEsacpeProcessing()) // not allowed 
in this mode
                                eErrorCode = eNativeMode;
-                       else if (SQL_ISRULE(pParseTree,select_statement))
+                break;
+            }
+
+                       if ( !( SQL_ISRULE( pParseTree, select_statement ) ) )
                        {
+                               eErrorCode = eNoSelectStatement;
+                break;
+            }
+
+                       Reference< XConnection> xConnection = 
pController->getConnection();
+                       if ( !xConnection.is() )
+            {
+                DBG_ERROR( "InitFromParseNodeImpl: no connection? no 
connection!" );
+                break;
+            }
+
                                ::connectivity::OSQLParseNode* pTree = 
pParseTree->getChild(1);
 
                                const OSQLTables& aMap = aIterator.getTables();
                                ::comphelper::UStringMixLess 
aTmp(aMap.key_comp());
-                               ::comphelper::UStringMixEqual 
aKeyComp(static_cast< ::comphelper::UStringMixLess*>(&aTmp)->isCaseSensitive());
+                       ::comphelper::UStringMixEqual aKeyComp( 
aTmp.isCaseSensitive() );
 
-                               Reference< XConnection> xConnection = 
pController->getConnection();
-                               if(xConnection.is())
-                               {
-                                       sal_Int32 nMax = 0;
                                        Reference< XDatabaseMetaData >  
xMetaData = xConnection->getMetaData();
                                        try
                                        {
-                                               nMax = 
xMetaData->getMaxTablesInSelect();
-
-                                               if(!nMax || nMax >= 
(sal_Int32)aMap.size()) // Anzahl der Tabellen im Select-Statement "uberpr"ufen
+                           sal_Int32 nMax = xMetaData->getMaxTablesInSelect();
+                if ( nMax && nMax < (sal_Int32)aMap.size() )
                                                {
+                                       eErrorCode = eTooManyTables;
+                    break;
+                }
+
                                                        ::rtl::OUString 
sComposedName;
                                                        ::rtl::OUString 
aQualifierName;
                                                        ::rtl::OUString sAlias;
@@ -1896,26 +1925,40 @@
                                                        for(;aIter != 
aMap.end();++aIter)
                                                        {
                                                                OSQLTable 
xTable = aIter->second;
-                                                               sComposedName = 
::dbtools::composeTableName(xMetaData,Reference<XPropertySet>(xTable,UNO_QUERY),sal_False,::dbtools::eInDataManipulation);
+                    Reference< XPropertySet > xTableProps( xTable, 
UNO_QUERY_THROW );
+
                                                                sAlias = 
aIter->first;
-                                                               
if(aKeyComp(sComposedName,aIter->first))
+
+                    // check whether this is a query
+                    Reference< XPropertySetInfo > xPSI = 
xTableProps->getPropertySetInfo();
+                    bool bIsQuery = xPSI.is() && xPSI->hasPropertyByName( 
PROPERTY_COMMAND );
+
+                    if ( bIsQuery )
+                        OSL_VERIFY( xTableProps->getPropertyValue( 
PROPERTY_NAME ) >>= sComposedName );
+                    else
                                                                {
-                                                                       
::rtl::OUString sCatalog,sSchema,sTable;
-                                                                       
::dbtools::qualifiedNameComponents(xMetaData,sComposedName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
+                        sComposedName = ::dbtools::composeTableName( 
xMetaData, xTableProps, sal_False, ::dbtools::eInDataManipulation );
+
+                        // if the alias is the complete (composed) table, then 
shorten it
+                        if ( aKeyComp( sComposedName, aIter->first ) )
+                                           {
+                                                   ::rtl::OUString sCatalog, 
sSchema, sTable;
+                                                   
::dbtools::qualifiedNameComponents( xMetaData, sComposedName, sCatalog, 
sSchema, sTable, ::dbtools::eInDataManipulation );
                                                                        sAlias 
= sTable;
                                                                }
+                    }
 
-
-                                                               
OQueryTableWindow* pExistentWin = pTableView->FindTable(sAlias);
-                                                               if 
(!pExistentWin)
+                    // find the existent window for this alias
+                                       OQueryTableWindow* pExistentWin = 
pTableView->FindTable( sAlias );
+                                       if ( !pExistentWin )
                                                                {
-                                                                       
pTableView->AddTabWin(sComposedName, sAlias,sal_False);// don't create data here
+                                               pTableView->AddTabWin( 
sComposedName, sAlias, sal_False );  // don't create data here
                                                                }
                                                                else
                                                                {
-                                                                       // es 
existiert schon ein Fenster mit dem selben Alias ...
-                                                                       if 
(!aKeyComp(pExistentWin->GetData()->GetComposedName(),sComposedName))
-                                                                               
// ... aber anderem Tabellennamen -> neues Fenster
+                                               // there already exists a 
window for this alias ....
+                                               if ( !aKeyComp( 
pExistentWin->GetData()->GetComposedName(), sComposedName ) )
+                                                       // ... but for another 
complete table name -> new window
                                                                                
pTableView->AddTabWin(sComposedName, sAlias);
                                                                }
                                                        }
@@ -1957,23 +2000,12 @@
                                                                }
                                                        }
                                                }
-                                               else
-                                                       eErrorCode = 
eTooManyTables;
-                                       }
                                        catch(SQLException&)
                                        {
                                                
OSL_ASSERT(!"getMaxTablesInSelect!");
                                        }
                                }
-                       }
-                       else
-                               eErrorCode = eNoSelectStatement;
-               }
-               else
-               {
-                       // now we have to insert the fields which aren't in the 
statement
-                       insertUnUsedFields(_pView,_pSelectionBrw);
-               }
+        while ( false );
 
                // Durch das Neuerzeugen wurden wieder Undo-Actions in den 
Manager gestellt
                pController->getUndoMgr()->Clear();
@@ -2035,21 +2067,25 @@
                        {
                                ::connectivity::OSQLParseNode * pColumnRef = 
pParseTree->getChild(i);
 
+                do {
+
                                if ( SQL_ISRULE(pColumnRef,select_sublist) )
                                {
                                         eErrorCode = 
fillSelectSubList(_pView,pTabList);
+                     break;
                                }
-                               else if ( SQL_ISRULE(pColumnRef,derived_column) 
)
-                               {
-                                       if ( xConnection.is() )
+
+                if ( SQL_ISRULE(pColumnRef,derived_column) )
                                        {
+                                       if ( !xConnection.is() )
+                        break;
+
                                                Reference< XDatabaseMetaData >  
xMetaData = xConnection->getMetaData();
                                                ::rtl::OUString 
aColumnAlias(pController->getParseIterator().getColumnAlias(pColumnRef)); // 
kann leer sein
                                                pColumnRef = 
pColumnRef->getChild(0);
                                                OTableFieldDescRef aInfo = new 
OTableFieldDesc();
 
-                                               if (    
-                                                               
pColumnRef->count() == 3 &&
+                                       if (    pColumnRef->count() == 3 &&
                                                                
SQL_ISPUNCTUATION(pColumnRef->getChild(0),"(") &&
                                                                
SQL_ISPUNCTUATION(pColumnRef->getChild(2),")")
                                                        )
@@ -2170,9 +2206,26 @@
                                                                                
                                sal_True,
                                                                                
                                sal_True); // quote is to true because we need 
quoted elements inside the function
 
+                        aInfo->SetTabWindow( NULL );
+
+                        // since we support queries in queries, the thingie 
might belong to an existing "table"
+                        if ( !lcl_findColumnInTables( aColumns, *pTabList, 
aInfo ) )
+                        {
+                            // okay, it doesn't belong to a query directly. In 
this case, it must belong
+                            // to the (only) table in the FROM part
+                            DBG_ASSERT( pTabList->size() == 1, "InstallFields: 
unqualified expression, but more or less than one table!" );
+                            if ( !pTabList->empty() )
+                            {
+                                OQueryTableWindow* pWindow = static_cast< 
OQueryTableWindow* >( pTabList->begin()->second );
+                                aInfo->SetTabWindow( pWindow );
+                                                   aInfo->SetTable( 
pWindow->GetTableName() );
+                                                   aInfo->SetAlias( 
pWindow->GetAliasName() );
+                                                   aInfo->SetDatabase( 
pWindow->GetComposedName() );
+                            }
+                        }
+
                                                        
aInfo->SetDataType(DataType::DOUBLE);
                                                        
aInfo->SetFieldType(TAB_NORMAL_FIELD);
-                                                       
aInfo->SetTabWindow(NULL);
                                                        
aInfo->SetField(aColumns);
                                                        
aInfo->SetFieldAlias(aColumnAlias);
                                                        
aInfo->SetFunctionType(FKT_NUMERIC | FKT_OTHER);
@@ -2180,8 +2233,13 @@
                                                        eErrorCode = 
_pView->InsertField(aInfo, sal_True, bFirstField);
                                                        bFirstField = sal_False;
                                                }
+
+                    break;
                                        }
-                               }
+
+                DBG_ERROR( "InstallFields: don't know how to interpret this 
parse node!" );
+
+                } while ( false );
                        }
                }
                else




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to