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 "queryfilter.hxx"
21 : #include "moduledbu.hxx"
22 : #include <com/sun/star/sdbc/DataType.hpp>
23 : #include <com/sun/star/util/Date.hpp>
24 : #include <com/sun/star/util/DateTime.hpp>
25 : #include <com/sun/star/util/Time.hpp>
26 : #include <com/sun/star/sdb/XSQLQueryComposer.hpp>
27 : #include <com/sun/star/sdbc/ColumnSearch.hpp>
28 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
29 : #include <com/sun/star/sdb/SQLFilterOperator.hpp>
30 : #include <com/sun/star/sdbc/XRow.hpp>
31 : #include <com/sun/star/sdbc/XResultSet.hpp>
32 : #include <com/sun/star/container/XNameAccess.hpp>
33 : #include <comphelper/string.hxx>
34 : #include <tools/diagnose_ex.h>
35 : #include <osl/diagnose.h>
36 : #include <connectivity/sqliterator.hxx>
37 : #include <connectivity/dbtools.hxx>
38 : #include "dbustrings.hrc"
39 : #include <com/sun/star/sdb/XSingleSelectQueryComposer.hpp>
40 :
41 : using namespace dbaui;
42 : using namespace connectivity;
43 : using namespace ::com::sun::star::uno;
44 : using namespace ::com::sun::star::lang;
45 : using namespace ::com::sun::star::container;
46 : using namespace ::com::sun::star::util;
47 : using namespace ::com::sun::star::sdb;
48 : using namespace ::com::sun::star::sdbc;
49 : using namespace ::com::sun::star::sdbcx;
50 : using namespace ::com::sun::star::beans;
51 :
52 0 : void Replace_OS_PlaceHolder(OUString& aString)
53 : {
54 0 : aString = aString.replaceAll( "*", "%" );
55 0 : aString = aString.replaceAll( "?", "_" );
56 0 : }
57 :
58 0 : void Replace_SQL_PlaceHolder(OUString& aString)
59 : {
60 0 : aString = aString.replaceAll( "%", "*" );
61 0 : aString = aString.replaceAll( "_", "?" );
62 0 : }
63 :
64 :
65 0 : DlgFilterCrit::DlgFilterCrit(Window * pParent,
66 : const Reference< XComponentContext >& rxContext,
67 : const Reference< XConnection>& _rxConnection,
68 : const Reference< XSingleSelectQueryComposer >& _rxComposer,
69 : const Reference< XNameAccess>& _rxCols
70 : )
71 : : ModalDialog(pParent, "QueryFilterDialog",
72 : "dbaccess/ui/queryfilterdialog.ui")
73 :
74 : ,m_xQueryComposer(_rxComposer)
75 : ,m_xColumns( _rxCols )
76 : ,m_xConnection( _rxConnection )
77 0 : ,m_xMetaData( _rxConnection->getMetaData() )
78 0 : ,m_aPredicateInput( rxContext, _rxConnection, getParseContext() )
79 : {
80 0 : get(m_pLB_WHEREFIELD1, "field1");
81 0 : get(m_pLB_WHERECOMP1, "cond1");
82 0 : get(m_pET_WHEREVALUE1, "value1");
83 :
84 0 : get(m_pLB_WHERECOND2, "op2");
85 0 : get(m_pLB_WHEREFIELD2, "field2");
86 0 : get(m_pLB_WHERECOMP2, "cond2");
87 0 : get(m_pET_WHEREVALUE2, "value2");
88 :
89 0 : get(m_pLB_WHERECOND3, "op3");
90 0 : get(m_pLB_WHEREFIELD3, "field3");
91 0 : get(m_pLB_WHERECOMP3, "cond3");
92 0 : get(m_pET_WHEREVALUE3, "value3");
93 :
94 : //set all condition preferred width to max width
95 : //if all entries exist
96 0 : Size aSize(m_pLB_WHERECOMP1->get_preferred_size());
97 0 : m_pLB_WHERECOMP1->set_width_request(aSize.Width());
98 0 : m_pLB_WHERECOMP2->set_width_request(aSize.Width());
99 0 : m_pLB_WHERECOMP3->set_width_request(aSize.Width());
100 0 : sal_uInt16 nEntryCount = m_pLB_WHERECOMP1->GetEntryCount();
101 0 : for (sal_uInt16 i = 0; i < nEntryCount; ++i)
102 : {
103 0 : if (i > 0)
104 0 : m_aSTR_COMPARE_OPERATORS += ";";
105 0 : m_aSTR_COMPARE_OPERATORS += m_pLB_WHERECOMP1->GetEntry(i);
106 : }
107 0 : m_pLB_WHERECOMP1->Clear();
108 :
109 : // ... also write it into the remaining fields
110 0 : Sequence< OUString> aNames = m_xColumns->getElementNames();
111 0 : const OUString* pIter = aNames.getConstArray();
112 0 : const OUString* pEnd = pIter + aNames.getLength();
113 0 : Reference<XPropertySet> xColumn;
114 0 : for(;pIter != pEnd;++pIter)
115 : {
116 : try
117 : {
118 0 : xColumn.set( m_xColumns->getByName( *pIter ), UNO_QUERY_THROW );
119 :
120 0 : sal_Int32 nDataType( 0 );
121 0 : OSL_VERIFY( xColumn->getPropertyValue( PROPERTY_TYPE ) >>= nDataType );
122 0 : sal_Int32 eColumnSearch = ::dbtools::getSearchColumnFlag( m_xConnection, nDataType );
123 0 : if ( eColumnSearch == ColumnSearch::NONE )
124 0 : continue;
125 :
126 0 : sal_Bool bIsSearchable( sal_True );
127 0 : OSL_VERIFY( xColumn->getPropertyValue( PROPERTY_ISSEARCHABLE ) >>= bIsSearchable );
128 0 : if ( !bIsSearchable )
129 0 : continue;
130 : }
131 0 : catch( const Exception& )
132 : {
133 : DBG_UNHANDLED_EXCEPTION();
134 : }
135 0 : m_pLB_WHEREFIELD1->InsertEntry( *pIter );
136 0 : m_pLB_WHEREFIELD2->InsertEntry( *pIter );
137 0 : m_pLB_WHEREFIELD3->InsertEntry( *pIter );
138 : }
139 :
140 0 : Reference<XNameAccess> xSelectColumns = Reference<XColumnsSupplier>(m_xQueryComposer,UNO_QUERY)->getColumns();
141 0 : aNames = xSelectColumns->getElementNames();
142 0 : pIter = aNames.getConstArray();
143 0 : pEnd = pIter + aNames.getLength();
144 0 : for(;pIter != pEnd;++pIter)
145 : {
146 : // don't insert a column name twice
147 0 : if ( !m_xColumns->hasByName(*pIter) )
148 : {
149 0 : xColumn.set(xSelectColumns->getByName(*pIter),UNO_QUERY);
150 : OSL_ENSURE(xColumn.is(),"DlgFilterCrit::DlgFilterCrit: Column is null!");
151 0 : sal_Int32 nDataType(0);
152 0 : xColumn->getPropertyValue(PROPERTY_TYPE) >>= nDataType;
153 0 : sal_Int32 eColumnSearch = dbtools::getSearchColumnFlag(m_xConnection,nDataType);
154 : // TODO
155 : // !pColumn->IsFunction()
156 0 : if(eColumnSearch != ColumnSearch::NONE)
157 : {
158 0 : m_pLB_WHEREFIELD1->InsertEntry( *pIter );
159 0 : m_pLB_WHEREFIELD2->InsertEntry( *pIter );
160 0 : m_pLB_WHEREFIELD3->InsertEntry( *pIter );
161 : }
162 : }
163 : }
164 : // initialize the listboxes with noEntry
165 0 : m_pLB_WHEREFIELD1->SelectEntryPos(0);
166 0 : m_pLB_WHEREFIELD2->SelectEntryPos(0);
167 0 : m_pLB_WHEREFIELD3->SelectEntryPos(0);
168 :
169 : // insert the criteria into the dialog
170 0 : Sequence<Sequence<PropertyValue > > aValues = m_xQueryComposer->getStructuredFilter();
171 0 : fillLines(aValues);
172 0 : aValues = m_xQueryComposer->getStructuredHavingClause();
173 0 : fillLines(aValues);
174 :
175 0 : EnableLines();
176 :
177 0 : m_pLB_WHEREFIELD1->SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectHdl));
178 0 : m_pLB_WHEREFIELD2->SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectHdl));
179 0 : m_pLB_WHEREFIELD3->SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectHdl));
180 :
181 0 : m_pLB_WHERECOMP1->SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectCompHdl));
182 0 : m_pLB_WHERECOMP2->SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectCompHdl));
183 0 : m_pLB_WHERECOMP3->SetSelectHdl(LINK(this,DlgFilterCrit,ListSelectCompHdl));
184 :
185 0 : m_pET_WHEREVALUE1->SetLoseFocusHdl( LINK( this, DlgFilterCrit, PredicateLoseFocus ) );
186 0 : m_pET_WHEREVALUE2->SetLoseFocusHdl( LINK( this, DlgFilterCrit, PredicateLoseFocus ) );
187 0 : m_pET_WHEREVALUE3->SetLoseFocusHdl( LINK( this, DlgFilterCrit, PredicateLoseFocus ) );
188 :
189 0 : if ( m_pET_WHEREVALUE1->IsEnabled() )
190 0 : m_pET_WHEREVALUE1->GrabFocus();
191 0 : }
192 :
193 0 : DlgFilterCrit::~DlgFilterCrit()
194 : {
195 0 : }
196 :
197 : #define LbText(x) ((x).GetSelectEntry())
198 : #define LbPos(x) ((x).GetSelectEntryPos())
199 :
200 0 : sal_Int32 DlgFilterCrit::GetOSQLPredicateType( const OUString& _rSelectedPredicate ) const
201 : {
202 0 : sal_Int32 nPredicateIndex = -1;
203 0 : for ( sal_Int32 i=0; i < comphelper::string::getTokenCount(m_aSTR_COMPARE_OPERATORS, ';'); ++i)
204 0 : if ( m_aSTR_COMPARE_OPERATORS.getToken(i, ';') == _rSelectedPredicate )
205 : {
206 0 : nPredicateIndex = i;
207 0 : break;
208 : }
209 :
210 0 : sal_Int32 nPredicateType = SQLFilterOperator::NOT_SQLNULL;
211 0 : switch ( nPredicateIndex )
212 : {
213 : case 0:
214 0 : nPredicateType = SQLFilterOperator::EQUAL;
215 0 : break;
216 : case 1:
217 0 : nPredicateType = SQLFilterOperator::NOT_EQUAL;
218 0 : break;
219 : case 2:
220 0 : nPredicateType = SQLFilterOperator::LESS;
221 0 : break;
222 : case 3:
223 0 : nPredicateType = SQLFilterOperator::LESS_EQUAL;
224 0 : break;
225 : case 4:
226 0 : nPredicateType = SQLFilterOperator::GREATER;
227 0 : break;
228 : case 5:
229 0 : nPredicateType = SQLFilterOperator::GREATER_EQUAL;
230 0 : break;
231 : case 6:
232 0 : nPredicateType = SQLFilterOperator::LIKE;
233 0 : break;
234 : case 7:
235 0 : nPredicateType = SQLFilterOperator::NOT_LIKE;
236 0 : break;
237 : case 8:
238 0 : nPredicateType = SQLFilterOperator::SQLNULL;
239 0 : break;
240 : case 9:
241 0 : nPredicateType = SQLFilterOperator::NOT_SQLNULL;
242 0 : break;
243 : default:
244 : OSL_FAIL( "DlgFilterCrit::GetOSQLPredicateType: unknown predicate string!" );
245 0 : break;
246 : }
247 :
248 0 : return nPredicateType;
249 : }
250 :
251 0 : sal_uInt16 DlgFilterCrit::GetSelectionPos(sal_Int32 eType,const ListBox& rListBox) const
252 : {
253 : sal_uInt16 nPos;
254 0 : switch(eType)
255 : {
256 : case SQLFilterOperator::EQUAL:
257 0 : nPos = 0;
258 0 : break;
259 : case SQLFilterOperator::NOT_EQUAL:
260 0 : nPos = 1;
261 0 : break;
262 : case SQLFilterOperator::LESS:
263 0 : nPos = 2;
264 0 : break;
265 : case SQLFilterOperator::LESS_EQUAL:
266 0 : nPos = 3;
267 0 : break;
268 : case SQLFilterOperator::GREATER:
269 0 : nPos = 4;
270 0 : break;
271 : case SQLFilterOperator::GREATER_EQUAL:
272 0 : nPos = 5;
273 0 : break;
274 : case SQLFilterOperator::NOT_LIKE:
275 0 : nPos = rListBox.GetEntryCount() > 2 ? rListBox.GetEntryCount()-3 : 0;
276 0 : break;
277 : case SQLFilterOperator::LIKE:
278 0 : nPos = rListBox.GetEntryCount() > 2 ? rListBox.GetEntryCount()-4 : 1;
279 0 : break;
280 : case SQLFilterOperator::SQLNULL:
281 0 : nPos = rListBox.GetEntryCount()-2;
282 0 : break;
283 : case SQLFilterOperator::NOT_SQLNULL:
284 0 : nPos = rListBox.GetEntryCount()-1;
285 0 : break;
286 : default:
287 : // TODO What value should this be?
288 0 : nPos = 0;
289 0 : break;
290 : }
291 0 : return nPos;
292 : }
293 :
294 0 : sal_Bool DlgFilterCrit::getCondition(const ListBox& _rField,const ListBox& _rComp,const Edit& _rValue,PropertyValue& _rFilter) const
295 : {
296 0 : sal_Bool bHaving = sal_False;
297 : try
298 : {
299 0 : OUString sTableName;
300 0 : sal_Bool bFunction = sal_False;
301 0 : _rFilter.Name = _rField.GetSelectEntry();
302 0 : Reference< XPropertySet > xColumn = getQueryColumn(_rFilter.Name);
303 0 : if ( xColumn.is() )
304 : {
305 0 : Reference< XPropertySetInfo > xInfo = xColumn->getPropertySetInfo();
306 0 : if ( xInfo->hasPropertyByName(PROPERTY_REALNAME) )
307 : {
308 0 : if ( xInfo->hasPropertyByName(PROPERTY_TABLENAME) )
309 : {
310 0 : xColumn->getPropertyValue(PROPERTY_TABLENAME) >>= sTableName;
311 0 : if ( !sTableName.isEmpty() )
312 : {
313 : // properly quote all parts of the table name, so
314 : // e.g. <schema>.<table> becomes "<schema>"."<table>"
315 0 : OUString aCatlog,aSchema,aTable;
316 0 : ::dbtools::qualifiedNameComponents( m_xMetaData, sTableName, aCatlog, aSchema, aTable, ::dbtools::eInDataManipulation );
317 0 : sTableName = ::dbtools::composeTableName( m_xMetaData, aCatlog, aSchema, aTable, true, ::dbtools::eInDataManipulation );
318 : }
319 : }
320 0 : xColumn->getPropertyValue(PROPERTY_REALNAME) >>= _rFilter.Name;
321 0 : static OUString sAgg("AggregateFunction");
322 0 : if ( xInfo->hasPropertyByName(sAgg) )
323 0 : xColumn->getPropertyValue(sAgg) >>= bHaving;
324 0 : static OUString sFunction("Function");
325 0 : if ( xInfo->hasPropertyByName(sFunction) )
326 0 : xColumn->getPropertyValue(sFunction) >>= bFunction;
327 : }
328 0 : if ( !bFunction )
329 : {
330 0 : const OUString aQuote = m_xMetaData.is() ? m_xMetaData->getIdentifierQuoteString() : OUString();
331 0 : _rFilter.Name = ::dbtools::quoteName(aQuote,_rFilter.Name);
332 0 : if ( !sTableName.isEmpty() )
333 : {
334 0 : static OUString sSep(".");
335 0 : sTableName += sSep;
336 0 : sTableName += _rFilter.Name;
337 0 : _rFilter.Name = sTableName;
338 0 : }
339 0 : }
340 0 : }
341 : }
342 0 : catch(const Exception&)
343 : {
344 : }
345 :
346 0 : _rFilter.Handle = GetOSQLPredicateType( _rComp.GetSelectEntry() );
347 0 : if ( SQLFilterOperator::SQLNULL != _rFilter.Handle && _rFilter.Handle != SQLFilterOperator::NOT_SQLNULL )
348 : {
349 0 : OUString sPredicateValue = m_aPredicateInput.getPredicateValue( _rValue.GetText(), getMatchingColumn( _rValue ), false );
350 0 : if ( _rFilter.Handle == SQLFilterOperator::LIKE ||
351 0 : _rFilter.Handle == SQLFilterOperator::NOT_LIKE )
352 0 : ::Replace_OS_PlaceHolder( sPredicateValue );
353 0 : _rFilter.Value <<= OUString(sPredicateValue);
354 : }
355 0 : return bHaving;
356 : }
357 :
358 0 : Reference< XPropertySet > DlgFilterCrit::getColumn( const OUString& _rFieldName ) const
359 : {
360 0 : Reference< XPropertySet > xColumn;
361 : try
362 : {
363 0 : if ( m_xColumns.is() && m_xColumns->hasByName( _rFieldName ) )
364 0 : m_xColumns->getByName( _rFieldName ) >>= xColumn;
365 :
366 0 : Reference< XNameAccess> xColumns = Reference< XColumnsSupplier >(m_xQueryComposer,UNO_QUERY)->getColumns();
367 0 : if ( xColumns.is() && !xColumn.is() )
368 : {
369 0 : Sequence< OUString> aSeq = xColumns->getElementNames();
370 0 : const OUString* pIter = aSeq.getConstArray();
371 0 : const OUString* pEnd = pIter + aSeq.getLength();
372 0 : for(;pIter != pEnd;++pIter)
373 : {
374 0 : Reference<XPropertySet> xProp(xColumns->getByName(*pIter),UNO_QUERY);
375 0 : if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(PROPERTY_REALNAME) )
376 : {
377 0 : OUString sRealName;
378 0 : xProp->getPropertyValue(PROPERTY_REALNAME) >>= sRealName;
379 0 : if ( sRealName == _rFieldName )
380 : {
381 0 : if ( m_xColumns.is() && m_xColumns->hasByName( *pIter ) )
382 0 : m_xColumns->getByName( *pIter ) >>= xColumn;
383 0 : break;
384 0 : }
385 : }
386 0 : }
387 0 : }
388 : }
389 0 : catch( const Exception& )
390 : {
391 : DBG_UNHANDLED_EXCEPTION();
392 : }
393 :
394 0 : return xColumn;
395 : }
396 :
397 0 : Reference< XPropertySet > DlgFilterCrit::getQueryColumn( const OUString& _rFieldName ) const
398 : {
399 0 : Reference< XPropertySet > xColumn;
400 : try
401 : {
402 0 : Reference< XNameAccess> xColumns = Reference< XColumnsSupplier >(m_xQueryComposer,UNO_QUERY)->getColumns();
403 0 : if ( xColumns.is() && xColumns->hasByName( _rFieldName ) )
404 0 : xColumns->getByName( _rFieldName ) >>= xColumn;
405 : }
406 0 : catch( const Exception& )
407 : {
408 : DBG_UNHANDLED_EXCEPTION();
409 : }
410 :
411 0 : return xColumn;
412 : }
413 :
414 0 : Reference< XPropertySet > DlgFilterCrit::getMatchingColumn( const Edit& _rValueInput ) const
415 : {
416 : // the name
417 0 : OUString sField;
418 0 : if ( &_rValueInput == m_pET_WHEREVALUE1 )
419 : {
420 0 : sField = m_pLB_WHEREFIELD1->GetSelectEntry();
421 : }
422 0 : else if ( &_rValueInput == m_pET_WHEREVALUE2 )
423 : {
424 0 : sField = m_pLB_WHEREFIELD2->GetSelectEntry();
425 : }
426 0 : else if ( &_rValueInput == m_pET_WHEREVALUE3 )
427 : {
428 0 : sField = m_pLB_WHEREFIELD3->GetSelectEntry();
429 : }
430 : else {
431 : OSL_FAIL( "DlgFilterCrit::getMatchingColumn: invalid event source!" );
432 : }
433 :
434 : // the field itself
435 0 : return getColumn( sField );
436 : }
437 :
438 0 : IMPL_LINK( DlgFilterCrit, PredicateLoseFocus, Edit*, _pField )
439 : {
440 : OSL_ENSURE( _pField, "DlgFilterCrit::PredicateLoseFocus: invalid event source!" );
441 0 : if ( _pField )
442 : {
443 : // retrieve the field affected
444 0 : Reference< XPropertySet> xColumn( getMatchingColumn( *_pField ) );
445 : // and normalize it's content
446 0 : if ( xColumn.is() )
447 : {
448 0 : OUString sText( _pField->GetText() );
449 0 : m_aPredicateInput.normalizePredicateString( sText, xColumn );
450 0 : _pField->SetText( sText );
451 0 : }
452 : }
453 :
454 0 : return 0L;
455 : }
456 :
457 0 : void DlgFilterCrit::SetLine( sal_uInt16 nIdx,const PropertyValue& _rItem,sal_Bool _bOr )
458 : {
459 0 : OUString aCondition;
460 0 : _rItem.Value >>= aCondition;
461 0 : OUString aStr = aCondition;
462 0 : if ( _rItem.Handle == SQLFilterOperator::LIKE ||
463 0 : _rItem.Handle == SQLFilterOperator::NOT_LIKE )
464 0 : ::Replace_SQL_PlaceHolder(aStr);
465 0 : aStr = comphelper::string::stripEnd(aStr, ' ');
466 :
467 0 : Reference< XPropertySet > xColumn = getColumn( _rItem.Name );
468 :
469 : // remove the predicate from the condition
470 0 : switch(_rItem.Handle)
471 : {
472 : case SQLFilterOperator::EQUAL:
473 : // aStr.Erase(0,1);
474 0 : break;
475 : case SQLFilterOperator::NOT_EQUAL:
476 0 : aStr = aStr.copy(2);
477 0 : break;
478 : case SQLFilterOperator::LESS:
479 0 : aStr = aStr.copy(1);
480 0 : break;
481 : case SQLFilterOperator::LESS_EQUAL:
482 0 : aStr = aStr.copy(2);
483 0 : break;
484 : case SQLFilterOperator::GREATER:
485 0 : aStr = aStr.copy(1);
486 0 : break;
487 : case SQLFilterOperator::GREATER_EQUAL:
488 0 : aStr = aStr.copy(2);
489 0 : break;
490 : case SQLFilterOperator::NOT_LIKE:
491 0 : aStr = aStr.copy(8);
492 0 : break;
493 : case SQLFilterOperator::LIKE:
494 0 : aStr = aStr.copy(4);
495 0 : break;
496 : case SQLFilterOperator::SQLNULL:
497 0 : aStr = aStr.copy(7);
498 0 : break;
499 : case SQLFilterOperator::NOT_SQLNULL:
500 0 : aStr = aStr.copy(11);
501 0 : break;
502 : }
503 0 : aStr = comphelper::string::stripStart(aStr, ' ');
504 :
505 : // to make sure that we only set first three
506 0 : ListBox* pColumnListControl = NULL;
507 0 : ListBox* pPredicateListControl = NULL;
508 0 : Edit* pPredicateValueControl = NULL;
509 0 : switch( nIdx )
510 : {
511 : case 0:
512 0 : pColumnListControl = m_pLB_WHEREFIELD1;
513 0 : pPredicateListControl = m_pLB_WHERECOMP1;
514 0 : pPredicateValueControl = m_pET_WHEREVALUE1;
515 0 : break;
516 : case 1:
517 0 : m_pLB_WHERECOND2->SelectEntryPos( _bOr ? 1 : 0 );
518 :
519 0 : pColumnListControl = m_pLB_WHEREFIELD2;
520 0 : pPredicateListControl = m_pLB_WHERECOMP2;
521 0 : pPredicateValueControl = m_pET_WHEREVALUE2;
522 0 : break;
523 : case 2:
524 0 : m_pLB_WHERECOND3->SelectEntryPos( _bOr ? 1 : 0 );
525 :
526 0 : pColumnListControl = m_pLB_WHEREFIELD3;
527 0 : pPredicateListControl = m_pLB_WHERECOMP3;
528 0 : pPredicateValueControl = m_pET_WHEREVALUE3;
529 0 : break;
530 : }
531 :
532 0 : if ( pColumnListControl && pPredicateListControl && pPredicateValueControl )
533 : {
534 0 : OUString sName;
535 0 : if ( xColumn.is() )
536 0 : xColumn->getPropertyValue(PROPERTY_NAME) >>= sName;
537 : else
538 0 : sName = _rItem.Name;
539 : // select the appropriate field name
540 0 : SelectField( *pColumnListControl, sName );
541 0 : ListSelectHdl( pColumnListControl );
542 :
543 : // select the appropriate condition
544 0 : pPredicateListControl->SelectEntryPos( GetSelectionPos( (sal_Int32)_rItem.Handle, *pPredicateListControl ) );
545 :
546 : // initially normalize this value
547 0 : OUString aString( aStr );
548 0 : m_aPredicateInput.normalizePredicateString( aString, xColumn );
549 0 : pPredicateValueControl->SetText( aString );
550 0 : }
551 0 : }
552 :
553 0 : void DlgFilterCrit::SelectField( ListBox& rBox, const OUString& rField )
554 : {
555 0 : sal_uInt16 nCnt = rBox.GetEntryCount();
556 :
557 0 : for( sal_uInt16 i=0 ; i<nCnt ; i++ )
558 : {
559 0 : if(rBox.GetEntry(i) == rField)
560 : {
561 0 : rBox.SelectEntryPos(i);
562 0 : return;
563 : }
564 : }
565 :
566 0 : rBox.SelectEntryPos(0);
567 : }
568 :
569 0 : void DlgFilterCrit::EnableLines()
570 : {
571 : // enabling/disabling of whole lines
572 0 : if( LbPos(*m_pLB_WHEREFIELD1) == 0 )
573 : {
574 0 : m_pLB_WHEREFIELD2->Disable();
575 0 : m_pLB_WHERECOND2->Disable();
576 0 : m_pLB_WHERECOMP2->Disable();
577 0 : m_pET_WHEREVALUE2->Disable();
578 :
579 0 : m_pLB_WHEREFIELD3->Disable();
580 0 : m_pLB_WHERECOND3->Disable();
581 0 : m_pLB_WHERECOMP3->Disable();
582 0 : m_pET_WHEREVALUE3->Disable();
583 : }
584 : else
585 : {
586 0 : m_pLB_WHEREFIELD2->Enable();
587 0 : m_pLB_WHERECOND2->Enable();
588 0 : m_pLB_WHERECOMP2->Enable();
589 0 : m_pET_WHEREVALUE2->Enable();
590 :
591 0 : m_pLB_WHEREFIELD3->Enable();
592 0 : m_pLB_WHERECOND3->Enable();
593 0 : m_pLB_WHERECOMP3->Enable();
594 0 : m_pET_WHEREVALUE3->Enable();
595 : }
596 :
597 0 : if( LbPos(*m_pLB_WHEREFIELD2) == 0 )
598 : {
599 0 : m_pLB_WHEREFIELD3->Disable();
600 0 : m_pLB_WHERECOND3->Disable();
601 0 : m_pLB_WHERECOMP3->Disable();
602 0 : m_pET_WHEREVALUE3->Disable();
603 : }
604 : else
605 : {
606 0 : m_pLB_WHEREFIELD3->Enable();
607 0 : m_pLB_WHERECOND3->Enable();
608 0 : m_pLB_WHERECOMP3->Enable();
609 0 : m_pET_WHEREVALUE3->Enable();
610 : }
611 :
612 : // comparison field equal to NOENTRY
613 0 : if( LbPos(*m_pLB_WHEREFIELD1) == 0 )
614 : {
615 0 : m_pLB_WHERECOMP1->Disable();
616 0 : m_pET_WHEREVALUE1->Disable();
617 : }
618 : else
619 : {
620 0 : m_pLB_WHEREFIELD1->Enable();
621 0 : m_pLB_WHERECOMP1->Enable();
622 0 : m_pET_WHEREVALUE1->Enable();
623 : }
624 :
625 0 : if( LbPos(*m_pLB_WHEREFIELD2) == 0 )
626 : {
627 0 : m_pLB_WHERECOND2->Disable();
628 0 : m_pLB_WHERECOMP2->Disable();
629 0 : m_pET_WHEREVALUE2->Disable();
630 : }
631 : else
632 : {
633 0 : m_pLB_WHERECOND2->Enable();
634 0 : m_pLB_WHEREFIELD2->Enable();
635 0 : m_pLB_WHERECOMP2->Enable();
636 0 : m_pET_WHEREVALUE2->Enable();
637 : }
638 :
639 0 : if( LbPos(*m_pLB_WHEREFIELD3) == 0 )
640 : {
641 0 : m_pLB_WHERECOND3->Disable();
642 0 : m_pLB_WHERECOMP3->Disable();
643 0 : m_pET_WHEREVALUE3->Disable();
644 : }
645 : else
646 : {
647 0 : m_pLB_WHERECOND3->Enable();
648 0 : m_pLB_WHERECOND3->Enable();
649 0 : m_pLB_WHEREFIELD3->Enable();
650 0 : m_pLB_WHERECOMP3->Enable();
651 0 : m_pET_WHEREVALUE3->Enable();
652 : }
653 :
654 : // comparison operator equal to ISNULL or ISNOTNULL
655 0 : if(m_pLB_WHERECOMP1->GetEntryCount() > 2 &&
656 0 : ((LbPos(*m_pLB_WHERECOMP1) == m_pLB_WHERECOMP1->GetEntryCount()-1) ||
657 0 : (LbPos(*m_pLB_WHERECOMP1) == m_pLB_WHERECOMP1->GetEntryCount()-2)) )
658 0 : m_pET_WHEREVALUE1->Disable();
659 :
660 0 : if(m_pLB_WHERECOMP2->GetEntryCount() > 2 &&
661 0 : ((LbPos(*m_pLB_WHERECOMP2) == m_pLB_WHERECOMP2->GetEntryCount()-1) ||
662 0 : (LbPos(*m_pLB_WHERECOMP2) == m_pLB_WHERECOMP2->GetEntryCount()-2)) )
663 0 : m_pET_WHEREVALUE2->Disable();
664 :
665 0 : if(m_pLB_WHERECOMP3->GetEntryCount() > 2 &&
666 0 : ((LbPos(*m_pLB_WHERECOMP3) == m_pLB_WHERECOMP3->GetEntryCount()-1) ||
667 0 : (LbPos(*m_pLB_WHERECOMP3) == m_pLB_WHERECOMP3->GetEntryCount()-2)) )
668 0 : m_pET_WHEREVALUE3->Disable();
669 0 : }
670 :
671 0 : IMPL_LINK( DlgFilterCrit, ListSelectHdl, ListBox *, pListBox )
672 : {
673 0 : OUString aName;
674 : ListBox* pComp;
675 0 : if(pListBox == m_pLB_WHEREFIELD1)
676 : {
677 0 : aName = LbText(*m_pLB_WHEREFIELD1);
678 0 : pComp = m_pLB_WHERECOMP1;
679 : }
680 0 : else if(pListBox == m_pLB_WHEREFIELD2)
681 : {
682 0 : aName = LbText(*m_pLB_WHEREFIELD2);
683 0 : pComp = m_pLB_WHERECOMP2;
684 : }
685 : else
686 : {
687 0 : aName = LbText(*m_pLB_WHEREFIELD3);
688 0 : pComp = m_pLB_WHERECOMP3;
689 : }
690 :
691 0 : pComp->Clear();
692 :
693 0 : Reference<XPropertySet> xColumn = getColumn(aName);
694 0 : if ( xColumn.is() )
695 : {
696 0 : sal_Int32 nDataType = 0;
697 0 : xColumn->getPropertyValue(PROPERTY_TYPE) >>= nDataType;
698 0 : sal_Int32 eColumnSearch = dbtools::getSearchColumnFlag(m_xConnection,nDataType);
699 :
700 0 : if(eColumnSearch == ColumnSearch::FULL)
701 : {
702 0 : for(sal_Int32 i=0;i < comphelper::string::getTokenCount(m_aSTR_COMPARE_OPERATORS, ';');i++)
703 0 : pComp->InsertEntry(m_aSTR_COMPARE_OPERATORS.getToken(i, ';'));
704 : }
705 0 : else if(eColumnSearch == ColumnSearch::CHAR)
706 : {
707 0 : for(sal_Int32 i=6; i<10; i++)
708 0 : pComp->InsertEntry(m_aSTR_COMPARE_OPERATORS.getToken(i, ';'));
709 : }
710 0 : else if(eColumnSearch == ColumnSearch::BASIC)
711 : {
712 : sal_Int32 i;
713 0 : for( i = 0; i < 6; i++ )
714 0 : pComp->InsertEntry(m_aSTR_COMPARE_OPERATORS.getToken(i, ';'));
715 0 : for(i=8; i < comphelper::string::getTokenCount(m_aSTR_COMPARE_OPERATORS, ';'); ++i)
716 0 : pComp->InsertEntry(m_aSTR_COMPARE_OPERATORS.getToken(i, ';'));
717 : }
718 : else
719 : {
720 : OSL_FAIL("DlgFilterCrit::ListSelectHdl: This column should not exist at all.");
721 : }
722 : }
723 0 : pComp->SelectEntryPos(0);
724 :
725 0 : EnableLines();
726 0 : return 0;
727 : }
728 :
729 0 : IMPL_LINK_INLINE_START( DlgFilterCrit, ListSelectCompHdl, ListBox *, /*pListBox*/ )
730 : {
731 0 : EnableLines();
732 0 : return 0;
733 : }
734 :
735 0 : IMPL_LINK_INLINE_END( DlgFilterCrit, ListSelectCompHdl, ListBox *, pListBox )
736 :
737 0 : void DlgFilterCrit::BuildWherePart()
738 : {
739 0 : Sequence<Sequence<PropertyValue> > aFilter,aHaving;
740 0 : aFilter.realloc(1);
741 0 : aHaving.realloc(1);
742 :
743 0 : if( LbPos(*m_pLB_WHEREFIELD1) != 0 )
744 : {
745 0 : PropertyValue aValue;
746 0 : if ( getCondition(*m_pLB_WHEREFIELD1,*m_pLB_WHERECOMP1,*m_pET_WHEREVALUE1,aValue) )
747 : {
748 0 : aHaving[0].realloc(1);
749 0 : aHaving[0][0] = aValue;
750 : }
751 : else
752 : {
753 0 : aFilter[0].realloc(1);
754 0 : aFilter[0][0] = aValue;
755 0 : }
756 : }
757 :
758 0 : if( LbPos(*m_pLB_WHEREFIELD2) != 0 )
759 : {
760 0 : PropertyValue aValue;
761 0 : Sequence<Sequence<PropertyValue> >& _rValues = aFilter;
762 0 : if ( getCondition(*m_pLB_WHEREFIELD2,*m_pLB_WHERECOMP2,*m_pET_WHEREVALUE2,aValue) )
763 0 : _rValues = aHaving;
764 0 : PropertyValue* pPos = NULL;
765 0 : if ( m_pLB_WHERECOND2->GetSelectEntryPos() )
766 : {
767 0 : sal_Int32 nPos = _rValues.getLength();
768 0 : _rValues.realloc( nPos + 1);
769 0 : _rValues[nPos].realloc( 1);
770 0 : pPos = &_rValues[nPos][0];
771 : }
772 : else
773 : {
774 0 : sal_Int32 nPos = _rValues.getLength() - 1;
775 0 : sal_Int32 nAndPos = _rValues[nPos].getLength();
776 0 : _rValues[nPos].realloc( _rValues[nPos].getLength() + 1);
777 0 : pPos = &_rValues[nPos][nAndPos];
778 : }
779 0 : *pPos = aValue;
780 : }
781 :
782 0 : if( LbPos(*m_pLB_WHEREFIELD3) != 0 )
783 : {
784 0 : PropertyValue aValue;
785 0 : Sequence<Sequence<PropertyValue> >& _rValues = aFilter;
786 0 : if ( getCondition(*m_pLB_WHEREFIELD3,*m_pLB_WHERECOMP3,*m_pET_WHEREVALUE3,aValue) )
787 0 : _rValues = aHaving;
788 0 : PropertyValue* pPos = NULL;
789 0 : if ( m_pLB_WHERECOND3->GetSelectEntryPos() )
790 : {
791 0 : sal_Int32 nPos = _rValues.getLength();
792 0 : _rValues.realloc( nPos + 1);
793 0 : _rValues[nPos].realloc( 1);
794 0 : pPos = &_rValues[nPos][0];
795 : }
796 : else
797 : {
798 0 : sal_Int32 nPos = _rValues.getLength() - 1;
799 0 : sal_Int32 nAndPos = _rValues[nPos].getLength();
800 0 : _rValues[nPos].realloc( _rValues[nPos].getLength() + 1);
801 0 : pPos = &_rValues[nPos][nAndPos];
802 : }
803 0 : *pPos = aValue;
804 : }
805 : try
806 : {
807 0 : m_xQueryComposer->setStructuredFilter(aFilter);
808 0 : m_xQueryComposer->setStructuredHavingClause(aHaving);
809 : }
810 0 : catch(const Exception&)
811 : {
812 : DBG_UNHANDLED_EXCEPTION();
813 0 : }
814 0 : }
815 :
816 0 : void DlgFilterCrit::fillLines(const Sequence<Sequence<PropertyValue > >& _aValues)
817 : {
818 0 : const Sequence<PropertyValue >* pOrIter = _aValues.getConstArray();
819 0 : const Sequence<PropertyValue >* pOrEnd = pOrIter + _aValues.getLength();
820 0 : sal_Bool bOr = sal_True;
821 0 : for(sal_uInt16 i=0;pOrIter != pOrEnd; ++pOrIter)
822 : {
823 0 : bOr = sal_True;
824 0 : const PropertyValue* pAndIter = pOrIter->getConstArray();
825 0 : const PropertyValue* pAndEnd = pAndIter + pOrIter->getLength();
826 0 : for(;pAndIter != pAndEnd; ++pAndIter)
827 : {
828 0 : SetLine( i++,*pAndIter,bOr);
829 0 : bOr = sal_False;
830 : }
831 : }
832 0 : }
833 :
834 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|