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