xref: /AOO41X/main/dbaccess/source/ui/dlg/queryfilter.cxx (revision 96de54900b79e13b861fbc62cbf36018b54e21b7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbaccess.hxx"
26 
27 #ifndef DBAUI_QUERYFILTER_HXX
28 #include "queryfilter.hxx"
29 #endif
30 #ifndef _DBAUI_MODULE_DBU_HXX_
31 #include "moduledbu.hxx"
32 #endif
33 #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
34 #include <com/sun/star/sdbc/DataType.hpp>
35 #endif
36 #ifndef _COM_SUN_STAR_UTIL_DATE_HPP_
37 #include <com/sun/star/util/Date.hpp>
38 #endif
39 #ifndef _COM_SUN_STAR_UTIL_DATETIME_HPP_
40 #include <com/sun/star/util/DateTime.hpp>
41 #endif
42 #ifndef _COM_SUN_STAR_UTIL_TIME_HPP_
43 #include <com/sun/star/util/Time.hpp>
44 #endif
45 #ifndef _COM_SUN_STAR_SDB_XSQLQUERYCOMPOSER_HPP_
46 #include <com/sun/star/sdb/XSQLQueryComposer.hpp>
47 #endif
48 #ifndef _COM_SUN_STAR_SDBC_COLUMNSEARCH_HPP_
49 #include <com/sun/star/sdbc/ColumnSearch.hpp>
50 #endif
51 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
52 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
53 #endif
54 #ifndef _COM_SUN_STAR_SDB_SQLFILTEROPERATOR_HPP_
55 #include <com/sun/star/sdb/SQLFilterOperator.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
58 #include <com/sun/star/sdbc/XRow.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_SDBC_XRESULTSET_HPP_
61 #include <com/sun/star/sdbc/XResultSet.hpp>
62 #endif
63 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
64 #include <com/sun/star/container/XNameAccess.hpp>
65 #endif
66 #ifndef _TOOLS_DEBUG_HXX
67 #include <tools/debug.hxx>
68 #endif
69 #ifndef TOOLS_DIAGNOSE_EX_H
70 #include <tools/diagnose_ex.h>
71 #endif
72 #ifndef TOOLS_DIAGNOSE_EX_H
73 #include <tools/diagnose_ex.h>
74 #endif
75 #ifndef _DBAUI_MODULE_DBU_HXX_
76 #include "moduledbu.hxx"
77 #endif
78 #ifndef _CONNECTIVITY_PARSE_SQLITERATOR_HXX_
79 #include <connectivity/sqliterator.hxx>
80 #endif
81 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
82 #include <connectivity/dbtools.hxx>
83 #endif
84 #ifndef DBAUI_QUERYFILTER_HRC
85 #include "queryfilter.hrc"
86 #endif
87 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
88 #include "dbustrings.hrc"
89 #endif
90 #ifndef _COM_SUN_STAR_SDB_XSINGLESELECTQUERYCOMPOSER_HPP_
91 #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
92 #endif
93 #ifndef _COM_SUN_STAR_SDB_SQLFILTEROPERATOR_HPP_
94 #include <com/sun/star/sdb/SQLFilterOperator.hpp>
95 #endif
96 
97 using namespace dbaui;
98 using namespace connectivity;
99 using namespace ::com::sun::star::uno;
100 using namespace ::com::sun::star::lang;
101 using namespace ::com::sun::star::container;
102 using namespace ::com::sun::star::util;
103 using namespace ::com::sun::star::sdb;
104 using namespace ::com::sun::star::sdbc;
105 using namespace ::com::sun::star::sdbcx;
106 using namespace ::com::sun::star::sdb;
107 using namespace ::com::sun::star::beans;
108 
109 //------------------------------------------------------------------------------
Replace_OS_PlaceHolder(String & aString)110 void Replace_OS_PlaceHolder(String& aString)
111 {
112     while (aString.SearchAndReplace( '*', '%' ) != STRING_NOTFOUND) ;
113     while (aString.SearchAndReplace( '?', '_' ) != STRING_NOTFOUND) ;
114 }
115 
116 //------------------------------------------------------------------------------
Replace_SQL_PlaceHolder(String & aString)117 void Replace_SQL_PlaceHolder(String& aString)
118 {
119     while (aString.SearchAndReplace( '%', '*' ) != STRING_NOTFOUND) ;
120     while (aString.SearchAndReplace( '_', '?' ) != STRING_NOTFOUND) ;
121 }
122 
123 DBG_NAME(DlgFilterCrit);
124 //------------------------------------------------------------------------------
DlgFilterCrit(Window * pParent,const Reference<XMultiServiceFactory> & _rxORB,const Reference<XConnection> & _rxConnection,const Reference<XSingleSelectQueryComposer> & _rxComposer,const Reference<XNameAccess> & _rxCols)125 DlgFilterCrit::DlgFilterCrit(Window * pParent,
126                              const Reference< XMultiServiceFactory >& _rxORB,
127                              const Reference< XConnection>& _rxConnection,
128                              const Reference< XSingleSelectQueryComposer >& _rxComposer,
129                              const Reference< XNameAccess>& _rxCols
130                              )
131     :ModalDialog( pParent, ModuleRes( DLG_FILTERCRIT ) )
132     ,aLB_WHEREFIELD1    ( this, ModuleRes( LB_WHEREFIELD1 ) )
133     ,aLB_WHERECOMP1     ( this, ModuleRes( LB_WHERECOMP1 ) )
134     ,aET_WHEREVALUE1    ( this, ModuleRes( ET_WHEREVALUE1 ) )
135     ,aLB_WHERECOND2     ( this, ModuleRes( LB_WHERECOND2 ) )
136     ,aLB_WHEREFIELD2    ( this, ModuleRes( LB_WHEREFIELD2 ) )
137     ,aLB_WHERECOMP2     ( this, ModuleRes( LB_WHERECOMP2 ) )
138     ,aET_WHEREVALUE2    ( this, ModuleRes( ET_WHEREVALUE2 ) )
139     ,aLB_WHERECOND3     ( this, ModuleRes( LB_WHERECOND3 ) )
140     ,aLB_WHEREFIELD3    ( this, ModuleRes( LB_WHEREFIELD3 ) )
141     ,aLB_WHERECOMP3     ( this, ModuleRes( LB_WHERECOMP3 ) )
142     ,aET_WHEREVALUE3    ( this, ModuleRes( ET_WHEREVALUE3 ) )
143     ,aFT_WHEREFIELD     ( this, ModuleRes( FT_WHEREFIELD ) )
144     ,aFT_WHERECOMP      ( this, ModuleRes( FT_WHERECOMP ) )
145     ,aFT_WHEREVALUE     ( this, ModuleRes( FT_WHEREVALUE ) )
146     ,aFT_WHEREOPER      ( this, ModuleRes( FT_WHEREOPER ) )
147     ,aFL_FIELDS         ( this, ModuleRes( FL_FIELDS ) )
148     ,aBT_OK             ( this, ModuleRes( BT_OK ) )
149     ,aBT_CANCEL         ( this, ModuleRes( BT_CANCEL ) )
150     ,aBT_HELP           ( this, ModuleRes( BT_HELP ) )
151     ,aSTR_NOENTRY       ( ModuleRes( STR_NOENTRY ) )
152     ,aSTR_COMPARE_OPERATORS( ModuleRes( STR_COMPARE_OPERATORS ) )
153     ,m_xQueryComposer(_rxComposer)
154     ,m_xColumns( _rxCols )
155     ,m_xConnection( _rxConnection )
156     ,m_xMetaData( _rxConnection->getMetaData() )
157     ,m_aPredicateInput( _rxORB, _rxConnection, getParseContext() )
158 {
159     DBG_CTOR(DlgFilterCrit,NULL);
160     // Den String fuer noEntry in die ListBoxen der Feldnamen schreiben
161     aLB_WHEREFIELD1.InsertEntry( aSTR_NOENTRY );
162     aLB_WHEREFIELD2.InsertEntry( aSTR_NOENTRY );
163     aLB_WHEREFIELD3.InsertEntry( aSTR_NOENTRY );
164 
165     try
166     {
167         // ... sowie auch die restlichen Felder
168         Sequence< ::rtl::OUString> aNames = m_xColumns->getElementNames();
169         const ::rtl::OUString* pIter = aNames.getConstArray();
170         const ::rtl::OUString* pEnd   = pIter + aNames.getLength();
171         Reference<XPropertySet> xColumn;
172         for(;pIter != pEnd;++pIter)
173         {
174             try
175             {
176                 xColumn.set( m_xColumns->getByName( *pIter ), UNO_QUERY_THROW );
177 
178                 sal_Int32 nDataType( 0 );
179                 OSL_VERIFY( xColumn->getPropertyValue( PROPERTY_TYPE ) >>= nDataType );
180                 sal_Int32 eColumnSearch = ::dbtools::getSearchColumnFlag( m_xConnection, nDataType );
181                 if ( eColumnSearch == ColumnSearch::NONE )
182                     continue;
183 
184                 sal_Bool bIsSearchable( sal_True );
185                 OSL_VERIFY( xColumn->getPropertyValue( PROPERTY_ISSEARCHABLE ) >>= bIsSearchable );
186                 if ( !bIsSearchable )
187                     continue;
188             }
189             catch( const Exception& )
190             {
191                 DBG_UNHANDLED_EXCEPTION();
192             }
193             aLB_WHEREFIELD1.InsertEntry( *pIter );
194             aLB_WHEREFIELD2.InsertEntry( *pIter );
195             aLB_WHEREFIELD3.InsertEntry( *pIter );
196         }
197 
198         Reference<XNameAccess> xSelectColumns = Reference<XColumnsSupplier>(m_xQueryComposer,UNO_QUERY)->getColumns();
199         aNames = xSelectColumns->getElementNames();
200         pIter = aNames.getConstArray();
201         pEnd   = pIter + aNames.getLength();
202         for(;pIter != pEnd;++pIter)
203         {
204             // don't insert a column name twice
205             if ( !m_xColumns->hasByName(*pIter) )
206             {
207                 xColumn.set(xSelectColumns->getByName(*pIter),UNO_QUERY);
208                 OSL_ENSURE(xColumn.is(),"DlgFilterCrit::DlgFilterCrit: Column is null!");
209                 sal_Int32 nDataType(0);
210                 xColumn->getPropertyValue(PROPERTY_TYPE) >>= nDataType;
211                 sal_Int32 eColumnSearch = dbtools::getSearchColumnFlag(m_xConnection,nDataType);
212                 // TODO
213                 // !pColumn->IsFunction()
214                 if(eColumnSearch != ColumnSearch::NONE)
215                 {
216                     aLB_WHEREFIELD1.InsertEntry( *pIter );
217                     aLB_WHEREFIELD2.InsertEntry( *pIter );
218                     aLB_WHEREFIELD3.InsertEntry( *pIter );
219                 }
220             }
221         }
222         // initialize the listboxes with noEntry
223         aLB_WHEREFIELD1.SelectEntryPos(0);
224         aLB_WHEREFIELD2.SelectEntryPos(0);
225         aLB_WHEREFIELD3.SelectEntryPos(0);
226 
227         // insert the criteria into the dialog
228         Sequence<Sequence<PropertyValue > > aValues = m_xQueryComposer->getStructuredFilter();
229         fillLines(aValues);
230         aValues = m_xQueryComposer->getStructuredHavingClause();
231         fillLines(aValues);
232 
233     }
234     catch(Exception&)
235     {
236         FreeResource();
237         throw;
238     }
239 
240     EnableLines();
241 
242     aLB_WHEREFIELD1.SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectHdl));
243     aLB_WHEREFIELD2.SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectHdl));
244     aLB_WHEREFIELD3.SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectHdl));
245 
246     aLB_WHERECOMP1.SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectCompHdl));
247     aLB_WHERECOMP2.SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectCompHdl));
248     aLB_WHERECOMP3.SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectCompHdl));
249 
250     aET_WHEREVALUE1.SetLoseFocusHdl( LINK( this, DlgFilterCrit, PredicateLoseFocus ) );
251     aET_WHEREVALUE2.SetLoseFocusHdl( LINK( this, DlgFilterCrit, PredicateLoseFocus ) );
252     aET_WHEREVALUE3.SetLoseFocusHdl( LINK( this, DlgFilterCrit, PredicateLoseFocus ) );
253 
254     if ( aET_WHEREVALUE1.IsEnabled() )
255         aET_WHEREVALUE1.GrabFocus();
256 
257     FreeResource();
258 }
259 //------------------------------------------------------------------------------
~DlgFilterCrit()260 DlgFilterCrit::~DlgFilterCrit()
261 {
262     DBG_DTOR(DlgFilterCrit,NULL);
263 }
264 
265 #define LbText(x)       ((x).GetSelectEntry())
266 #define LbPos(x)        ((x).GetSelectEntryPos())
267 
268 //------------------------------------------------------------------------------
GetOSQLPredicateType(const String & _rSelectedPredicate) const269 sal_Int32 DlgFilterCrit::GetOSQLPredicateType( const String& _rSelectedPredicate ) const
270 {
271     sal_Int32 nPredicateIndex = -1;
272     for ( xub_StrLen i=0; i<aSTR_COMPARE_OPERATORS.GetTokenCount(); ++i)
273         if ( aSTR_COMPARE_OPERATORS.GetToken(i) == _rSelectedPredicate )
274         {
275             nPredicateIndex = i;
276             break;
277         }
278 
279     sal_Int32 nPredicateType = SQLFilterOperator::NOT_SQLNULL;
280     switch ( nPredicateIndex )
281     {
282     case 0:
283         nPredicateType = SQLFilterOperator::EQUAL;
284         break;
285     case 1:
286         nPredicateType = SQLFilterOperator::NOT_EQUAL;
287         break;
288     case 2:
289         nPredicateType = SQLFilterOperator::LESS;
290         break;
291     case 3:
292         nPredicateType = SQLFilterOperator::LESS_EQUAL;
293         break;
294     case 4:
295         nPredicateType = SQLFilterOperator::GREATER;
296         break;
297     case 5:
298         nPredicateType = SQLFilterOperator::GREATER_EQUAL;
299         break;
300     case 6:
301         nPredicateType = SQLFilterOperator::LIKE;
302         break;
303     case 7:
304         nPredicateType = SQLFilterOperator::NOT_LIKE;
305         break;
306     case 8:
307         nPredicateType = SQLFilterOperator::SQLNULL;
308         break;
309     case 9:
310         nPredicateType = SQLFilterOperator::NOT_SQLNULL;
311         break;
312     default:
313         OSL_ENSURE( false, "DlgFilterCrit::GetOSQLPredicateType: unknown predicate string!" );
314         break;
315     }
316 
317     return nPredicateType;
318 }
319 //------------------------------------------------------------------------------
GetSelectionPos(sal_Int32 eType,const ListBox & rListBox) const320 sal_uInt16 DlgFilterCrit::GetSelectionPos(sal_Int32 eType,const ListBox& rListBox) const
321 {
322     sal_uInt16 nPos;
323     switch(eType)
324     {
325         case SQLFilterOperator::EQUAL:
326             nPos = 0;
327             break;
328         case SQLFilterOperator::NOT_EQUAL:
329             nPos = 1;
330             break;
331         case SQLFilterOperator::LESS:
332             nPos = 2;
333             break;
334         case SQLFilterOperator::LESS_EQUAL:
335             nPos = 3;
336             break;
337         case SQLFilterOperator::GREATER:
338             nPos = 4;
339             break;
340         case SQLFilterOperator::GREATER_EQUAL:
341             nPos = 5;
342             break;
343         case SQLFilterOperator::NOT_LIKE:
344             nPos = rListBox.GetEntryCount() > 2 ? rListBox.GetEntryCount()-3 : 0;
345             break;
346         case SQLFilterOperator::LIKE:
347             nPos = rListBox.GetEntryCount() > 2 ? rListBox.GetEntryCount()-4 : 1;
348             break;
349         case SQLFilterOperator::SQLNULL:
350             nPos = rListBox.GetEntryCount()-2;
351             break;
352         case SQLFilterOperator::NOT_SQLNULL:
353             nPos = rListBox.GetEntryCount()-1;
354             break;
355         default:
356             //  TODO  What value should this be?
357             nPos = 0;
358             break;
359     }
360     return nPos;
361 }
362 // -----------------------------------------------------------------------------
getCondition(const ListBox & _rField,const ListBox & _rComp,const Edit & _rValue,PropertyValue & _rFilter) const363 sal_Bool DlgFilterCrit::getCondition(const ListBox& _rField,const ListBox& _rComp,const Edit& _rValue,PropertyValue& _rFilter) const
364 {
365     sal_Bool bHaving = sal_False;
366     try
367     {
368         ::rtl::OUString sTableName;
369         sal_Bool bFunction = sal_False;
370         _rFilter.Name = _rField.GetSelectEntry();
371         Reference< XPropertySet > xColumn = getQueryColumn(_rFilter.Name);
372         if ( xColumn.is() )
373         {
374             Reference< XPropertySetInfo > xInfo = xColumn->getPropertySetInfo();
375             if ( xInfo->hasPropertyByName(PROPERTY_REALNAME) )
376             {
377                 if ( xInfo->hasPropertyByName(PROPERTY_TABLENAME) )
378                 {
379                     xColumn->getPropertyValue(PROPERTY_TABLENAME)   >>= sTableName;
380                     if ( sTableName.getLength() )
381                     {
382                         // properly quote all parts of the table name, so e.g. <schema>.<table> becomes "<schema>"."<table>"
383                         ::rtl::OUString aCatlog,aSchema,aTable;
384                         ::dbtools::qualifiedNameComponents( m_xMetaData, sTableName, aCatlog, aSchema, aTable, ::dbtools::eInDataManipulation );
385                         sTableName = ::dbtools::composeTableName( m_xMetaData, aCatlog, aSchema, aTable, sal_True, ::dbtools::eInDataManipulation );
386                     }
387                 }
388                 xColumn->getPropertyValue(PROPERTY_REALNAME)    >>= _rFilter.Name;
389                 static ::rtl::OUString sAgg(RTL_CONSTASCII_USTRINGPARAM("AggregateFunction"));
390                 if ( xInfo->hasPropertyByName(sAgg) )
391                     xColumn->getPropertyValue(sAgg) >>= bHaving;
392                 static ::rtl::OUString sFunction(RTL_CONSTASCII_USTRINGPARAM("Function"));
393                 if ( xInfo->hasPropertyByName(sFunction) )
394                     xColumn->getPropertyValue(sFunction) >>= bFunction;
395             }
396             if ( !bFunction )
397             {
398                 const ::rtl::OUString aQuote    = m_xMetaData.is() ? m_xMetaData->getIdentifierQuoteString() : ::rtl::OUString();
399                 _rFilter.Name = ::dbtools::quoteName(aQuote,_rFilter.Name);
400                 if ( sTableName.getLength() )
401                 {
402                     static ::rtl::OUString sSep(RTL_CONSTASCII_USTRINGPARAM("."));
403                     sTableName += sSep;
404                     sTableName += _rFilter.Name;
405                     _rFilter.Name = sTableName;
406                 }
407             } // if ( !bFunction )
408         }
409     }
410     catch(Exception)
411     {
412     }
413 
414     _rFilter.Handle = GetOSQLPredicateType( _rComp.GetSelectEntry() );
415     if ( SQLFilterOperator::SQLNULL != _rFilter.Handle && _rFilter.Handle != SQLFilterOperator::NOT_SQLNULL )
416     {
417         String sPredicateValue = m_aPredicateInput.getPredicateValue( _rValue.GetText(), getMatchingColumn( _rValue ), sal_True );
418         ::Replace_OS_PlaceHolder( sPredicateValue );
419         _rFilter.Value <<= ::rtl::OUString(sPredicateValue);
420     }
421     return bHaving;
422 }
423 
424 //------------------------------------------------------------------------------
getColumn(const::rtl::OUString & _rFieldName) const425 Reference< XPropertySet > DlgFilterCrit::getColumn( const ::rtl::OUString& _rFieldName ) const
426 {
427     Reference< XPropertySet > xColumn;
428     try
429     {
430         if ( m_xColumns.is() && m_xColumns->hasByName( _rFieldName ) )
431             m_xColumns->getByName( _rFieldName ) >>= xColumn;
432 
433         Reference< XNameAccess> xColumns = Reference< XColumnsSupplier >(m_xQueryComposer,UNO_QUERY)->getColumns();
434         if ( xColumns.is() && !xColumn.is() )
435         {
436             Sequence< ::rtl::OUString> aSeq = xColumns->getElementNames();
437             const ::rtl::OUString* pIter = aSeq.getConstArray();
438             const ::rtl::OUString* pEnd   = pIter + aSeq.getLength();
439             for(;pIter != pEnd;++pIter)
440             {
441                 Reference<XPropertySet> xProp(xColumns->getByName(*pIter),UNO_QUERY);
442                 if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME) )
443                 {
444                     ::rtl::OUString sRealName;
445                     xProp->getPropertyValue(PROPERTY_REALNAME)  >>= sRealName;
446                     if ( sRealName == _rFieldName )
447                     {
448                         if ( m_xColumns.is() && m_xColumns->hasByName( *pIter ) )
449                             m_xColumns->getByName( *pIter ) >>= xColumn;
450                         break;
451                     }
452                 }
453             }
454         }
455     }
456     catch( const Exception& )
457     {
458         DBG_UNHANDLED_EXCEPTION();
459     }
460 
461     return xColumn;
462 }
463 //------------------------------------------------------------------------------
getQueryColumn(const::rtl::OUString & _rFieldName) const464 Reference< XPropertySet > DlgFilterCrit::getQueryColumn( const ::rtl::OUString& _rFieldName ) const
465 {
466     Reference< XPropertySet > xColumn;
467     try
468     {
469         Reference< XNameAccess> xColumns = Reference< XColumnsSupplier >(m_xQueryComposer,UNO_QUERY)->getColumns();
470         if ( xColumns.is() && xColumns->hasByName( _rFieldName ) )
471             xColumns->getByName( _rFieldName ) >>= xColumn;
472     }
473     catch( const Exception& )
474     {
475         DBG_UNHANDLED_EXCEPTION();
476     }
477 
478     return xColumn;
479 }
480 
481 //------------------------------------------------------------------------------
getMatchingColumn(const Edit & _rValueInput) const482 Reference< XPropertySet > DlgFilterCrit::getMatchingColumn( const Edit& _rValueInput ) const
483 {
484     // the name
485     ::rtl::OUString sField;
486     if ( &_rValueInput == &aET_WHEREVALUE1 )
487     {
488         sField = aLB_WHEREFIELD1.GetSelectEntry();
489     }
490     else if ( &_rValueInput == &aET_WHEREVALUE2 )
491     {
492         sField = aLB_WHEREFIELD2.GetSelectEntry();
493     }
494     else if ( &_rValueInput == &aET_WHEREVALUE3 )
495     {
496         sField = aLB_WHEREFIELD3.GetSelectEntry();
497     }
498     else {
499         DBG_ERROR( "DlgFilterCrit::getMatchingColumn: invalid event source!" );
500     }
501 
502     // the field itself
503     return getColumn( sField );
504 }
505 
506 //------------------------------------------------------------------------------
IMPL_LINK(DlgFilterCrit,PredicateLoseFocus,Edit *,_pField)507 IMPL_LINK( DlgFilterCrit, PredicateLoseFocus, Edit*, _pField )
508 {
509     DBG_ASSERT( _pField, "DlgFilterCrit::PredicateLoseFocus: invalid event source!" );
510     if ( _pField )
511     {
512         // retrieve the field affected
513         Reference< XPropertySet> xColumn( getMatchingColumn( *_pField ) );
514         // and normalize it's content
515         if ( xColumn.is() )
516         {
517             ::rtl::OUString sText( _pField->GetText() );
518             m_aPredicateInput.normalizePredicateString( sText, xColumn );
519             _pField->SetText( sText );
520         }
521     }
522 
523     return 0L;
524 }
525 
526 //------------------------------------------------------------------------------
527 //------------------------------------------------------------------------------
SetLine(sal_uInt16 nIdx,const PropertyValue & _rItem,sal_Bool _bOr)528 void DlgFilterCrit::SetLine( sal_uInt16 nIdx,const PropertyValue& _rItem,sal_Bool _bOr  )
529 {
530     DBG_CHKTHIS(DlgFilterCrit,NULL);
531     ::rtl::OUString aCondition;
532     _rItem.Value >>= aCondition;
533     String aStr = aCondition.getStr();
534     ::Replace_SQL_PlaceHolder(aStr);
535     aStr.EraseTrailingChars();
536 
537     Reference< XPropertySet > xColumn = getColumn( _rItem.Name );
538 
539     // remove the predicate from the condition
540     switch(_rItem.Handle)
541     {
542         case SQLFilterOperator::EQUAL:
543             //  aStr.Erase(0,1);
544             break;
545         case SQLFilterOperator::NOT_EQUAL:
546             aStr.Erase(0,2);
547             break;
548         case SQLFilterOperator::LESS:
549             aStr.Erase(0,1);
550             break;
551         case SQLFilterOperator::LESS_EQUAL:
552             aStr.Erase(0,2);
553             break;
554         case SQLFilterOperator::GREATER:
555             aStr.Erase(0,1);
556             break;
557         case SQLFilterOperator::GREATER_EQUAL:
558             aStr.Erase(0,2);
559             break;
560         case SQLFilterOperator::NOT_LIKE:
561             aStr.Erase(0,8);
562             break;
563         case SQLFilterOperator::LIKE:
564             aStr.Erase(0,4);
565             break;
566         case SQLFilterOperator::SQLNULL:
567             aStr.Erase(0,7);
568             break;
569         case SQLFilterOperator::NOT_SQLNULL:
570             aStr.Erase(0,11);
571             break;
572     }
573     aStr.EraseLeadingChars();
574 
575     // to make sure that we only set first three
576     ListBox* pColumnListControl =  NULL;
577     ListBox* pPredicateListControl = NULL;
578     Edit* pPredicateValueControl = NULL;
579     switch( nIdx )
580     {
581         case 0:
582             pColumnListControl = &aLB_WHEREFIELD1;
583             pPredicateListControl = &aLB_WHERECOMP1;
584             pPredicateValueControl = &aET_WHEREVALUE1;
585             break;
586 
587         case 1:
588             aLB_WHERECOND2.SelectEntryPos( _bOr ? 1 : 0 );
589 
590             pColumnListControl = &aLB_WHEREFIELD2;
591             pPredicateListControl = &aLB_WHERECOMP2;
592             pPredicateValueControl = &aET_WHEREVALUE2;
593             break;
594 
595         case 2:
596             aLB_WHERECOND3.SelectEntryPos( _bOr ? 1 : 0 );
597 
598             pColumnListControl = &aLB_WHEREFIELD3;
599             pPredicateListControl = &aLB_WHERECOMP3;
600             pPredicateValueControl = &aET_WHEREVALUE3;
601             break;
602     }
603 
604     if ( pColumnListControl && pPredicateListControl && pPredicateValueControl )
605     {
606         ::rtl::OUString sName;
607         if ( xColumn.is() )
608             xColumn->getPropertyValue(PROPERTY_NAME) >>= sName;
609         else
610             sName = _rItem.Name;
611         // select the appropriate field name
612         SelectField( *pColumnListControl, sName );
613         ListSelectHdl( pColumnListControl );
614 
615         // select the appropriate condition
616         pPredicateListControl->SelectEntryPos( GetSelectionPos( (sal_Int32)_rItem.Handle, *pPredicateListControl ) );
617 
618         // initially normalize this value
619         ::rtl::OUString aString( aStr );
620         m_aPredicateInput.normalizePredicateString( aString, xColumn );
621         pPredicateValueControl->SetText( aString );
622     }
623 }
624 
625 //------------------------------------------------------------------------------
SelectField(ListBox & rBox,const String & rField)626 void DlgFilterCrit::SelectField( ListBox& rBox, const String& rField )
627 {
628     DBG_CHKTHIS(DlgFilterCrit,NULL);
629     sal_uInt16 nCnt = rBox.GetEntryCount();
630     //  sal_Bool bCase = m_rIterator.TablesAreSensitive();
631 
632     for( sal_uInt16 i=0 ; i<nCnt ; i++ )
633     {
634         //  if(bCase ? rBox.GetEntry(i) == rField : rBox.GetEntry(i).EqualsIgnoreCaseAscii(rField))
635         if(rBox.GetEntry(i) == rField)
636         {
637             rBox.SelectEntryPos(i);
638             return;
639         }
640     }
641 
642     rBox.SelectEntryPos(0);
643 }
644 
645 //------------------------------------------------------------------------------
EnableLines()646 void DlgFilterCrit::EnableLines()
647 {
648     DBG_CHKTHIS(DlgFilterCrit,NULL);
649     // Enablen/Disablen ganzer Zeilen
650     if( LbPos(aLB_WHEREFIELD1) == 0 )
651     {
652         aLB_WHEREFIELD2.Disable();
653         aLB_WHERECOND2.Disable();
654         aLB_WHERECOMP2.Disable();
655         aET_WHEREVALUE2.Disable();
656 
657         aLB_WHEREFIELD3.Disable();
658         aLB_WHERECOND3.Disable();
659         aLB_WHERECOMP3.Disable();
660         aET_WHEREVALUE3.Disable();
661     }
662     else
663     {
664         aLB_WHEREFIELD2.Enable();
665         aLB_WHERECOND2.Enable();
666         aLB_WHERECOMP2.Enable();
667         aET_WHEREVALUE2.Enable();
668 
669         aLB_WHEREFIELD3.Enable();
670         aLB_WHERECOND3.Enable();
671         aLB_WHERECOMP3.Enable();
672         aET_WHEREVALUE3.Enable();
673     }
674 
675     if( LbPos(aLB_WHEREFIELD2) == 0 )
676     {
677         aLB_WHEREFIELD3.Disable();
678         aLB_WHERECOND3.Disable();
679         aLB_WHERECOMP3.Disable();
680         aET_WHEREVALUE3.Disable();
681     }
682     else
683     {
684         aLB_WHEREFIELD3.Enable();
685         aLB_WHERECOND3.Enable();
686         aLB_WHERECOMP3.Enable();
687         aET_WHEREVALUE3.Enable();
688     }
689 
690     // Vergleichsfeld gleich NOENTRY
691     if( LbPos(aLB_WHEREFIELD1) == 0 )
692     {
693         aLB_WHERECOMP1.Disable();
694         aET_WHEREVALUE1.Disable();
695     }
696     else
697     {
698         aLB_WHEREFIELD1.Enable();
699         aLB_WHERECOMP1.Enable();
700         aET_WHEREVALUE1.Enable();
701     }
702 
703     if( LbPos(aLB_WHEREFIELD2) == 0 )
704     {
705         aLB_WHERECOND2.Disable();
706         aLB_WHERECOMP2.Disable();
707         aET_WHEREVALUE2.Disable();
708     }
709     else
710     {
711         aLB_WHERECOND2.Enable();
712         aLB_WHEREFIELD2.Enable();
713         aLB_WHERECOMP2.Enable();
714         aET_WHEREVALUE2.Enable();
715     }
716 
717     if( LbPos(aLB_WHEREFIELD3) == 0 )
718     {
719         aLB_WHERECOND3.Disable();
720         aLB_WHERECOMP3.Disable();
721         aET_WHEREVALUE3.Disable();
722     }
723     else
724     {
725         aLB_WHERECOND3.Enable();
726         aLB_WHERECOND3.Enable();
727         aLB_WHEREFIELD3.Enable();
728         aLB_WHERECOMP3.Enable();
729         aET_WHEREVALUE3.Enable();
730     }
731 
732     // Vergleichsoperator gleich ISNULL oder ISNOTNULL
733     if(aLB_WHERECOMP1.GetEntryCount() > 2 &&
734         ((LbPos(aLB_WHERECOMP1) == aLB_WHERECOMP1.GetEntryCount()-1) ||
735          (LbPos(aLB_WHERECOMP1) == aLB_WHERECOMP1.GetEntryCount()-2)) )
736         aET_WHEREVALUE1.Disable();
737 
738     if(aLB_WHERECOMP2.GetEntryCount() > 2 &&
739         ((LbPos(aLB_WHERECOMP2) == aLB_WHERECOMP2.GetEntryCount()-1) ||
740          (LbPos(aLB_WHERECOMP2) == aLB_WHERECOMP2.GetEntryCount()-2)) )
741         aET_WHEREVALUE2.Disable();
742 
743     if(aLB_WHERECOMP3.GetEntryCount() > 2 &&
744         ((LbPos(aLB_WHERECOMP3) == aLB_WHERECOMP3.GetEntryCount()-1) ||
745          (LbPos(aLB_WHERECOMP3) == aLB_WHERECOMP3.GetEntryCount()-2)) )
746         aET_WHEREVALUE3.Disable();
747 
748 }
749 
750 //------------------------------------------------------------------------------
IMPL_LINK(DlgFilterCrit,ListSelectHdl,ListBox *,pListBox)751 IMPL_LINK( DlgFilterCrit, ListSelectHdl, ListBox *, pListBox )
752 {
753     String aName;
754     ListBox* pComp;
755     if(pListBox == &aLB_WHEREFIELD1)
756     {
757         aName = LbText(aLB_WHEREFIELD1);
758         pComp = &aLB_WHERECOMP1;
759     }
760     else if(pListBox == &aLB_WHEREFIELD2)
761     {
762         aName = LbText(aLB_WHEREFIELD2);
763         pComp = &aLB_WHERECOMP2;
764     }
765     else
766     {
767         aName = LbText(aLB_WHEREFIELD3);
768         pComp = &aLB_WHERECOMP3;
769     }
770 
771     pComp->Clear();
772 
773     Reference<XPropertySet> xColumn = getColumn(aName);
774     if ( xColumn.is() )
775     {
776         sal_Int32 nDataType = 0;
777         xColumn->getPropertyValue(PROPERTY_TYPE) >>= nDataType;
778         sal_Int32 eColumnSearch = dbtools::getSearchColumnFlag(m_xConnection,nDataType);
779 
780         if(eColumnSearch  == ColumnSearch::FULL)
781         {
782             for(xub_StrLen i=0;i<aSTR_COMPARE_OPERATORS.GetTokenCount();i++)
783                 pComp->InsertEntry(aSTR_COMPARE_OPERATORS.GetToken(i));
784         }
785         else if(eColumnSearch == ColumnSearch::CHAR)
786         {
787             for(xub_StrLen i=6;i<10;i++)
788                 pComp->InsertEntry(aSTR_COMPARE_OPERATORS.GetToken(i));
789         }
790         else if(eColumnSearch == ColumnSearch::BASIC)
791         {
792             xub_StrLen i;
793             for( i = 0; i < 6; i++ )
794                 pComp->InsertEntry(aSTR_COMPARE_OPERATORS.GetToken(i));
795             for(i=8;i<aSTR_COMPARE_OPERATORS.GetTokenCount();i++)
796                 pComp->InsertEntry(aSTR_COMPARE_OPERATORS.GetToken(i));
797         }
798         else
799         {
800             DBG_ASSERT(0,"DlgFilterCrit::ListSelectHdl: Diese Column d�rfte garnicht vorhanden sein!");
801         }
802     }
803     pComp->SelectEntryPos(0);
804 
805     EnableLines();
806     return 0;
807 }
808 
809 
810 
811 //------------------------------------------------------------------------------
812 IMPL_LINK_INLINE_START( DlgFilterCrit, ListSelectCompHdl, ListBox *, /*pListBox*/ )
813 {
814     EnableLines();
815     return 0;
816 }
IMPL_LINK_INLINE_END(DlgFilterCrit,ListSelectCompHdl,ListBox *,pListBox)817 IMPL_LINK_INLINE_END( DlgFilterCrit, ListSelectCompHdl, ListBox *, pListBox )
818 //------------------------------------------------------------------------------
819 void DlgFilterCrit::BuildWherePart()
820 {
821     DBG_CHKTHIS(DlgFilterCrit,NULL);
822     Sequence<Sequence<PropertyValue> > aFilter,aHaving;
823     aFilter.realloc(1);
824     aHaving.realloc(1);
825     //  ::rtl::OUString aFilter;
826 
827     if( LbPos(aLB_WHEREFIELD1) != 0 )
828     {
829         PropertyValue aValue;
830         if ( getCondition(aLB_WHEREFIELD1,aLB_WHERECOMP1,aET_WHEREVALUE1,aValue) )
831         {
832             aHaving[0].realloc(1);
833             aHaving[0][0] = aValue;
834         }
835         else
836         {
837             aFilter[0].realloc(1);
838             aFilter[0][0] = aValue;
839         }
840     }
841 
842     if( LbPos(aLB_WHEREFIELD2) != 0 )
843     {
844         PropertyValue aValue;
845         Sequence<Sequence<PropertyValue> >& _rValues = aFilter;
846         if ( getCondition(aLB_WHEREFIELD2,aLB_WHERECOMP2,aET_WHEREVALUE2,aValue) )
847             _rValues = aHaving;
848         PropertyValue* pPos = NULL;
849         if ( aLB_WHERECOND2.GetSelectEntryPos() )
850         {
851             sal_Int32 nPos = _rValues.getLength();
852             _rValues.realloc( nPos + 1);
853             _rValues[nPos].realloc( 1);
854             pPos = &_rValues[nPos][0];
855         }
856         else
857         {
858             sal_Int32 nPos = _rValues.getLength() - 1;
859             sal_Int32 nAndPos = _rValues[nPos].getLength();
860             _rValues[nPos].realloc( _rValues[nPos].getLength() + 1);
861             pPos = &_rValues[nPos][nAndPos];
862         }
863         *pPos = aValue;
864     }
865 
866     if( LbPos(aLB_WHEREFIELD3) != 0 )
867     {
868         PropertyValue aValue;
869         Sequence<Sequence<PropertyValue> >& _rValues = aFilter;
870         if ( getCondition(aLB_WHEREFIELD3,aLB_WHERECOMP3,aET_WHEREVALUE3,aValue) )
871             _rValues = aHaving;
872         PropertyValue* pPos = NULL;
873         if ( aLB_WHERECOND3.GetSelectEntryPos() )
874         {
875             sal_Int32 nPos = _rValues.getLength();
876             _rValues.realloc( nPos + 1);
877             _rValues[nPos].realloc( 1);
878             pPos = &_rValues[nPos][0];
879         }
880         else
881         {
882             sal_Int32 nPos = _rValues.getLength() - 1;
883             sal_Int32 nAndPos = _rValues[nPos].getLength();
884             _rValues[nPos].realloc( _rValues[nPos].getLength() + 1);
885             pPos = &_rValues[nPos][nAndPos];
886         }
887         *pPos = aValue;
888     }
889     try
890     {
891         m_xQueryComposer->setStructuredFilter(aFilter);
892         m_xQueryComposer->setStructuredHavingClause(aHaving);
893     }
894     catch(Exception)
895     {
896         DBG_UNHANDLED_EXCEPTION();
897     }
898 }
899 // -----------------------------------------------------------------------------
fillLines(const Sequence<Sequence<PropertyValue>> & _aValues)900 void DlgFilterCrit::fillLines(const Sequence<Sequence<PropertyValue > >& _aValues)
901 {
902     const Sequence<PropertyValue >* pOrIter = _aValues.getConstArray();
903     const Sequence<PropertyValue >* pOrEnd   = pOrIter + _aValues.getLength();
904     sal_Bool bOr = sal_True;
905     for(sal_uInt16 i=0;pOrIter != pOrEnd; ++pOrIter)
906     {
907         bOr = sal_True;
908         const PropertyValue* pAndIter   = pOrIter->getConstArray();
909         const PropertyValue* pAndEnd    = pAndIter + pOrIter->getLength();
910         for(;pAndIter != pAndEnd; ++pAndIter)
911         {
912             SetLine( i++,*pAndIter,bOr);
913             bOr = sal_False;
914         }
915     }
916 }
917 
918