1*96de5490SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*96de5490SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*96de5490SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*96de5490SAndrew Rist  * distributed with this work for additional information
6*96de5490SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*96de5490SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*96de5490SAndrew Rist  * "License"); you may not use this file except in compliance
9*96de5490SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*96de5490SAndrew Rist  *
11*96de5490SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*96de5490SAndrew Rist  *
13*96de5490SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*96de5490SAndrew Rist  * software distributed under the License is distributed on an
15*96de5490SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*96de5490SAndrew Rist  * KIND, either express or implied.  See the License for the
17*96de5490SAndrew Rist  * specific language governing permissions and limitations
18*96de5490SAndrew Rist  * under the License.
19*96de5490SAndrew Rist  *
20*96de5490SAndrew Rist  *************************************************************/
21*96de5490SAndrew Rist 
22*96de5490SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
26cdf0e10cSrcweir #ifndef DBAUI_QUERYDESIGNVIEW_HXX
27cdf0e10cSrcweir #include "QueryDesignView.hxx"
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #ifndef DBAUI_QUERYTABLEVIEW_HXX
30cdf0e10cSrcweir #include "QueryTableView.hxx"
31cdf0e10cSrcweir #endif
32cdf0e10cSrcweir #ifndef DBAUI_QUERY_TABLEWINDOW_HXX
33cdf0e10cSrcweir #include "QTableWindow.hxx"
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir #ifndef _SV_TOOLBOX_HXX
36cdf0e10cSrcweir #include <vcl/toolbox.hxx>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #ifndef DBAUI_QUERYCONTROLLER_HXX
39cdf0e10cSrcweir #include "querycontroller.hxx"
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir #ifndef _SV_SPLIT_HXX
42cdf0e10cSrcweir #include <vcl/split.hxx>
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir #ifndef _UNDO_HXX
45cdf0e10cSrcweir #include <svl/undo.hxx>
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir #ifndef TOOLS_DIAGNOSE_EX_H
48cdf0e10cSrcweir #include <tools/diagnose_ex.h>
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir #ifndef DBAUI_QYDLGTAB_HXX
51cdf0e10cSrcweir #include "adtabdlg.hxx"
52cdf0e10cSrcweir #endif
53cdf0e10cSrcweir #ifndef _SV_SVAPP_HXX
54cdf0e10cSrcweir #include <vcl/svapp.hxx>
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir #ifndef _SV_COMBOBOX_HXX
57cdf0e10cSrcweir #include <vcl/combobox.hxx>
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir #ifndef _SV_MSGBOX_HXX
60cdf0e10cSrcweir #include <vcl/msgbox.hxx>
61cdf0e10cSrcweir #endif
62cdf0e10cSrcweir #ifndef DBACCESS_UI_BROWSER_ID_HXX
63cdf0e10cSrcweir #include "browserids.hxx"
64cdf0e10cSrcweir #endif
65cdf0e10cSrcweir #ifndef DBAUI_QUERYDESIGN_OSELECTIONBROWSEBOX_HXX
66cdf0e10cSrcweir #include "SelectionBrowseBox.hxx"
67cdf0e10cSrcweir #endif
68cdf0e10cSrcweir #ifndef _DBU_QRY_HRC_
69cdf0e10cSrcweir #include "dbu_qry.hrc"
70cdf0e10cSrcweir #endif
71cdf0e10cSrcweir #ifndef _UTL_CONFIGMGR_HXX_
72cdf0e10cSrcweir #include <unotools/configmgr.hxx>
73cdf0e10cSrcweir #endif
74cdf0e10cSrcweir #ifndef _COMPHELPER_TYPES_HXX_
75cdf0e10cSrcweir #include <comphelper/types.hxx>
76cdf0e10cSrcweir #endif
77cdf0e10cSrcweir #ifndef _CONNECTIVITY_DBTOOLS_HXX_
78cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
79cdf0e10cSrcweir #endif
80cdf0e10cSrcweir #ifndef _DBHELPER_DBEXCEPTION_HXX_
81cdf0e10cSrcweir #include <connectivity/dbexception.hxx>
82cdf0e10cSrcweir #endif
83cdf0e10cSrcweir #ifndef _COM_SUN_STAR_I18N_XLOCALEDATA_HPP_
84cdf0e10cSrcweir #include <com/sun/star/i18n/XLocaleData.hpp>
85cdf0e10cSrcweir #endif
86cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
87cdf0e10cSrcweir #include <com/sun/star/sdbc/DataType.hpp>
88cdf0e10cSrcweir #endif
89cdf0e10cSrcweir #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
90cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
91cdf0e10cSrcweir #endif
92cdf0e10cSrcweir #ifndef _COM_SUN_STAR_SDBC_COLUMNVALUE_HPP_
93cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
94cdf0e10cSrcweir #endif
95cdf0e10cSrcweir #ifndef _CONNECTIVITY_PCOLUMN_HXX_
96cdf0e10cSrcweir #include <connectivity/PColumn.hxx>
97cdf0e10cSrcweir #endif
98cdf0e10cSrcweir #ifndef DBAUI_QUERYTABLECONNECTION_HXX
99cdf0e10cSrcweir #include "QTableConnection.hxx"
100cdf0e10cSrcweir #endif
101cdf0e10cSrcweir #ifndef DBAUI_CONNECTIONLINE_HXX
102cdf0e10cSrcweir #include "ConnectionLine.hxx"
103cdf0e10cSrcweir #endif
104cdf0e10cSrcweir #ifndef DBAUI_CONNECTIONLINEDATA_HXX
105cdf0e10cSrcweir #include "ConnectionLineData.hxx"
106cdf0e10cSrcweir #endif
107cdf0e10cSrcweir #ifndef DBAUI_QTABLECONNECTIONDATA_HXX
108cdf0e10cSrcweir #include "QTableConnectionData.hxx"
109cdf0e10cSrcweir #endif
110cdf0e10cSrcweir #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
111cdf0e10cSrcweir #include "dbustrings.hrc"
112cdf0e10cSrcweir #endif
113cdf0e10cSrcweir #ifndef _COMPHELPER_EXTRACT_HXX_
114cdf0e10cSrcweir #include <comphelper/extract.hxx>
115cdf0e10cSrcweir #endif
116cdf0e10cSrcweir #ifndef DBAUI_TOOLS_HXX
117cdf0e10cSrcweir #include "UITools.hxx"
118cdf0e10cSrcweir #endif
119cdf0e10cSrcweir #ifndef DBAUI_QUERYCONTAINERWINDOW_HXX
120cdf0e10cSrcweir #include "querycontainerwindow.hxx"
121cdf0e10cSrcweir #endif
122cdf0e10cSrcweir #ifndef DBAUI_QUERYTABLEVIEW_HXX
123cdf0e10cSrcweir #include "QueryTableView.hxx"
124cdf0e10cSrcweir #endif
125cdf0e10cSrcweir #ifndef _DBAUI_SQLMESSAGE_HXX_
126cdf0e10cSrcweir #include "sqlmessage.hxx"
127cdf0e10cSrcweir #endif
128cdf0e10cSrcweir #ifndef INCLUDED_SVTOOLS_SYSLOCALE_HXX
129cdf0e10cSrcweir #include <unotools/syslocale.hxx>
130cdf0e10cSrcweir #endif
131cdf0e10cSrcweir 
132cdf0e10cSrcweir using namespace ::dbaui;
133cdf0e10cSrcweir using namespace ::utl;
134cdf0e10cSrcweir using namespace ::connectivity;
135cdf0e10cSrcweir using namespace ::dbtools;
136cdf0e10cSrcweir using namespace ::com::sun::star::uno;
137cdf0e10cSrcweir using namespace ::com::sun::star::lang;
138cdf0e10cSrcweir using namespace ::com::sun::star::i18n;
139cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
140cdf0e10cSrcweir using namespace ::com::sun::star::beans;
141cdf0e10cSrcweir using namespace ::com::sun::star::container;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir #define SQL_ISRULEOR2(pParseNode, e1,e2) 	((pParseNode)->isRule() && (\
144cdf0e10cSrcweir 											(pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e1) || \
145cdf0e10cSrcweir 											(pParseNode)->getRuleID() == OSQLParser::RuleID(OSQLParseNode::e2)))
146cdf0e10cSrcweir 
147cdf0e10cSrcweir // here we define our functions used in the anonymous namespace to get our header file smaller
148cdf0e10cSrcweir // please look at the book LargeScale C++ to know why
149cdf0e10cSrcweir namespace
150cdf0e10cSrcweir {
151cdf0e10cSrcweir 	static const ::rtl::OUString C_AND = ::rtl::OUString::createFromAscii(" AND ");
152cdf0e10cSrcweir 	static const ::rtl::OUString C_OR  = ::rtl::OUString::createFromAscii(" OR ");
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	// forward declarations
155cdf0e10cSrcweir 	sal_Bool InsertJoin(	const OQueryDesignView* _pView,
156cdf0e10cSrcweir 							const ::connectivity::OSQLParseNode *pNode);
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	SqlParseError InstallFields(OQueryDesignView* _pView,
159cdf0e10cSrcweir 								const ::connectivity::OSQLParseNode* pNode,
160cdf0e10cSrcweir 								OJoinTableView::OTableWindowMap* pTabList );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	SqlParseError GetGroupCriteria(	OQueryDesignView* _pView,
163cdf0e10cSrcweir 									OSelectionBrowseBox* _pSelectionBrw,
164cdf0e10cSrcweir 									const ::connectivity::OSQLParseNode* pSelectRoot );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	SqlParseError GetHavingCriteria(OQueryDesignView* _pView,
167cdf0e10cSrcweir 									OSelectionBrowseBox* _pSelectionBrw,
168cdf0e10cSrcweir 									const ::connectivity::OSQLParseNode* pSelectRoot,
169cdf0e10cSrcweir 									sal_uInt16& rLevel );
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	SqlParseError GetOrderCriteria(	OQueryDesignView* _pView,
172cdf0e10cSrcweir 									OSelectionBrowseBox* _pSelectionBrw,
173cdf0e10cSrcweir 									const ::connectivity::OSQLParseNode* pParseRoot );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 	SqlParseError AddFunctionCondition(OQueryDesignView* _pView,
176cdf0e10cSrcweir 									OSelectionBrowseBox* _pSelectionBrw,
177cdf0e10cSrcweir 									const ::connectivity::OSQLParseNode * pCondition,
178cdf0e10cSrcweir 									const sal_uInt16 nLevel,
179cdf0e10cSrcweir 									sal_Bool bHaving,
180cdf0e10cSrcweir                                     bool _bAddOrOnOneLine);
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 	//------------------------------------------------------------------------------
quoteTableAlias(sal_Bool _bQuote,const::rtl::OUString & _sAliasName,const::rtl::OUString & _sQuote)183cdf0e10cSrcweir 	::rtl::OUString quoteTableAlias(sal_Bool _bQuote, const ::rtl::OUString& _sAliasName, const ::rtl::OUString& _sQuote)
184cdf0e10cSrcweir 	{
185cdf0e10cSrcweir 		::rtl::OUString sRet;
186cdf0e10cSrcweir 		if ( _bQuote && _sAliasName.getLength() )
187cdf0e10cSrcweir 		{
188cdf0e10cSrcweir 			sRet = ::dbtools::quoteName(_sQuote,_sAliasName);
189cdf0e10cSrcweir 			const static ::rtl::OUString sTableSeparater('.');
190cdf0e10cSrcweir 			sRet += sTableSeparater;
191cdf0e10cSrcweir 		}
192cdf0e10cSrcweir 		return sRet;
193cdf0e10cSrcweir 	}
194cdf0e10cSrcweir     //------------------------------------------------------------------------------
getTableRange(const OQueryDesignView * _pView,const::connectivity::OSQLParseNode * _pTableRef)195cdf0e10cSrcweir     ::rtl::OUString getTableRange(const OQueryDesignView* _pView,const ::connectivity::OSQLParseNode* _pTableRef)
196cdf0e10cSrcweir     {
197cdf0e10cSrcweir         Reference< XConnection> xConnection = static_cast<OQueryController&>(_pView->getController()).getConnection();
198cdf0e10cSrcweir         ::rtl::OUString sTableRange;
199cdf0e10cSrcweir         if ( _pTableRef )
200cdf0e10cSrcweir         {
201cdf0e10cSrcweir             sTableRange = ::connectivity::OSQLParseNode::getTableRange(_pTableRef);
202cdf0e10cSrcweir             if ( !sTableRange.getLength() )
203cdf0e10cSrcweir                 _pTableRef->parseNodeToStr(sTableRange,xConnection,NULL,sal_False,sal_False);
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir         return sTableRange;
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir     //------------------------------------------------------------------------------
insertConnection(const OQueryDesignView * _pView,const EJoinType & _eJoinType,OTableFieldDescRef _aDragLeft,OTableFieldDescRef _aDragRight,bool _bNatural=false)208cdf0e10cSrcweir     void insertConnection(const OQueryDesignView* _pView,const EJoinType& _eJoinType,OTableFieldDescRef _aDragLeft,OTableFieldDescRef _aDragRight,bool _bNatural = false)
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         OQueryTableView* pTableView = static_cast<OQueryTableView*>(_pView->getTableView());
211cdf0e10cSrcweir 		OQueryTableConnection* pConn = static_cast<OQueryTableConnection*>( pTableView->GetTabConn(static_cast<OTableWindow*>(_aDragLeft->GetTabWindow()),static_cast<OTableWindow*>(_aDragRight->GetTabWindow()),true));
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 		if ( !pConn )
214cdf0e10cSrcweir 		{
215cdf0e10cSrcweir 			OQueryTableConnectionData* pInfoData = new OQueryTableConnectionData();
216cdf0e10cSrcweir             TTableConnectionData::value_type aInfoData(pInfoData);
217cdf0e10cSrcweir 			pInfoData->InitFromDrag(_aDragLeft, _aDragRight);
218cdf0e10cSrcweir 			pInfoData->SetJoinType(_eJoinType);
219cdf0e10cSrcweir 
220cdf0e10cSrcweir             if ( _bNatural )
221cdf0e10cSrcweir             {
222cdf0e10cSrcweir                 aInfoData->ResetConnLines();
223cdf0e10cSrcweir                 pInfoData->setNatural(_bNatural);
224cdf0e10cSrcweir                 try
225cdf0e10cSrcweir                 {
226cdf0e10cSrcweir                     Reference<XNameAccess> xReferencedTableColumns(aInfoData->getReferencedTable()->getColumns());
227cdf0e10cSrcweir                     Sequence< ::rtl::OUString> aSeq = aInfoData->getReferencingTable()->getColumns()->getElementNames();
228cdf0e10cSrcweir                     const ::rtl::OUString* pIter = aSeq.getConstArray();
229cdf0e10cSrcweir                     const ::rtl::OUString* pEnd	  = pIter + aSeq.getLength();
230cdf0e10cSrcweir                     for(;pIter != pEnd;++pIter)
231cdf0e10cSrcweir                     {
232cdf0e10cSrcweir                         if ( xReferencedTableColumns->hasByName(*pIter) )
233cdf0e10cSrcweir                             aInfoData->AppendConnLine(*pIter,*pIter);
234cdf0e10cSrcweir                     }
235cdf0e10cSrcweir                 }
236cdf0e10cSrcweir                 catch( const Exception& )
237cdf0e10cSrcweir                 {
238cdf0e10cSrcweir                     DBG_UNHANDLED_EXCEPTION();
239cdf0e10cSrcweir                 }
240cdf0e10cSrcweir             }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 			OQueryTableConnection aInfo(pTableView, aInfoData);
243cdf0e10cSrcweir 			// da ein OQueryTableConnection-Objekt nie den Besitz der uebergebenen Daten uebernimmt, sondern sich nur den Zeiger merkt,
244cdf0e10cSrcweir 			// ist dieser Zeiger auf eine lokale Variable hier unkritisch, denn aInfoData und aInfo haben die selbe Lebensdauer
245cdf0e10cSrcweir 			pTableView->NotifyTabConnection( aInfo );
246cdf0e10cSrcweir 		}
247cdf0e10cSrcweir 		else
248cdf0e10cSrcweir 		{
249cdf0e10cSrcweir 			::rtl::OUString aSourceFieldName(_aDragLeft->GetField());
250cdf0e10cSrcweir 			::rtl::OUString aDestFieldName(_aDragRight->GetField());
251cdf0e10cSrcweir 			// the connection could point on the other side
252cdf0e10cSrcweir 			if(pConn->GetSourceWin() == _aDragRight->GetTabWindow())
253cdf0e10cSrcweir 			{
254cdf0e10cSrcweir 				::rtl::OUString aTmp(aSourceFieldName);
255cdf0e10cSrcweir 				aSourceFieldName = aDestFieldName;
256cdf0e10cSrcweir 				aDestFieldName = aTmp;
257cdf0e10cSrcweir 			}
258cdf0e10cSrcweir 			pConn->GetData()->AppendConnLine( aSourceFieldName,aDestFieldName);
259cdf0e10cSrcweir 			pConn->UpdateLineList();
260cdf0e10cSrcweir 			// Modified-Flag
261cdf0e10cSrcweir 			//	SetModified();
262cdf0e10cSrcweir 			// und neu zeichnen
263cdf0e10cSrcweir 			pConn->RecalcLines();
264cdf0e10cSrcweir 				// fuer das unten folgende Invalidate muss ich dieser neuen Connection erst mal die Moeglichkeit geben,
265cdf0e10cSrcweir 				// ihr BoundingRect zu ermitteln
266cdf0e10cSrcweir 			pConn->InvalidateConnection();
267cdf0e10cSrcweir 		}
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir 	//------------------------------------------------------------------------------
ParseCondition(OQueryController & rController,const::connectivity::OSQLParseNode * pCondition,const::rtl::OUString _sDecimal,const::com::sun::star::lang::Locale & _rLocale,sal_uInt32 _nStartIndex)270cdf0e10cSrcweir 	::rtl::OUString ParseCondition(	OQueryController& rController
271cdf0e10cSrcweir 									,const ::connectivity::OSQLParseNode* pCondition
272cdf0e10cSrcweir 									,const ::rtl::OUString _sDecimal
273cdf0e10cSrcweir 									,const ::com::sun::star::lang::Locale& _rLocale
274cdf0e10cSrcweir 									,sal_uInt32 _nStartIndex)
275cdf0e10cSrcweir 	{
276cdf0e10cSrcweir 		::rtl::OUString	aCondition;
277cdf0e10cSrcweir 		Reference< XConnection> xConnection = rController.getConnection();
278cdf0e10cSrcweir 		if ( xConnection.is() )
279cdf0e10cSrcweir 		{
280cdf0e10cSrcweir 			sal_uInt32 nCount = pCondition->count();
281cdf0e10cSrcweir 			for(sal_uInt32 i = _nStartIndex ; i < nCount ; ++i)
282cdf0e10cSrcweir 				pCondition->getChild(i)->parseNodeToPredicateStr(aCondition,
283cdf0e10cSrcweir 								xConnection,
284cdf0e10cSrcweir 								rController.getNumberFormatter(),
285cdf0e10cSrcweir 								_rLocale,
286cdf0e10cSrcweir 								static_cast<sal_Char>(_sDecimal.toChar()),
287cdf0e10cSrcweir 								&rController.getParser().getContext());
288cdf0e10cSrcweir 		}
289cdf0e10cSrcweir 		return aCondition;
290cdf0e10cSrcweir 	}
291cdf0e10cSrcweir 	//------------------------------------------------------------------------------
FillOuterJoins(OQueryDesignView * _pView,const::connectivity::OSQLParseNode * pTableRefList)292cdf0e10cSrcweir 	SqlParseError FillOuterJoins(OQueryDesignView* _pView,
293cdf0e10cSrcweir 								const ::connectivity::OSQLParseNode* pTableRefList)
294cdf0e10cSrcweir 	{
295cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
296cdf0e10cSrcweir 		sal_uInt32 nCount = pTableRefList->count();
297cdf0e10cSrcweir 		sal_Bool bError = sal_False;
298cdf0e10cSrcweir 		for (sal_uInt32 i=0; !bError && i < nCount; ++i)
299cdf0e10cSrcweir 		{
300cdf0e10cSrcweir 			const ::connectivity::OSQLParseNode* pParseNode = pTableRefList->getChild(i);
301cdf0e10cSrcweir 			const ::connectivity::OSQLParseNode* pJoinNode = NULL;
302cdf0e10cSrcweir 
303cdf0e10cSrcweir 			if ( SQL_ISRULE( pParseNode, qualified_join ) || SQL_ISRULE( pParseNode, joined_table ) || SQL_ISRULE( pParseNode, cross_union ) )
304cdf0e10cSrcweir 				pJoinNode = pParseNode;
305cdf0e10cSrcweir 			else if(	SQL_ISRULE(pParseNode,table_ref)
306cdf0e10cSrcweir 					&&	pParseNode->count() == 4 ) // '{' SQL_TOKEN_OJ joined_table '}'
307cdf0e10cSrcweir 				pJoinNode = pParseNode->getChild(2);
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 			if ( pJoinNode )
310cdf0e10cSrcweir 			{
311cdf0e10cSrcweir 				if ( !InsertJoin(_pView,pJoinNode) )
312cdf0e10cSrcweir 					bError = sal_True;
313cdf0e10cSrcweir 			}
314cdf0e10cSrcweir 		}
315cdf0e10cSrcweir 		// check if error occured
316cdf0e10cSrcweir 		if ( bError )
317cdf0e10cSrcweir 			eErrorCode = eIllegalJoin;
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 		return eErrorCode;
320cdf0e10cSrcweir 	}
321cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 	/** FillDragInfo fills the field description out of the table
324cdf0e10cSrcweir 	*/
325cdf0e10cSrcweir 	//------------------------------------------------------------------------------
FillDragInfo(const OQueryDesignView * _pView,const::connectivity::OSQLParseNode * pColumnRef,OTableFieldDescRef & _rDragInfo)326cdf0e10cSrcweir 	SqlParseError FillDragInfo(	const OQueryDesignView* _pView,
327cdf0e10cSrcweir 							const ::connectivity::OSQLParseNode* pColumnRef,
328cdf0e10cSrcweir 							OTableFieldDescRef& _rDragInfo)
329cdf0e10cSrcweir 	{
330cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
331cdf0e10cSrcweir 
332cdf0e10cSrcweir 		sal_Bool bErg = sal_False;
333cdf0e10cSrcweir 
334cdf0e10cSrcweir 		::rtl::OUString aTableRange,aColumnName;
335cdf0e10cSrcweir 		sal_uInt16 nCntAccount;
336cdf0e10cSrcweir 		::connectivity::OSQLParseTreeIterator& rParseIter = static_cast<OQueryController&>(_pView->getController()).getParseIterator();
337cdf0e10cSrcweir 		rParseIter.getColumnRange( pColumnRef, aColumnName, aTableRange );
338cdf0e10cSrcweir 
339cdf0e10cSrcweir 		if ( aTableRange.getLength() )
340cdf0e10cSrcweir 		{
341cdf0e10cSrcweir 			OQueryTableWindow*	pSTW = static_cast<OQueryTableView*>(_pView->getTableView())->FindTable( aTableRange );
342cdf0e10cSrcweir 			bErg = (pSTW && pSTW->ExistsField( aColumnName, _rDragInfo ) );
343cdf0e10cSrcweir 		}
344cdf0e10cSrcweir 		if ( !bErg )
345cdf0e10cSrcweir         {
346cdf0e10cSrcweir 			bErg = static_cast<OQueryTableView*>(_pView->getTableView())->FindTableFromField(aColumnName, _rDragInfo, nCntAccount);
347cdf0e10cSrcweir             if ( !bErg )
348cdf0e10cSrcweir                 bErg = _pView->HasFieldByAliasName(aColumnName, _rDragInfo);
349cdf0e10cSrcweir         }
350cdf0e10cSrcweir 		if ( !bErg )
351cdf0e10cSrcweir 		{
352cdf0e10cSrcweir 			eErrorCode = eColumnNotFound;
353cdf0e10cSrcweir 			String sError(ModuleRes(STR_QRY_COLUMN_NOT_FOUND));
354cdf0e10cSrcweir 			sError.SearchAndReplaceAscii("$name$",aColumnName);
355cdf0e10cSrcweir 			_pView->getController().appendError( sError );
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 			try
358cdf0e10cSrcweir 			{
359cdf0e10cSrcweir 				Reference<XDatabaseMetaData> xMeta = _pView->getController().getConnection()->getMetaData();
360cdf0e10cSrcweir                 if ( xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers() )
361cdf0e10cSrcweir                     _pView->getController().appendError( String( ModuleRes( STR_QRY_CHECK_CASESENSITIVE ) ) );
362cdf0e10cSrcweir 			}
363cdf0e10cSrcweir 			catch(Exception&)
364cdf0e10cSrcweir 			{
365cdf0e10cSrcweir 			}
366cdf0e10cSrcweir 		}
367cdf0e10cSrcweir 
368cdf0e10cSrcweir 		return eErrorCode;
369cdf0e10cSrcweir 	}
370cdf0e10cSrcweir 	//------------------------------------------------------------------------------
BuildJoinCriteria(const Reference<XConnection> & _xConnection,OConnectionLineDataVec * pLineDataList,OQueryTableConnectionData * pData)371cdf0e10cSrcweir 	::rtl::OUString BuildJoinCriteria(	const Reference< XConnection>& _xConnection,
372cdf0e10cSrcweir 										OConnectionLineDataVec* pLineDataList,
373cdf0e10cSrcweir 										OQueryTableConnectionData* pData)
374cdf0e10cSrcweir 	{
375cdf0e10cSrcweir 		::rtl::OUStringBuffer aCondition;
376cdf0e10cSrcweir 		if ( _xConnection.is() )
377cdf0e10cSrcweir         {
378cdf0e10cSrcweir 		    OConnectionLineDataVec::iterator aIter = pLineDataList->begin();
379cdf0e10cSrcweir             OConnectionLineDataVec::iterator aEnd = pLineDataList->end();
380cdf0e10cSrcweir 		    try
381cdf0e10cSrcweir 		    {
382cdf0e10cSrcweir 			    const Reference< XDatabaseMetaData >  xMetaData = _xConnection->getMetaData();
383cdf0e10cSrcweir 			    const ::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString();
384cdf0e10cSrcweir                 const ::rtl::OUString sEqual(RTL_CONSTASCII_USTRINGPARAM(" = "));
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 			    for(;aIter != aEnd;++aIter)
387cdf0e10cSrcweir 			    {
388cdf0e10cSrcweir 				    OConnectionLineDataRef pLineData = *aIter;
389cdf0e10cSrcweir 				    if(aCondition.getLength())
390cdf0e10cSrcweir 					    aCondition.append(C_AND);
391cdf0e10cSrcweir 				    aCondition.append(quoteTableAlias(sal_True,pData->GetAliasName(JTCS_FROM),aQuote));
392cdf0e10cSrcweir 				    aCondition.append(::dbtools::quoteName(aQuote, pLineData->GetFieldName(JTCS_FROM) ));
393cdf0e10cSrcweir 				    aCondition.append(sEqual);
394cdf0e10cSrcweir 				    aCondition.append(quoteTableAlias(sal_True,pData->GetAliasName(JTCS_TO),aQuote));
395cdf0e10cSrcweir 				    aCondition.append(::dbtools::quoteName(aQuote, pLineData->GetFieldName(JTCS_TO) ));
396cdf0e10cSrcweir 			    }
397cdf0e10cSrcweir 		    }
398cdf0e10cSrcweir 		    catch(SQLException&)
399cdf0e10cSrcweir 		    {
400cdf0e10cSrcweir 			    OSL_ASSERT(!"Failure while building Join criteria!");
401cdf0e10cSrcweir 		    }
402cdf0e10cSrcweir         }
403cdf0e10cSrcweir 
404cdf0e10cSrcweir 		return aCondition.makeStringAndClear();
405cdf0e10cSrcweir 	}
406cdf0e10cSrcweir 	//------------------------------------------------------------------------------
407cdf0e10cSrcweir 	/** JoinCycle looks for a join cycle and append it to the string
408cdf0e10cSrcweir 		@param	_xConnection	the connection
409cdf0e10cSrcweir 		@param	_pEntryConn		the table connection which holds the data
410cdf0e10cSrcweir 		@param	_pEntryTabTo	the corresponding table window
411cdf0e10cSrcweir 		@param	_rJoin			the String which will contain the resulting string
412cdf0e10cSrcweir 	*/
JoinCycle(const Reference<XConnection> & _xConnection,OQueryTableConnection * _pEntryConn,const OQueryTableWindow * _pEntryTabTo,::rtl::OUString & _rJoin)413cdf0e10cSrcweir 	void JoinCycle(	const Reference< XConnection>& _xConnection,
414cdf0e10cSrcweir 					OQueryTableConnection* _pEntryConn,
415cdf0e10cSrcweir 					const OQueryTableWindow* _pEntryTabTo,
416cdf0e10cSrcweir 					::rtl::OUString& _rJoin )
417cdf0e10cSrcweir 	{
418cdf0e10cSrcweir 		OSL_ENSURE(_pEntryConn,"TableConnection can not be null!");
419cdf0e10cSrcweir 
420cdf0e10cSrcweir 		OQueryTableConnectionData* pData = static_cast< OQueryTableConnectionData*>(_pEntryConn->GetData().get());
421cdf0e10cSrcweir 		if ( pData->GetJoinType() != INNER_JOIN && _pEntryTabTo->ExistsAVisitedConn() )
422cdf0e10cSrcweir 		{
423cdf0e10cSrcweir 			sal_Bool bBrace = sal_False;
424cdf0e10cSrcweir 			if(_rJoin.getLength() && _rJoin.lastIndexOf(')') == (_rJoin.getLength()-1))
425cdf0e10cSrcweir 			{
426cdf0e10cSrcweir 				bBrace = sal_True;
427cdf0e10cSrcweir 				_rJoin = _rJoin.replaceAt(_rJoin.getLength()-1,1,::rtl::OUString(' '));
428cdf0e10cSrcweir 			}
429cdf0e10cSrcweir 			(_rJoin += C_AND) += BuildJoinCriteria(_xConnection,pData->GetConnLineDataList(),pData);
430cdf0e10cSrcweir 			if(bBrace)
431cdf0e10cSrcweir 				_rJoin += ::rtl::OUString(')');
432cdf0e10cSrcweir 			_pEntryConn->SetVisited(sal_True);
433cdf0e10cSrcweir 		}
434cdf0e10cSrcweir 	}
435cdf0e10cSrcweir 	//------------------------------------------------------------------------------
BuildTable(const Reference<XConnection> & _xConnection,const OQueryTableWindow * pEntryTab,bool _bForce=false)436cdf0e10cSrcweir 	::rtl::OUString BuildTable(	const Reference< XConnection>& _xConnection,
437cdf0e10cSrcweir 								const OQueryTableWindow* pEntryTab,
438cdf0e10cSrcweir                                 bool _bForce = false
439cdf0e10cSrcweir 								)
440cdf0e10cSrcweir 	{
441cdf0e10cSrcweir 		::rtl::OUString aDBName(pEntryTab->GetComposedName());
442cdf0e10cSrcweir 
443cdf0e10cSrcweir 		//	Reference< XConnection> xConnection = static_cast<OQueryController&>(_pView->getController()).getConnection();
444cdf0e10cSrcweir 		if( _xConnection.is() )
445cdf0e10cSrcweir 		{
446cdf0e10cSrcweir 			try
447cdf0e10cSrcweir 			{
448cdf0e10cSrcweir 				Reference< XDatabaseMetaData >  xMetaData = _xConnection->getMetaData();
449cdf0e10cSrcweir 
450cdf0e10cSrcweir                 ::rtl::OUString sCatalog, sSchema, sTable;
451cdf0e10cSrcweir                 ::dbtools::qualifiedNameComponents( xMetaData, aDBName, sCatalog, sSchema, sTable, ::dbtools::eInDataManipulation );
452cdf0e10cSrcweir 				::rtl::OUString aTableListStr = ::dbtools::composeTableNameForSelect( _xConnection, sCatalog, sSchema, sTable );
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 				::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString();
455cdf0e10cSrcweir 				if ( _bForce || isAppendTableAliasEnabled( _xConnection ) || pEntryTab->GetAliasName() != aDBName )
456cdf0e10cSrcweir 				{
457cdf0e10cSrcweir                     aTableListStr += ::rtl::OUString::createFromAscii(" ");
458cdf0e10cSrcweir                     if ( generateAsBeforeTableAlias( _xConnection ) )
459cdf0e10cSrcweir                         aTableListStr += ::rtl::OUString::createFromAscii("AS ");
460cdf0e10cSrcweir 					aTableListStr += ::dbtools::quoteName( aQuote, pEntryTab->GetAliasName() );
461cdf0e10cSrcweir 				}
462cdf0e10cSrcweir 				aDBName = aTableListStr;
463cdf0e10cSrcweir 			}
464cdf0e10cSrcweir 			catch(const SQLException&)
465cdf0e10cSrcweir 			{
466cdf0e10cSrcweir                 DBG_UNHANDLED_EXCEPTION();
467cdf0e10cSrcweir 			}
468cdf0e10cSrcweir 		}
469cdf0e10cSrcweir 		return aDBName;
470cdf0e10cSrcweir 	}
471cdf0e10cSrcweir 	//------------------------------------------------------------------------------
BuildJoin(const Reference<XConnection> & _xConnection,const::rtl::OUString & rLh,const::rtl::OUString & rRh,OQueryTableConnectionData * pData)472cdf0e10cSrcweir 	::rtl::OUString BuildJoin(	const Reference< XConnection>& _xConnection,
473cdf0e10cSrcweir 								const ::rtl::OUString& rLh,
474cdf0e10cSrcweir 								const ::rtl::OUString& rRh,
475cdf0e10cSrcweir 								OQueryTableConnectionData* pData)
476cdf0e10cSrcweir 	{
477cdf0e10cSrcweir 
478cdf0e10cSrcweir 		String aErg(rLh);
479cdf0e10cSrcweir         if ( pData->isNatural() && pData->GetJoinType() != CROSS_JOIN )
480cdf0e10cSrcweir             aErg.AppendAscii(" NATURAL ");
481cdf0e10cSrcweir 		switch(pData->GetJoinType())
482cdf0e10cSrcweir 		{
483cdf0e10cSrcweir 			case LEFT_JOIN:
484cdf0e10cSrcweir 				aErg.AppendAscii(" LEFT OUTER ");
485cdf0e10cSrcweir 				break;
486cdf0e10cSrcweir 			case RIGHT_JOIN:
487cdf0e10cSrcweir 				aErg.AppendAscii(" RIGHT OUTER ");
488cdf0e10cSrcweir 				break;
489cdf0e10cSrcweir             case CROSS_JOIN:
490cdf0e10cSrcweir                 OSL_ENSURE(!pData->isNatural(),"OQueryDesignView::BuildJoin: This should not happen!");
491cdf0e10cSrcweir 				aErg.AppendAscii(" CROSS ");
492cdf0e10cSrcweir 				break;
493cdf0e10cSrcweir 			case INNER_JOIN:
494cdf0e10cSrcweir                 OSL_ENSURE(pData->isNatural(),"OQueryDesignView::BuildJoin: This should not happen!");
495cdf0e10cSrcweir 				aErg.AppendAscii(" INNER ");
496cdf0e10cSrcweir 				break;
497cdf0e10cSrcweir 			default:
498cdf0e10cSrcweir 				aErg.AppendAscii(" FULL OUTER ");
499cdf0e10cSrcweir 				break;
500cdf0e10cSrcweir 		}
501cdf0e10cSrcweir 		aErg.AppendAscii("JOIN ");
502cdf0e10cSrcweir 		aErg += String(rRh);
503cdf0e10cSrcweir         if ( CROSS_JOIN != pData->GetJoinType() && !pData->isNatural() )
504cdf0e10cSrcweir         {
505cdf0e10cSrcweir 		    aErg.AppendAscii(" ON ");
506cdf0e10cSrcweir 		    aErg += String(BuildJoinCriteria(_xConnection,pData->GetConnLineDataList(),pData));
507cdf0e10cSrcweir         }
508cdf0e10cSrcweir 
509cdf0e10cSrcweir 		return aErg;
510cdf0e10cSrcweir 	}
511cdf0e10cSrcweir 	//------------------------------------------------------------------------------
BuildJoin(const Reference<XConnection> & _xConnection,OQueryTableWindow * pLh,OQueryTableWindow * pRh,OQueryTableConnectionData * pData)512cdf0e10cSrcweir 	::rtl::OUString BuildJoin(	const Reference< XConnection>& _xConnection,
513cdf0e10cSrcweir 								OQueryTableWindow* pLh,
514cdf0e10cSrcweir 								OQueryTableWindow* pRh,
515cdf0e10cSrcweir 								OQueryTableConnectionData* pData
516cdf0e10cSrcweir 								)
517cdf0e10cSrcweir 	{
518cdf0e10cSrcweir         bool bForce = pData->GetJoinType() == CROSS_JOIN || pData->isNatural();
519cdf0e10cSrcweir 		return BuildJoin(_xConnection,BuildTable(_xConnection,pLh,bForce),BuildTable(_xConnection,pRh,bForce),pData);
520cdf0e10cSrcweir 	}
521cdf0e10cSrcweir 	//------------------------------------------------------------------------------
BuildJoin(const Reference<XConnection> & _xConnection,const::rtl::OUString & rLh,OQueryTableWindow * pRh,OQueryTableConnectionData * pData)522cdf0e10cSrcweir 	::rtl::OUString BuildJoin(	const Reference< XConnection>& _xConnection,
523cdf0e10cSrcweir 								const ::rtl::OUString &rLh,
524cdf0e10cSrcweir 								OQueryTableWindow* pRh,
525cdf0e10cSrcweir 								OQueryTableConnectionData* pData
526cdf0e10cSrcweir 								)
527cdf0e10cSrcweir 	{
528cdf0e10cSrcweir 		return BuildJoin(_xConnection,rLh,BuildTable(_xConnection,pRh),pData);
529cdf0e10cSrcweir 	}
530cdf0e10cSrcweir 	//------------------------------------------------------------------------------
BuildJoin(const Reference<XConnection> & _xConnection,OQueryTableWindow * pLh,const::rtl::OUString & rRh,OQueryTableConnectionData * pData)531cdf0e10cSrcweir 	::rtl::OUString BuildJoin(	const Reference< XConnection>& _xConnection,
532cdf0e10cSrcweir 								OQueryTableWindow* pLh,
533cdf0e10cSrcweir 								const ::rtl::OUString &rRh,
534cdf0e10cSrcweir 								OQueryTableConnectionData* pData
535cdf0e10cSrcweir 								)
536cdf0e10cSrcweir 	{
537cdf0e10cSrcweir 		return BuildJoin(_xConnection,BuildTable(_xConnection,pLh),rRh,pData);
538cdf0e10cSrcweir 	}
539cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GetNextJoin(const Reference<XConnection> & _xConnection,OQueryTableConnection * pEntryConn,OQueryTableWindow * pEntryTabTo,::rtl::OUString & aJoin)540cdf0e10cSrcweir 	void GetNextJoin(	const Reference< XConnection>& _xConnection,
541cdf0e10cSrcweir 						OQueryTableConnection* pEntryConn,
542cdf0e10cSrcweir 						OQueryTableWindow* pEntryTabTo,
543cdf0e10cSrcweir 						::rtl::OUString &aJoin)
544cdf0e10cSrcweir 	{
545cdf0e10cSrcweir 		OQueryTableConnectionData* pEntryConnData = static_cast<OQueryTableConnectionData*>(pEntryConn->GetData().get());
546cdf0e10cSrcweir         if ( pEntryConnData->GetJoinType() == INNER_JOIN && !pEntryConnData->isNatural() )
547cdf0e10cSrcweir 			return;
548cdf0e10cSrcweir 
549cdf0e10cSrcweir 		//	Reference< XConnection> xConnection = static_cast<OQueryController&>(_pView->getController()).getConnection();
550cdf0e10cSrcweir 
551cdf0e10cSrcweir 		if(!aJoin.getLength())
552cdf0e10cSrcweir 		{
553cdf0e10cSrcweir 			OQueryTableWindow* pEntryTabFrom = static_cast<OQueryTableWindow*>(pEntryConn->GetSourceWin());
554cdf0e10cSrcweir 			aJoin = BuildJoin(_xConnection,pEntryTabFrom,pEntryTabTo,pEntryConnData);
555cdf0e10cSrcweir 		}
556cdf0e10cSrcweir 		else if(pEntryTabTo == pEntryConn->GetDestWin())
557cdf0e10cSrcweir 		{
558cdf0e10cSrcweir 			aJoin = BuildJoin(_xConnection,aJoin,pEntryTabTo,pEntryConnData);
559cdf0e10cSrcweir 		}
560cdf0e10cSrcweir 		else if(pEntryTabTo == pEntryConn->GetSourceWin())
561cdf0e10cSrcweir 		{
562cdf0e10cSrcweir 			aJoin = BuildJoin(_xConnection,pEntryTabTo,aJoin,pEntryConnData);
563cdf0e10cSrcweir 		}
564cdf0e10cSrcweir 
565cdf0e10cSrcweir 		pEntryConn->SetVisited(sal_True);
566cdf0e10cSrcweir 
567cdf0e10cSrcweir 		// first search for the "to" window
568cdf0e10cSrcweir 		const ::std::vector<OTableConnection*>* pConnections = pEntryConn->GetParent()->getTableConnections();
569cdf0e10cSrcweir 		::std::vector<OTableConnection*>::const_iterator aIter = pConnections->begin();
570cdf0e10cSrcweir         ::std::vector<OTableConnection*>::const_iterator aEnd = pConnections->end();
571cdf0e10cSrcweir 		for(;aIter != aEnd;++aIter)
572cdf0e10cSrcweir 		{
573cdf0e10cSrcweir 			OQueryTableConnection* pNext = static_cast<OQueryTableConnection*>(*aIter);
574cdf0e10cSrcweir 			if(!pNext->IsVisited() && (pNext->GetSourceWin() == pEntryTabTo || pNext->GetDestWin() == pEntryTabTo))
575cdf0e10cSrcweir 			{
576cdf0e10cSrcweir 				OQueryTableWindow* pEntryTab = pNext->GetSourceWin() == pEntryTabTo ? static_cast<OQueryTableWindow*>(pNext->GetDestWin()) : static_cast<OQueryTableWindow*>(pNext->GetSourceWin());
577cdf0e10cSrcweir 				// exists there a connection to a OQueryTableWindow that holds a connection that has been already visited
578cdf0e10cSrcweir 				JoinCycle(_xConnection,pNext,pEntryTab,aJoin);
579cdf0e10cSrcweir 				if(!pNext->IsVisited())
580cdf0e10cSrcweir 					GetNextJoin(_xConnection,pNext,pEntryTab,aJoin);
581cdf0e10cSrcweir 			}
582cdf0e10cSrcweir 		}
583cdf0e10cSrcweir 
584cdf0e10cSrcweir 		// when nothing found found look for the "from" window
585cdf0e10cSrcweir 		if(aIter == aEnd)
586cdf0e10cSrcweir 		{
587cdf0e10cSrcweir 			OQueryTableWindow* pEntryTabFrom = static_cast<OQueryTableWindow*>(pEntryConn->GetSourceWin());
588cdf0e10cSrcweir 			aIter = pConnections->begin();
589cdf0e10cSrcweir 			for(;aIter != aEnd;++aIter)
590cdf0e10cSrcweir 			{
591cdf0e10cSrcweir 				OQueryTableConnection* pNext = static_cast<OQueryTableConnection*>(*aIter);
592cdf0e10cSrcweir 				if(!pNext->IsVisited() && (pNext->GetSourceWin() == pEntryTabFrom || pNext->GetDestWin() == pEntryTabFrom))
593cdf0e10cSrcweir 				{
594cdf0e10cSrcweir 					OQueryTableWindow* pEntryTab = pNext->GetSourceWin() == pEntryTabFrom ? static_cast<OQueryTableWindow*>(pNext->GetDestWin()) : static_cast<OQueryTableWindow*>(pNext->GetSourceWin());
595cdf0e10cSrcweir 					// exists there a connection to a OQueryTableWindow that holds a connection that has been already visited
596cdf0e10cSrcweir 					JoinCycle(_xConnection,pNext,pEntryTab,aJoin);
597cdf0e10cSrcweir 					if(!pNext->IsVisited())
598cdf0e10cSrcweir 						GetNextJoin(_xConnection,pNext,pEntryTab,aJoin);
599cdf0e10cSrcweir 				}
600cdf0e10cSrcweir 			}
601cdf0e10cSrcweir 		}
602cdf0e10cSrcweir 	}
603cdf0e10cSrcweir 	//------------------------------------------------------------------------------
InsertJoinConnection(const OQueryDesignView * _pView,const::connectivity::OSQLParseNode * pNode,const EJoinType & _eJoinType,const::connectivity::OSQLParseNode * pLeftTable,const::connectivity::OSQLParseNode * pRightTable)604cdf0e10cSrcweir 	SqlParseError InsertJoinConnection(	const OQueryDesignView* _pView,
605cdf0e10cSrcweir 									const ::connectivity::OSQLParseNode *pNode,
606cdf0e10cSrcweir 									const EJoinType& _eJoinType,
607cdf0e10cSrcweir                                     const ::connectivity::OSQLParseNode *pLeftTable,
608cdf0e10cSrcweir                                     const ::connectivity::OSQLParseNode *pRightTable)
609cdf0e10cSrcweir 	{
610cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
611cdf0e10cSrcweir 		if (pNode->count() == 3 &&	// Ausdruck is geklammert
612cdf0e10cSrcweir 			SQL_ISPUNCTUATION(pNode->getChild(0),"(") &&
613cdf0e10cSrcweir 			SQL_ISPUNCTUATION(pNode->getChild(2),")"))
614cdf0e10cSrcweir 		{
615cdf0e10cSrcweir 			eErrorCode = InsertJoinConnection(_pView,pNode->getChild(1), _eJoinType,pLeftTable,pRightTable);
616cdf0e10cSrcweir 		}
617cdf0e10cSrcweir 		else if (SQL_ISRULEOR2(pNode,search_condition,boolean_term)	&&			// AND/OR-Verknuepfung:
618cdf0e10cSrcweir 				 pNode->count() == 3)
619cdf0e10cSrcweir 		{
620cdf0e10cSrcweir 			// nur AND Verkn�pfung zulassen
621cdf0e10cSrcweir 			if (!SQL_ISTOKEN(pNode->getChild(1),AND))
622cdf0e10cSrcweir 				eErrorCode = eIllegalJoinCondition;
623cdf0e10cSrcweir 			else if ( eOk == (eErrorCode = InsertJoinConnection(_pView,pNode->getChild(0), _eJoinType,pLeftTable,pRightTable)) )
624cdf0e10cSrcweir 					eErrorCode = InsertJoinConnection(_pView,pNode->getChild(2), _eJoinType,pLeftTable,pRightTable);
625cdf0e10cSrcweir 		}
626cdf0e10cSrcweir 		else if (SQL_ISRULE(pNode,comparison_predicate))
627cdf0e10cSrcweir 		{
628cdf0e10cSrcweir 			// only the comparison of columns is allowed
629cdf0e10cSrcweir 			DBG_ASSERT(pNode->count() == 3,"OQueryDesignView::InsertJoinConnection: Fehler im Parse Tree");
630cdf0e10cSrcweir 			if (!(SQL_ISRULE(pNode->getChild(0),column_ref) &&
631cdf0e10cSrcweir 				  SQL_ISRULE(pNode->getChild(2),column_ref) &&
632cdf0e10cSrcweir 				   pNode->getChild(1)->getNodeType() == SQL_NODE_EQUAL))
633cdf0e10cSrcweir 			{
634cdf0e10cSrcweir 				String sError(ModuleRes(STR_QRY_JOIN_COLUMN_COMPARE));
635cdf0e10cSrcweir 				_pView->getController().appendError( sError );
636cdf0e10cSrcweir 				return eIllegalJoin;
637cdf0e10cSrcweir 			}
638cdf0e10cSrcweir 
639cdf0e10cSrcweir 			OTableFieldDescRef aDragLeft  = new OTableFieldDesc();
640cdf0e10cSrcweir 			OTableFieldDescRef aDragRight = new OTableFieldDesc();
641cdf0e10cSrcweir 			if ( eOk != ( eErrorCode = FillDragInfo(_pView,pNode->getChild(0),aDragLeft)) ||
642cdf0e10cSrcweir 				eOk != ( eErrorCode = FillDragInfo(_pView,pNode->getChild(2),aDragRight)))
643cdf0e10cSrcweir 				return eErrorCode;
644cdf0e10cSrcweir 
645cdf0e10cSrcweir             if ( pLeftTable )
646cdf0e10cSrcweir             {
647cdf0e10cSrcweir                 OQueryTableWindow*	pLeftWindow = static_cast<OQueryTableView*>(_pView->getTableView())->FindTable( getTableRange(_pView,pLeftTable->getByRule(OSQLParseNode::table_ref) ));
648cdf0e10cSrcweir                 // OQueryTableWindow*	pRightWindow = static_cast<OQueryTableView*>(_pView->getTableView())->FindTable( getTableRange(_pView,pRightTable->getByRule(OSQLParseNode::table_ref) ));
649cdf0e10cSrcweir                 if ( pLeftWindow == aDragLeft->GetTabWindow() )
650cdf0e10cSrcweir                     insertConnection(_pView,_eJoinType,aDragLeft,aDragRight);
651cdf0e10cSrcweir                 else
652cdf0e10cSrcweir                     insertConnection(_pView,_eJoinType,aDragRight,aDragLeft);
653cdf0e10cSrcweir             }
654cdf0e10cSrcweir             else
655cdf0e10cSrcweir                 insertConnection(_pView,_eJoinType,aDragLeft,aDragRight);
656cdf0e10cSrcweir 		}
657cdf0e10cSrcweir 		else
658cdf0e10cSrcweir 			eErrorCode = eIllegalJoin;
659cdf0e10cSrcweir 		return eErrorCode;
660cdf0e10cSrcweir 	}
661cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GetInnerJoinCriteria(const OQueryDesignView * _pView,const::connectivity::OSQLParseNode * pCondition)662cdf0e10cSrcweir 	sal_Bool GetInnerJoinCriteria(	const OQueryDesignView* _pView,
663cdf0e10cSrcweir 									const ::connectivity::OSQLParseNode *pCondition)
664cdf0e10cSrcweir 	{
665cdf0e10cSrcweir 		return InsertJoinConnection(_pView,pCondition, INNER_JOIN,NULL,NULL) != eOk;
666cdf0e10cSrcweir 	}
667cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GenerateSelectList(const OQueryDesignView * _pView,OTableFields & _rFieldList,sal_Bool bAlias)668cdf0e10cSrcweir 	::rtl::OUString GenerateSelectList(	const OQueryDesignView* _pView,
669cdf0e10cSrcweir 										OTableFields&	_rFieldList,
670cdf0e10cSrcweir 										sal_Bool bAlias)
671cdf0e10cSrcweir 	{
672cdf0e10cSrcweir         Reference< XConnection> xConnection = static_cast<OQueryController&>(_pView->getController()).getConnection();
673cdf0e10cSrcweir 		if ( !xConnection.is() )
674cdf0e10cSrcweir 			return ::rtl::OUString();
675cdf0e10cSrcweir 
676cdf0e10cSrcweir 		::rtl::OUStringBuffer aTmpStr,aFieldListStr;
677cdf0e10cSrcweir 
678cdf0e10cSrcweir 		sal_Bool bAsterix = sal_False;
679cdf0e10cSrcweir 		int nVis = 0;
680cdf0e10cSrcweir 		OTableFields::iterator aIter = _rFieldList.begin();
681cdf0e10cSrcweir         OTableFields::iterator aEnd = _rFieldList.end();
682cdf0e10cSrcweir 		for(;aIter != aEnd;++aIter)
683cdf0e10cSrcweir 		{
684cdf0e10cSrcweir 			OTableFieldDescRef pEntryField = *aIter;
685cdf0e10cSrcweir 			if ( pEntryField->IsVisible() )
686cdf0e10cSrcweir 			{
687cdf0e10cSrcweir 				if ( pEntryField->GetField().toChar() == '*' )
688cdf0e10cSrcweir 					bAsterix = sal_True;
689cdf0e10cSrcweir 				++nVis;
690cdf0e10cSrcweir 			}
691cdf0e10cSrcweir 		}
692cdf0e10cSrcweir 		if(nVis == 1)
693cdf0e10cSrcweir 			bAsterix = sal_False;
694cdf0e10cSrcweir 
695cdf0e10cSrcweir 		try
696cdf0e10cSrcweir 		{
697cdf0e10cSrcweir 			const Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
698cdf0e10cSrcweir 			const ::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString();
699cdf0e10cSrcweir 
700cdf0e10cSrcweir 			OJoinTableView::OTableWindowMap* pTabList = _pView->getTableView()->GetTabWinMap();
701cdf0e10cSrcweir 
702cdf0e10cSrcweir 			const static ::rtl::OUString sFieldSeparator(RTL_CONSTASCII_USTRINGPARAM(", "));
703cdf0e10cSrcweir             const static ::rtl::OUString s_sAs(RTL_CONSTASCII_USTRINGPARAM(" AS "));
704cdf0e10cSrcweir 
705cdf0e10cSrcweir 			aIter = _rFieldList.begin();
706cdf0e10cSrcweir 		    for(;aIter != aEnd;++aIter)
707cdf0e10cSrcweir 			{
708cdf0e10cSrcweir 				OTableFieldDescRef pEntryField = *aIter;
709cdf0e10cSrcweir 				::rtl::OUString rFieldName = pEntryField->GetField();
710cdf0e10cSrcweir 				if ( rFieldName.getLength() && pEntryField->IsVisible() )
711cdf0e10cSrcweir 				{
712cdf0e10cSrcweir 					aTmpStr = ::rtl::OUString();
713cdf0e10cSrcweir 					const ::rtl::OUString rAlias = pEntryField->GetAlias();
714cdf0e10cSrcweir 					const ::rtl::OUString rFieldAlias = pEntryField->GetFieldAlias();
715cdf0e10cSrcweir 
716cdf0e10cSrcweir 					aTmpStr.append(quoteTableAlias((bAlias || bAsterix),rAlias,aQuote));
717cdf0e10cSrcweir 
718cdf0e10cSrcweir 					// if we have a none numeric field, the table alias could be in the name
719cdf0e10cSrcweir 					// otherwise we are not allowed to do this (e.g. 0.1 * PRICE )
720cdf0e10cSrcweir 					if  ( !pEntryField->isOtherFunction() )
721cdf0e10cSrcweir 					{
722cdf0e10cSrcweir 						// we have to look if we have alias.* here but before we have to check if the column doesn't already exist
723cdf0e10cSrcweir 						String sTemp = rFieldName;
724cdf0e10cSrcweir 						OTableFieldDescRef	aInfo = new OTableFieldDesc();
725cdf0e10cSrcweir 						OJoinTableView::OTableWindowMap::iterator tableIter = pTabList->begin();
726cdf0e10cSrcweir                         OJoinTableView::OTableWindowMap::iterator tableEnd = pTabList->end();
727cdf0e10cSrcweir 						sal_Bool bFound = sal_False;
728cdf0e10cSrcweir 						for(;!bFound && tableIter != tableEnd ;++tableIter)
729cdf0e10cSrcweir 						{
730cdf0e10cSrcweir 							OQueryTableWindow* pTabWin = static_cast<OQueryTableWindow*>(tableIter->second);
731cdf0e10cSrcweir 
732cdf0e10cSrcweir                             bFound = pTabWin->ExistsField( rFieldName, aInfo );
733cdf0e10cSrcweir 							if ( bFound )
734cdf0e10cSrcweir 								rFieldName = aInfo->GetField();
735cdf0e10cSrcweir 						}
736cdf0e10cSrcweir 						if ( ( rFieldName.toChar() != '*' ) && ( rFieldName.indexOf( aQuote ) == -1 ) )
737cdf0e10cSrcweir 						{
738cdf0e10cSrcweir 							OSL_ENSURE(pEntryField->GetTable().getLength(),"No table field name!");
739cdf0e10cSrcweir 							aTmpStr.append(::dbtools::quoteName(aQuote, rFieldName));
740cdf0e10cSrcweir 						}
741cdf0e10cSrcweir 						else
742cdf0e10cSrcweir 							aTmpStr.append(rFieldName);
743cdf0e10cSrcweir 					}
744cdf0e10cSrcweir 					else
745cdf0e10cSrcweir 						aTmpStr.append(rFieldName);
746cdf0e10cSrcweir 
747cdf0e10cSrcweir 					if  ( pEntryField->isAggreateFunction() )
748cdf0e10cSrcweir 					{
749cdf0e10cSrcweir 						DBG_ASSERT(pEntryField->GetFunction().getLength(),"Functionname darf hier nicht leer sein! ;-(");
750cdf0e10cSrcweir 						::rtl::OUStringBuffer aTmpStr2( pEntryField->GetFunction());
751cdf0e10cSrcweir 						aTmpStr2.appendAscii("(");
752cdf0e10cSrcweir 						aTmpStr2.append(aTmpStr.makeStringAndClear());
753cdf0e10cSrcweir 						aTmpStr2.appendAscii(")");
754cdf0e10cSrcweir 						aTmpStr = aTmpStr2;
755cdf0e10cSrcweir 					}
756cdf0e10cSrcweir 
757cdf0e10cSrcweir 					if (rFieldAlias.getLength()							&&
758cdf0e10cSrcweir 						(rFieldName.toChar() != '*'						||
759cdf0e10cSrcweir 						pEntryField->isNumericOrAggreateFunction()		||
760cdf0e10cSrcweir 						pEntryField->isOtherFunction()))
761cdf0e10cSrcweir 					{
762cdf0e10cSrcweir 						aTmpStr.append(s_sAs);
763cdf0e10cSrcweir 						aTmpStr.append(::dbtools::quoteName(aQuote, rFieldAlias));
764cdf0e10cSrcweir 					}
765cdf0e10cSrcweir 					aFieldListStr.append(aTmpStr.makeStringAndClear());
766cdf0e10cSrcweir 					aFieldListStr.append(sFieldSeparator);
767cdf0e10cSrcweir 				}
768cdf0e10cSrcweir 			}
769cdf0e10cSrcweir 			if(aFieldListStr.getLength())
770cdf0e10cSrcweir 				aFieldListStr.setLength(aFieldListStr.getLength()-2);
771cdf0e10cSrcweir 		}
772cdf0e10cSrcweir 		catch(SQLException&)
773cdf0e10cSrcweir 		{
774cdf0e10cSrcweir 			OSL_ASSERT(!"Failure while building select list!");
775cdf0e10cSrcweir 		}
776cdf0e10cSrcweir 		return aFieldListStr.makeStringAndClear();
777cdf0e10cSrcweir 	}
778cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GenerateCriterias(OQueryDesignView * _pView,::rtl::OUStringBuffer & rRetStr,::rtl::OUStringBuffer & rHavingStr,OTableFields & _rFieldList,sal_Bool bMulti)779cdf0e10cSrcweir 	sal_Bool GenerateCriterias(	OQueryDesignView* _pView,
780cdf0e10cSrcweir 								::rtl::OUStringBuffer& rRetStr,
781cdf0e10cSrcweir 								::rtl::OUStringBuffer& rHavingStr,
782cdf0e10cSrcweir 								OTableFields& _rFieldList,
783cdf0e10cSrcweir 								sal_Bool bMulti )
784cdf0e10cSrcweir 	{
785cdf0e10cSrcweir 		// * darf keine Filter enthalten : habe ich die entsprechende Warnung schon angezeigt ?
786cdf0e10cSrcweir 		sal_Bool bCritsOnAsterikWarning = sal_False;		// ** TMFS **
787cdf0e10cSrcweir 
788cdf0e10cSrcweir 		::rtl::OUString aFieldName,aCriteria,aWhereStr,aHavingStr,aWork/*,aOrderStr*/;
789cdf0e10cSrcweir 		// Zeilenweise werden die Ausdr"ucke mit AND verknuepft
790cdf0e10cSrcweir 		sal_uInt16 nMaxCriteria = 0;
791cdf0e10cSrcweir 		OTableFields::iterator aIter = _rFieldList.begin();
792cdf0e10cSrcweir 		OTableFields::iterator aEnd = _rFieldList.end();
793cdf0e10cSrcweir 		for(;aIter != aEnd;++aIter)
794cdf0e10cSrcweir 		{
795cdf0e10cSrcweir 			nMaxCriteria = ::std::max<sal_uInt16>(nMaxCriteria,(sal_uInt16)(*aIter)->GetCriteria().size());
796cdf0e10cSrcweir 		}
797cdf0e10cSrcweir 		Reference< XConnection> xConnection = static_cast<OQueryController&>(_pView->getController()).getConnection();
798cdf0e10cSrcweir 		if(!xConnection.is())
799cdf0e10cSrcweir 			return sal_False;
800cdf0e10cSrcweir 		try
801cdf0e10cSrcweir 		{
802cdf0e10cSrcweir 			const Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
803cdf0e10cSrcweir 			const ::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString();
804cdf0e10cSrcweir 			const IParseContext& rContext = static_cast<OQueryController&>(_pView->getController()).getParser().getContext();
805cdf0e10cSrcweir 
806cdf0e10cSrcweir 			for (sal_uInt16 i=0 ; i < nMaxCriteria ; i++)
807cdf0e10cSrcweir 			{
808cdf0e10cSrcweir 				aHavingStr = aWhereStr = ::rtl::OUString();
809cdf0e10cSrcweir 
810cdf0e10cSrcweir 				for(aIter = _rFieldList.begin();aIter != aEnd;++aIter)
811cdf0e10cSrcweir 				{
812cdf0e10cSrcweir 					OTableFieldDescRef	pEntryField = *aIter;
813cdf0e10cSrcweir 					aFieldName = pEntryField->GetField();
814cdf0e10cSrcweir 
815cdf0e10cSrcweir 					if (!aFieldName.getLength())
816cdf0e10cSrcweir 						continue;
817cdf0e10cSrcweir 					aCriteria = pEntryField->GetCriteria( i );
818cdf0e10cSrcweir 					if ( aCriteria.getLength() )
819cdf0e10cSrcweir 					{
820cdf0e10cSrcweir 						// * is not allowed to contain any filter, only when used in combination an aggregate function
821cdf0e10cSrcweir 						if ( aFieldName.toChar() == '*' && pEntryField->isNoneFunction() )
822cdf0e10cSrcweir 						{
823cdf0e10cSrcweir 							// only show the messagebox the first time
824cdf0e10cSrcweir 							if (!bCritsOnAsterikWarning)
825cdf0e10cSrcweir 								ErrorBox(_pView, ModuleRes( ERR_QRY_CRITERIA_ON_ASTERISK)).Execute();
826cdf0e10cSrcweir 							bCritsOnAsterikWarning = sal_True;
827cdf0e10cSrcweir 							continue;
828cdf0e10cSrcweir 						}
829cdf0e10cSrcweir 						aWork = ::rtl::OUString();
830cdf0e10cSrcweir 
831cdf0e10cSrcweir 
832cdf0e10cSrcweir 						aWork += quoteTableAlias(bMulti,pEntryField->GetAlias(),aQuote);
833cdf0e10cSrcweir 
834cdf0e10cSrcweir 						if ( (pEntryField->GetFunctionType() & (FKT_OTHER|FKT_NUMERIC)) || (aFieldName.toChar() == '*') )
835cdf0e10cSrcweir 							aWork += aFieldName;
836cdf0e10cSrcweir 						else
837cdf0e10cSrcweir 							aWork += ::dbtools::quoteName(aQuote, aFieldName);
838cdf0e10cSrcweir 
839cdf0e10cSrcweir 						if ( pEntryField->isAggreateFunction() || pEntryField->IsGroupBy() )
840cdf0e10cSrcweir 						{
841cdf0e10cSrcweir 							if (!aHavingStr.getLength())			// noch keine Kriterien
842cdf0e10cSrcweir 								aHavingStr += ::rtl::OUString('(');			// Klammern
843cdf0e10cSrcweir 							else
844cdf0e10cSrcweir 								aHavingStr += C_AND;
845cdf0e10cSrcweir 
846cdf0e10cSrcweir 							if ( pEntryField->isAggreateFunction() )
847cdf0e10cSrcweir 							{
848cdf0e10cSrcweir 								OSL_ENSURE(pEntryField->GetFunction().getLength(),"No function name for aggregate given!");
849cdf0e10cSrcweir 								aHavingStr += pEntryField->GetFunction();
850cdf0e10cSrcweir 								aHavingStr += ::rtl::OUString('(');			// Klammern
851cdf0e10cSrcweir 								aHavingStr += aWork;
852cdf0e10cSrcweir 								aHavingStr += ::rtl::OUString(')');			// Klammern
853cdf0e10cSrcweir 							}
854cdf0e10cSrcweir 							else
855cdf0e10cSrcweir 								aHavingStr += aWork;
856cdf0e10cSrcweir 
857cdf0e10cSrcweir 							::rtl::OUString aTmp = aCriteria;
858cdf0e10cSrcweir 							::rtl::OUString aErrorMsg;
859cdf0e10cSrcweir 							Reference<XPropertySet> xColumn;
860cdf0e10cSrcweir                             ::std::auto_ptr< ::connectivity::OSQLParseNode> pParseNode(_pView->getPredicateTreeFromEntry(pEntryField,aTmp,aErrorMsg,xColumn));
861cdf0e10cSrcweir 							if (pParseNode.get())
862cdf0e10cSrcweir 							{
863cdf0e10cSrcweir 								if (bMulti && !(pEntryField->isOtherFunction() || (aFieldName.toChar() == '*')))
864cdf0e10cSrcweir 									pParseNode->replaceNodeValue(pEntryField->GetAlias(),aFieldName);
865cdf0e10cSrcweir 								::rtl::OUString sHavingStr = aHavingStr;
866cdf0e10cSrcweir 
867cdf0e10cSrcweir 								sal_uInt32 nCount = pParseNode->count();
868cdf0e10cSrcweir 								for( sal_uInt32 node = 1 ; node < nCount ; ++node)
869cdf0e10cSrcweir 									pParseNode->getChild(node)->parseNodeToStr(	sHavingStr,
870cdf0e10cSrcweir 																xConnection,
871cdf0e10cSrcweir 																&rContext,
872cdf0e10cSrcweir 																sal_False,
873cdf0e10cSrcweir 																!pEntryField->isOtherFunction());
874cdf0e10cSrcweir 								aHavingStr = sHavingStr;
875cdf0e10cSrcweir 							}
876cdf0e10cSrcweir 							else
877cdf0e10cSrcweir 								aHavingStr += aCriteria;
878cdf0e10cSrcweir 						}
879cdf0e10cSrcweir 						else
880cdf0e10cSrcweir 						{
881cdf0e10cSrcweir 							if ( !aWhereStr.getLength() )			// noch keine Kriterien
882cdf0e10cSrcweir 								aWhereStr += ::rtl::OUString('(');			// Klammern
883cdf0e10cSrcweir 							else
884cdf0e10cSrcweir 								aWhereStr += C_AND;
885cdf0e10cSrcweir 
886cdf0e10cSrcweir 							aWhereStr += ::rtl::OUString(' ');
887cdf0e10cSrcweir 							// aCriteria could have some german numbers so I have to be sure here
888cdf0e10cSrcweir 							::rtl::OUString aTmp = aCriteria;
889cdf0e10cSrcweir 							::rtl::OUString aErrorMsg;
890cdf0e10cSrcweir 							Reference<XPropertySet> xColumn;
891cdf0e10cSrcweir 							::std::auto_ptr< ::connectivity::OSQLParseNode> pParseNode( _pView->getPredicateTreeFromEntry(pEntryField,aTmp,aErrorMsg,xColumn));
892cdf0e10cSrcweir 							if (pParseNode.get())
893cdf0e10cSrcweir 							{
894cdf0e10cSrcweir 								if (bMulti && !(pEntryField->isOtherFunction() || (aFieldName.toChar() == '*')))
895cdf0e10cSrcweir 									pParseNode->replaceNodeValue(pEntryField->GetAlias(),aFieldName);
896cdf0e10cSrcweir 								::rtl::OUString aWhere = aWhereStr;
897cdf0e10cSrcweir 								pParseNode->parseNodeToStr(	aWhere,
898cdf0e10cSrcweir 															xConnection,
899cdf0e10cSrcweir 															&rContext,
900cdf0e10cSrcweir 															sal_False,
901cdf0e10cSrcweir 															!pEntryField->isOtherFunction() );
902cdf0e10cSrcweir 								aWhereStr = aWhere;
903cdf0e10cSrcweir 							}
904cdf0e10cSrcweir 							else
905cdf0e10cSrcweir 							{
906cdf0e10cSrcweir 								aWhereStr += aWork;
907cdf0e10cSrcweir 								aWhereStr += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("="));
908cdf0e10cSrcweir 								aWhereStr += aCriteria;
909cdf0e10cSrcweir 							}
910cdf0e10cSrcweir 						}
911cdf0e10cSrcweir 					}
912cdf0e10cSrcweir 					// nur einmal f�r jedes Feld
913cdf0e10cSrcweir 					else if ( !i && pEntryField->isCondition() )
914cdf0e10cSrcweir 					{
915cdf0e10cSrcweir 						if (!aWhereStr.getLength())			// noch keine Kriterien
916cdf0e10cSrcweir 							aWhereStr += ::rtl::OUString('(');			// Klammern
917cdf0e10cSrcweir 						else
918cdf0e10cSrcweir 							aWhereStr += C_AND;
919cdf0e10cSrcweir 						aWhereStr += pEntryField->GetField();
920cdf0e10cSrcweir 					}
921cdf0e10cSrcweir 				}
922cdf0e10cSrcweir 				if (aWhereStr.getLength())
923cdf0e10cSrcweir 				{
924cdf0e10cSrcweir 					aWhereStr += ::rtl::OUString(')');						// Klammern zu fuer 'AND' Zweig
925cdf0e10cSrcweir 					if (rRetStr.getLength())							// schon Feldbedingungen ?
926cdf0e10cSrcweir 						rRetStr.append(C_OR);
927cdf0e10cSrcweir 					else										// Klammern auf fuer 'OR' Zweig
928cdf0e10cSrcweir 						rRetStr.append(sal_Unicode('('));
929cdf0e10cSrcweir 					rRetStr.append(aWhereStr);
930cdf0e10cSrcweir 				}
931cdf0e10cSrcweir 				if (aHavingStr.getLength())
932cdf0e10cSrcweir 				{
933cdf0e10cSrcweir 					aHavingStr += ::rtl::OUString(')');						// Klammern zu fuer 'AND' Zweig
934cdf0e10cSrcweir 					if (rHavingStr.getLength())							// schon Feldbedingungen ?
935cdf0e10cSrcweir 						rHavingStr.append(C_OR);
936cdf0e10cSrcweir 					else										// Klammern auf fuer 'OR' Zweig
937cdf0e10cSrcweir 						rHavingStr.append(sal_Unicode('('));
938cdf0e10cSrcweir 					rHavingStr.append(aHavingStr);
939cdf0e10cSrcweir 				}
940cdf0e10cSrcweir 			}
941cdf0e10cSrcweir 
942cdf0e10cSrcweir 			if (rRetStr.getLength())
943cdf0e10cSrcweir 				rRetStr.append(sal_Unicode(')'));								// Klammern zu fuer 'OR' Zweig
944cdf0e10cSrcweir 			if (rHavingStr.getLength())
945cdf0e10cSrcweir 				rHavingStr.append(sal_Unicode(')'));								// Klammern zu fuer 'OR' Zweig
946cdf0e10cSrcweir 		}
947cdf0e10cSrcweir 		catch(SQLException&)
948cdf0e10cSrcweir 		{
949cdf0e10cSrcweir 			OSL_ASSERT(!"Failure while building where clause!");
950cdf0e10cSrcweir 		}
951cdf0e10cSrcweir 		return sal_True;
952cdf0e10cSrcweir 	}
953cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GenerateOrder(OQueryDesignView * _pView,OTableFields & _rFieldList,sal_Bool bMulti,::rtl::OUString & _rsRet)954cdf0e10cSrcweir 	SqlParseError GenerateOrder(	OQueryDesignView* _pView,
955cdf0e10cSrcweir 									OTableFields& _rFieldList,
956cdf0e10cSrcweir 									sal_Bool bMulti,
957cdf0e10cSrcweir 									::rtl::OUString& _rsRet)
958cdf0e10cSrcweir 	{
959cdf0e10cSrcweir         const OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
960cdf0e10cSrcweir 		Reference< XConnection> xConnection = rController.getConnection();
961cdf0e10cSrcweir 		if ( !xConnection.is() )
962cdf0e10cSrcweir 			return eNoConnection;
963cdf0e10cSrcweir 
964cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
965cdf0e10cSrcweir 
966cdf0e10cSrcweir 		::rtl::OUString aColumnName;
967cdf0e10cSrcweir 		::rtl::OUString aWorkStr;
968cdf0e10cSrcweir 		try
969cdf0e10cSrcweir 		{
970cdf0e10cSrcweir             const bool bColumnAliasInOrderBy = rController.getSdbMetaData().supportsColumnAliasInOrderBy();
971cdf0e10cSrcweir 			Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
972cdf0e10cSrcweir 			::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString();
973cdf0e10cSrcweir 			// * darf keine Filter enthalten : habe ich die entsprechende Warnung schon angezeigt ?
974cdf0e10cSrcweir 			sal_Bool bCritsOnAsterikWarning = sal_False;		// ** TMFS **
975cdf0e10cSrcweir 			OTableFields::iterator aIter = _rFieldList.begin();
976cdf0e10cSrcweir 			OTableFields::iterator aEnd = _rFieldList.end();
977cdf0e10cSrcweir 		    for(;aIter != aEnd;++aIter)
978cdf0e10cSrcweir 			{
979cdf0e10cSrcweir 				OTableFieldDescRef	pEntryField = *aIter;
980cdf0e10cSrcweir 				EOrderDir eOrder = pEntryField->GetOrderDir();
981cdf0e10cSrcweir 
982cdf0e10cSrcweir 				// nur wenn eine Sortierung und ein Tabellenname vorhanden ist-> erzeugen
983cdf0e10cSrcweir 				// sonst werden die Expressions vom Order By im GenerateCriteria mit erzeugt
984cdf0e10cSrcweir 				if ( eOrder != ORDER_NONE )
985cdf0e10cSrcweir 				{
986cdf0e10cSrcweir 					aColumnName = pEntryField->GetField();
987cdf0e10cSrcweir 					if(aColumnName.toChar() == '*')
988cdf0e10cSrcweir 					{
989cdf0e10cSrcweir 						// die entsprechende MessageBox nur beim ersten mal anzeigen
990cdf0e10cSrcweir 						if (!bCritsOnAsterikWarning)
991cdf0e10cSrcweir 							ErrorBox(_pView, ModuleRes( ERR_QRY_ORDERBY_ON_ASTERISK)).Execute();
992cdf0e10cSrcweir 						bCritsOnAsterikWarning = sal_True;
993cdf0e10cSrcweir 						continue;
994cdf0e10cSrcweir 					}
995cdf0e10cSrcweir 
996cdf0e10cSrcweir 					if ( bColumnAliasInOrderBy && pEntryField->GetFieldAlias().getLength() )
997cdf0e10cSrcweir 					{
998cdf0e10cSrcweir 						aWorkStr += ::dbtools::quoteName(aQuote, pEntryField->GetFieldAlias());
999cdf0e10cSrcweir 					}
1000cdf0e10cSrcweir 					else if ( pEntryField->isNumericOrAggreateFunction() )
1001cdf0e10cSrcweir 					{
1002cdf0e10cSrcweir 						DBG_ASSERT(pEntryField->GetFunction().getLength(),"Functionname darf hier nicht leer sein! ;-(");
1003cdf0e10cSrcweir 						aWorkStr += pEntryField->GetFunction();
1004cdf0e10cSrcweir 						aWorkStr +=  ::rtl::OUString('(');
1005cdf0e10cSrcweir 						aWorkStr += quoteTableAlias(bMulti,pEntryField->GetAlias(),aQuote);
1006cdf0e10cSrcweir 						// only quote column name when we don't have a numeric
1007cdf0e10cSrcweir 						if ( pEntryField->isNumeric() )
1008cdf0e10cSrcweir 							aWorkStr += aColumnName;
1009cdf0e10cSrcweir 						else
1010cdf0e10cSrcweir 							aWorkStr += ::dbtools::quoteName(aQuote, aColumnName);
1011cdf0e10cSrcweir 
1012cdf0e10cSrcweir 						aWorkStr +=  ::rtl::OUString(')');
1013cdf0e10cSrcweir 					}
1014cdf0e10cSrcweir                     else if ( pEntryField->isOtherFunction() )
1015cdf0e10cSrcweir                     {
1016cdf0e10cSrcweir                         aWorkStr += aColumnName;
1017cdf0e10cSrcweir                     }
1018cdf0e10cSrcweir 					else
1019cdf0e10cSrcweir 					{
1020cdf0e10cSrcweir 						aWorkStr += quoteTableAlias(bMulti,pEntryField->GetAlias(),aQuote);
1021cdf0e10cSrcweir 						aWorkStr += ::dbtools::quoteName(aQuote, aColumnName);
1022cdf0e10cSrcweir 					}
1023cdf0e10cSrcweir 					aWorkStr += ::rtl::OUString(' ');
1024cdf0e10cSrcweir 					aWorkStr += String::CreateFromAscii( ";ASC;DESC" ).GetToken( (sal_uInt16)eOrder );
1025cdf0e10cSrcweir 					aWorkStr += ::rtl::OUString(',');
1026cdf0e10cSrcweir 				}
1027cdf0e10cSrcweir 			}
1028cdf0e10cSrcweir 
1029cdf0e10cSrcweir 			{
1030cdf0e10cSrcweir 				String sTemp(aWorkStr);
1031cdf0e10cSrcweir 				sTemp.EraseTrailingChars( ',' );
1032cdf0e10cSrcweir 				aWorkStr = sTemp;
1033cdf0e10cSrcweir 			}
1034cdf0e10cSrcweir 
1035cdf0e10cSrcweir 			if ( aWorkStr.getLength() )
1036cdf0e10cSrcweir 			{
1037cdf0e10cSrcweir 				const sal_Int32 nMaxOrder = xMetaData->getMaxColumnsInOrderBy();
1038cdf0e10cSrcweir 				String sToken(aWorkStr);
1039cdf0e10cSrcweir 				if ( nMaxOrder && nMaxOrder < sToken.GetTokenCount(',') )
1040cdf0e10cSrcweir 					eErrorCode = eStatementTooLong;
1041cdf0e10cSrcweir 				else
1042cdf0e10cSrcweir 				{
1043cdf0e10cSrcweir 					_rsRet = ::rtl::OUString::createFromAscii(" ORDER BY ");
1044cdf0e10cSrcweir 					_rsRet += aWorkStr;
1045cdf0e10cSrcweir 				}
1046cdf0e10cSrcweir 			}
1047cdf0e10cSrcweir 		}
1048cdf0e10cSrcweir 		catch(SQLException&)
1049cdf0e10cSrcweir 		{
1050cdf0e10cSrcweir 			OSL_ASSERT(!"Failure while building group by!");
1051cdf0e10cSrcweir 		}
1052cdf0e10cSrcweir 
1053cdf0e10cSrcweir 		return eErrorCode;
1054cdf0e10cSrcweir 	}
1055cdf0e10cSrcweir 
1056cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GenerateInnerJoinCriterias(const Reference<XConnection> & _xConnection,::rtl::OUString & _rJoinCrit,const::std::vector<OTableConnection * > * _pConnList)1057cdf0e10cSrcweir 	void GenerateInnerJoinCriterias(const Reference< XConnection>& _xConnection,
1058cdf0e10cSrcweir 									::rtl::OUString& _rJoinCrit,
1059cdf0e10cSrcweir 									const ::std::vector<OTableConnection*>* _pConnList)
1060cdf0e10cSrcweir 	{
1061cdf0e10cSrcweir 		::std::vector<OTableConnection*>::const_iterator aIter = _pConnList->begin();
1062cdf0e10cSrcweir         ::std::vector<OTableConnection*>::const_iterator aEnd = _pConnList->end();
1063cdf0e10cSrcweir 		for(;aIter != aEnd;++aIter)
1064cdf0e10cSrcweir 		{
1065cdf0e10cSrcweir 			const OQueryTableConnection* pEntryConn = static_cast<const OQueryTableConnection*>(*aIter);
1066cdf0e10cSrcweir 			OQueryTableConnectionData* pEntryConnData = static_cast<OQueryTableConnectionData*>(pEntryConn->GetData().get());
1067cdf0e10cSrcweir             if ( pEntryConnData->GetJoinType() == INNER_JOIN && !pEntryConnData->isNatural() )
1068cdf0e10cSrcweir 			{
1069cdf0e10cSrcweir 				if(_rJoinCrit.getLength())
1070cdf0e10cSrcweir 					_rJoinCrit += C_AND;
1071cdf0e10cSrcweir 				_rJoinCrit += BuildJoinCriteria(_xConnection,pEntryConnData->GetConnLineDataList(),pEntryConnData);
1072cdf0e10cSrcweir 			}
1073cdf0e10cSrcweir 		}
1074cdf0e10cSrcweir 	}
1075cdf0e10cSrcweir 	//------------------------------------------------------------------------------
searchAndAppendName(const Reference<XConnection> & _xConnection,const OQueryTableWindow * _pTableWindow,::std::map<::rtl::OUString,sal_Bool,::comphelper::UStringMixLess> & _rTableNames,::rtl::OUString & _rsTableListStr)1076cdf0e10cSrcweir 	void searchAndAppendName(const Reference< XConnection>& _xConnection,
1077cdf0e10cSrcweir 							 const OQueryTableWindow* _pTableWindow,
1078cdf0e10cSrcweir 							 ::std::map< ::rtl::OUString,sal_Bool,::comphelper::UStringMixLess>& _rTableNames,
1079cdf0e10cSrcweir 							 ::rtl::OUString& _rsTableListStr
1080cdf0e10cSrcweir 							 )
1081cdf0e10cSrcweir 	{
1082cdf0e10cSrcweir 		::rtl::OUString sTabName(BuildTable(_xConnection,_pTableWindow));
1083cdf0e10cSrcweir 
1084cdf0e10cSrcweir 		if(_rTableNames.find(sTabName) == _rTableNames.end())
1085cdf0e10cSrcweir 		{
1086cdf0e10cSrcweir 			_rTableNames[sTabName] = sal_True;
1087cdf0e10cSrcweir 			_rsTableListStr += sTabName;
1088cdf0e10cSrcweir 			_rsTableListStr += ::rtl::OUString(',');
1089cdf0e10cSrcweir 		}
1090cdf0e10cSrcweir 	}
1091cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GenerateFromClause(const Reference<XConnection> & _xConnection,const OQueryTableView::OTableWindowMap * pTabList,const::std::vector<OTableConnection * > * pConnList)1092cdf0e10cSrcweir 	::rtl::OUString GenerateFromClause(	const Reference< XConnection>& _xConnection,
1093cdf0e10cSrcweir 										const OQueryTableView::OTableWindowMap*	pTabList,
1094cdf0e10cSrcweir 										const ::std::vector<OTableConnection*>*	pConnList
1095cdf0e10cSrcweir 										)
1096cdf0e10cSrcweir 	{
1097cdf0e10cSrcweir 
1098cdf0e10cSrcweir 		::rtl::OUString aTableListStr;
1099cdf0e10cSrcweir 		// wird gebraucht um sicher zustelllen das eine Tabelle nicht doppelt vorkommt
1100cdf0e10cSrcweir 		::std::map< ::rtl::OUString,sal_Bool,::comphelper::UStringMixLess> aTableNames;
1101cdf0e10cSrcweir 
1102cdf0e10cSrcweir 		// generate outer join clause in from
1103cdf0e10cSrcweir 		if(!pConnList->empty())
1104cdf0e10cSrcweir 		{
1105cdf0e10cSrcweir 			::std::vector<OTableConnection*>::const_iterator aIter = pConnList->begin();
1106cdf0e10cSrcweir             ::std::vector<OTableConnection*>::const_iterator aEnd = pConnList->end();
1107cdf0e10cSrcweir             ::std::map<OTableWindow*,sal_Int32> aConnectionCount;
1108cdf0e10cSrcweir 			for(;aIter != aEnd;++aIter)
1109cdf0e10cSrcweir             {
1110cdf0e10cSrcweir 				static_cast<OQueryTableConnection*>(*aIter)->SetVisited(sal_False);
1111cdf0e10cSrcweir                 if ( aConnectionCount.find((*aIter)->GetSourceWin()) == aConnectionCount.end() )
1112cdf0e10cSrcweir                     aConnectionCount.insert(::std::map<OTableWindow*,sal_Int32>::value_type((*aIter)->GetSourceWin(),0));
1113cdf0e10cSrcweir                 else
1114cdf0e10cSrcweir                     aConnectionCount[(*aIter)->GetSourceWin()]++;
1115cdf0e10cSrcweir                 if ( aConnectionCount.find((*aIter)->GetDestWin()) == aConnectionCount.end() )
1116cdf0e10cSrcweir                     aConnectionCount.insert(::std::map<OTableWindow*,sal_Int32>::value_type((*aIter)->GetDestWin(),0));
1117cdf0e10cSrcweir                 else
1118cdf0e10cSrcweir                     aConnectionCount[(*aIter)->GetDestWin()]++;
1119cdf0e10cSrcweir             }
1120cdf0e10cSrcweir             ::std::multimap<sal_Int32 , OTableWindow*> aMulti;
1121cdf0e10cSrcweir             ::std::map<OTableWindow*,sal_Int32>::iterator aCountIter = aConnectionCount.begin();
1122cdf0e10cSrcweir             ::std::map<OTableWindow*,sal_Int32>::iterator aCountEnd = aConnectionCount.end();
1123cdf0e10cSrcweir             for(;aCountIter != aCountEnd;++aCountIter)
1124cdf0e10cSrcweir             {
1125cdf0e10cSrcweir                 aMulti.insert(::std::multimap<sal_Int32 , OTableWindow*>::value_type(aCountIter->second,aCountIter->first));
1126cdf0e10cSrcweir             }
1127cdf0e10cSrcweir 
1128cdf0e10cSrcweir             const sal_Bool bUseEscape = ::dbtools::getBooleanDataSourceSetting( _xConnection, PROPERTY_OUTERJOINESCAPE );
1129cdf0e10cSrcweir             ::std::multimap<sal_Int32 , OTableWindow*>::reverse_iterator aRIter = aMulti.rbegin();
1130cdf0e10cSrcweir             ::std::multimap<sal_Int32 , OTableWindow*>::reverse_iterator aREnd = aMulti.rend();
1131cdf0e10cSrcweir             for(;aRIter != aREnd;++aRIter)
1132cdf0e10cSrcweir             {
1133cdf0e10cSrcweir                 ::std::vector<OTableConnection*>::const_iterator aConIter = aRIter->second->getTableView()->getTableConnections(aRIter->second);
1134cdf0e10cSrcweir                 for(;aConIter != aEnd;++aConIter)
1135cdf0e10cSrcweir                 {
1136cdf0e10cSrcweir                     OQueryTableConnection* pEntryConn = static_cast<OQueryTableConnection*>(*aConIter);
1137cdf0e10cSrcweir 				    if(!pEntryConn->IsVisited() && pEntryConn->GetSourceWin() == aRIter->second )
1138cdf0e10cSrcweir 				    {
1139cdf0e10cSrcweir 					    ::rtl::OUString aJoin;
1140cdf0e10cSrcweir 					    GetNextJoin(_xConnection,pEntryConn,static_cast<OQueryTableWindow*>(pEntryConn->GetDestWin()),aJoin);
1141cdf0e10cSrcweir 
1142cdf0e10cSrcweir 					    if(aJoin.getLength())
1143cdf0e10cSrcweir 					    {
1144cdf0e10cSrcweir 						    // insert tables into table list to avoid double entries
1145cdf0e10cSrcweir 						    OQueryTableWindow* pEntryTabFrom = static_cast<OQueryTableWindow*>(pEntryConn->GetSourceWin());
1146cdf0e10cSrcweir 						    OQueryTableWindow* pEntryTabTo = static_cast<OQueryTableWindow*>(pEntryConn->GetDestWin());
1147cdf0e10cSrcweir 
1148cdf0e10cSrcweir 						    ::rtl::OUString sTabName(BuildTable(_xConnection,pEntryTabFrom));
1149cdf0e10cSrcweir 						    if(aTableNames.find(sTabName) == aTableNames.end())
1150cdf0e10cSrcweir 							    aTableNames[sTabName] = sal_True;
1151cdf0e10cSrcweir 						    sTabName = BuildTable(_xConnection,pEntryTabTo);
1152cdf0e10cSrcweir 						    if(aTableNames.find(sTabName) == aTableNames.end())
1153cdf0e10cSrcweir 							    aTableNames[sTabName] = sal_True;
1154cdf0e10cSrcweir 
1155cdf0e10cSrcweir                             ::rtl::OUString aStr;
1156cdf0e10cSrcweir                             switch(static_cast<OQueryTableConnectionData*>(pEntryConn->GetData().get())->GetJoinType())
1157cdf0e10cSrcweir 		                    {
1158cdf0e10cSrcweir 			                    case LEFT_JOIN:
1159cdf0e10cSrcweir 			                    case RIGHT_JOIN:
1160cdf0e10cSrcweir                                 case FULL_JOIN:
1161cdf0e10cSrcweir                                     {
1162cdf0e10cSrcweir 						                // create outer join
1163cdf0e10cSrcweir 						                if ( bUseEscape )
1164cdf0e10cSrcweir 							                aStr += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("{ OJ "));
1165cdf0e10cSrcweir 						                aStr += aJoin;
1166cdf0e10cSrcweir 						                if ( bUseEscape )
1167cdf0e10cSrcweir 							                aStr += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" }"));
1168cdf0e10cSrcweir                                     }
1169cdf0e10cSrcweir                                     break;
1170cdf0e10cSrcweir                                 default:
1171cdf0e10cSrcweir                                     aStr += aJoin;
1172cdf0e10cSrcweir                                     break;
1173cdf0e10cSrcweir                             }
1174cdf0e10cSrcweir 						    aStr += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(","));
1175cdf0e10cSrcweir 						    aTableListStr += aStr;
1176cdf0e10cSrcweir 					    }
1177cdf0e10cSrcweir 				    }
1178cdf0e10cSrcweir 			    }
1179cdf0e10cSrcweir             }
1180cdf0e10cSrcweir 
1181cdf0e10cSrcweir 			// and now all inner joins
1182cdf0e10cSrcweir 			aIter = pConnList->begin();
1183cdf0e10cSrcweir 			for(;aIter != aEnd;++aIter)
1184cdf0e10cSrcweir 			{
1185cdf0e10cSrcweir 				OQueryTableConnection* pEntryConn = static_cast<OQueryTableConnection*>(*aIter);
1186cdf0e10cSrcweir 				if(!pEntryConn->IsVisited())
1187cdf0e10cSrcweir 				{
1188cdf0e10cSrcweir 					searchAndAppendName(_xConnection,
1189cdf0e10cSrcweir 										static_cast<OQueryTableWindow*>(pEntryConn->GetSourceWin()),
1190cdf0e10cSrcweir 										aTableNames,
1191cdf0e10cSrcweir 										aTableListStr);
1192cdf0e10cSrcweir 
1193cdf0e10cSrcweir 					searchAndAppendName(_xConnection,
1194cdf0e10cSrcweir 										static_cast<OQueryTableWindow*>(pEntryConn->GetDestWin()),
1195cdf0e10cSrcweir 										aTableNames,
1196cdf0e10cSrcweir 										aTableListStr);
1197cdf0e10cSrcweir 				}
1198cdf0e10cSrcweir 			}
1199cdf0e10cSrcweir 		}
1200cdf0e10cSrcweir 		// all tables that haven't a connection to anyone
1201cdf0e10cSrcweir 		OQueryTableView::OTableWindowMap::const_iterator aTabIter = pTabList->begin();
1202cdf0e10cSrcweir         OQueryTableView::OTableWindowMap::const_iterator aTabEnd = pTabList->end();
1203cdf0e10cSrcweir 		for(;aTabIter != aTabEnd;++aTabIter)
1204cdf0e10cSrcweir 		{
1205cdf0e10cSrcweir 			const OQueryTableWindow* pEntryTab = static_cast<const OQueryTableWindow*>(aTabIter->second);
1206cdf0e10cSrcweir 			if(!pEntryTab->ExistsAConn())
1207cdf0e10cSrcweir 			{
1208cdf0e10cSrcweir 				aTableListStr += BuildTable(_xConnection,pEntryTab);
1209cdf0e10cSrcweir 				aTableListStr += ::rtl::OUString(',');
1210cdf0e10cSrcweir 			}
1211cdf0e10cSrcweir 		}
1212cdf0e10cSrcweir 
1213cdf0e10cSrcweir 		if(aTableListStr.getLength())
1214cdf0e10cSrcweir 			aTableListStr = aTableListStr.replaceAt(aTableListStr.getLength()-1,1, ::rtl::OUString() );
1215cdf0e10cSrcweir 		return aTableListStr;
1216cdf0e10cSrcweir 	}
1217cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GenerateGroupBy(const OQueryDesignView * _pView,OTableFields & _rFieldList,sal_Bool bMulti)1218cdf0e10cSrcweir 	::rtl::OUString GenerateGroupBy(const OQueryDesignView* _pView,OTableFields& _rFieldList, sal_Bool bMulti )
1219cdf0e10cSrcweir 	{
1220cdf0e10cSrcweir         OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
1221cdf0e10cSrcweir 		const Reference< XConnection> xConnection = rController.getConnection();
1222cdf0e10cSrcweir 		if(!xConnection.is())
1223cdf0e10cSrcweir 			return ::rtl::OUString();
1224cdf0e10cSrcweir 
1225cdf0e10cSrcweir         ::std::map< rtl::OUString,bool> aGroupByNames;
1226cdf0e10cSrcweir 
1227cdf0e10cSrcweir 		::rtl::OUString aGroupByStr;
1228cdf0e10cSrcweir 		try
1229cdf0e10cSrcweir 		{
1230cdf0e10cSrcweir 			const Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
1231cdf0e10cSrcweir 			const ::rtl::OUString aQuote = xMetaData->getIdentifierQuoteString();
1232cdf0e10cSrcweir 
1233cdf0e10cSrcweir 			OTableFields::iterator aIter = _rFieldList.begin();
1234cdf0e10cSrcweir 			OTableFields::iterator aEnd = _rFieldList.end();
1235cdf0e10cSrcweir 		    for(;aIter != aEnd;++aIter)
1236cdf0e10cSrcweir 			{
1237cdf0e10cSrcweir 				OTableFieldDescRef	pEntryField = *aIter;
1238cdf0e10cSrcweir 				if ( pEntryField->IsGroupBy() )
1239cdf0e10cSrcweir 				{
1240cdf0e10cSrcweir 					DBG_ASSERT(pEntryField->GetField().getLength(),"Kein FieldName vorhanden!;-(");
1241cdf0e10cSrcweir 					::rtl::OUString sGroupByPart = quoteTableAlias(bMulti,pEntryField->GetAlias(),aQuote);
1242cdf0e10cSrcweir 
1243cdf0e10cSrcweir 					// only quote the field name when it isn't calculated
1244cdf0e10cSrcweir 					if ( pEntryField->isNoneFunction() )
1245cdf0e10cSrcweir                     {
1246cdf0e10cSrcweir 						sGroupByPart += ::dbtools::quoteName(aQuote, pEntryField->GetField());
1247cdf0e10cSrcweir                     }
1248cdf0e10cSrcweir 					else
1249cdf0e10cSrcweir                     {
1250cdf0e10cSrcweir                         ::rtl::OUString aTmp = pEntryField->GetField();
1251cdf0e10cSrcweir 						::rtl::OUString aErrorMsg;
1252cdf0e10cSrcweir 						Reference<XPropertySet> xColumn;
1253cdf0e10cSrcweir                         ::std::auto_ptr< ::connectivity::OSQLParseNode> pParseNode(_pView->getPredicateTreeFromEntry(pEntryField,aTmp,aErrorMsg,xColumn));
1254cdf0e10cSrcweir 						if (pParseNode.get())
1255cdf0e10cSrcweir 						{
1256cdf0e10cSrcweir 							::rtl::OUString sGroupBy;
1257cdf0e10cSrcweir 							pParseNode->getChild(0)->parseNodeToStr(	sGroupBy,
1258cdf0e10cSrcweir 														xConnection,
1259cdf0e10cSrcweir 														&rController.getParser().getContext(),
1260cdf0e10cSrcweir 														sal_False,
1261cdf0e10cSrcweir 														!pEntryField->isOtherFunction());
1262cdf0e10cSrcweir 							sGroupByPart += sGroupBy;
1263cdf0e10cSrcweir 						}
1264cdf0e10cSrcweir 						else
1265cdf0e10cSrcweir 							sGroupByPart += pEntryField->GetField();
1266cdf0e10cSrcweir                     }
1267cdf0e10cSrcweir                     if ( aGroupByNames.find(sGroupByPart) == aGroupByNames.end() )
1268cdf0e10cSrcweir                     {
1269cdf0e10cSrcweir                         aGroupByNames.insert(::std::map< rtl::OUString,bool>::value_type(sGroupByPart,true));
1270cdf0e10cSrcweir                         aGroupByStr += sGroupByPart;
1271cdf0e10cSrcweir 					    aGroupByStr += ::rtl::OUString(',');
1272cdf0e10cSrcweir                     }
1273cdf0e10cSrcweir 				}
1274cdf0e10cSrcweir 			}
1275cdf0e10cSrcweir 			if ( aGroupByStr.getLength() )
1276cdf0e10cSrcweir 			{
1277cdf0e10cSrcweir 				aGroupByStr = aGroupByStr.replaceAt(aGroupByStr.getLength()-1,1, ::rtl::OUString(' ') );
1278cdf0e10cSrcweir 				::rtl::OUString aGroupByStr2 = ::rtl::OUString::createFromAscii(" GROUP BY ");
1279cdf0e10cSrcweir 				aGroupByStr2 += aGroupByStr;
1280cdf0e10cSrcweir 				aGroupByStr = aGroupByStr2;
1281cdf0e10cSrcweir 			}
1282cdf0e10cSrcweir 		}
1283cdf0e10cSrcweir 		catch(SQLException&)
1284cdf0e10cSrcweir 		{
1285cdf0e10cSrcweir 			OSL_ASSERT(!"Failure while building group by!");
1286cdf0e10cSrcweir 		}
1287cdf0e10cSrcweir 		return aGroupByStr;
1288cdf0e10cSrcweir 	}
1289cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
1290cdf0e10cSrcweir 	SqlParseError GetORCriteria(OQueryDesignView* _pView,
1291cdf0e10cSrcweir 								OSelectionBrowseBox* _pSelectionBrw,
1292cdf0e10cSrcweir 								const ::connectivity::OSQLParseNode * pCondition,
1293cdf0e10cSrcweir 								sal_uInt16& nLevel ,
1294cdf0e10cSrcweir 								sal_Bool bHaving = sal_False,
1295cdf0e10cSrcweir                                 bool bAddOrOnOneLine = false);
1296cdf0e10cSrcweir 	// -----------------------------------------------------------------------------
GetSelectionCriteria(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw,const::connectivity::OSQLParseNode * pNode,sal_uInt16 & rLevel)1297cdf0e10cSrcweir 	SqlParseError GetSelectionCriteria(	OQueryDesignView* _pView,
1298cdf0e10cSrcweir 										OSelectionBrowseBox* _pSelectionBrw,
1299cdf0e10cSrcweir 										const ::connectivity::OSQLParseNode* pNode,
1300cdf0e10cSrcweir 										sal_uInt16& rLevel )
1301cdf0e10cSrcweir 	{
1302cdf0e10cSrcweir 		if (!SQL_ISRULE(pNode, select_statement))
1303cdf0e10cSrcweir 			return eNoSelectStatement;
1304cdf0e10cSrcweir 
1305cdf0e10cSrcweir 		// nyi: mehr Pruefung auf korrekte Struktur!
1306cdf0e10cSrcweir 		pNode = pNode ? pNode->getChild(3)->getChild(1) : NULL;
1307cdf0e10cSrcweir 		// no where clause found
1308cdf0e10cSrcweir 		if (!pNode || pNode->isLeaf())
1309cdf0e10cSrcweir 			return eOk;
1310cdf0e10cSrcweir 
1311cdf0e10cSrcweir 		// Naechster freier Satz ...
1312cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
1313cdf0e10cSrcweir 		::connectivity::OSQLParseNode * pCondition = pNode->getChild(1);
1314cdf0e10cSrcweir 		if ( pCondition ) // no where clause
1315cdf0e10cSrcweir 		{
1316cdf0e10cSrcweir 			// now we have to chech the other conditions
1317cdf0e10cSrcweir 			// first make the logical easier
1318cdf0e10cSrcweir 			::connectivity::OSQLParseNode::negateSearchCondition(pCondition);
1319cdf0e10cSrcweir 			::connectivity::OSQLParseNode *pNodeTmp = pNode->getChild(1);
1320cdf0e10cSrcweir 
1321cdf0e10cSrcweir 			::connectivity::OSQLParseNode::disjunctiveNormalForm(pNodeTmp);
1322cdf0e10cSrcweir 			pNodeTmp = pNode->getChild(1);
1323cdf0e10cSrcweir 			::connectivity::OSQLParseNode::absorptions(pNodeTmp);
1324cdf0e10cSrcweir 			pNodeTmp = pNode->getChild(1);
1325cdf0e10cSrcweir             // compress sort the criteria @see http://www.openoffice.org/issues/show_bug.cgi?id=24079
1326cdf0e10cSrcweir             OSQLParseNode::compress(pNodeTmp);
1327cdf0e10cSrcweir             pNodeTmp = pNode->getChild(1);
1328cdf0e10cSrcweir 
1329cdf0e10cSrcweir 			// first extract the inner joins conditions
1330cdf0e10cSrcweir 			GetInnerJoinCriteria(_pView,pNodeTmp);
1331cdf0e10cSrcweir 			// now simplify again, join are checked in ComparisonPredicate
1332cdf0e10cSrcweir 			::connectivity::OSQLParseNode::absorptions(pNodeTmp);
1333cdf0e10cSrcweir 			pNodeTmp = pNode->getChild(1);
1334cdf0e10cSrcweir 
1335cdf0e10cSrcweir 			// it could happen that pCondition is not more valid
1336cdf0e10cSrcweir 			eErrorCode = GetORCriteria(_pView,_pSelectionBrw,pNodeTmp, rLevel);
1337cdf0e10cSrcweir 		}
1338cdf0e10cSrcweir 		return eErrorCode;
1339cdf0e10cSrcweir 	}
1340cdf0e10cSrcweir 	//------------------------------------------------------------------------------
1341cdf0e10cSrcweir 	SqlParseError GetANDCriteria(	OQueryDesignView* _pView,
1342cdf0e10cSrcweir 									OSelectionBrowseBox* _pSelectionBrw,
1343cdf0e10cSrcweir 									const  ::connectivity::OSQLParseNode * pCondition,
1344cdf0e10cSrcweir 									sal_uInt16& nLevel,
1345cdf0e10cSrcweir 									sal_Bool bHaving,
1346cdf0e10cSrcweir                                     bool bAddOrOnOneLine);
1347cdf0e10cSrcweir 	//------------------------------------------------------------------------------
1348cdf0e10cSrcweir 	SqlParseError ComparisonPredicate(OQueryDesignView* _pView,
1349cdf0e10cSrcweir 							OSelectionBrowseBox* _pSelectionBrw,
1350cdf0e10cSrcweir 							const ::connectivity::OSQLParseNode * pCondition,
1351cdf0e10cSrcweir 							const sal_uInt16 nLevel,
1352cdf0e10cSrcweir 							sal_Bool bHaving,
1353cdf0e10cSrcweir                             bool bAddOrOnOneLine);
1354cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GetORCriteria(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw,const::connectivity::OSQLParseNode * pCondition,sal_uInt16 & nLevel,sal_Bool bHaving,bool bAddOrOnOneLine)1355cdf0e10cSrcweir 	SqlParseError GetORCriteria(OQueryDesignView* _pView,
1356cdf0e10cSrcweir 								OSelectionBrowseBox* _pSelectionBrw,
1357cdf0e10cSrcweir 								const ::connectivity::OSQLParseNode * pCondition,
1358cdf0e10cSrcweir 								sal_uInt16& nLevel ,
1359cdf0e10cSrcweir 								sal_Bool bHaving,
1360cdf0e10cSrcweir                                 bool bAddOrOnOneLine)
1361cdf0e10cSrcweir 	{
1362cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
1363cdf0e10cSrcweir 
1364cdf0e10cSrcweir 		// Runde Klammern um den Ausdruck
1365cdf0e10cSrcweir 		if (pCondition->count() == 3 &&
1366cdf0e10cSrcweir 			SQL_ISPUNCTUATION(pCondition->getChild(0),"(") &&
1367cdf0e10cSrcweir 			SQL_ISPUNCTUATION(pCondition->getChild(2),")"))
1368cdf0e10cSrcweir 		{
1369cdf0e10cSrcweir 			eErrorCode = GetORCriteria(_pView,_pSelectionBrw,pCondition->getChild(1),nLevel,bHaving,bAddOrOnOneLine);
1370cdf0e10cSrcweir 		}
1371cdf0e10cSrcweir 		// oder Verknuepfung
1372cdf0e10cSrcweir 		// a searchcondition can only look like this: search_condition SQL_TOKEN_OR boolean_term
1373cdf0e10cSrcweir 		else if (SQL_ISRULE(pCondition,search_condition))
1374cdf0e10cSrcweir 		{
1375cdf0e10cSrcweir 			for (int i = 0; i < 3 && eErrorCode == eOk ; i+=2)
1376cdf0e10cSrcweir 			{
1377cdf0e10cSrcweir                 const  ::connectivity::OSQLParseNode* pChild = pCondition->getChild(i);
1378cdf0e10cSrcweir 				if ( SQL_ISRULE(pChild,search_condition) )
1379cdf0e10cSrcweir 					eErrorCode = GetORCriteria(_pView,_pSelectionBrw,pChild,nLevel,bHaving,bAddOrOnOneLine);
1380cdf0e10cSrcweir 				else
1381cdf0e10cSrcweir 				{
1382cdf0e10cSrcweir                     eErrorCode = GetANDCriteria(_pView,_pSelectionBrw,pChild, nLevel,bHaving, i == 0 ? false : bAddOrOnOneLine);
1383cdf0e10cSrcweir 					if ( !bAddOrOnOneLine)
1384cdf0e10cSrcweir 						nLevel++;
1385cdf0e10cSrcweir 				}
1386cdf0e10cSrcweir 			}
1387cdf0e10cSrcweir 		}
1388cdf0e10cSrcweir 		else
1389cdf0e10cSrcweir 			eErrorCode = GetANDCriteria( _pView,_pSelectionBrw,pCondition, nLevel, bHaving,bAddOrOnOneLine );
1390cdf0e10cSrcweir 
1391cdf0e10cSrcweir 		return eErrorCode;
1392cdf0e10cSrcweir 	}
1393cdf0e10cSrcweir     //--------------------------------------------------------------------------------------------------
CheckOrCriteria(const::connectivity::OSQLParseNode * _pCondition,::connectivity::OSQLParseNode * _pFirstColumnRef)1394cdf0e10cSrcweir     bool CheckOrCriteria(const ::connectivity::OSQLParseNode* _pCondition,::connectivity::OSQLParseNode* _pFirstColumnRef)
1395cdf0e10cSrcweir     {
1396cdf0e10cSrcweir         bool bRet = true;
1397cdf0e10cSrcweir         ::connectivity::OSQLParseNode* pFirstColumnRef = _pFirstColumnRef;
1398cdf0e10cSrcweir         for (int i = 0; i < 3 && bRet; i+=2)
1399cdf0e10cSrcweir 		{
1400cdf0e10cSrcweir             const  ::connectivity::OSQLParseNode* pChild = _pCondition->getChild(i);
1401cdf0e10cSrcweir             if ( SQL_ISRULE(pChild,search_condition) )
1402cdf0e10cSrcweir                 bRet = CheckOrCriteria(pChild,pFirstColumnRef);
1403cdf0e10cSrcweir             else
1404cdf0e10cSrcweir             {
1405cdf0e10cSrcweir                 // this is a simple way to test columns are the same, may be we have to adjust this algo a little bit in future. :-)
1406cdf0e10cSrcweir                 ::connectivity::OSQLParseNode* pSecondColumnRef = pChild->getByRule(::connectivity::OSQLParseNode::column_ref);
1407cdf0e10cSrcweir                 if ( pFirstColumnRef && pSecondColumnRef )
1408cdf0e10cSrcweir                     bRet = *pFirstColumnRef == *pSecondColumnRef;
1409cdf0e10cSrcweir                 else if ( !pFirstColumnRef )
1410cdf0e10cSrcweir                     pFirstColumnRef = pSecondColumnRef;
1411cdf0e10cSrcweir             }
1412cdf0e10cSrcweir         }
1413cdf0e10cSrcweir         return bRet;
1414cdf0e10cSrcweir     }
1415cdf0e10cSrcweir 	//--------------------------------------------------------------------------------------------------
GetANDCriteria(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw,const::connectivity::OSQLParseNode * pCondition,sal_uInt16 & nLevel,sal_Bool bHaving,bool bAddOrOnOneLine)1416cdf0e10cSrcweir 	SqlParseError GetANDCriteria(	OQueryDesignView* _pView,
1417cdf0e10cSrcweir 									OSelectionBrowseBox* _pSelectionBrw,
1418cdf0e10cSrcweir 									const  ::connectivity::OSQLParseNode * pCondition,
1419cdf0e10cSrcweir 									sal_uInt16& nLevel,
1420cdf0e10cSrcweir 									sal_Bool bHaving,
1421cdf0e10cSrcweir                                     bool bAddOrOnOneLine)
1422cdf0e10cSrcweir 	{
1423cdf0e10cSrcweir 		const ::com::sun::star::lang::Locale	aLocale = _pView->getLocale();
1424cdf0e10cSrcweir 		const ::rtl::OUString sDecimal = _pView->getDecimalSeparator();
1425cdf0e10cSrcweir 
1426cdf0e10cSrcweir 		// ich werde ein paar Mal einen gecasteten Pointer auf meinen ::com::sun::star::sdbcx::Container brauchen
1427cdf0e10cSrcweir 		OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
1428cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
1429cdf0e10cSrcweir 
1430cdf0e10cSrcweir 		// Runde Klammern
1431cdf0e10cSrcweir 		if (SQL_ISRULE(pCondition,boolean_primary))
1432cdf0e10cSrcweir 		{
1433cdf0e10cSrcweir             // check if we have to put the or criteria on one line.
1434cdf0e10cSrcweir 			const  ::connectivity::OSQLParseNode* pSearchCondition = pCondition->getChild(1);
1435cdf0e10cSrcweir             bool bMustAddOrOnOneLine = CheckOrCriteria(pSearchCondition,NULL);
1436cdf0e10cSrcweir 			if ( SQL_ISRULE( pSearchCondition, search_condition) ) // we have a or
1437cdf0e10cSrcweir 			{
1438cdf0e10cSrcweir 				_pSelectionBrw->DuplicateConditionLevel( nLevel);
1439cdf0e10cSrcweir 				eErrorCode = GetORCriteria(_pView,_pSelectionBrw,pSearchCondition->getChild(0), nLevel,bHaving,bMustAddOrOnOneLine );
1440cdf0e10cSrcweir 				++nLevel;
1441cdf0e10cSrcweir 				eErrorCode = GetORCriteria(_pView,_pSelectionBrw,pSearchCondition->getChild(2), nLevel,bHaving,bMustAddOrOnOneLine );
1442cdf0e10cSrcweir 			}
1443cdf0e10cSrcweir 			else
1444cdf0e10cSrcweir 				eErrorCode = GetORCriteria(_pView,_pSelectionBrw,pSearchCondition, nLevel,bHaving,bMustAddOrOnOneLine );
1445cdf0e10cSrcweir 		}
1446cdf0e10cSrcweir 		// Das erste Element ist (wieder) eine AND-Verknuepfung
1447cdf0e10cSrcweir 		else if ( SQL_ISRULE(pCondition,boolean_term) )
1448cdf0e10cSrcweir 		{
1449cdf0e10cSrcweir             OSL_ENSURE(pCondition->count() == 3,"Illegal definifiton of boolean_term");
1450cdf0e10cSrcweir 			eErrorCode = GetANDCriteria(_pView,_pSelectionBrw,pCondition->getChild(0), nLevel,bHaving,bAddOrOnOneLine );
1451cdf0e10cSrcweir 			if ( eErrorCode == eOk )
1452cdf0e10cSrcweir 				eErrorCode = GetANDCriteria(_pView,_pSelectionBrw,pCondition->getChild(2), nLevel,bHaving,bAddOrOnOneLine );
1453cdf0e10cSrcweir 		}
1454cdf0e10cSrcweir 		else if (SQL_ISRULE( pCondition, comparison_predicate))
1455cdf0e10cSrcweir 		{
1456cdf0e10cSrcweir 			eErrorCode = ComparisonPredicate(_pView,_pSelectionBrw,pCondition,nLevel,bHaving,bAddOrOnOneLine);
1457cdf0e10cSrcweir 		}
1458cdf0e10cSrcweir 		else if( SQL_ISRULE(pCondition,like_predicate) )
1459cdf0e10cSrcweir 		{
1460cdf0e10cSrcweir             const  ::connectivity::OSQLParseNode* pValueExp = pCondition->getChild(0);
1461cdf0e10cSrcweir 			if (SQL_ISRULE(pValueExp, column_ref ) )
1462cdf0e10cSrcweir 			{
1463cdf0e10cSrcweir 				::rtl::OUString aColumnName;
1464cdf0e10cSrcweir 				::rtl::OUString	aCondition;
1465cdf0e10cSrcweir 				Reference< XConnection> xConnection = rController.getConnection();
1466cdf0e10cSrcweir 				if ( xConnection.is() )
1467cdf0e10cSrcweir 				{
1468cdf0e10cSrcweir 					Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
1469cdf0e10cSrcweir 					// the international doesn't matter I have a string
1470cdf0e10cSrcweir 					pCondition->parseNodeToPredicateStr(aCondition,
1471cdf0e10cSrcweir 														xConnection,
1472cdf0e10cSrcweir 														rController.getNumberFormatter(),
1473cdf0e10cSrcweir 														aLocale,
1474cdf0e10cSrcweir 														static_cast<sal_Char>(sDecimal.toChar()),
1475cdf0e10cSrcweir 														&rController.getParser().getContext());
1476cdf0e10cSrcweir 
1477cdf0e10cSrcweir 					pValueExp->parseNodeToPredicateStr(	aColumnName,
1478cdf0e10cSrcweir 														xConnection,
1479cdf0e10cSrcweir 														rController.getNumberFormatter(),
1480cdf0e10cSrcweir 														aLocale,
1481cdf0e10cSrcweir 														static_cast<sal_Char>(sDecimal.toChar()),
1482cdf0e10cSrcweir 														&rController.getParser().getContext());
1483cdf0e10cSrcweir 
1484cdf0e10cSrcweir 					// don't display the column name
1485cdf0e10cSrcweir 					aCondition = aCondition.copy(aColumnName.getLength());
1486cdf0e10cSrcweir 					aCondition = aCondition.trim();
1487cdf0e10cSrcweir 				}
1488cdf0e10cSrcweir 
1489cdf0e10cSrcweir 				OTableFieldDescRef aDragLeft = new OTableFieldDesc();
1490cdf0e10cSrcweir 				if ( eOk == ( eErrorCode = FillDragInfo(_pView,pValueExp,aDragLeft) ))
1491cdf0e10cSrcweir                 {
1492cdf0e10cSrcweir                     if ( bHaving )
1493cdf0e10cSrcweir 					    aDragLeft->SetGroupBy(sal_True);
1494cdf0e10cSrcweir 					_pSelectionBrw->AddCondition(aDragLeft, aCondition, nLevel,bAddOrOnOneLine);
1495cdf0e10cSrcweir                 }
1496cdf0e10cSrcweir 			}
1497cdf0e10cSrcweir             else if(SQL_ISRULEOR2(pValueExp,general_set_fct ,set_fct_spec)	        ||
1498cdf0e10cSrcweir 							SQL_ISRULEOR2(pValueExp,position_exp,extract_exp)	||
1499cdf0e10cSrcweir 							SQL_ISRULEOR2(pValueExp,fold,char_substring_fct)	||
1500cdf0e10cSrcweir 							SQL_ISRULEOR2(pValueExp,length_exp,char_value_fct))
1501cdf0e10cSrcweir 		    {
1502cdf0e10cSrcweir 			    AddFunctionCondition(	_pView,
1503cdf0e10cSrcweir 									    _pSelectionBrw,
1504cdf0e10cSrcweir 									    pCondition,
1505cdf0e10cSrcweir 									    nLevel,
1506cdf0e10cSrcweir 									    bHaving,
1507cdf0e10cSrcweir                                         bAddOrOnOneLine);
1508cdf0e10cSrcweir 		    }
1509cdf0e10cSrcweir 			else
1510cdf0e10cSrcweir 			{
1511cdf0e10cSrcweir 				eErrorCode = eNoColumnInLike;
1512cdf0e10cSrcweir 				String sError(ModuleRes(STR_QRY_LIKE_LEFT_NO_COLUMN));
1513cdf0e10cSrcweir 				_pView->getController().appendError( sError );
1514cdf0e10cSrcweir 			}
1515cdf0e10cSrcweir 		}
1516cdf0e10cSrcweir 		else if(	SQL_ISRULEOR2(pCondition,test_for_null,in_predicate)
1517cdf0e10cSrcweir 				||	SQL_ISRULEOR2(pCondition,all_or_any_predicate,between_predicate))
1518cdf0e10cSrcweir 		{
1519cdf0e10cSrcweir 			if ( SQL_ISRULEOR2(pCondition->getChild(0), set_fct_spec , general_set_fct ) )
1520cdf0e10cSrcweir 			{
1521cdf0e10cSrcweir 				AddFunctionCondition(	_pView,
1522cdf0e10cSrcweir 										_pSelectionBrw,
1523cdf0e10cSrcweir 										pCondition,
1524cdf0e10cSrcweir 										nLevel,
1525cdf0e10cSrcweir 										bHaving,
1526cdf0e10cSrcweir                                         bAddOrOnOneLine);
1527cdf0e10cSrcweir 			}
1528cdf0e10cSrcweir 			else if ( SQL_ISRULE(pCondition->getChild(0), column_ref ) )
1529cdf0e10cSrcweir 			{
1530cdf0e10cSrcweir 				// parse condition
1531cdf0e10cSrcweir 				::rtl::OUString sCondition = ParseCondition(rController,pCondition,sDecimal,aLocale,1);
1532cdf0e10cSrcweir 				OTableFieldDescRef	aDragLeft = new OTableFieldDesc();
1533cdf0e10cSrcweir 				if ( eOk == ( eErrorCode = FillDragInfo(_pView,pCondition->getChild(0),aDragLeft)) )
1534cdf0e10cSrcweir                 {
1535cdf0e10cSrcweir                     if ( bHaving )
1536cdf0e10cSrcweir 					    aDragLeft->SetGroupBy(sal_True);
1537cdf0e10cSrcweir 					_pSelectionBrw->AddCondition(aDragLeft, sCondition, nLevel,bAddOrOnOneLine);
1538cdf0e10cSrcweir                 }
1539cdf0e10cSrcweir 			}
1540cdf0e10cSrcweir 			else
1541cdf0e10cSrcweir 			{
1542cdf0e10cSrcweir 				// Funktions-Bedingung parsen
1543cdf0e10cSrcweir 				::rtl::OUString	sCondition = ParseCondition(rController,pCondition,sDecimal,aLocale,1);
1544cdf0e10cSrcweir 				Reference< XConnection> xConnection = rController.getConnection();
1545cdf0e10cSrcweir 				Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
1546cdf0e10cSrcweir 					// the international doesn't matter I have a string
1547cdf0e10cSrcweir 				::rtl::OUString sName;
1548cdf0e10cSrcweir 				pCondition->getChild(0)->parseNodeToPredicateStr(sName,
1549cdf0e10cSrcweir 													xConnection,
1550cdf0e10cSrcweir 													rController.getNumberFormatter(),
1551cdf0e10cSrcweir 													aLocale,
1552cdf0e10cSrcweir 													static_cast<sal_Char>(sDecimal.toChar()),
1553cdf0e10cSrcweir 													&rController.getParser().getContext());
1554cdf0e10cSrcweir 
1555cdf0e10cSrcweir 				OTableFieldDescRef aDragLeft = new OTableFieldDesc();
1556cdf0e10cSrcweir 				aDragLeft->SetField(sName);
1557cdf0e10cSrcweir 				aDragLeft->SetFunctionType(FKT_OTHER);
1558cdf0e10cSrcweir 
1559cdf0e10cSrcweir 				if ( bHaving )
1560cdf0e10cSrcweir 					aDragLeft->SetGroupBy(sal_True);
1561cdf0e10cSrcweir 				_pSelectionBrw->AddCondition(aDragLeft, sCondition, nLevel,bAddOrOnOneLine);
1562cdf0e10cSrcweir 			}
1563cdf0e10cSrcweir 		}
1564cdf0e10cSrcweir 		else if( SQL_ISRULEOR2(pCondition,existence_test,unique_test) )
1565cdf0e10cSrcweir 		{
1566cdf0e10cSrcweir 			// Funktions-Bedingung parsen
1567cdf0e10cSrcweir 			::rtl::OUString	aCondition = ParseCondition(rController,pCondition,sDecimal,aLocale,0);
1568cdf0e10cSrcweir 
1569cdf0e10cSrcweir 			OTableFieldDescRef aDragLeft = new OTableFieldDesc();
1570cdf0e10cSrcweir 			aDragLeft->SetField(aCondition);
1571cdf0e10cSrcweir 			aDragLeft->SetFunctionType(FKT_CONDITION);
1572cdf0e10cSrcweir 
1573cdf0e10cSrcweir 			eErrorCode = _pSelectionBrw->InsertField(aDragLeft,BROWSER_INVALIDID,sal_False,sal_True).isValid() ? eOk : eTooManyColumns;
1574cdf0e10cSrcweir 		}
1575cdf0e10cSrcweir 		else //! TODO not supported yet
1576cdf0e10cSrcweir 			eErrorCode = eStatementTooComplex;
1577cdf0e10cSrcweir 		// Fehler einfach weiterreichen.
1578cdf0e10cSrcweir 		return eErrorCode;
1579cdf0e10cSrcweir 	}
1580cdf0e10cSrcweir 	//------------------------------------------------------------------------------
AddFunctionCondition(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw,const::connectivity::OSQLParseNode * pCondition,const sal_uInt16 nLevel,sal_Bool bHaving,bool bAddOrOnOneLine)1581cdf0e10cSrcweir 	SqlParseError AddFunctionCondition(OQueryDesignView* _pView,
1582cdf0e10cSrcweir 							OSelectionBrowseBox* _pSelectionBrw,
1583cdf0e10cSrcweir 							const ::connectivity::OSQLParseNode * pCondition,
1584cdf0e10cSrcweir 							const sal_uInt16 nLevel,
1585cdf0e10cSrcweir 							sal_Bool bHaving,
1586cdf0e10cSrcweir                             bool bAddOrOnOneLine)
1587cdf0e10cSrcweir 	{
1588cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
1589cdf0e10cSrcweir 		OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
1590cdf0e10cSrcweir 
1591cdf0e10cSrcweir         OSQLParseNode* pFunction = pCondition->getChild(0);
1592cdf0e10cSrcweir 
1593cdf0e10cSrcweir         OSL_ENSURE(SQL_ISRULEOR2(pFunction,general_set_fct ,set_fct_spec)	        ||
1594cdf0e10cSrcweir 							SQL_ISRULEOR2(pFunction,position_exp,extract_exp)	||
1595cdf0e10cSrcweir 							SQL_ISRULEOR2(pFunction,fold,char_substring_fct)	||
1596cdf0e10cSrcweir 							SQL_ISRULEOR2(pFunction,length_exp,char_value_fct),"Illegal call!");
1597cdf0e10cSrcweir 		::rtl::OUString	aCondition;
1598cdf0e10cSrcweir 		OTableFieldDescRef aDragLeft = new OTableFieldDesc();
1599cdf0e10cSrcweir 
1600cdf0e10cSrcweir 		::rtl::OUString aColumnName;
1601cdf0e10cSrcweir 		Reference< XConnection> xConnection = rController.getConnection();
1602cdf0e10cSrcweir 		if(xConnection.is())
1603cdf0e10cSrcweir 		{
1604cdf0e10cSrcweir 			Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
1605cdf0e10cSrcweir 			pCondition->parseNodeToPredicateStr(aCondition,
1606cdf0e10cSrcweir 												xConnection,
1607cdf0e10cSrcweir 												rController.getNumberFormatter(),
1608cdf0e10cSrcweir 												_pView->getLocale(),
1609cdf0e10cSrcweir 												static_cast<sal_Char>(_pView->getDecimalSeparator().toChar()),
1610cdf0e10cSrcweir 												&rController.getParser().getContext());
1611cdf0e10cSrcweir 
1612cdf0e10cSrcweir             pFunction->parseNodeToStr(	aColumnName,
1613cdf0e10cSrcweir 										xConnection,
1614cdf0e10cSrcweir 										&rController.getParser().getContext(),
1615cdf0e10cSrcweir 										sal_True,
1616cdf0e10cSrcweir 										sal_True); // quote is to true because we need quoted elements inside the function
1617cdf0e10cSrcweir             // i75557
1618cdf0e10cSrcweir 			//pFunction->parseNodeToPredicateStr(aColumnName,
1619cdf0e10cSrcweir 			//									xConnection,
1620cdf0e10cSrcweir 			//									rController.getNumberFormatter(),
1621cdf0e10cSrcweir 			//									_pView->getLocale(),
1622cdf0e10cSrcweir 			//									static_cast<sal_Char>(_pView->getDecimalSeparator().toChar()),
1623cdf0e10cSrcweir 			//									&rController.getParser().getContext());
1624cdf0e10cSrcweir 			// don't display the column name
1625cdf0e10cSrcweir 			aCondition = aCondition.copy(aColumnName.getLength());
1626cdf0e10cSrcweir 			aCondition = aCondition.trim();
1627cdf0e10cSrcweir 			if ( aCondition.indexOf('=',0) == 0 ) // ignore the equal sign
1628cdf0e10cSrcweir 				aCondition = aCondition.copy(1);
1629cdf0e10cSrcweir 
1630cdf0e10cSrcweir 
1631cdf0e10cSrcweir 			if ( SQL_ISRULE(pFunction, general_set_fct ) )
1632cdf0e10cSrcweir 			{
1633cdf0e10cSrcweir 				sal_Int32 nFunctionType = FKT_AGGREGATE;
1634cdf0e10cSrcweir 				OSQLParseNode* pParamNode = pFunction->getChild(pFunction->count()-2);
1635cdf0e10cSrcweir 				if ( pParamNode && pParamNode->getTokenValue().toChar() == '*' )
1636cdf0e10cSrcweir 				{
1637cdf0e10cSrcweir 					OJoinTableView::OTableWindowMap* pTabList = _pView->getTableView()->GetTabWinMap();
1638cdf0e10cSrcweir 					OJoinTableView::OTableWindowMap::iterator aIter = pTabList->begin();
1639cdf0e10cSrcweir                     OJoinTableView::OTableWindowMap::iterator aTabEnd = pTabList->end();
1640cdf0e10cSrcweir 					for(;aIter != aTabEnd;++aIter)
1641cdf0e10cSrcweir 					{
1642cdf0e10cSrcweir 						OQueryTableWindow* pTabWin = static_cast<OQueryTableWindow*>(aIter->second);
1643cdf0e10cSrcweir 						if (pTabWin->ExistsField( ::rtl::OUString::createFromAscii("*"), aDragLeft ))
1644cdf0e10cSrcweir 						{
1645cdf0e10cSrcweir 							aDragLeft->SetAlias(String());
1646cdf0e10cSrcweir 							aDragLeft->SetTable(String());
1647cdf0e10cSrcweir 							break;
1648cdf0e10cSrcweir 						}
1649cdf0e10cSrcweir 					}
1650cdf0e10cSrcweir 				}
1651cdf0e10cSrcweir 				else if( eOk != ( eErrorCode = FillDragInfo(_pView,pParamNode,aDragLeft))
1652cdf0e10cSrcweir 						&& SQL_ISRULE(pParamNode,num_value_exp) )
1653cdf0e10cSrcweir 				{
1654cdf0e10cSrcweir 					::rtl::OUString sParameterValue;
1655cdf0e10cSrcweir 					pParamNode->parseNodeToStr(	sParameterValue,
1656cdf0e10cSrcweir 												xConnection,
1657cdf0e10cSrcweir 												&rController.getParser().getContext());
1658cdf0e10cSrcweir 					nFunctionType |= FKT_NUMERIC;
1659cdf0e10cSrcweir 					aDragLeft->SetField(sParameterValue);
1660cdf0e10cSrcweir 					eErrorCode = eOk;
1661cdf0e10cSrcweir 				}
1662cdf0e10cSrcweir 				aDragLeft->SetFunctionType(nFunctionType);
1663cdf0e10cSrcweir 				if ( bHaving )
1664cdf0e10cSrcweir 					aDragLeft->SetGroupBy(sal_True);
1665cdf0e10cSrcweir 				sal_Int32 nIndex = 0;
1666cdf0e10cSrcweir 				aDragLeft->SetFunction(aColumnName.getToken(0,'(',nIndex));
1667cdf0e10cSrcweir 			}
1668cdf0e10cSrcweir 			else
1669cdf0e10cSrcweir 			{
1670cdf0e10cSrcweir 				// bei unbekannten Funktionen wird der gesamte Text in das Field gechrieben
1671cdf0e10cSrcweir 				aDragLeft->SetField(aColumnName);
1672cdf0e10cSrcweir 				if(bHaving)
1673cdf0e10cSrcweir 					aDragLeft->SetGroupBy(sal_True);
1674cdf0e10cSrcweir 				aDragLeft->SetFunctionType(FKT_OTHER|FKT_NUMERIC);
1675cdf0e10cSrcweir 			}
1676cdf0e10cSrcweir 			_pSelectionBrw->AddCondition(aDragLeft, aCondition, nLevel,bAddOrOnOneLine);
1677cdf0e10cSrcweir 		}
1678cdf0e10cSrcweir 
1679cdf0e10cSrcweir 		return eErrorCode;
1680cdf0e10cSrcweir 	}
1681cdf0e10cSrcweir 	//------------------------------------------------------------------------------
ComparisonPredicate(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw,const::connectivity::OSQLParseNode * pCondition,const sal_uInt16 nLevel,sal_Bool bHaving,bool bAddOrOnOneLine)1682cdf0e10cSrcweir 	SqlParseError ComparisonPredicate(OQueryDesignView* _pView,
1683cdf0e10cSrcweir 							OSelectionBrowseBox* _pSelectionBrw,
1684cdf0e10cSrcweir 							const ::connectivity::OSQLParseNode * pCondition,
1685cdf0e10cSrcweir 							const sal_uInt16 nLevel,
1686cdf0e10cSrcweir 							sal_Bool bHaving
1687cdf0e10cSrcweir                             ,bool bAddOrOnOneLine)
1688cdf0e10cSrcweir 	{
1689cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
1690cdf0e10cSrcweir 		OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
1691cdf0e10cSrcweir 
1692cdf0e10cSrcweir 		DBG_ASSERT(SQL_ISRULE( pCondition, comparison_predicate),"ComparisonPredicate: pCondition ist kein ComparisonPredicate");
1693cdf0e10cSrcweir 		if ( SQL_ISRULE(pCondition->getChild(0), column_ref )
1694cdf0e10cSrcweir 			|| SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref) )
1695cdf0e10cSrcweir 		{
1696cdf0e10cSrcweir 			::rtl::OUString	aCondition;
1697cdf0e10cSrcweir 			OTableFieldDescRef aDragLeft = new OTableFieldDesc();
1698cdf0e10cSrcweir 
1699cdf0e10cSrcweir 			if ( SQL_ISRULE(pCondition->getChild(0), column_ref ) && SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref ) )
1700cdf0e10cSrcweir 			{
1701cdf0e10cSrcweir 				OTableFieldDescRef aDragRight = new OTableFieldDesc();
1702cdf0e10cSrcweir 				if (eOk != ( eErrorCode = FillDragInfo(_pView,pCondition->getChild(0),aDragLeft)) ||
1703cdf0e10cSrcweir 					eOk != ( eErrorCode = FillDragInfo(_pView,pCondition->getChild(2),aDragRight)))
1704cdf0e10cSrcweir 					return eErrorCode;
1705cdf0e10cSrcweir 
1706cdf0e10cSrcweir                 OQueryTableConnection* pConn = static_cast<OQueryTableConnection*>(
1707cdf0e10cSrcweir                                                     _pView->getTableView()->GetTabConn(static_cast<OQueryTableWindow*>(aDragLeft->GetTabWindow()),
1708cdf0e10cSrcweir 																					   static_cast<OQueryTableWindow*>(aDragRight->GetTabWindow()),
1709cdf0e10cSrcweir                                                                                        true));
1710cdf0e10cSrcweir 				if ( pConn )
1711cdf0e10cSrcweir 				{
1712cdf0e10cSrcweir 					OConnectionLineDataVec* pLineDataList = pConn->GetData()->GetConnLineDataList();
1713cdf0e10cSrcweir 					OConnectionLineDataVec::iterator aIter = pLineDataList->begin();
1714cdf0e10cSrcweir                     OConnectionLineDataVec::iterator aEnd = pLineDataList->end();
1715cdf0e10cSrcweir 					for(;aIter != aEnd;++aIter)
1716cdf0e10cSrcweir 					{
1717cdf0e10cSrcweir 						if((*aIter)->GetSourceFieldName() == aDragLeft->GetField() ||
1718cdf0e10cSrcweir 						   (*aIter)->GetDestFieldName() == aDragLeft->GetField() )
1719cdf0e10cSrcweir 							break;
1720cdf0e10cSrcweir 					}
1721cdf0e10cSrcweir 					if(aIter != aEnd)
1722cdf0e10cSrcweir 						return eOk;
1723cdf0e10cSrcweir 				}
1724cdf0e10cSrcweir 			}
1725cdf0e10cSrcweir 
1726cdf0e10cSrcweir 			sal_uInt32 nPos = 0;
1727cdf0e10cSrcweir 			if(SQL_ISRULE(pCondition->getChild(0), column_ref ))
1728cdf0e10cSrcweir 			{
1729cdf0e10cSrcweir 				nPos = 0;
1730cdf0e10cSrcweir 				sal_uInt32 i=1;
1731cdf0e10cSrcweir 
1732cdf0e10cSrcweir 				// don't display the equal
1733cdf0e10cSrcweir 				if (pCondition->getChild(i)->getNodeType() == SQL_NODE_EQUAL)
1734cdf0e10cSrcweir 					i++;
1735cdf0e10cSrcweir 
1736cdf0e10cSrcweir 				// Bedingung parsen
1737cdf0e10cSrcweir 				aCondition = ParseCondition(rController
1738cdf0e10cSrcweir 											,pCondition
1739cdf0e10cSrcweir 											,_pView->getDecimalSeparator()
1740cdf0e10cSrcweir 											,_pView->getLocale()
1741cdf0e10cSrcweir 											,i);
1742cdf0e10cSrcweir 			}
1743cdf0e10cSrcweir 			else if( SQL_ISRULE(pCondition->getChild(pCondition->count()-1), column_ref ) )
1744cdf0e10cSrcweir 			{
1745cdf0e10cSrcweir 				nPos = pCondition->count()-1;
1746cdf0e10cSrcweir 
1747cdf0e10cSrcweir 				sal_Int32 i = static_cast<sal_Int32>(pCondition->count() - 2);
1748cdf0e10cSrcweir 				switch (pCondition->getChild(i)->getNodeType())
1749cdf0e10cSrcweir 				{
1750cdf0e10cSrcweir 					case SQL_NODE_EQUAL:
1751cdf0e10cSrcweir 						// don't display the equal
1752cdf0e10cSrcweir 						i--;
1753cdf0e10cSrcweir 						break;
1754cdf0e10cSrcweir 					case SQL_NODE_LESS:
1755cdf0e10cSrcweir 						// take the opposite as we change the order
1756cdf0e10cSrcweir 						i--;
1757cdf0e10cSrcweir 						aCondition = aCondition + ::rtl::OUString::createFromAscii(">");
1758cdf0e10cSrcweir 						break;
1759cdf0e10cSrcweir 					case SQL_NODE_LESSEQ:
1760cdf0e10cSrcweir 						// take the opposite as we change the order
1761cdf0e10cSrcweir 						i--;
1762cdf0e10cSrcweir 						aCondition = aCondition + ::rtl::OUString::createFromAscii(">=");
1763cdf0e10cSrcweir 						break;
1764cdf0e10cSrcweir 					case SQL_NODE_GREAT:
1765cdf0e10cSrcweir 						// take the opposite as we change the order
1766cdf0e10cSrcweir 						i--;
1767cdf0e10cSrcweir 						aCondition = aCondition + ::rtl::OUString::createFromAscii("<");
1768cdf0e10cSrcweir 						break;
1769cdf0e10cSrcweir 					case SQL_NODE_GREATEQ:
1770cdf0e10cSrcweir 						// take the opposite as we change the order
1771cdf0e10cSrcweir 						i--;
1772cdf0e10cSrcweir 						aCondition = aCondition + ::rtl::OUString::createFromAscii("<=");
1773cdf0e10cSrcweir 						break;
1774cdf0e10cSrcweir                     default:
1775cdf0e10cSrcweir                         break;
1776cdf0e10cSrcweir 				}
1777cdf0e10cSrcweir 
1778cdf0e10cSrcweir 				// go backward
1779cdf0e10cSrcweir 				Reference< XConnection> xConnection = rController.getConnection();
1780cdf0e10cSrcweir 				if(xConnection.is())
1781cdf0e10cSrcweir 				{
1782cdf0e10cSrcweir 					Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
1783cdf0e10cSrcweir 					for (; i >= 0; i--)
1784cdf0e10cSrcweir 						pCondition->getChild(i)->parseNodeToPredicateStr(aCondition,
1785cdf0e10cSrcweir 												xConnection,
1786cdf0e10cSrcweir 												rController.getNumberFormatter(),
1787cdf0e10cSrcweir 												_pView->getLocale(),
1788cdf0e10cSrcweir 												static_cast<sal_Char>(_pView->getDecimalSeparator().toChar()),
1789cdf0e10cSrcweir 												&rController.getParser().getContext());
1790cdf0e10cSrcweir 				}
1791cdf0e10cSrcweir 			}
1792cdf0e10cSrcweir 			// else ???
1793cdf0e10cSrcweir 
1794cdf0e10cSrcweir 
1795cdf0e10cSrcweir 			if( eOk == ( eErrorCode = FillDragInfo(_pView,pCondition->getChild(nPos),aDragLeft)))
1796cdf0e10cSrcweir 			{
1797cdf0e10cSrcweir 				if(bHaving)
1798cdf0e10cSrcweir 					aDragLeft->SetGroupBy(sal_True);
1799cdf0e10cSrcweir 				_pSelectionBrw->AddCondition(aDragLeft, aCondition, nLevel,bAddOrOnOneLine);
1800cdf0e10cSrcweir 			}
1801cdf0e10cSrcweir 		}
1802cdf0e10cSrcweir 		else if( SQL_ISRULEOR2(pCondition->getChild(0), set_fct_spec , general_set_fct ) )
1803cdf0e10cSrcweir 		{
1804cdf0e10cSrcweir 			AddFunctionCondition(	_pView,
1805cdf0e10cSrcweir 									_pSelectionBrw,
1806cdf0e10cSrcweir 									pCondition,
1807cdf0e10cSrcweir 									nLevel,
1808cdf0e10cSrcweir 									bHaving,
1809cdf0e10cSrcweir                                     bAddOrOnOneLine);
1810cdf0e10cSrcweir 		}
1811cdf0e10cSrcweir 		else // kann sich nur um einen Expr. Ausdruck handeln
1812cdf0e10cSrcweir 		{
1813cdf0e10cSrcweir 			::rtl::OUString aName,aCondition;
1814cdf0e10cSrcweir 
1815cdf0e10cSrcweir 			::connectivity::OSQLParseNode *pLhs = pCondition->getChild(0);
1816cdf0e10cSrcweir 			::connectivity::OSQLParseNode *pRhs = pCondition->getChild(2);
1817cdf0e10cSrcweir 			// Feldnamen
1818cdf0e10cSrcweir 			Reference< XConnection> xConnection = rController.getConnection();
1819cdf0e10cSrcweir 			if(xConnection.is())
1820cdf0e10cSrcweir 			{
1821cdf0e10cSrcweir 				pLhs->parseNodeToStr(aName,
1822cdf0e10cSrcweir 									 xConnection,
1823cdf0e10cSrcweir 									 &rController.getParser().getContext(),
1824cdf0e10cSrcweir 									 sal_True);
1825cdf0e10cSrcweir 				// Kriterium
1826cdf0e10cSrcweir 				aCondition = pCondition->getChild(1)->getTokenValue();
1827cdf0e10cSrcweir 				pRhs->parseNodeToPredicateStr(aCondition,
1828cdf0e10cSrcweir 															xConnection,
1829cdf0e10cSrcweir 															rController.getNumberFormatter(),
1830cdf0e10cSrcweir 															_pView->getLocale(),
1831cdf0e10cSrcweir 															static_cast<sal_Char>(_pView->getDecimalSeparator().toChar()),
1832cdf0e10cSrcweir 															&rController.getParser().getContext());
1833cdf0e10cSrcweir 			}
1834cdf0e10cSrcweir 
1835cdf0e10cSrcweir 			OTableFieldDescRef aDragLeft = new OTableFieldDesc();
1836cdf0e10cSrcweir 			aDragLeft->SetField(aName);
1837cdf0e10cSrcweir 			aDragLeft->SetFunctionType(FKT_OTHER|FKT_NUMERIC);
1838cdf0e10cSrcweir 			// und anh"angen
1839cdf0e10cSrcweir 			_pSelectionBrw->AddCondition(aDragLeft, aCondition, nLevel,bAddOrOnOneLine);
1840cdf0e10cSrcweir 		}
1841cdf0e10cSrcweir 		return eErrorCode;
1842cdf0e10cSrcweir 	}
1843cdf0e10cSrcweir 
1844cdf0e10cSrcweir     //------------------------------------------------------------------------------
1845cdf0e10cSrcweir     namespace
1846cdf0e10cSrcweir     {
lcl_findColumnInTables(const::rtl::OUString & _rColumName,const OJoinTableView::OTableWindowMap & _rTabList,OTableFieldDescRef & _rInfo)1847cdf0e10cSrcweir         OQueryTableWindow* lcl_findColumnInTables( const ::rtl::OUString& _rColumName, const OJoinTableView::OTableWindowMap& _rTabList, OTableFieldDescRef& _rInfo )
1848cdf0e10cSrcweir         {
1849cdf0e10cSrcweir 			OJoinTableView::OTableWindowMap::const_iterator aIter = _rTabList.begin();
1850cdf0e10cSrcweir             OJoinTableView::OTableWindowMap::const_iterator aEnd = _rTabList.end();
1851cdf0e10cSrcweir 			for ( ; aIter != aEnd; ++aIter )
1852cdf0e10cSrcweir 			{
1853cdf0e10cSrcweir 				OQueryTableWindow* pTabWin = static_cast< OQueryTableWindow* >( aIter->second );
1854cdf0e10cSrcweir 				if ( pTabWin && pTabWin->ExistsField( _rColumName, _rInfo ) )
1855cdf0e10cSrcweir 					return pTabWin;
1856cdf0e10cSrcweir 			}
1857cdf0e10cSrcweir             return NULL;
1858cdf0e10cSrcweir         }
1859cdf0e10cSrcweir     }
1860cdf0e10cSrcweir 
1861cdf0e10cSrcweir     //------------------------------------------------------------------------------
InsertColumnRef(const OQueryDesignView * _pView,const::connectivity::OSQLParseNode * pColumnRef,::rtl::OUString & aColumnName,const::rtl::OUString & aColumnAlias,::rtl::OUString & aTableRange,OTableFieldDescRef & _raInfo,OJoinTableView::OTableWindowMap * pTabList)1862cdf0e10cSrcweir 	void InsertColumnRef(const OQueryDesignView* _pView,
1863cdf0e10cSrcweir 						const ::connectivity::OSQLParseNode * pColumnRef,
1864cdf0e10cSrcweir 						::rtl::OUString& aColumnName,
1865cdf0e10cSrcweir 						const ::rtl::OUString& aColumnAlias,
1866cdf0e10cSrcweir 						::rtl::OUString& aTableRange,
1867cdf0e10cSrcweir 						OTableFieldDescRef& _raInfo,
1868cdf0e10cSrcweir 						OJoinTableView::OTableWindowMap* pTabList)
1869cdf0e10cSrcweir 	{
1870cdf0e10cSrcweir 
1871cdf0e10cSrcweir 		// Tabellennamen zusammen setzen
1872cdf0e10cSrcweir 		::connectivity::OSQLParseTreeIterator& rParseIter = static_cast<OQueryController&>(_pView->getController()).getParseIterator();
1873cdf0e10cSrcweir 		rParseIter.getColumnRange( pColumnRef, aColumnName, aTableRange );
1874cdf0e10cSrcweir 
1875cdf0e10cSrcweir 		sal_Bool bFound(sal_False);
1876cdf0e10cSrcweir 		DBG_ASSERT(aColumnName.getLength(),"Columnname darf nicht leer sein");
1877cdf0e10cSrcweir 		if (!aTableRange.getLength())
1878cdf0e10cSrcweir 		{
1879cdf0e10cSrcweir 			// SELECT column, ...
1880cdf0e10cSrcweir             bFound = NULL != lcl_findColumnInTables( aColumnName, *pTabList, _raInfo );
1881cdf0e10cSrcweir 			if ( bFound && ( aColumnName.toChar() != '*' ) )
1882cdf0e10cSrcweir 				_raInfo->SetFieldAlias(aColumnAlias);
1883cdf0e10cSrcweir 		}
1884cdf0e10cSrcweir 		else
1885cdf0e10cSrcweir 		{
1886cdf0e10cSrcweir 			// SELECT range.column, ...
1887cdf0e10cSrcweir 			OQueryTableWindow* pTabWin = static_cast<OQueryTableView*>(_pView->getTableView())->FindTable(aTableRange);
1888cdf0e10cSrcweir 
1889cdf0e10cSrcweir 			if (pTabWin && pTabWin->ExistsField(aColumnName, _raInfo))
1890cdf0e10cSrcweir 			{
1891cdf0e10cSrcweir 				if(aColumnName.toChar() != '*')
1892cdf0e10cSrcweir 					_raInfo->SetFieldAlias(aColumnAlias);
1893cdf0e10cSrcweir 				bFound = sal_True;
1894cdf0e10cSrcweir 			}
1895cdf0e10cSrcweir 		}
1896cdf0e10cSrcweir 		if (!bFound)
1897cdf0e10cSrcweir 		{
1898cdf0e10cSrcweir 			_raInfo->SetTable(::rtl::OUString());
1899cdf0e10cSrcweir 			_raInfo->SetAlias(::rtl::OUString());
1900cdf0e10cSrcweir 			_raInfo->SetField(aColumnName);
1901cdf0e10cSrcweir 			_raInfo->SetFieldAlias(aColumnAlias);	// nyi : hier ein fortlaufendes Expr_1, Expr_2 ...
1902cdf0e10cSrcweir 			_raInfo->SetFunctionType(FKT_OTHER);
1903cdf0e10cSrcweir 		}
1904cdf0e10cSrcweir 	}
1905cdf0e10cSrcweir 	//-----------------------------------------------------------------------------
checkJoinConditions(const OQueryDesignView * _pView,const::connectivity::OSQLParseNode * _pNode)1906cdf0e10cSrcweir 	sal_Bool checkJoinConditions(	const OQueryDesignView* _pView,
1907cdf0e10cSrcweir 									const ::connectivity::OSQLParseNode* _pNode )
1908cdf0e10cSrcweir 	{
1909cdf0e10cSrcweir 		const ::connectivity::OSQLParseNode* pJoinNode = NULL;
1910cdf0e10cSrcweir 		sal_Bool bRet = sal_True;
1911cdf0e10cSrcweir 		if (SQL_ISRULE(_pNode,qualified_join))
1912cdf0e10cSrcweir 			pJoinNode = _pNode;
1913cdf0e10cSrcweir         else if (SQL_ISRULE(_pNode,table_ref)
1914cdf0e10cSrcweir                 &&  _pNode->count() == 3
1915cdf0e10cSrcweir                 &&  SQL_ISPUNCTUATION(_pNode->getChild(0),"(")
1916cdf0e10cSrcweir                 &&  SQL_ISPUNCTUATION(_pNode->getChild(2),")") ) // '(' joined_table ')'
1917cdf0e10cSrcweir 			pJoinNode = _pNode->getChild(1);
1918cdf0e10cSrcweir 		else if (! ( SQL_ISRULE(_pNode, table_ref) && _pNode->count() == 2) ) // table_node table_primary_as_range_column
1919cdf0e10cSrcweir 			bRet = sal_False;
1920cdf0e10cSrcweir 
1921cdf0e10cSrcweir 		if (pJoinNode && !InsertJoin(_pView,pJoinNode))
1922cdf0e10cSrcweir 			bRet = sal_False;
1923cdf0e10cSrcweir 		return bRet;
1924cdf0e10cSrcweir 	}
1925cdf0e10cSrcweir 	//-----------------------------------------------------------------------------
InsertJoin(const OQueryDesignView * _pView,const::connectivity::OSQLParseNode * pNode)1926cdf0e10cSrcweir 	sal_Bool InsertJoin(const OQueryDesignView* _pView,
1927cdf0e10cSrcweir 						const ::connectivity::OSQLParseNode *pNode)
1928cdf0e10cSrcweir 	{
1929cdf0e10cSrcweir 		DBG_ASSERT( SQL_ISRULE( pNode, qualified_join ) || SQL_ISRULE( pNode, joined_table ) || SQL_ISRULE( pNode, cross_union ),
1930cdf0e10cSrcweir 			"OQueryDesignView::InsertJoin: Fehler im Parse Tree");
1931cdf0e10cSrcweir 
1932cdf0e10cSrcweir         if (SQL_ISRULE(pNode,joined_table))
1933cdf0e10cSrcweir             return InsertJoin(_pView,pNode->getChild(1));
1934cdf0e10cSrcweir 
1935cdf0e10cSrcweir 		// first check the left and right side
1936cdf0e10cSrcweir         const ::connectivity::OSQLParseNode* pRightTableRef = pNode->getChild(3); // table_ref
1937cdf0e10cSrcweir         if ( SQL_ISRULE(pNode, qualified_join) && SQL_ISTOKEN(pNode->getChild(1),NATURAL) )
1938cdf0e10cSrcweir             pRightTableRef = pNode->getChild(4); // table_ref
1939cdf0e10cSrcweir 
1940cdf0e10cSrcweir 		if ( !checkJoinConditions(_pView,pNode->getChild(0)) || !checkJoinConditions(_pView,pRightTableRef))
1941cdf0e10cSrcweir 			return sal_False;
1942cdf0e10cSrcweir 
1943cdf0e10cSrcweir 		// named column join wird sp�ter vieleicht noch implementiert
1944cdf0e10cSrcweir 		// SQL_ISRULE(pNode->getChild(4),named_columns_join)
1945cdf0e10cSrcweir         EJoinType eJoinType = INNER_JOIN;
1946cdf0e10cSrcweir         bool bNatural = false;
1947cdf0e10cSrcweir 		if ( SQL_ISRULE(pNode, qualified_join) )
1948cdf0e10cSrcweir 		{
1949cdf0e10cSrcweir 			::connectivity::OSQLParseNode* pJoinType = pNode->getChild(1); // join_type
1950cdf0e10cSrcweir             if ( SQL_ISTOKEN(pJoinType,NATURAL) )
1951cdf0e10cSrcweir             {
1952cdf0e10cSrcweir                 bNatural = true;
1953cdf0e10cSrcweir                 pJoinType = pNode->getChild(2);
1954cdf0e10cSrcweir             }
1955cdf0e10cSrcweir 
1956cdf0e10cSrcweir 			if (SQL_ISRULE(pJoinType,join_type) && (!pJoinType->count() || SQL_ISTOKEN(pJoinType->getChild(0),INNER)))
1957cdf0e10cSrcweir 			{
1958cdf0e10cSrcweir 				eJoinType = INNER_JOIN;
1959cdf0e10cSrcweir 			}
1960cdf0e10cSrcweir 			else
1961cdf0e10cSrcweir 			{
1962cdf0e10cSrcweir 				if (SQL_ISRULE(pJoinType,join_type))	   // eine Ebene tiefer
1963cdf0e10cSrcweir 					pJoinType = pJoinType->getChild(0);
1964cdf0e10cSrcweir 
1965cdf0e10cSrcweir 				if (SQL_ISTOKEN(pJoinType->getChild(0),LEFT))
1966cdf0e10cSrcweir 					eJoinType = LEFT_JOIN;
1967cdf0e10cSrcweir 				else if(SQL_ISTOKEN(pJoinType->getChild(0),RIGHT))
1968cdf0e10cSrcweir 					eJoinType = RIGHT_JOIN;
1969cdf0e10cSrcweir 				else
1970cdf0e10cSrcweir 					eJoinType = FULL_JOIN;
1971cdf0e10cSrcweir 			}
1972cdf0e10cSrcweir             if ( SQL_ISRULE(pNode->getChild(4),join_condition) )
1973cdf0e10cSrcweir             {
1974cdf0e10cSrcweir 			    if ( InsertJoinConnection(_pView,pNode->getChild(4)->getChild(1), eJoinType,pNode->getChild(0),pRightTableRef) != eOk )
1975cdf0e10cSrcweir 				    return sal_False;
1976cdf0e10cSrcweir             }
1977cdf0e10cSrcweir 		}
1978cdf0e10cSrcweir         else if ( SQL_ISRULE(pNode, cross_union) )
1979cdf0e10cSrcweir         {
1980cdf0e10cSrcweir             eJoinType = CROSS_JOIN;
1981cdf0e10cSrcweir             pRightTableRef = pNode->getChild(pNode->count() - 1);
1982cdf0e10cSrcweir         }
1983cdf0e10cSrcweir         else
1984cdf0e10cSrcweir 			return sal_False;
1985cdf0e10cSrcweir 
1986cdf0e10cSrcweir         if ( eJoinType == CROSS_JOIN || bNatural )
1987cdf0e10cSrcweir         {
1988cdf0e10cSrcweir 
1989cdf0e10cSrcweir             OQueryTableWindow*	pLeftWindow = static_cast<OQueryTableView*>(_pView->getTableView())->FindTable( getTableRange(_pView,pNode->getChild(0)) );
1990cdf0e10cSrcweir             OQueryTableWindow*	pRightWindow = static_cast<OQueryTableView*>(_pView->getTableView())->FindTable( getTableRange(_pView,pRightTableRef) );
1991cdf0e10cSrcweir             OSL_ENSURE(pLeftWindow && pRightWindow,"Table Windows could not be found!");
1992cdf0e10cSrcweir             if ( !pLeftWindow || !pRightWindow )
1993cdf0e10cSrcweir                 return sal_False;
1994cdf0e10cSrcweir 
1995cdf0e10cSrcweir             OTableFieldDescRef aDragLeft  = new OTableFieldDesc();
1996cdf0e10cSrcweir             aDragLeft->SetTabWindow(pLeftWindow);
1997cdf0e10cSrcweir             aDragLeft->SetTable(pLeftWindow->GetTableName());
1998cdf0e10cSrcweir             aDragLeft->SetAlias(pLeftWindow->GetAliasName());
1999cdf0e10cSrcweir 
2000cdf0e10cSrcweir 			OTableFieldDescRef aDragRight = new OTableFieldDesc();
2001cdf0e10cSrcweir             aDragRight->SetTabWindow(pRightWindow);
2002cdf0e10cSrcweir             aDragRight->SetTable(pRightWindow->GetTableName());
2003cdf0e10cSrcweir             aDragRight->SetAlias(pRightWindow->GetAliasName());
2004cdf0e10cSrcweir 
2005cdf0e10cSrcweir             insertConnection(_pView,eJoinType,aDragLeft,aDragRight,bNatural);
2006cdf0e10cSrcweir         }
2007cdf0e10cSrcweir 
2008cdf0e10cSrcweir 
2009cdf0e10cSrcweir 		return sal_True;
2010cdf0e10cSrcweir 	}
2011cdf0e10cSrcweir 	//------------------------------------------------------------------------------
insertUnUsedFields(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw)2012cdf0e10cSrcweir 	void insertUnUsedFields(OQueryDesignView* _pView,OSelectionBrowseBox* _pSelectionBrw)
2013cdf0e10cSrcweir 	{
2014cdf0e10cSrcweir 		// now we have to insert the fields which aren't in the statement
2015cdf0e10cSrcweir 		OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
2016cdf0e10cSrcweir 		OTableFields& rUnUsedFields = rController.getUnUsedFields();
2017cdf0e10cSrcweir         OTableFields::iterator aEnd = rUnUsedFields.end();
2018cdf0e10cSrcweir 		for(OTableFields::iterator aIter = rUnUsedFields.begin();aIter != aEnd;++aIter)
2019cdf0e10cSrcweir 			if(_pSelectionBrw->InsertField(*aIter,BROWSER_INVALIDID,sal_False,sal_False).isValid())
2020cdf0e10cSrcweir 				(*aIter) = NULL;
2021cdf0e10cSrcweir 		OTableFields().swap( rUnUsedFields );
2022cdf0e10cSrcweir 	}
2023cdf0e10cSrcweir 
2024cdf0e10cSrcweir 	//------------------------------------------------------------------------------
InitFromParseNodeImpl(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw)2025cdf0e10cSrcweir 	SqlParseError InitFromParseNodeImpl(OQueryDesignView* _pView,OSelectionBrowseBox* _pSelectionBrw)
2026cdf0e10cSrcweir 	{
2027cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
2028cdf0e10cSrcweir 
2029cdf0e10cSrcweir 		OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
2030cdf0e10cSrcweir 
2031cdf0e10cSrcweir 		_pSelectionBrw->PreFill();
2032cdf0e10cSrcweir 		_pSelectionBrw->SetReadOnly(rController.isReadOnly());
2033cdf0e10cSrcweir 		_pSelectionBrw->Fill();
2034cdf0e10cSrcweir 
2035cdf0e10cSrcweir 
2036cdf0e10cSrcweir 		::connectivity::OSQLParseTreeIterator& aIterator = rController.getParseIterator();
2037cdf0e10cSrcweir 		const ::connectivity::OSQLParseNode* pParseTree = aIterator.getParseTree();
2038cdf0e10cSrcweir 
2039cdf0e10cSrcweir         do
2040cdf0e10cSrcweir         {
2041cdf0e10cSrcweir             if ( !pParseTree )
2042cdf0e10cSrcweir             {
2043cdf0e10cSrcweir 			    // now we have to insert the fields which aren't in the statement
2044cdf0e10cSrcweir 			    insertUnUsedFields(_pView,_pSelectionBrw);
2045cdf0e10cSrcweir                 break;
2046cdf0e10cSrcweir             }
2047cdf0e10cSrcweir 
2048cdf0e10cSrcweir             if ( !rController.isEsacpeProcessing() ) // not allowed in this mode
2049cdf0e10cSrcweir             {
2050cdf0e10cSrcweir 			    eErrorCode = eNativeMode;
2051cdf0e10cSrcweir                 break;
2052cdf0e10cSrcweir             }
2053cdf0e10cSrcweir 
2054cdf0e10cSrcweir 			if ( !( SQL_ISRULE( pParseTree, select_statement ) ) )
2055cdf0e10cSrcweir             {
2056cdf0e10cSrcweir 				eErrorCode = eNoSelectStatement;
2057cdf0e10cSrcweir                 break;
2058cdf0e10cSrcweir             }
2059cdf0e10cSrcweir 
2060cdf0e10cSrcweir             const OSQLParseNode* pTableExp = pParseTree->getChild(3);
2061cdf0e10cSrcweir             if ( pTableExp->getChild(6)->count() > 0 || pTableExp->getChild(7)->count() > 0 || pTableExp->getChild(8)->count() > 0)
2062cdf0e10cSrcweir             {
2063cdf0e10cSrcweir 				eErrorCode = eStatementTooComplex;
2064cdf0e10cSrcweir                 break;
2065cdf0e10cSrcweir             }
2066cdf0e10cSrcweir 
2067cdf0e10cSrcweir 			Reference< XConnection> xConnection = rController.getConnection();
2068cdf0e10cSrcweir 			if ( !xConnection.is() )
2069cdf0e10cSrcweir             {
2070cdf0e10cSrcweir                 DBG_ERROR( "InitFromParseNodeImpl: no connection? no connection!" );
2071cdf0e10cSrcweir                 break;
2072cdf0e10cSrcweir             }
2073cdf0e10cSrcweir 
2074cdf0e10cSrcweir 			const OSQLTables& aMap = aIterator.getTables();
2075cdf0e10cSrcweir 			::comphelper::UStringMixLess aTmp(aMap.key_comp());
2076cdf0e10cSrcweir 			::comphelper::UStringMixEqual aKeyComp( aTmp.isCaseSensitive() );
2077cdf0e10cSrcweir 
2078cdf0e10cSrcweir 			Reference< XDatabaseMetaData >  xMetaData = xConnection->getMetaData();
2079cdf0e10cSrcweir 			try
2080cdf0e10cSrcweir 			{
2081cdf0e10cSrcweir 			    sal_Int32 nMax = xMetaData->getMaxTablesInSelect();
2082cdf0e10cSrcweir                 if ( nMax && nMax < (sal_Int32)aMap.size() )
2083cdf0e10cSrcweir                 {
2084cdf0e10cSrcweir 					eErrorCode = eTooManyTables;
2085cdf0e10cSrcweir                     break;
2086cdf0e10cSrcweir                 }
2087cdf0e10cSrcweir 
2088cdf0e10cSrcweir 				::rtl::OUString sComposedName;
2089cdf0e10cSrcweir 				::rtl::OUString aQualifierName;
2090cdf0e10cSrcweir 				::rtl::OUString sAlias;
2091cdf0e10cSrcweir 
2092cdf0e10cSrcweir 				OQueryTableView* pTableView = static_cast<OQueryTableView*>(_pView->getTableView());
2093cdf0e10cSrcweir                 pTableView->clearLayoutInformation();
2094cdf0e10cSrcweir 				OSQLTables::const_iterator aIter = aMap.begin();
2095cdf0e10cSrcweir                 OSQLTables::const_iterator aEnd = aMap.end();
2096cdf0e10cSrcweir 				for(;aIter != aEnd;++aIter)
2097cdf0e10cSrcweir 				{
2098cdf0e10cSrcweir 					OSQLTable xTable = aIter->second;
2099cdf0e10cSrcweir                     Reference< XPropertySet > xTableProps( xTable, UNO_QUERY_THROW );
2100cdf0e10cSrcweir 
2101cdf0e10cSrcweir 					sAlias = aIter->first;
2102cdf0e10cSrcweir 
2103cdf0e10cSrcweir                     // check whether this is a query
2104cdf0e10cSrcweir                     Reference< XPropertySetInfo > xPSI = xTableProps->getPropertySetInfo();
2105cdf0e10cSrcweir                     bool bIsQuery = xPSI.is() && xPSI->hasPropertyByName( PROPERTY_COMMAND );
2106cdf0e10cSrcweir 
2107cdf0e10cSrcweir                     if ( bIsQuery )
2108cdf0e10cSrcweir                         OSL_VERIFY( xTableProps->getPropertyValue( PROPERTY_NAME ) >>= sComposedName );
2109cdf0e10cSrcweir                     else
2110cdf0e10cSrcweir                     {
2111cdf0e10cSrcweir                         sComposedName = ::dbtools::composeTableName( xMetaData, xTableProps, ::dbtools::eInDataManipulation, false, false, false );
2112cdf0e10cSrcweir 
2113cdf0e10cSrcweir                         // if the alias is the complete (composed) table, then shorten it
2114cdf0e10cSrcweir                         if ( aKeyComp( sComposedName, aIter->first ) )
2115cdf0e10cSrcweir 					    {
2116cdf0e10cSrcweir 						    ::rtl::OUString sCatalog, sSchema, sTable;
2117cdf0e10cSrcweir 						    ::dbtools::qualifiedNameComponents( xMetaData, sComposedName, sCatalog, sSchema, sTable, ::dbtools::eInDataManipulation );
2118cdf0e10cSrcweir 						    sAlias = sTable;
2119cdf0e10cSrcweir 					    }
2120cdf0e10cSrcweir                     }
2121cdf0e10cSrcweir 
2122cdf0e10cSrcweir                     // find the existent window for this alias
2123cdf0e10cSrcweir 					OQueryTableWindow* pExistentWin	= pTableView->FindTable( sAlias );
2124cdf0e10cSrcweir 					if ( !pExistentWin )
2125cdf0e10cSrcweir 					{
2126cdf0e10cSrcweir 						pTableView->AddTabWin( sComposedName, sAlias, sal_False );  // don't create data here
2127cdf0e10cSrcweir 					}
2128cdf0e10cSrcweir 					else
2129cdf0e10cSrcweir 					{
2130cdf0e10cSrcweir 						// there already exists a window for this alias ....
2131cdf0e10cSrcweir 						if ( !aKeyComp( pExistentWin->GetData()->GetComposedName(), sComposedName ) )
2132cdf0e10cSrcweir 							// ... but for another complete table name -> new window
2133cdf0e10cSrcweir 							pTableView->AddTabWin(sComposedName, sAlias);
2134cdf0e10cSrcweir 					}
2135cdf0e10cSrcweir 				}
2136cdf0e10cSrcweir 
2137cdf0e10cSrcweir 				// now delete the data for which we haven't any tablewindow
2138cdf0e10cSrcweir 				OJoinTableView::OTableWindowMap aTableMap(*pTableView->GetTabWinMap());
2139cdf0e10cSrcweir 				OJoinTableView::OTableWindowMap::iterator aIterTableMap = aTableMap.begin();
2140cdf0e10cSrcweir                 OJoinTableView::OTableWindowMap::iterator aIterTableEnd = aTableMap.end();
2141cdf0e10cSrcweir 				for(;aIterTableMap != aIterTableEnd;++aIterTableMap)
2142cdf0e10cSrcweir 				{
2143cdf0e10cSrcweir 					if(aMap.find(aIterTableMap->second->GetComposedName())	== aMap.end() &&
2144cdf0e10cSrcweir 						aMap.find(aIterTableMap->first)						== aMap.end())
2145cdf0e10cSrcweir 						pTableView->RemoveTabWin(aIterTableMap->second);
2146cdf0e10cSrcweir 				}
2147cdf0e10cSrcweir 
2148cdf0e10cSrcweir 				if ( eOk == (eErrorCode = FillOuterJoins(_pView,pTableExp->getChild(0)->getChild(1))) )
2149cdf0e10cSrcweir 				{
2150cdf0e10cSrcweir 					// check if we have a distinct statement
2151cdf0e10cSrcweir 					if(SQL_ISTOKEN(pParseTree->getChild(1),DISTINCT))
2152cdf0e10cSrcweir 					{
2153cdf0e10cSrcweir 						rController.setDistinct(sal_True);
2154cdf0e10cSrcweir 						rController.InvalidateFeature(SID_QUERY_DISTINCT_VALUES);
2155cdf0e10cSrcweir 					}
2156cdf0e10cSrcweir 					if ( (eErrorCode = InstallFields(_pView,pParseTree, pTableView->GetTabWinMap())) == eOk )
2157cdf0e10cSrcweir 					{
2158cdf0e10cSrcweir 						// GetSelectionCriteria must be called before GetHavingCriteria
2159cdf0e10cSrcweir 						sal_uInt16 nLevel=0;
2160cdf0e10cSrcweir 
2161cdf0e10cSrcweir 						if ( eOk == (eErrorCode = GetSelectionCriteria(_pView,_pSelectionBrw,pParseTree,nLevel)) )
2162cdf0e10cSrcweir 						{
2163cdf0e10cSrcweir 							if ( eOk == (eErrorCode = GetGroupCriteria(_pView,_pSelectionBrw,pParseTree)) )
2164cdf0e10cSrcweir 							{
2165cdf0e10cSrcweir 								if ( eOk == (eErrorCode = GetHavingCriteria(_pView,_pSelectionBrw,pParseTree,nLevel)) )
2166cdf0e10cSrcweir 								{
2167cdf0e10cSrcweir 									if ( eOk == (eErrorCode = GetOrderCriteria(_pView,_pSelectionBrw,pParseTree)) )
2168cdf0e10cSrcweir 										insertUnUsedFields(_pView,_pSelectionBrw);
2169cdf0e10cSrcweir 								}
2170cdf0e10cSrcweir 							}
2171cdf0e10cSrcweir 						}
2172cdf0e10cSrcweir 					}
2173cdf0e10cSrcweir 				}
2174cdf0e10cSrcweir 			}
2175cdf0e10cSrcweir 			catch(SQLException&)
2176cdf0e10cSrcweir 			{
2177cdf0e10cSrcweir 				OSL_ASSERT(!"getMaxTablesInSelect!");
2178cdf0e10cSrcweir 			}
2179cdf0e10cSrcweir 		}
2180cdf0e10cSrcweir         while ( false );
2181cdf0e10cSrcweir 
2182cdf0e10cSrcweir 		// Durch das Neuerzeugen wurden wieder Undo-Actions in den Manager gestellt
2183cdf0e10cSrcweir 		rController.ClearUndoManager();
2184cdf0e10cSrcweir 		_pSelectionBrw->Invalidate();
2185cdf0e10cSrcweir 		return eErrorCode;
2186cdf0e10cSrcweir 	}
2187cdf0e10cSrcweir 	//------------------------------------------------------------------------------
2188cdf0e10cSrcweir 	/** fillSelectSubList
2189cdf0e10cSrcweir 		@return
2190cdf0e10cSrcweir 			<TRUE/> when columns could be inserted otherwise <FALSE/>
2191cdf0e10cSrcweir 	*/
2192cdf0e10cSrcweir 	//------------------------------------------------------------------------------
fillSelectSubList(OQueryDesignView * _pView,OJoinTableView::OTableWindowMap * _pTabList)2193cdf0e10cSrcweir 	SqlParseError fillSelectSubList(	OQueryDesignView* _pView,
2194cdf0e10cSrcweir 								OJoinTableView::OTableWindowMap* _pTabList)
2195cdf0e10cSrcweir 	{
2196cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
2197cdf0e10cSrcweir 		sal_Bool bFirstField = sal_True;
2198cdf0e10cSrcweir 		::rtl::OUString sAsterix(RTL_CONSTASCII_USTRINGPARAM("*"));
2199cdf0e10cSrcweir 		OJoinTableView::OTableWindowMap::iterator aIter = _pTabList->begin();
2200cdf0e10cSrcweir         OJoinTableView::OTableWindowMap::iterator aEnd = _pTabList->end();
2201cdf0e10cSrcweir 		for(;aIter != aEnd && eOk == eErrorCode ;++aIter)
2202cdf0e10cSrcweir 		{
2203cdf0e10cSrcweir 			OQueryTableWindow* pTabWin = static_cast<OQueryTableWindow*>(aIter->second);
2204cdf0e10cSrcweir             OTableFieldDescRef	aInfo = new OTableFieldDesc();
2205cdf0e10cSrcweir 			if (pTabWin->ExistsField( sAsterix, aInfo ))
2206cdf0e10cSrcweir 			{
2207cdf0e10cSrcweir 				eErrorCode = _pView->InsertField(aInfo, sal_True, bFirstField);
2208cdf0e10cSrcweir 				bFirstField = sal_False;
2209cdf0e10cSrcweir 			}
2210cdf0e10cSrcweir 		}
2211cdf0e10cSrcweir 		return eErrorCode;
2212cdf0e10cSrcweir 	}
2213cdf0e10cSrcweir 	//------------------------------------------------------------------------------
InstallFields(OQueryDesignView * _pView,const::connectivity::OSQLParseNode * pNode,OJoinTableView::OTableWindowMap * pTabList)2214cdf0e10cSrcweir 	SqlParseError InstallFields(OQueryDesignView* _pView,
2215cdf0e10cSrcweir 								const ::connectivity::OSQLParseNode* pNode,
2216cdf0e10cSrcweir 								OJoinTableView::OTableWindowMap* pTabList )
2217cdf0e10cSrcweir 	{
2218cdf0e10cSrcweir 		if( pNode==0 || !SQL_ISRULE(pNode,select_statement))
2219cdf0e10cSrcweir 			return eNoSelectStatement;
2220cdf0e10cSrcweir 
2221cdf0e10cSrcweir 		::connectivity::OSQLParseNode* pParseTree = pNode->getChild(2); // selection
2222cdf0e10cSrcweir 		sal_Bool bFirstField = sal_True;	// bei der Initialisierung mu� auf alle Faelle das erste Feld neu aktiviert werden
2223cdf0e10cSrcweir 
2224cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
2225cdf0e10cSrcweir 
2226cdf0e10cSrcweir 		if ( pParseTree->isRule() && SQL_ISPUNCTUATION(pParseTree->getChild(0),"*") )
2227cdf0e10cSrcweir 		{
2228cdf0e10cSrcweir 			// SELECT * ...
2229cdf0e10cSrcweir 			eErrorCode = fillSelectSubList(_pView,pTabList);
2230cdf0e10cSrcweir 		}
2231cdf0e10cSrcweir 		else if (SQL_ISRULE(pParseTree,scalar_exp_commalist) )
2232cdf0e10cSrcweir 		{
2233cdf0e10cSrcweir 			// SELECT column, ...
2234cdf0e10cSrcweir 			OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
2235cdf0e10cSrcweir 			Reference< XConnection> xConnection = rController.getConnection();
2236cdf0e10cSrcweir 
2237cdf0e10cSrcweir 			::rtl::OUString aColumnName,aTableRange;
2238cdf0e10cSrcweir 			for (sal_uInt32 i = 0; i < pParseTree->count() && eOk == eErrorCode ; ++i)
2239cdf0e10cSrcweir 			{
2240cdf0e10cSrcweir 				::connectivity::OSQLParseNode * pColumnRef = pParseTree->getChild(i);
2241cdf0e10cSrcweir 
2242cdf0e10cSrcweir                 do {
2243cdf0e10cSrcweir 
2244cdf0e10cSrcweir                 if ( SQL_ISRULE(pColumnRef,select_sublist) )
2245cdf0e10cSrcweir 				{
2246cdf0e10cSrcweir 					 eErrorCode = fillSelectSubList(_pView,pTabList);
2247cdf0e10cSrcweir                      break;
2248cdf0e10cSrcweir 				}
2249cdf0e10cSrcweir 
2250cdf0e10cSrcweir                 if ( SQL_ISRULE(pColumnRef,derived_column) )
2251cdf0e10cSrcweir 				{
2252cdf0e10cSrcweir 					::rtl::OUString aColumnAlias(rController.getParseIterator().getColumnAlias(pColumnRef)); // kann leer sein
2253cdf0e10cSrcweir 					pColumnRef = pColumnRef->getChild(0);
2254cdf0e10cSrcweir 					OTableFieldDescRef aInfo = new OTableFieldDesc();
2255cdf0e10cSrcweir 
2256cdf0e10cSrcweir 					if (	pColumnRef->count() == 3 &&
2257cdf0e10cSrcweir 							SQL_ISPUNCTUATION(pColumnRef->getChild(0),"(") &&
2258cdf0e10cSrcweir 							SQL_ISPUNCTUATION(pColumnRef->getChild(2),")")
2259cdf0e10cSrcweir 						)
2260cdf0e10cSrcweir 						pColumnRef = pColumnRef->getChild(1);
2261cdf0e10cSrcweir 
2262cdf0e10cSrcweir 					if (SQL_ISRULE(pColumnRef,column_ref))
2263cdf0e10cSrcweir 					{
2264cdf0e10cSrcweir 						InsertColumnRef(_pView,pColumnRef,aColumnName,aColumnAlias,aTableRange,aInfo,pTabList);
2265cdf0e10cSrcweir 						eErrorCode = _pView->InsertField(aInfo, sal_True, bFirstField);
2266cdf0e10cSrcweir 						bFirstField = sal_False;
2267cdf0e10cSrcweir 					}
2268cdf0e10cSrcweir 					else if(SQL_ISRULEOR2(pColumnRef,general_set_fct ,set_fct_spec)	||
2269cdf0e10cSrcweir 							SQL_ISRULEOR2(pColumnRef,position_exp,extract_exp)		||
2270cdf0e10cSrcweir 							SQL_ISRULEOR2(pColumnRef,fold,char_substring_fct)		||
2271cdf0e10cSrcweir 							SQL_ISRULEOR2(pColumnRef,length_exp,char_value_fct))
2272cdf0e10cSrcweir 					{
2273cdf0e10cSrcweir 						::rtl::OUString aColumns;
2274cdf0e10cSrcweir                         pColumnRef->parseNodeToPredicateStr(aColumns,
2275cdf0e10cSrcweir 												            xConnection,
2276cdf0e10cSrcweir 												            rController.getNumberFormatter(),
2277cdf0e10cSrcweir 												            _pView->getLocale(),
2278cdf0e10cSrcweir 												            static_cast<sal_Char>(_pView->getDecimalSeparator().toChar()),
2279cdf0e10cSrcweir 												            &rController.getParser().getContext());
2280cdf0e10cSrcweir 						//pColumnRef->parseNodeToStr(	aColumns,
2281cdf0e10cSrcweir 						//							xConnection,
2282cdf0e10cSrcweir 						//							&rController.getParser().getContext(),
2283cdf0e10cSrcweir 						//							sal_True,
2284cdf0e10cSrcweir 						//							sal_True); // quote is to true because we need quoted elements inside the function
2285cdf0e10cSrcweir 
2286cdf0e10cSrcweir 						sal_Int32 nFunctionType = FKT_NONE;
2287cdf0e10cSrcweir 						::connectivity::OSQLParseNode* pParamRef = NULL;
2288cdf0e10cSrcweir 						sal_Int32 nColumnRefPos = pColumnRef->count() - 2;
2289cdf0e10cSrcweir 						if ( nColumnRefPos >= 0 && static_cast<sal_uInt32>(nColumnRefPos) < pColumnRef->count() )
2290cdf0e10cSrcweir 							pParamRef = pColumnRef->getChild(nColumnRefPos);
2291cdf0e10cSrcweir 
2292cdf0e10cSrcweir 						if (	SQL_ISRULE(pColumnRef,general_set_fct)
2293cdf0e10cSrcweir 							&&	SQL_ISRULE(pParamRef,column_ref) )
2294cdf0e10cSrcweir 						{
2295cdf0e10cSrcweir 							// Parameter auf Columnref pr"ufen
2296cdf0e10cSrcweir 							InsertColumnRef(_pView,pParamRef,aColumnName,aColumnAlias,aTableRange,aInfo,pTabList);
2297cdf0e10cSrcweir 						}
2298cdf0e10cSrcweir 						else if ( SQL_ISRULE(pColumnRef,general_set_fct) )
2299cdf0e10cSrcweir 						{
2300cdf0e10cSrcweir 							if ( pParamRef && pParamRef->getTokenValue().toChar() == '*' )
2301cdf0e10cSrcweir 							{
2302cdf0e10cSrcweir 								OJoinTableView::OTableWindowMap::iterator aIter = pTabList->begin();
2303cdf0e10cSrcweir                                 OJoinTableView::OTableWindowMap::iterator aEnd  = pTabList->end();
2304cdf0e10cSrcweir 								for(;aIter != aEnd;++aIter)
2305cdf0e10cSrcweir 								{
2306cdf0e10cSrcweir 									OQueryTableWindow* pTabWin = static_cast<OQueryTableWindow*>(aIter->second);
2307cdf0e10cSrcweir 									if (pTabWin->ExistsField( ::rtl::OUString::createFromAscii("*"), aInfo ))
2308cdf0e10cSrcweir 									{
2309cdf0e10cSrcweir 										aInfo->SetAlias(String());
2310cdf0e10cSrcweir 										aInfo->SetTable(String());
2311cdf0e10cSrcweir 										break;
2312cdf0e10cSrcweir 									}
2313cdf0e10cSrcweir 								}
2314cdf0e10cSrcweir 							}
2315cdf0e10cSrcweir 							else
2316cdf0e10cSrcweir 							{
2317cdf0e10cSrcweir 								::rtl::OUString sFieldName = aColumns;
2318cdf0e10cSrcweir 								if ( pParamRef )
2319cdf0e10cSrcweir 								{	// we got an aggregate function but without column name inside
2320cdf0e10cSrcweir 									// so we set the whole argument of the function as field name
2321cdf0e10cSrcweir 									nFunctionType |= FKT_NUMERIC;
2322cdf0e10cSrcweir 									sFieldName = ::rtl::OUString();
2323cdf0e10cSrcweir 									pParamRef->parseNodeToStr(	sFieldName,
2324cdf0e10cSrcweir 														xConnection,
2325cdf0e10cSrcweir 														&rController.getParser().getContext(),
2326cdf0e10cSrcweir 														sal_True,
2327cdf0e10cSrcweir 														sal_True); // quote is to true because we need quoted elements inside the function
2328cdf0e10cSrcweir 								}
2329cdf0e10cSrcweir 								aInfo->SetDataType(DataType::DOUBLE);
2330cdf0e10cSrcweir 								aInfo->SetFieldType(TAB_NORMAL_FIELD);
2331cdf0e10cSrcweir 								aInfo->SetField(sFieldName);
2332cdf0e10cSrcweir 							}
2333cdf0e10cSrcweir 							aInfo->SetTabWindow(NULL);
2334cdf0e10cSrcweir 							aInfo->SetFieldAlias(aColumnAlias);
2335cdf0e10cSrcweir 						}
2336cdf0e10cSrcweir 						else
2337cdf0e10cSrcweir 						{
2338cdf0e10cSrcweir                             _pView->fillFunctionInfo(pColumnRef,aColumns,aInfo);
2339cdf0e10cSrcweir 							aInfo->SetFieldAlias(aColumnAlias);
2340cdf0e10cSrcweir 						}
2341cdf0e10cSrcweir 
2342cdf0e10cSrcweir 						if ( SQL_ISRULE(pColumnRef,general_set_fct) )
2343cdf0e10cSrcweir 						{
2344cdf0e10cSrcweir 							aInfo->SetFunctionType(nFunctionType|FKT_AGGREGATE);
2345cdf0e10cSrcweir 							String aCol(aColumns);
2346cdf0e10cSrcweir 							aInfo->SetFunction(aCol.GetToken(0,'(').EraseTrailingChars(' '));
2347cdf0e10cSrcweir 						}
2348cdf0e10cSrcweir 						else
2349cdf0e10cSrcweir 							aInfo->SetFunctionType(nFunctionType|FKT_OTHER);
2350cdf0e10cSrcweir 
2351cdf0e10cSrcweir 						eErrorCode = _pView->InsertField(aInfo, sal_True, bFirstField);
2352cdf0e10cSrcweir 						bFirstField = sal_False;
2353cdf0e10cSrcweir 					}
2354cdf0e10cSrcweir 					else //if(SQL_ISRULE(pColumnRef,num_value_exp)	|| SQL_ISRULE(pColumnRef,term))
2355cdf0e10cSrcweir 					{
2356cdf0e10cSrcweir 						::rtl::OUString aColumns;
2357cdf0e10cSrcweir 						pColumnRef->parseNodeToStr(	aColumns,
2358cdf0e10cSrcweir 													xConnection,
2359cdf0e10cSrcweir 													&rController.getParser().getContext(),
2360cdf0e10cSrcweir 													sal_True,
2361cdf0e10cSrcweir 													sal_True); // quote is to true because we need quoted elements inside the function
2362cdf0e10cSrcweir 
2363cdf0e10cSrcweir                         aInfo->SetTabWindow( NULL );
2364cdf0e10cSrcweir 
2365cdf0e10cSrcweir                         // since we support queries in queries, the thingie might belong to an existing "table"
2366cdf0e10cSrcweir                         OQueryTableWindow* pExistingTable = lcl_findColumnInTables( aColumns, *pTabList, aInfo );
2367cdf0e10cSrcweir                         if ( pExistingTable )
2368cdf0e10cSrcweir                         {
2369cdf0e10cSrcweir                             aInfo->SetTabWindow( pExistingTable );
2370cdf0e10cSrcweir 					        aInfo->SetTable( pExistingTable->GetTableName() );
2371cdf0e10cSrcweir 					        aInfo->SetAlias( pExistingTable->GetAliasName() );
2372cdf0e10cSrcweir                         }
2373cdf0e10cSrcweir 
2374cdf0e10cSrcweir                         aInfo->SetDataType(DataType::DOUBLE);
2375cdf0e10cSrcweir 						aInfo->SetFieldType(TAB_NORMAL_FIELD);
2376cdf0e10cSrcweir 						aInfo->SetField(aColumns);
2377cdf0e10cSrcweir 						aInfo->SetFieldAlias(aColumnAlias);
2378cdf0e10cSrcweir 						aInfo->SetFunctionType(FKT_NUMERIC | FKT_OTHER);
2379cdf0e10cSrcweir 
2380cdf0e10cSrcweir 						eErrorCode = _pView->InsertField(aInfo, sal_True, bFirstField);
2381cdf0e10cSrcweir 						bFirstField = sal_False;
2382cdf0e10cSrcweir 					}
2383cdf0e10cSrcweir 
2384cdf0e10cSrcweir                     break;
2385cdf0e10cSrcweir 				}
2386cdf0e10cSrcweir 
2387cdf0e10cSrcweir                 DBG_ERROR( "InstallFields: don't know how to interpret this parse node!" );
2388cdf0e10cSrcweir 
2389cdf0e10cSrcweir                 } while ( false );
2390cdf0e10cSrcweir 			}
2391cdf0e10cSrcweir 		}
2392cdf0e10cSrcweir 		else
2393cdf0e10cSrcweir 			eErrorCode = eStatementTooComplex;
2394cdf0e10cSrcweir 
2395cdf0e10cSrcweir 		return eErrorCode;
2396cdf0e10cSrcweir 	}
2397cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GetOrderCriteria(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw,const::connectivity::OSQLParseNode * pParseRoot)2398cdf0e10cSrcweir 	SqlParseError GetOrderCriteria(	OQueryDesignView* _pView,
2399cdf0e10cSrcweir 							OSelectionBrowseBox* _pSelectionBrw,
2400cdf0e10cSrcweir 							const ::connectivity::OSQLParseNode* pParseRoot )
2401cdf0e10cSrcweir 	{
2402cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
2403cdf0e10cSrcweir 		if (!pParseRoot->getChild(3)->getChild(4)->isLeaf())
2404cdf0e10cSrcweir 		{
2405cdf0e10cSrcweir 			::connectivity::OSQLParseNode* pNode = pParseRoot->getChild(3)->getChild(4)->getChild(2);
2406cdf0e10cSrcweir 			::connectivity::OSQLParseNode* pParamRef = NULL;
2407cdf0e10cSrcweir 
2408cdf0e10cSrcweir             OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
2409cdf0e10cSrcweir 			EOrderDir eOrderDir;
2410cdf0e10cSrcweir 			OTableFieldDescRef aDragLeft = new OTableFieldDesc();
2411cdf0e10cSrcweir 			for( sal_uInt32 i=0 ; i<pNode->count() ; i++ )
2412cdf0e10cSrcweir 			{
2413cdf0e10cSrcweir 				eOrderDir = ORDER_ASC;
2414cdf0e10cSrcweir 				::connectivity::OSQLParseNode*	pChild = pNode->getChild( i );
2415cdf0e10cSrcweir 
2416cdf0e10cSrcweir 				if (SQL_ISTOKEN( pChild->getChild(1), DESC ) )
2417cdf0e10cSrcweir 					eOrderDir = ORDER_DESC;
2418cdf0e10cSrcweir 
2419cdf0e10cSrcweir                 ::connectivity::OSQLParseNode* pArgument = pChild->getChild(0);
2420cdf0e10cSrcweir 
2421cdf0e10cSrcweir 				if(SQL_ISRULE(pArgument,column_ref))
2422cdf0e10cSrcweir 				{
2423cdf0e10cSrcweir 					if( eOk == FillDragInfo(_pView,pArgument,aDragLeft))
2424cdf0e10cSrcweir 						_pSelectionBrw->AddOrder( aDragLeft, eOrderDir, i);
2425cdf0e10cSrcweir 					else // it could be a alias name for a field
2426cdf0e10cSrcweir 					{
2427cdf0e10cSrcweir 						::rtl::OUString aTableRange,aColumnName;
2428cdf0e10cSrcweir 						::connectivity::OSQLParseTreeIterator& rParseIter = rController.getParseIterator();
2429cdf0e10cSrcweir 						rParseIter.getColumnRange( pArgument, aColumnName, aTableRange );
2430cdf0e10cSrcweir 
2431cdf0e10cSrcweir 						OTableFields& aList = rController.getTableFieldDesc();
2432cdf0e10cSrcweir 						OTableFields::iterator aIter = aList.begin();
2433cdf0e10cSrcweir                         OTableFields::iterator aEnd = aList.end();
2434cdf0e10cSrcweir 						for(;aIter != aEnd;++aIter)
2435cdf0e10cSrcweir 						{
2436cdf0e10cSrcweir 							OTableFieldDescRef pEntry = *aIter;
2437cdf0e10cSrcweir 							if(pEntry.isValid() && pEntry->GetFieldAlias() == aColumnName)
2438cdf0e10cSrcweir 								pEntry->SetOrderDir( eOrderDir );
2439cdf0e10cSrcweir 						}
2440cdf0e10cSrcweir 					}
2441cdf0e10cSrcweir 				}
2442cdf0e10cSrcweir 				else if(SQL_ISRULE(pArgument, general_set_fct ) &&
2443cdf0e10cSrcweir 						SQL_ISRULE(pParamRef = pArgument->getChild(pArgument->count()-2),column_ref) &&
2444cdf0e10cSrcweir 						eOk == FillDragInfo(_pView,pParamRef,aDragLeft))
2445cdf0e10cSrcweir 					_pSelectionBrw->AddOrder( aDragLeft, eOrderDir, i );
2446cdf0e10cSrcweir                 else if( SQL_ISRULE(pArgument, set_fct_spec ) )
2447cdf0e10cSrcweir                 {
2448cdf0e10cSrcweir 
2449cdf0e10cSrcweir 		            Reference< XConnection> xConnection = rController.getConnection();
2450cdf0e10cSrcweir 		            if(xConnection.is())
2451cdf0e10cSrcweir 		            {
2452cdf0e10cSrcweir                         ::rtl::OUString sCondition;
2453cdf0e10cSrcweir 			            pArgument->parseNodeToPredicateStr(sCondition,
2454cdf0e10cSrcweir 												            xConnection,
2455cdf0e10cSrcweir 												            rController.getNumberFormatter(),
2456cdf0e10cSrcweir 												            _pView->getLocale(),
2457cdf0e10cSrcweir 												            static_cast<sal_Char>(_pView->getDecimalSeparator().toChar()),
2458cdf0e10cSrcweir 												            &rController.getParser().getContext());
2459cdf0e10cSrcweir                         _pView->fillFunctionInfo(pArgument,sCondition,aDragLeft);
2460cdf0e10cSrcweir                         aDragLeft->SetFunctionType(FKT_OTHER);
2461cdf0e10cSrcweir                         aDragLeft->SetOrderDir(eOrderDir);
2462cdf0e10cSrcweir                         aDragLeft->SetVisible(sal_False);
2463cdf0e10cSrcweir                         _pSelectionBrw->AddOrder( aDragLeft, eOrderDir, i );
2464cdf0e10cSrcweir                     }
2465cdf0e10cSrcweir                     else
2466cdf0e10cSrcweir                         eErrorCode = eColumnNotFound;
2467cdf0e10cSrcweir                 }
2468cdf0e10cSrcweir 				else
2469cdf0e10cSrcweir 					eErrorCode = eColumnNotFound;
2470cdf0e10cSrcweir 			}
2471cdf0e10cSrcweir 		}
2472cdf0e10cSrcweir 		return eErrorCode;
2473cdf0e10cSrcweir 	}
2474cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GetHavingCriteria(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw,const::connectivity::OSQLParseNode * pSelectRoot,sal_uInt16 & rLevel)2475cdf0e10cSrcweir 	SqlParseError GetHavingCriteria(	OQueryDesignView* _pView,
2476cdf0e10cSrcweir 							OSelectionBrowseBox* _pSelectionBrw,
2477cdf0e10cSrcweir 							const ::connectivity::OSQLParseNode* pSelectRoot,
2478cdf0e10cSrcweir 							sal_uInt16& rLevel )
2479cdf0e10cSrcweir 	{
2480cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
2481cdf0e10cSrcweir 		if (!pSelectRoot->getChild(3)->getChild(3)->isLeaf())
2482cdf0e10cSrcweir 			eErrorCode = GetORCriteria(_pView,_pSelectionBrw,pSelectRoot->getChild(3)->getChild(3)->getChild(1),rLevel, sal_True);
2483cdf0e10cSrcweir 		return eErrorCode;
2484cdf0e10cSrcweir 	}
2485cdf0e10cSrcweir 	//------------------------------------------------------------------------------
GetGroupCriteria(OQueryDesignView * _pView,OSelectionBrowseBox * _pSelectionBrw,const::connectivity::OSQLParseNode * pSelectRoot)2486cdf0e10cSrcweir 	SqlParseError GetGroupCriteria(	OQueryDesignView* _pView,
2487cdf0e10cSrcweir 							OSelectionBrowseBox* _pSelectionBrw,
2488cdf0e10cSrcweir 							const ::connectivity::OSQLParseNode* pSelectRoot )
2489cdf0e10cSrcweir 	{
2490cdf0e10cSrcweir 		SqlParseError eErrorCode = eOk;
2491cdf0e10cSrcweir 		if (!pSelectRoot->getChild(3)->getChild(2)->isLeaf()) // opt_group_by_clause
2492cdf0e10cSrcweir 		{
2493cdf0e10cSrcweir             OQueryController& rController = static_cast<OQueryController&>(_pView->getController());
2494cdf0e10cSrcweir 			::connectivity::OSQLParseNode* pGroupBy = pSelectRoot->getChild(3)->getChild(2)->getChild(2);
2495cdf0e10cSrcweir 
2496cdf0e10cSrcweir 			for( sal_uInt32 i=0 ; i < pGroupBy->count() && eOk == eErrorCode; ++i )
2497cdf0e10cSrcweir 			{
2498cdf0e10cSrcweir                 OTableFieldDescRef aDragInfo = new OTableFieldDesc();
2499cdf0e10cSrcweir                 ::connectivity::OSQLParseNode* pParamRef = NULL;
2500cdf0e10cSrcweir 				::connectivity::OSQLParseNode* pArgument = pGroupBy->getChild( i );
2501cdf0e10cSrcweir 				if(SQL_ISRULE(pArgument,column_ref))
2502cdf0e10cSrcweir 				{
2503cdf0e10cSrcweir 					if ( eOk == (eErrorCode = FillDragInfo(_pView,pArgument,aDragInfo)) )
2504cdf0e10cSrcweir 					{
2505cdf0e10cSrcweir 						aDragInfo->SetGroupBy(sal_True);
2506cdf0e10cSrcweir 						_pSelectionBrw->AddGroupBy(aDragInfo,i);
2507cdf0e10cSrcweir 					}
2508cdf0e10cSrcweir 				}
2509cdf0e10cSrcweir                 else if(SQL_ISRULE(pArgument, general_set_fct ) &&
2510cdf0e10cSrcweir 						SQL_ISRULE(pParamRef = pArgument->getChild(pArgument->count()-2),column_ref) &&
2511cdf0e10cSrcweir 						eOk == FillDragInfo(_pView,pParamRef,aDragInfo))
2512cdf0e10cSrcweir                 {
2513cdf0e10cSrcweir                     aDragInfo->SetGroupBy(sal_True);
2514cdf0e10cSrcweir 					_pSelectionBrw->AddGroupBy( aDragInfo, i );
2515cdf0e10cSrcweir                 }
2516cdf0e10cSrcweir                 else if( SQL_ISRULE(pArgument, set_fct_spec ) )
2517cdf0e10cSrcweir                 {
2518cdf0e10cSrcweir 		            Reference< XConnection> xConnection = rController.getConnection();
2519cdf0e10cSrcweir 		            if(xConnection.is())
2520cdf0e10cSrcweir 		            {
2521cdf0e10cSrcweir                         ::rtl::OUString sGroupByExpression;
2522cdf0e10cSrcweir 			            pArgument->parseNodeToStr(	sGroupByExpression,
2523cdf0e10cSrcweir 													xConnection,
2524cdf0e10cSrcweir 													&rController.getParser().getContext(),
2525cdf0e10cSrcweir 													sal_True,
2526cdf0e10cSrcweir 													sal_True); // quote is to true because we need quoted elements inside the function
2527cdf0e10cSrcweir                         _pView->fillFunctionInfo(pArgument,sGroupByExpression,aDragInfo);
2528cdf0e10cSrcweir                         aDragInfo->SetFunctionType(FKT_OTHER);
2529cdf0e10cSrcweir                         aDragInfo->SetGroupBy(sal_True);
2530cdf0e10cSrcweir                         aDragInfo->SetVisible(sal_False);
2531cdf0e10cSrcweir                         _pSelectionBrw->AddGroupBy( aDragInfo, i );
2532cdf0e10cSrcweir                     }
2533cdf0e10cSrcweir                     else
2534cdf0e10cSrcweir                         eErrorCode = eColumnNotFound;
2535cdf0e10cSrcweir                 }
2536cdf0e10cSrcweir 			}
2537cdf0e10cSrcweir 		}
2538cdf0e10cSrcweir 		return eErrorCode;
2539cdf0e10cSrcweir 	}
2540cdf0e10cSrcweir 
2541cdf0e10cSrcweir     //------------------------------------------------------------------------------
getParseErrorMessage(SqlParseError _eErrorCode)2542cdf0e10cSrcweir 	String getParseErrorMessage( SqlParseError _eErrorCode )
2543cdf0e10cSrcweir 	{
2544cdf0e10cSrcweir 		sal_uInt16 nResId;
2545cdf0e10cSrcweir 		switch(_eErrorCode)
2546cdf0e10cSrcweir 		{
2547cdf0e10cSrcweir 			case eIllegalJoin:
2548cdf0e10cSrcweir 				nResId = STR_QRY_ILLEGAL_JOIN;
2549cdf0e10cSrcweir 				break;
2550cdf0e10cSrcweir 			case eStatementTooLong:
2551cdf0e10cSrcweir 				nResId = STR_QRY_TOO_LONG_STATEMENT;
2552cdf0e10cSrcweir 				break;
2553cdf0e10cSrcweir 			case eNoConnection:
2554cdf0e10cSrcweir 				nResId = STR_QRY_SYNTAX;
2555cdf0e10cSrcweir 				break;
2556cdf0e10cSrcweir 			case eNoSelectStatement:
2557cdf0e10cSrcweir 				nResId = STR_QRY_NOSELECT;
2558cdf0e10cSrcweir 				break;
2559cdf0e10cSrcweir 			case eColumnInLikeNotFound:
2560cdf0e10cSrcweir 				nResId = STR_QRY_SYNTAX;
2561cdf0e10cSrcweir 				break;
2562cdf0e10cSrcweir 			case eNoColumnInLike:
2563cdf0e10cSrcweir 				nResId = STR_QRY_SYNTAX;
2564cdf0e10cSrcweir 				break;
2565cdf0e10cSrcweir 			case eColumnNotFound:
2566cdf0e10cSrcweir 				nResId = STR_QRY_SYNTAX;
2567cdf0e10cSrcweir 				break;
2568cdf0e10cSrcweir 			case eNativeMode:
2569cdf0e10cSrcweir 				nResId = STR_QRY_NATIVE;
2570cdf0e10cSrcweir 				break;
2571cdf0e10cSrcweir 			case eTooManyTables:
2572cdf0e10cSrcweir 				nResId = STR_QRY_TOO_MANY_TABLES;
2573cdf0e10cSrcweir 				break;
2574cdf0e10cSrcweir 			case eTooManyConditions:
2575cdf0e10cSrcweir 				nResId = STR_QRY_TOOMANYCOND;
2576cdf0e10cSrcweir 				break;
2577cdf0e10cSrcweir 			case eTooManyColumns:
2578cdf0e10cSrcweir 				nResId = STR_QRY_TOO_MANY_COLUMNS;
2579cdf0e10cSrcweir 				break;
2580cdf0e10cSrcweir 			case eStatementTooComplex:
2581cdf0e10cSrcweir 				nResId = STR_QRY_TOOCOMPLEX;
2582cdf0e10cSrcweir 				break;
2583cdf0e10cSrcweir 			default:
2584cdf0e10cSrcweir 				nResId = STR_QRY_SYNTAX;
2585cdf0e10cSrcweir 				break;
2586cdf0e10cSrcweir 		}
2587cdf0e10cSrcweir 		;
2588cdf0e10cSrcweir         return String( ModuleRes( nResId ) );
2589cdf0e10cSrcweir 	}
2590cdf0e10cSrcweir 
2591cdf0e10cSrcweir 	//------------------------------------------------------------------------------
2592cdf0e10cSrcweir 	//------------------------------------------------------------------------------
2593cdf0e10cSrcweir }
2594cdf0e10cSrcweir // end of anonymouse namespace
DBG_NAME(OQueryDesignView)2595cdf0e10cSrcweir DBG_NAME(OQueryDesignView)
2596cdf0e10cSrcweir 
2597cdf0e10cSrcweir OQueryDesignView::OQueryDesignView(	OQueryContainerWindow* _pParent,
2598cdf0e10cSrcweir 									OQueryController& _rController,
2599cdf0e10cSrcweir 									const Reference< XMultiServiceFactory >& _rFactory)
2600cdf0e10cSrcweir 	:OQueryView( _pParent, _rController, _rFactory )
2601cdf0e10cSrcweir 	,m_aSplitter( this )
2602cdf0e10cSrcweir 	,m_eChildFocus(NONE)
2603cdf0e10cSrcweir 	,m_bInKeyEvent(sal_False)
2604cdf0e10cSrcweir 	,m_bInSplitHandler( sal_False )
2605cdf0e10cSrcweir {
2606cdf0e10cSrcweir     DBG_CTOR(OQueryDesignView,NULL);
2607cdf0e10cSrcweir 
2608cdf0e10cSrcweir 	try
2609cdf0e10cSrcweir 	{
2610cdf0e10cSrcweir 		SvtSysLocale aSysLocale;
2611cdf0e10cSrcweir 		m_aLocale = aSysLocale.GetLocaleData().getLocale();
2612cdf0e10cSrcweir 		m_sDecimalSep = aSysLocale.GetLocaleData().getNumDecimalSep();
2613cdf0e10cSrcweir 	}
2614cdf0e10cSrcweir 	catch(Exception&)
2615cdf0e10cSrcweir 	{
2616cdf0e10cSrcweir 	}
2617cdf0e10cSrcweir 
2618cdf0e10cSrcweir 	m_pSelectionBox = new OSelectionBrowseBox(this);
2619cdf0e10cSrcweir 
2620cdf0e10cSrcweir 	setNoneVisbleRow(static_cast<OQueryController&>(getController()).getVisibleRows());
2621cdf0e10cSrcweir 	m_pSelectionBox->Show();
2622cdf0e10cSrcweir 	// Splitter einrichten
2623cdf0e10cSrcweir 	m_aSplitter.SetSplitHdl(LINK(this, OQueryDesignView,SplitHdl));
2624cdf0e10cSrcweir 	m_aSplitter.Show();
2625cdf0e10cSrcweir 
2626cdf0e10cSrcweir }
2627cdf0e10cSrcweir // -----------------------------------------------------------------------------
~OQueryDesignView()2628cdf0e10cSrcweir OQueryDesignView::~OQueryDesignView()
2629cdf0e10cSrcweir {
2630cdf0e10cSrcweir 	if ( m_pTableView )
2631cdf0e10cSrcweir 		::dbaui::notifySystemWindow(this,m_pTableView,::comphelper::mem_fun(&TaskPaneList::RemoveWindow));
2632cdf0e10cSrcweir 	::std::auto_ptr<Window> aTemp(m_pSelectionBox);
2633cdf0e10cSrcweir 	m_pSelectionBox = NULL;
2634cdf0e10cSrcweir 
2635cdf0e10cSrcweir     DBG_DTOR(OQueryDesignView,NULL);
2636cdf0e10cSrcweir }
2637cdf0e10cSrcweir //------------------------------------------------------------------------------
2638cdf0e10cSrcweir IMPL_LINK( OQueryDesignView, SplitHdl, void*, /*p*/ )
2639cdf0e10cSrcweir {
2640cdf0e10cSrcweir 	if (!getController().isReadOnly())
2641cdf0e10cSrcweir 	{
2642cdf0e10cSrcweir 		m_bInSplitHandler = sal_True;
2643cdf0e10cSrcweir 		m_aSplitter.SetPosPixel( Point( m_aSplitter.GetPosPixel().X(),m_aSplitter.GetSplitPosPixel() ) );
2644cdf0e10cSrcweir 		static_cast<OQueryController&>(getController()).setSplitPos(m_aSplitter.GetSplitPosPixel());
2645cdf0e10cSrcweir 		static_cast<OQueryController&>(getController()).setModified( sal_True );
2646cdf0e10cSrcweir 		Resize();
2647cdf0e10cSrcweir 		m_bInSplitHandler = sal_True;
2648cdf0e10cSrcweir 	}
2649cdf0e10cSrcweir 	return 0L;
2650cdf0e10cSrcweir }
2651cdf0e10cSrcweir // -------------------------------------------------------------------------
Construct()2652cdf0e10cSrcweir void OQueryDesignView::Construct()
2653cdf0e10cSrcweir {
2654cdf0e10cSrcweir 	m_pTableView = new OQueryTableView(m_pScrollWindow,this);
2655cdf0e10cSrcweir 	::dbaui::notifySystemWindow(this,m_pTableView,::comphelper::mem_fun(&TaskPaneList::AddWindow));
2656cdf0e10cSrcweir 	OQueryView::Construct();
2657cdf0e10cSrcweir }
2658cdf0e10cSrcweir // -----------------------------------------------------------------------------
initialize()2659cdf0e10cSrcweir void OQueryDesignView::initialize()
2660cdf0e10cSrcweir {
2661cdf0e10cSrcweir 	if(static_cast<OQueryController&>(getController()).getSplitPos() != -1)
2662cdf0e10cSrcweir 	{
2663cdf0e10cSrcweir 		m_aSplitter.SetPosPixel( Point( m_aSplitter.GetPosPixel().X(),static_cast<OQueryController&>(getController()).getSplitPos() ) );
2664cdf0e10cSrcweir 		m_aSplitter.SetSplitPosPixel(static_cast<OQueryController&>(getController()).getSplitPos());
2665cdf0e10cSrcweir 	}
2666cdf0e10cSrcweir 	m_pSelectionBox->initialize();
2667cdf0e10cSrcweir 	reset();
2668cdf0e10cSrcweir }
2669cdf0e10cSrcweir // -------------------------------------------------------------------------
resizeDocumentView(Rectangle & _rPlayground)2670cdf0e10cSrcweir void OQueryDesignView::resizeDocumentView(Rectangle& _rPlayground)
2671cdf0e10cSrcweir {
2672cdf0e10cSrcweir 	Point aPlaygroundPos( _rPlayground.TopLeft() );
2673cdf0e10cSrcweir 	Size aPlaygroundSize( _rPlayground.GetSize() );
2674cdf0e10cSrcweir 
2675cdf0e10cSrcweir 	// calc the split pos, and forward it to the controller
2676cdf0e10cSrcweir 	sal_Int32 nSplitPos = static_cast<OQueryController&>(getController()).getSplitPos();
2677cdf0e10cSrcweir 	if ( 0 != aPlaygroundSize.Height() )
2678cdf0e10cSrcweir 	{
2679cdf0e10cSrcweir 		if	(	( -1 == nSplitPos )
2680cdf0e10cSrcweir 			||	( nSplitPos >= aPlaygroundSize.Height() )
2681cdf0e10cSrcweir 			)
2682cdf0e10cSrcweir 		{
2683cdf0e10cSrcweir 			// let the selection browse box determine an optimal size
2684cdf0e10cSrcweir 			Size aSelectionBoxSize = m_pSelectionBox->CalcOptimalSize( aPlaygroundSize );
2685cdf0e10cSrcweir 			nSplitPos = aPlaygroundSize.Height() - aSelectionBoxSize.Height() - m_aSplitter.GetSizePixel().Height();
2686cdf0e10cSrcweir 			// still an invalid size?
2687cdf0e10cSrcweir 			if ( nSplitPos == -1 || nSplitPos >= aPlaygroundSize.Height() )
2688cdf0e10cSrcweir 				nSplitPos = sal_Int32(aPlaygroundSize.Height()*0.6);
2689cdf0e10cSrcweir 
2690cdf0e10cSrcweir 			static_cast<OQueryController&>(getController()).setSplitPos(nSplitPos);
2691cdf0e10cSrcweir 		}
2692cdf0e10cSrcweir 
2693cdf0e10cSrcweir 		if ( !m_bInSplitHandler )
2694cdf0e10cSrcweir 		{	// the resize is triggered by something else than the split handler
2695cdf0e10cSrcweir 			// our main focus is to try to preserve the size of the selectionbrowse box
2696cdf0e10cSrcweir 			Size aSelBoxSize = m_pSelectionBox->GetSizePixel();
2697cdf0e10cSrcweir 			if ( aSelBoxSize.Height() )
2698cdf0e10cSrcweir 			{
2699cdf0e10cSrcweir 				// keep the size of the sel box constant
2700cdf0e10cSrcweir 				nSplitPos = aPlaygroundSize.Height() - m_aSplitter.GetSizePixel().Height() - aSelBoxSize.Height();
2701cdf0e10cSrcweir 
2702cdf0e10cSrcweir 				// and if the box is smaller than the optimal size, try to do something about it
2703cdf0e10cSrcweir 				Size aSelBoxOptSize = m_pSelectionBox->CalcOptimalSize( aPlaygroundSize );
2704cdf0e10cSrcweir 				if ( aSelBoxOptSize.Height() > aSelBoxSize.Height() )
2705cdf0e10cSrcweir 				{
2706cdf0e10cSrcweir 					nSplitPos = aPlaygroundSize.Height() - m_aSplitter.GetSizePixel().Height() - aSelBoxOptSize.Height();
2707cdf0e10cSrcweir 				}
2708cdf0e10cSrcweir 
2709cdf0e10cSrcweir 				static_cast< OQueryController& >(getController()).setSplitPos( nSplitPos );
2710cdf0e10cSrcweir 			}
2711cdf0e10cSrcweir 		}
2712cdf0e10cSrcweir 	}
2713cdf0e10cSrcweir 
2714cdf0e10cSrcweir 	// normalize the split pos
2715cdf0e10cSrcweir 	Point	aSplitPos		= Point( _rPlayground.Left(), nSplitPos );
2716cdf0e10cSrcweir 	Size	aSplitSize		= Size( _rPlayground.GetSize().Width(), m_aSplitter.GetSizePixel().Height() );
2717cdf0e10cSrcweir 
2718cdf0e10cSrcweir 	if( ( aSplitPos.Y() + aSplitSize.Height() ) > ( aPlaygroundSize.Height() ))
2719cdf0e10cSrcweir 		aSplitPos.Y() = aPlaygroundSize.Height() - aSplitSize.Height();
2720cdf0e10cSrcweir 
2721cdf0e10cSrcweir 	if( aSplitPos.Y() <= aPlaygroundPos.Y() )
2722cdf0e10cSrcweir 		aSplitPos.Y() = aPlaygroundPos.Y() + sal_Int32(aPlaygroundSize.Height() * 0.2);
2723cdf0e10cSrcweir 
2724cdf0e10cSrcweir 	// position the table
2725cdf0e10cSrcweir 	Size aTableViewSize(aPlaygroundSize.Width(), aSplitPos.Y() - aPlaygroundPos.Y());
2726cdf0e10cSrcweir 	m_pScrollWindow->SetPosSizePixel(aPlaygroundPos, aTableViewSize);
2727cdf0e10cSrcweir 
2728cdf0e10cSrcweir 	// position the selection browse box
2729cdf0e10cSrcweir 	Point aPos( aPlaygroundPos.X(), aSplitPos.Y() + aSplitSize.Height() );
2730cdf0e10cSrcweir 	m_pSelectionBox->SetPosSizePixel( aPos, Size( aPlaygroundSize.Width(), aPlaygroundSize.Height() - aSplitSize.Height() - aTableViewSize.Height() ));
2731cdf0e10cSrcweir 
2732cdf0e10cSrcweir 	// set the size of the splitter
2733cdf0e10cSrcweir 	m_aSplitter.SetPosSizePixel( aSplitPos, aSplitSize );
2734cdf0e10cSrcweir 	m_aSplitter.SetDragRectPixel( _rPlayground );
2735cdf0e10cSrcweir 
2736cdf0e10cSrcweir 	// just for completeness: there is no space left, we occupied it all ...
2737cdf0e10cSrcweir 	_rPlayground.SetPos( _rPlayground.BottomRight() );
2738cdf0e10cSrcweir 	_rPlayground.SetSize( Size( 0, 0 ) );
2739cdf0e10cSrcweir }
2740cdf0e10cSrcweir // -----------------------------------------------------------------------------
setReadOnly(sal_Bool _bReadOnly)2741cdf0e10cSrcweir void OQueryDesignView::setReadOnly(sal_Bool _bReadOnly)
2742cdf0e10cSrcweir {
2743cdf0e10cSrcweir 	m_pSelectionBox->SetReadOnly(_bReadOnly);
2744cdf0e10cSrcweir }
2745cdf0e10cSrcweir // -----------------------------------------------------------------------------
clear()2746cdf0e10cSrcweir void OQueryDesignView::clear()
2747cdf0e10cSrcweir {
2748cdf0e10cSrcweir 	m_pSelectionBox->ClearAll(); // clear the whole selection
2749cdf0e10cSrcweir 	m_pTableView->ClearAll();
2750cdf0e10cSrcweir }
2751cdf0e10cSrcweir // -----------------------------------------------------------------------------
setStatement(const::rtl::OUString &)2752cdf0e10cSrcweir void OQueryDesignView::setStatement(const ::rtl::OUString& /*_rsStatement*/)
2753cdf0e10cSrcweir {
2754cdf0e10cSrcweir }
2755cdf0e10cSrcweir // -----------------------------------------------------------------------------
copy()2756cdf0e10cSrcweir void OQueryDesignView::copy()
2757cdf0e10cSrcweir {
2758cdf0e10cSrcweir 	if( m_eChildFocus == SELECTION)
2759cdf0e10cSrcweir 		m_pSelectionBox->copy();
2760cdf0e10cSrcweir }
2761cdf0e10cSrcweir // -----------------------------------------------------------------------------
isCutAllowed()2762cdf0e10cSrcweir sal_Bool OQueryDesignView::isCutAllowed()
2763cdf0e10cSrcweir {
2764cdf0e10cSrcweir 	sal_Bool bAllowed = sal_False;
2765cdf0e10cSrcweir 	if ( SELECTION == m_eChildFocus )
2766cdf0e10cSrcweir 		bAllowed = m_pSelectionBox->isCutAllowed();
2767cdf0e10cSrcweir 	return bAllowed;
2768cdf0e10cSrcweir }
2769cdf0e10cSrcweir // -----------------------------------------------------------------------------
isPasteAllowed()2770cdf0e10cSrcweir sal_Bool OQueryDesignView::isPasteAllowed()
2771cdf0e10cSrcweir {
2772cdf0e10cSrcweir 	sal_Bool bAllowed = sal_False;
2773cdf0e10cSrcweir 	if ( SELECTION == m_eChildFocus )
2774cdf0e10cSrcweir 		bAllowed = m_pSelectionBox->isPasteAllowed();
2775cdf0e10cSrcweir 	return bAllowed;
2776cdf0e10cSrcweir }
2777cdf0e10cSrcweir // -----------------------------------------------------------------------------
isCopyAllowed()2778cdf0e10cSrcweir sal_Bool OQueryDesignView::isCopyAllowed()
2779cdf0e10cSrcweir {
2780cdf0e10cSrcweir 	sal_Bool bAllowed = sal_False;
2781cdf0e10cSrcweir 	if ( SELECTION == m_eChildFocus )
2782cdf0e10cSrcweir 		bAllowed = m_pSelectionBox->isCopyAllowed();
2783cdf0e10cSrcweir 	return bAllowed;
2784cdf0e10cSrcweir }
2785cdf0e10cSrcweir // -----------------------------------------------------------------------------
stopTimer()2786cdf0e10cSrcweir void OQueryDesignView::stopTimer()
2787cdf0e10cSrcweir {
2788cdf0e10cSrcweir 	m_pSelectionBox->stopTimer();
2789cdf0e10cSrcweir }
2790cdf0e10cSrcweir // -----------------------------------------------------------------------------
startTimer()2791cdf0e10cSrcweir void OQueryDesignView::startTimer()
2792cdf0e10cSrcweir {
2793cdf0e10cSrcweir 	m_pSelectionBox->startTimer();
2794cdf0e10cSrcweir }
2795cdf0e10cSrcweir // -----------------------------------------------------------------------------
cut()2796cdf0e10cSrcweir void OQueryDesignView::cut()
2797cdf0e10cSrcweir {
2798cdf0e10cSrcweir 	if( m_eChildFocus == SELECTION)
2799cdf0e10cSrcweir 	{
2800cdf0e10cSrcweir 		m_pSelectionBox->cut();
2801cdf0e10cSrcweir 		static_cast<OQueryController&>(getController()).setModified(sal_True);
2802cdf0e10cSrcweir 	}
2803cdf0e10cSrcweir }
2804cdf0e10cSrcweir // -----------------------------------------------------------------------------
paste()2805cdf0e10cSrcweir void OQueryDesignView::paste()
2806cdf0e10cSrcweir {
2807cdf0e10cSrcweir 	if( m_eChildFocus == SELECTION)
2808cdf0e10cSrcweir 	{
2809cdf0e10cSrcweir 		m_pSelectionBox->paste();
2810cdf0e10cSrcweir 		static_cast<OQueryController&>(getController()).setModified(sal_True);
2811cdf0e10cSrcweir 	}
2812cdf0e10cSrcweir }
2813cdf0e10cSrcweir // -----------------------------------------------------------------------------
TableDeleted(const::rtl::OUString & rAliasName)2814cdf0e10cSrcweir void OQueryDesignView::TableDeleted(const ::rtl::OUString& rAliasName)
2815cdf0e10cSrcweir {
2816cdf0e10cSrcweir 	// Nachricht, dass Tabelle aus dem Fenster gel"oscht wurde
2817cdf0e10cSrcweir 	DeleteFields(rAliasName);
2818cdf0e10cSrcweir 	static_cast<OQueryController&>(getController()).InvalidateFeature(ID_BROWSER_ADDTABLE);	// view nochmal bescheid sagen
2819cdf0e10cSrcweir }
2820cdf0e10cSrcweir //------------------------------------------------------------------------------
DeleteFields(const::rtl::OUString & rAliasName)2821cdf0e10cSrcweir void OQueryDesignView::DeleteFields( const ::rtl::OUString& rAliasName )
2822cdf0e10cSrcweir {
2823cdf0e10cSrcweir 	m_pSelectionBox->DeleteFields( rAliasName );
2824cdf0e10cSrcweir }
2825cdf0e10cSrcweir // -----------------------------------------------------------------------------
HasFieldByAliasName(const::rtl::OUString & rFieldName,OTableFieldDescRef & rInfo) const2826cdf0e10cSrcweir bool OQueryDesignView::HasFieldByAliasName(const ::rtl::OUString& rFieldName, OTableFieldDescRef& rInfo)  const
2827cdf0e10cSrcweir {
2828cdf0e10cSrcweir     return m_pSelectionBox->HasFieldByAliasName( rFieldName, rInfo);
2829cdf0e10cSrcweir }
2830cdf0e10cSrcweir // -----------------------------------------------------------------------------
InsertField(const OTableFieldDescRef & rInfo,sal_Bool bVis,sal_Bool bActivate)2831cdf0e10cSrcweir SqlParseError OQueryDesignView::InsertField( const OTableFieldDescRef& rInfo, sal_Bool bVis, sal_Bool bActivate)
2832cdf0e10cSrcweir {
2833cdf0e10cSrcweir 	return m_pSelectionBox->InsertField( rInfo, BROWSER_INVALIDID,bVis, bActivate ).isValid() ? eOk : eTooManyColumns;
2834cdf0e10cSrcweir }
2835cdf0e10cSrcweir // -----------------------------------------------------------------------------
getColWidth(sal_uInt16 _nColPos) const2836cdf0e10cSrcweir sal_Int32 OQueryDesignView::getColWidth(sal_uInt16 _nColPos) const
2837cdf0e10cSrcweir {
2838cdf0e10cSrcweir     static sal_Int32 s_nDefaultWidth = GetTextWidth(String(RTL_CONSTASCII_USTRINGPARAM("0"))) * 15;
2839cdf0e10cSrcweir     sal_Int32 nWidth = static_cast<OQueryController&>(getController()).getColWidth(_nColPos);
2840cdf0e10cSrcweir     if ( !nWidth )
2841cdf0e10cSrcweir         nWidth = s_nDefaultWidth;
2842cdf0e10cSrcweir 	return nWidth;
2843cdf0e10cSrcweir }
2844cdf0e10cSrcweir //------------------------------------------------------------------------------
fillValidFields(const::rtl::OUString & sAliasName,ComboBox * pFieldList)2845cdf0e10cSrcweir void OQueryDesignView::fillValidFields(const ::rtl::OUString& sAliasName, ComboBox* pFieldList)
2846cdf0e10cSrcweir {
2847cdf0e10cSrcweir 	DBG_ASSERT(pFieldList != NULL, "OQueryDesignView::FillValidFields : What the hell do you think I can do with a NULL-ptr ? This will crash !");
2848cdf0e10cSrcweir 	pFieldList->Clear();
2849cdf0e10cSrcweir 
2850cdf0e10cSrcweir 	sal_Bool bAllTables = sAliasName.getLength() == 0;
2851cdf0e10cSrcweir 
2852cdf0e10cSrcweir 	OJoinTableView::OTableWindowMap* pTabWins = m_pTableView->GetTabWinMap();
2853cdf0e10cSrcweir 	::rtl::OUString strCurrentPrefix;
2854cdf0e10cSrcweir 	::std::vector< ::rtl::OUString> aFields;
2855cdf0e10cSrcweir 	OJoinTableView::OTableWindowMap::iterator aIter = pTabWins->begin();
2856cdf0e10cSrcweir     OJoinTableView::OTableWindowMap::iterator aEnd  = pTabWins->end();
2857cdf0e10cSrcweir     for(;aIter != aEnd;++aIter)
2858cdf0e10cSrcweir 	{
2859cdf0e10cSrcweir 		OQueryTableWindow* pCurrentWin = static_cast<OQueryTableWindow*>(aIter->second);
2860cdf0e10cSrcweir 		if (bAllTables || (pCurrentWin->GetAliasName() == sAliasName))
2861cdf0e10cSrcweir 		{
2862cdf0e10cSrcweir 			strCurrentPrefix = pCurrentWin->GetAliasName();
2863cdf0e10cSrcweir 			strCurrentPrefix += ::rtl::OUString('.');
2864cdf0e10cSrcweir 
2865cdf0e10cSrcweir 			pCurrentWin->EnumValidFields(aFields);
2866cdf0e10cSrcweir 
2867cdf0e10cSrcweir 			::std::vector< ::rtl::OUString>::iterator aStrIter = aFields.begin();
2868cdf0e10cSrcweir             ::std::vector< ::rtl::OUString>::iterator aStrEnd = aFields.end();
2869cdf0e10cSrcweir 			for(;aStrIter != aStrEnd;++aStrIter)
2870cdf0e10cSrcweir 			{
2871cdf0e10cSrcweir 				if (bAllTables || aStrIter->toChar() == '*')
2872cdf0e10cSrcweir 					pFieldList->InsertEntry(::rtl::OUString(strCurrentPrefix) += *aStrIter);
2873cdf0e10cSrcweir 				else
2874cdf0e10cSrcweir 					pFieldList->InsertEntry(*aStrIter);
2875cdf0e10cSrcweir 			}
2876cdf0e10cSrcweir 
2877cdf0e10cSrcweir 			if (!bAllTables)
2878cdf0e10cSrcweir 				// das heisst, dass ich in diesen Block kam, weil der Tabellenname genau der gesuchte war, also bin ich fertig
2879cdf0e10cSrcweir 				// (dadurch verhindere ich auch das doppelte Einfuegen von Feldern, wenn eine Tabelle mehrmals als TabWin vorkommt)
2880cdf0e10cSrcweir 				break;
2881cdf0e10cSrcweir 		}
2882cdf0e10cSrcweir 	}
2883cdf0e10cSrcweir }
2884cdf0e10cSrcweir // -----------------------------------------------------------------------------
PreNotify(NotifyEvent & rNEvt)2885cdf0e10cSrcweir long OQueryDesignView::PreNotify(NotifyEvent& rNEvt)
2886cdf0e10cSrcweir {
2887cdf0e10cSrcweir 	switch (rNEvt.GetType())
2888cdf0e10cSrcweir 	{
2889cdf0e10cSrcweir 		case EVENT_GETFOCUS:
2890cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
2891cdf0e10cSrcweir 			{
2892cdf0e10cSrcweir 				Window* pFocus = Application::GetFocusWindow();
2893cdf0e10cSrcweir                 (void)pFocus;
2894cdf0e10cSrcweir 			}
2895cdf0e10cSrcweir #endif
2896cdf0e10cSrcweir 
2897cdf0e10cSrcweir 			if ( m_pSelectionBox && m_pSelectionBox->HasChildPathFocus() )
2898cdf0e10cSrcweir 				m_eChildFocus = SELECTION;
2899cdf0e10cSrcweir 			else
2900cdf0e10cSrcweir 				m_eChildFocus = TABLEVIEW;
2901cdf0e10cSrcweir 			break;
2902cdf0e10cSrcweir 	}
2903cdf0e10cSrcweir 
2904cdf0e10cSrcweir 	return OQueryView::PreNotify(rNEvt);
2905cdf0e10cSrcweir }
2906cdf0e10cSrcweir //------------------------------------------------------------------------------
2907cdf0e10cSrcweir 
2908cdf0e10cSrcweir 
2909cdf0e10cSrcweir // -----------------------------------------------------------------------------
2910cdf0e10cSrcweir // check if the statement is correct when not returning false
checkStatement()2911cdf0e10cSrcweir sal_Bool OQueryDesignView::checkStatement()
2912cdf0e10cSrcweir {
2913cdf0e10cSrcweir 	sal_Bool bRet = sal_True;
2914cdf0e10cSrcweir 	if ( m_pSelectionBox )
2915cdf0e10cSrcweir 		bRet = m_pSelectionBox->Save(); // a error occured so we return no
2916cdf0e10cSrcweir 	return bRet;
2917cdf0e10cSrcweir }
2918cdf0e10cSrcweir //-------------------------------------------------------------------------------
getStatement()2919cdf0e10cSrcweir ::rtl::OUString OQueryDesignView::getStatement()
2920cdf0e10cSrcweir {
2921cdf0e10cSrcweir 	OQueryController& rController = static_cast<OQueryController&>(getController());
2922cdf0e10cSrcweir 	m_rController.clearError();
2923cdf0e10cSrcweir 	// used for fields which aren't any longer in the statement
2924cdf0e10cSrcweir 	OTableFields& rUnUsedFields = rController.getUnUsedFields();
2925cdf0e10cSrcweir 	OTableFields().swap( rUnUsedFields );
2926cdf0e10cSrcweir 
2927cdf0e10cSrcweir 	// create the select columns
2928cdf0e10cSrcweir 	sal_uInt32 nFieldcount = 0;
2929cdf0e10cSrcweir 	OTableFields& rFieldList = rController.getTableFieldDesc();
2930cdf0e10cSrcweir 	OTableFields::iterator aIter = rFieldList.begin();
2931cdf0e10cSrcweir     OTableFields::iterator aEnd = rFieldList.end();
2932cdf0e10cSrcweir 	for(;aIter != aEnd;++aIter)
2933cdf0e10cSrcweir 	{
2934cdf0e10cSrcweir 		OTableFieldDescRef pEntryField = *aIter;
2935cdf0e10cSrcweir 		if ( pEntryField->GetField().getLength() && pEntryField->IsVisible() )
2936cdf0e10cSrcweir 			++nFieldcount;
2937cdf0e10cSrcweir 		else if (pEntryField->GetField().getLength()			&&
2938cdf0e10cSrcweir 				!pEntryField->HasCriteria()					&&
2939cdf0e10cSrcweir 				pEntryField->isNoneFunction()				&&
2940cdf0e10cSrcweir 				pEntryField->GetOrderDir() == ORDER_NONE	&&
2941cdf0e10cSrcweir                 !pEntryField->IsGroupBy()                   &&
2942cdf0e10cSrcweir 				!pEntryField->GetFunction().getLength() )
2943cdf0e10cSrcweir 			rUnUsedFields.push_back(pEntryField);
2944cdf0e10cSrcweir 	}
2945cdf0e10cSrcweir 	if ( !nFieldcount )	// keine Felder sichtbar also zur"uck
2946cdf0e10cSrcweir 	{
2947cdf0e10cSrcweir 		rUnUsedFields = rFieldList;
2948cdf0e10cSrcweir 		return ::rtl::OUString();
2949cdf0e10cSrcweir 	}
2950cdf0e10cSrcweir 
2951cdf0e10cSrcweir 	OQueryTableView::OTableWindowMap* pTabList   = m_pTableView->GetTabWinMap();
2952cdf0e10cSrcweir 	sal_uInt32 nTabcount		= pTabList->size();
2953cdf0e10cSrcweir 
2954cdf0e10cSrcweir 	::rtl::OUString	aFieldListStr(GenerateSelectList(this,rFieldList,nTabcount>1));
2955cdf0e10cSrcweir 	if( !aFieldListStr.getLength() )
2956cdf0e10cSrcweir 		return ::rtl::OUString();
2957cdf0e10cSrcweir 	// Ausnahmebehandlung, wenn keine Felder angegeben worden sind
2958cdf0e10cSrcweir 	// Dann darf die Tabpage nicht gewechselt werden
2959cdf0e10cSrcweir 	// Im TabBarSelectHdl wird der SQL-::rtl::OUString auf STATEMENT_NOFIELDS abgefragt
2960cdf0e10cSrcweir 	// und eine Errormeldung erzeugt
2961cdf0e10cSrcweir 	// ----------------- Tabellenliste aufbauen ----------------------
2962cdf0e10cSrcweir 
2963cdf0e10cSrcweir 	const ::std::vector<OTableConnection*>*	pConnList = m_pTableView->getTableConnections();
2964cdf0e10cSrcweir 	Reference< XConnection> xConnection = rController.getConnection();
2965cdf0e10cSrcweir 	::rtl::OUString aTableListStr(GenerateFromClause(xConnection,pTabList,pConnList));
2966cdf0e10cSrcweir 	DBG_ASSERT(aTableListStr.getLength(), "OQueryDesignView::getStatement() : unerwartet : habe Felder, aber keine Tabellen !");
2967cdf0e10cSrcweir 	// wenn es Felder gibt, koennen die nur durch Einfuegen aus einer schon existenten Tabelle entstanden sein; wenn andererseits
2968cdf0e10cSrcweir 	// eine Tabelle geloescht wird, verschwinden auch die zugehoerigen Felder -> ergo KANN es das nicht geben, dass Felder
2969cdf0e10cSrcweir 	// existieren, aber keine Tabellen (und aFieldListStr hat schon eine Laenge, das stelle ich oben sicher)
2970cdf0e10cSrcweir 	::rtl::OUStringBuffer aHavingStr,aCriteriaListStr;
2971cdf0e10cSrcweir 	// ----------------- Kriterien aufbauen ----------------------
2972cdf0e10cSrcweir 	if (!GenerateCriterias(this,aCriteriaListStr,aHavingStr,rFieldList, nTabcount > 1))
2973cdf0e10cSrcweir 		return ::rtl::OUString();
2974cdf0e10cSrcweir 
2975cdf0e10cSrcweir 	::rtl::OUString aJoinCrit;
2976cdf0e10cSrcweir 	GenerateInnerJoinCriterias(xConnection,aJoinCrit,pConnList);
2977cdf0e10cSrcweir 	if(aJoinCrit.getLength())
2978cdf0e10cSrcweir 	{
2979cdf0e10cSrcweir 		::rtl::OUString aTmp = ::rtl::OUString::createFromAscii("( ");
2980cdf0e10cSrcweir 		aTmp += aJoinCrit;
2981cdf0e10cSrcweir 		aTmp += ::rtl::OUString::createFromAscii(" )");
2982cdf0e10cSrcweir 		if(aCriteriaListStr.getLength())
2983cdf0e10cSrcweir 		{
2984cdf0e10cSrcweir 			aTmp += C_AND;
2985cdf0e10cSrcweir 			aTmp += aCriteriaListStr.makeStringAndClear();
2986cdf0e10cSrcweir 		}
2987cdf0e10cSrcweir 		aCriteriaListStr = aTmp;
2988cdf0e10cSrcweir 	}
2989cdf0e10cSrcweir 	// ----------------- Statement aufbauen ----------------------
2990cdf0e10cSrcweir 	::rtl::OUStringBuffer aSqlCmd(::rtl::OUString::createFromAscii("SELECT "));
2991cdf0e10cSrcweir 	if(static_cast<OQueryController&>(getController()).isDistinct())
2992cdf0e10cSrcweir 		aSqlCmd.append(::rtl::OUString::createFromAscii(" DISTINCT "));
2993cdf0e10cSrcweir 	aSqlCmd.append(aFieldListStr);
2994cdf0e10cSrcweir 	aSqlCmd.append(::rtl::OUString::createFromAscii(" FROM "));
2995cdf0e10cSrcweir 	aSqlCmd.append(aTableListStr);
2996cdf0e10cSrcweir 
2997cdf0e10cSrcweir 	if (aCriteriaListStr.getLength())
2998cdf0e10cSrcweir 	{
2999cdf0e10cSrcweir 		aSqlCmd.append(::rtl::OUString::createFromAscii(" WHERE "));
3000cdf0e10cSrcweir 		aSqlCmd.append(aCriteriaListStr.makeStringAndClear());
3001cdf0e10cSrcweir 	}
3002cdf0e10cSrcweir 	// ----------------- GroupBy aufbauen und Anh"angen ------------
3003cdf0e10cSrcweir 	Reference<XDatabaseMetaData> xMeta;
3004cdf0e10cSrcweir 	if ( xConnection.is() )
3005cdf0e10cSrcweir 		xMeta = xConnection->getMetaData();
3006cdf0e10cSrcweir 	sal_Bool bUseAlias = nTabcount > 1;
3007cdf0e10cSrcweir 	if ( xMeta.is() )
3008cdf0e10cSrcweir 		bUseAlias = bUseAlias || !xMeta->supportsGroupByUnrelated();
3009cdf0e10cSrcweir 
3010cdf0e10cSrcweir 	aSqlCmd.append(GenerateGroupBy(this,rFieldList,bUseAlias));
3011cdf0e10cSrcweir 	// ----------------- having Anh"angen ------------
3012cdf0e10cSrcweir 	if(aHavingStr.getLength())
3013cdf0e10cSrcweir 	{
3014cdf0e10cSrcweir 		aSqlCmd.append(::rtl::OUString::createFromAscii(" HAVING "));
3015cdf0e10cSrcweir 		aSqlCmd.append(aHavingStr.makeStringAndClear());
3016cdf0e10cSrcweir 	}
3017cdf0e10cSrcweir 	// ----------------- Sortierung aufbauen und Anh"angen ------------
3018cdf0e10cSrcweir 	::rtl::OUString sOrder;
3019cdf0e10cSrcweir 	SqlParseError eErrorCode = eOk;
3020cdf0e10cSrcweir 	if ( (eErrorCode = GenerateOrder(this,rFieldList,nTabcount > 1,sOrder)) == eOk)
3021cdf0e10cSrcweir 		aSqlCmd.append(sOrder);
3022cdf0e10cSrcweir 	else
3023cdf0e10cSrcweir 	{
3024cdf0e10cSrcweir 		if ( !m_rController.hasError() )
3025cdf0e10cSrcweir             m_rController.appendError( getParseErrorMessage( eErrorCode ) );
3026cdf0e10cSrcweir 
3027cdf0e10cSrcweir         m_rController.displayError();
3028cdf0e10cSrcweir 	}
3029cdf0e10cSrcweir 
3030cdf0e10cSrcweir     ::rtl::OUString sSQL = aSqlCmd.makeStringAndClear();
3031cdf0e10cSrcweir     if ( xConnection.is() )
3032cdf0e10cSrcweir     {
3033cdf0e10cSrcweir         ::connectivity::OSQLParser& rParser( rController.getParser() );
3034cdf0e10cSrcweir         ::rtl::OUString sErrorMessage;
3035cdf0e10cSrcweir         ::std::auto_ptr<OSQLParseNode> pParseNode( rParser.parseTree( sErrorMessage, sSQL, sal_True ) );
3036cdf0e10cSrcweir         if ( pParseNode.get() )
3037cdf0e10cSrcweir         {
3038cdf0e10cSrcweir             OSQLParseNode* pNode = pParseNode->getChild(3)->getChild(1);
3039cdf0e10cSrcweir             if ( pNode->count() > 1 )
3040cdf0e10cSrcweir             {
3041cdf0e10cSrcweir 		        ::connectivity::OSQLParseNode * pCondition = pNode->getChild(1);
3042cdf0e10cSrcweir 		        if ( pCondition ) // no where clause
3043cdf0e10cSrcweir                 {
3044cdf0e10cSrcweir                     OSQLParseNode::compress(pCondition);
3045cdf0e10cSrcweir                     ::rtl::OUString sTemp;
3046cdf0e10cSrcweir                     pParseNode->parseNodeToStr(sTemp,xConnection);
3047cdf0e10cSrcweir                     sSQL = sTemp;
3048cdf0e10cSrcweir                 }
3049cdf0e10cSrcweir             }
3050cdf0e10cSrcweir         }
3051cdf0e10cSrcweir     }
3052cdf0e10cSrcweir 	return sSQL;
3053cdf0e10cSrcweir }
3054cdf0e10cSrcweir // -----------------------------------------------------------------------------
3055cdf0e10cSrcweir // -----------------------------------------------------------------------------
setSlotEnabled(sal_Int32 _nSlotId,sal_Bool _bEnable)3056cdf0e10cSrcweir void OQueryDesignView::setSlotEnabled(sal_Int32 _nSlotId,sal_Bool _bEnable)
3057cdf0e10cSrcweir {
3058cdf0e10cSrcweir 	sal_uInt16 nRow;
3059cdf0e10cSrcweir 	switch (_nSlotId)
3060cdf0e10cSrcweir 	{
3061cdf0e10cSrcweir 		case SID_QUERY_VIEW_FUNCTIONS:
3062cdf0e10cSrcweir 			nRow = BROW_FUNCTION_ROW;
3063cdf0e10cSrcweir 			break;
3064cdf0e10cSrcweir 		case SID_QUERY_VIEW_TABLES:
3065cdf0e10cSrcweir 			nRow = BROW_TABLE_ROW;
3066cdf0e10cSrcweir 			break;
3067cdf0e10cSrcweir 		case SID_QUERY_VIEW_ALIASES:
3068cdf0e10cSrcweir 			nRow = BROW_COLUMNALIAS_ROW;
3069cdf0e10cSrcweir 			break;
3070cdf0e10cSrcweir 		default:
3071cdf0e10cSrcweir 			// ????????????
3072cdf0e10cSrcweir 			nRow = 0;
3073cdf0e10cSrcweir 			break;
3074cdf0e10cSrcweir 	}
3075cdf0e10cSrcweir 	m_pSelectionBox->SetRowVisible(nRow,_bEnable);
3076cdf0e10cSrcweir 	m_pSelectionBox->Invalidate();
3077cdf0e10cSrcweir }
3078cdf0e10cSrcweir // -----------------------------------------------------------------------------
isSlotEnabled(sal_Int32 _nSlotId)3079cdf0e10cSrcweir sal_Bool OQueryDesignView::isSlotEnabled(sal_Int32 _nSlotId)
3080cdf0e10cSrcweir {
3081cdf0e10cSrcweir 	sal_uInt16 nRow;
3082cdf0e10cSrcweir 	switch (_nSlotId)
3083cdf0e10cSrcweir 	{
3084cdf0e10cSrcweir 		case SID_QUERY_VIEW_FUNCTIONS:
3085cdf0e10cSrcweir 			nRow = BROW_FUNCTION_ROW;
3086cdf0e10cSrcweir 			break;
3087cdf0e10cSrcweir 		case SID_QUERY_VIEW_TABLES:
3088cdf0e10cSrcweir 			nRow = BROW_TABLE_ROW;
3089cdf0e10cSrcweir 			break;
3090cdf0e10cSrcweir 		case SID_QUERY_VIEW_ALIASES:
3091cdf0e10cSrcweir 			nRow = BROW_COLUMNALIAS_ROW;
3092cdf0e10cSrcweir 			break;
3093cdf0e10cSrcweir 		default:
3094cdf0e10cSrcweir 			// ?????????
3095cdf0e10cSrcweir 			nRow = 0;
3096cdf0e10cSrcweir 			break;
3097cdf0e10cSrcweir 	}
3098cdf0e10cSrcweir 	return m_pSelectionBox->IsRowVisible(nRow);
3099cdf0e10cSrcweir }
3100cdf0e10cSrcweir // -----------------------------------------------------------------------------
SaveUIConfig()3101cdf0e10cSrcweir void OQueryDesignView::SaveUIConfig()
3102cdf0e10cSrcweir {
3103cdf0e10cSrcweir 	OQueryController& rCtrl = static_cast<OQueryController&>(getController());
3104cdf0e10cSrcweir 	rCtrl.SaveTabWinsPosSize( m_pTableView->GetTabWinMap(), m_pScrollWindow->GetHScrollBar()->GetThumbPos(), m_pScrollWindow->GetVScrollBar()->GetThumbPos() );
3105cdf0e10cSrcweir 	//	rCtrl.SaveTabFieldsWidth( m_pSelectionBox );
3106cdf0e10cSrcweir 	rCtrl.setVisibleRows( m_pSelectionBox->GetNoneVisibleRows() );
3107cdf0e10cSrcweir     if ( m_aSplitter.GetSplitPosPixel() != 0 )
3108cdf0e10cSrcweir 	    rCtrl.setSplitPos( m_aSplitter.GetSplitPosPixel() );
3109cdf0e10cSrcweir }
3110cdf0e10cSrcweir // -----------------------------------------------------------------------------
getPredicateTreeFromEntry(OTableFieldDescRef pEntry,const String & _sCriteria,::rtl::OUString & _rsErrorMessage,Reference<XPropertySet> & _rxColumn) const3111cdf0e10cSrcweir OSQLParseNode* OQueryDesignView::getPredicateTreeFromEntry(OTableFieldDescRef pEntry,
3112cdf0e10cSrcweir 														   const String& _sCriteria,
3113cdf0e10cSrcweir 														   ::rtl::OUString& _rsErrorMessage,
3114cdf0e10cSrcweir 														   Reference<XPropertySet>& _rxColumn) const
3115cdf0e10cSrcweir {
3116cdf0e10cSrcweir 	OSL_ENSURE(pEntry.isValid(),"Entry is null!");
3117cdf0e10cSrcweir 	if(!pEntry.isValid())
3118cdf0e10cSrcweir 		return NULL;
3119cdf0e10cSrcweir 	Reference< XConnection> xConnection = static_cast<OQueryController&>(getController()).getConnection();
3120cdf0e10cSrcweir 	if(!xConnection.is())
3121cdf0e10cSrcweir 		return NULL;
3122cdf0e10cSrcweir 
3123cdf0e10cSrcweir 	::connectivity::OSQLParser& rParser( static_cast<OQueryController&>(getController()).getParser() );
3124cdf0e10cSrcweir 	OQueryTableWindow* pWin = static_cast<OQueryTableWindow*>(pEntry->GetTabWindow());
3125cdf0e10cSrcweir 
3126cdf0e10cSrcweir 	String sTest(_sCriteria);
3127cdf0e10cSrcweir 	// special handling for functions
3128cdf0e10cSrcweir 	if ( pEntry->GetFunctionType() & (FKT_OTHER | FKT_AGGREGATE | FKT_NUMERIC) )
3129cdf0e10cSrcweir 	{
3130cdf0e10cSrcweir 		// we have a function here so we have to distinguish the type of return value
3131cdf0e10cSrcweir 		String sFunction;
3132cdf0e10cSrcweir 		if ( pEntry->isNumericOrAggreateFunction() )
3133cdf0e10cSrcweir 			sFunction = pEntry->GetFunction();
3134cdf0e10cSrcweir 
3135cdf0e10cSrcweir         if ( !sFunction.Len() )
3136cdf0e10cSrcweir 			sFunction = pEntry->GetField();
3137cdf0e10cSrcweir 
3138cdf0e10cSrcweir 		if(sFunction.GetTokenCount('(') > 1)
3139cdf0e10cSrcweir 			sFunction = sFunction.GetToken(0,'('); // this should be the name of the function
3140cdf0e10cSrcweir 
3141cdf0e10cSrcweir 		sal_Int32 nType = ::connectivity::OSQLParser::getFunctionReturnType(sFunction,&rParser.getContext());
3142cdf0e10cSrcweir 		if ( nType == DataType::OTHER || (!sFunction.Len() && pEntry->isNumericOrAggreateFunction()) )
3143cdf0e10cSrcweir 		{
3144cdf0e10cSrcweir 			// first try the international version
3145cdf0e10cSrcweir 			::rtl::OUString sSql;
3146cdf0e10cSrcweir 			sSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SELECT * "));
3147cdf0e10cSrcweir 			sSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" FROM x WHERE "));
3148cdf0e10cSrcweir 			sSql += pEntry->GetField();
3149cdf0e10cSrcweir 			sSql += _sCriteria;
3150cdf0e10cSrcweir             ::std::auto_ptr<OSQLParseNode> pParseNode( rParser.parseTree( _rsErrorMessage, sSql, sal_True ) );
3151cdf0e10cSrcweir 			nType = DataType::DOUBLE;
3152cdf0e10cSrcweir 			if ( pParseNode.get() )
3153cdf0e10cSrcweir 			{
3154cdf0e10cSrcweir 				OSQLParseNode* pColumnRef = pParseNode->getByRule(OSQLParseNode::column_ref);
3155cdf0e10cSrcweir 				if ( pColumnRef )
3156cdf0e10cSrcweir 				{
3157cdf0e10cSrcweir 					OTableFieldDescRef aField = new OTableFieldDesc();
3158cdf0e10cSrcweir 					if ( eOk == FillDragInfo(this,pColumnRef,aField) )
3159cdf0e10cSrcweir 					{
3160cdf0e10cSrcweir 						nType = aField->GetDataType();
3161cdf0e10cSrcweir 					}
3162cdf0e10cSrcweir 				}
3163cdf0e10cSrcweir 			}
3164cdf0e10cSrcweir 		}
3165cdf0e10cSrcweir 
3166cdf0e10cSrcweir 		Reference<XDatabaseMetaData> xMeta = xConnection->getMetaData();
3167cdf0e10cSrcweir 		parse::OParseColumn* pColumn = new parse::OParseColumn(	pEntry->GetField(),
3168cdf0e10cSrcweir 																::rtl::OUString(),
3169cdf0e10cSrcweir 																::rtl::OUString(),
3170cdf0e10cSrcweir                                                                 ::rtl::OUString(),
3171cdf0e10cSrcweir 																ColumnValue::NULLABLE_UNKNOWN,
3172cdf0e10cSrcweir 																0,
3173cdf0e10cSrcweir 																0,
3174cdf0e10cSrcweir 																nType,
3175cdf0e10cSrcweir 																sal_False,
3176cdf0e10cSrcweir 																sal_False,
3177cdf0e10cSrcweir 																xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers());
3178cdf0e10cSrcweir 		_rxColumn = pColumn;
3179cdf0e10cSrcweir 		pColumn->setFunction(sal_True);
3180cdf0e10cSrcweir 		pColumn->setRealName(pEntry->GetField());
3181cdf0e10cSrcweir 	}
3182cdf0e10cSrcweir 	else
3183cdf0e10cSrcweir 	{
3184cdf0e10cSrcweir 		if (pWin)
3185cdf0e10cSrcweir 		{
3186cdf0e10cSrcweir 			Reference<XNameAccess> xColumns = pWin->GetOriginalColumns();
3187cdf0e10cSrcweir 			if (xColumns.is() && xColumns->hasByName(pEntry->GetField()))
3188cdf0e10cSrcweir 				xColumns->getByName(pEntry->GetField()) >>= _rxColumn;
3189cdf0e10cSrcweir 		}
3190cdf0e10cSrcweir 	}
3191cdf0e10cSrcweir 
3192cdf0e10cSrcweir 	OSQLParseNode* pParseNode = rParser.predicateTree(	_rsErrorMessage,
3193cdf0e10cSrcweir 														sTest,
3194cdf0e10cSrcweir 														static_cast<OQueryController&>(getController()).getNumberFormatter(),
3195cdf0e10cSrcweir 														_rxColumn);
3196cdf0e10cSrcweir 	return pParseNode;
3197cdf0e10cSrcweir }
3198cdf0e10cSrcweir // -----------------------------------------------------------------------------
GetFocus()3199cdf0e10cSrcweir void OQueryDesignView::GetFocus()
3200cdf0e10cSrcweir {
3201cdf0e10cSrcweir 	OQueryView::GetFocus();
3202cdf0e10cSrcweir 	if ( m_pSelectionBox && !m_pSelectionBox->HasChildPathFocus() )
3203cdf0e10cSrcweir 	{
3204cdf0e10cSrcweir 		// first we have to deactivate the current cell to refill when nescessary
3205cdf0e10cSrcweir 		m_pSelectionBox->DeactivateCell();
3206cdf0e10cSrcweir 		m_pSelectionBox->ActivateCell(m_pSelectionBox->GetCurRow(), m_pSelectionBox->GetCurColumnId());
3207cdf0e10cSrcweir 		m_pSelectionBox->GrabFocus();
3208cdf0e10cSrcweir 	}
3209cdf0e10cSrcweir }
3210cdf0e10cSrcweir // -----------------------------------------------------------------------------
reset()3211cdf0e10cSrcweir void OQueryDesignView::reset()
3212cdf0e10cSrcweir {
3213cdf0e10cSrcweir 	m_pTableView->ClearAll();
3214cdf0e10cSrcweir 	m_pTableView->ReSync();
3215cdf0e10cSrcweir }
3216cdf0e10cSrcweir // -----------------------------------------------------------------------------
setNoneVisbleRow(sal_Int32 _nRows)3217cdf0e10cSrcweir void OQueryDesignView::setNoneVisbleRow(sal_Int32 _nRows)
3218cdf0e10cSrcweir {
3219cdf0e10cSrcweir 	m_pSelectionBox->SetNoneVisbleRow(_nRows);
3220cdf0e10cSrcweir }
3221cdf0e10cSrcweir 
3222cdf0e10cSrcweir // -----------------------------------------------------------------------------
initByFieldDescriptions(const Sequence<PropertyValue> & i_rFieldDescriptions)3223cdf0e10cSrcweir void OQueryDesignView::initByFieldDescriptions( const Sequence< PropertyValue >& i_rFieldDescriptions )
3224cdf0e10cSrcweir {
3225cdf0e10cSrcweir 	OQueryController& rController = static_cast< OQueryController& >( getController() );
3226cdf0e10cSrcweir 
3227cdf0e10cSrcweir 	m_pSelectionBox->PreFill();
3228cdf0e10cSrcweir 	m_pSelectionBox->SetReadOnly( rController.isReadOnly() );
3229cdf0e10cSrcweir 	m_pSelectionBox->Fill();
3230cdf0e10cSrcweir 
3231cdf0e10cSrcweir     for (   const PropertyValue* field = i_rFieldDescriptions.getConstArray();
3232cdf0e10cSrcweir             field != i_rFieldDescriptions.getConstArray() + i_rFieldDescriptions.getLength();
3233cdf0e10cSrcweir             ++field
3234cdf0e10cSrcweir         )
3235cdf0e10cSrcweir     {
3236cdf0e10cSrcweir         ::vos::ORef< OTableFieldDesc > pField( new OTableFieldDesc() );
3237cdf0e10cSrcweir         pField->Load( *field, true );
3238cdf0e10cSrcweir         InsertField( pField, sal_True, sal_False );
3239cdf0e10cSrcweir     }
3240cdf0e10cSrcweir 
3241cdf0e10cSrcweir     rController.ClearUndoManager();
3242cdf0e10cSrcweir 	m_pSelectionBox->Invalidate();
3243cdf0e10cSrcweir }
3244cdf0e10cSrcweir 
3245cdf0e10cSrcweir // -----------------------------------------------------------------------------
initByParseIterator(::dbtools::SQLExceptionInfo * _pErrorInfo)3246cdf0e10cSrcweir bool OQueryDesignView::initByParseIterator( ::dbtools::SQLExceptionInfo* _pErrorInfo )
3247cdf0e10cSrcweir {
3248cdf0e10cSrcweir 	SqlParseError eErrorCode = eNativeMode;
3249cdf0e10cSrcweir 	m_rController.clearError();
3250cdf0e10cSrcweir 
3251cdf0e10cSrcweir     try
3252cdf0e10cSrcweir     {
3253cdf0e10cSrcweir         eErrorCode = InitFromParseNodeImpl( this, m_pSelectionBox );
3254cdf0e10cSrcweir 
3255cdf0e10cSrcweir         if ( eErrorCode != eOk )
3256cdf0e10cSrcweir 	    {
3257cdf0e10cSrcweir 	        if ( !m_rController.hasError() )
3258cdf0e10cSrcweir                 m_rController.appendError( getParseErrorMessage( eErrorCode ) );
3259cdf0e10cSrcweir 
3260cdf0e10cSrcweir             if ( _pErrorInfo )
3261cdf0e10cSrcweir             {
3262cdf0e10cSrcweir                 *_pErrorInfo = m_rController.getError();
3263cdf0e10cSrcweir             }
3264cdf0e10cSrcweir             else
3265cdf0e10cSrcweir             {
3266cdf0e10cSrcweir                 m_rController.displayError();
3267cdf0e10cSrcweir             }
3268cdf0e10cSrcweir 	    }
3269cdf0e10cSrcweir     }
3270cdf0e10cSrcweir     catch ( const Exception& )
3271cdf0e10cSrcweir     {
3272cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
3273cdf0e10cSrcweir     }
3274cdf0e10cSrcweir 	return eErrorCode == eOk;
3275cdf0e10cSrcweir }
3276cdf0e10cSrcweir //------------------------------------------------------------------------------
fillFunctionInfo(const::connectivity::OSQLParseNode * pNode,const::rtl::OUString & sFunctionTerm,OTableFieldDescRef & aInfo)3277cdf0e10cSrcweir void OQueryDesignView::fillFunctionInfo(  const ::connectivity::OSQLParseNode* pNode
3278cdf0e10cSrcweir                                         ,const ::rtl::OUString& sFunctionTerm
3279cdf0e10cSrcweir                                         ,OTableFieldDescRef& aInfo)
3280cdf0e10cSrcweir {
3281cdf0e10cSrcweir     // get the type out of the funtion name
3282cdf0e10cSrcweir     OQueryController& rController = static_cast<OQueryController&>(getController());
3283cdf0e10cSrcweir 	sal_Int32 nDataType = DataType::DOUBLE;
3284cdf0e10cSrcweir 	::rtl::OUString sFieldName = sFunctionTerm;
3285cdf0e10cSrcweir 	OSQLParseNode* pFunctionName = pNode->getChild(0);
3286cdf0e10cSrcweir 	if ( !SQL_ISPUNCTUATION(pFunctionName,"{") )
3287cdf0e10cSrcweir 	{
3288cdf0e10cSrcweir 		if ( SQL_ISRULEOR2(pNode,length_exp,char_value_fct) )
3289cdf0e10cSrcweir 			pFunctionName = pFunctionName->getChild(0);
3290cdf0e10cSrcweir 
3291cdf0e10cSrcweir 		::rtl::OUString sFunctionName = pFunctionName->getTokenValue();
3292cdf0e10cSrcweir 		if ( !sFunctionName.getLength() )
3293cdf0e10cSrcweir 			sFunctionName = ::rtl::OStringToOUString(OSQLParser::TokenIDToStr(pFunctionName->getTokenID()),RTL_TEXTENCODING_UTF8);
3294cdf0e10cSrcweir 
3295cdf0e10cSrcweir 		nDataType = OSQLParser::getFunctionReturnType(
3296cdf0e10cSrcweir 							sFunctionName
3297cdf0e10cSrcweir 							,&rController.getParser().getContext());
3298cdf0e10cSrcweir 	}
3299cdf0e10cSrcweir 	aInfo->SetDataType(nDataType);
3300cdf0e10cSrcweir 	aInfo->SetFieldType(TAB_NORMAL_FIELD);
3301cdf0e10cSrcweir 	aInfo->SetField(sFieldName);
3302cdf0e10cSrcweir 	aInfo->SetTabWindow(NULL);
3303cdf0e10cSrcweir }
3304cdf0e10cSrcweir // -----------------------------------------------------------------------------
3305