1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "kcondition.hxx"
28cdf0e10cSrcweir #include "kfields.hxx"
29cdf0e10cSrcweir #include "connectivity/CommonTools.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace ::connectivity::kab;
32cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
33cdf0e10cSrcweir // -----------------------------------------------------------------------------
~KabCondition()34cdf0e10cSrcweir KabCondition::~KabCondition()
35cdf0e10cSrcweir {
36cdf0e10cSrcweir }
37cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionConstant(const sal_Bool bValue)38cdf0e10cSrcweir KabConditionConstant::KabConditionConstant(const sal_Bool bValue)
39cdf0e10cSrcweir     : KabCondition(),
40cdf0e10cSrcweir       m_bValue(bValue)
41cdf0e10cSrcweir {
42cdf0e10cSrcweir }
43cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAlwaysTrue() const44cdf0e10cSrcweir sal_Bool KabConditionConstant::isAlwaysTrue() const
45cdf0e10cSrcweir {
46cdf0e10cSrcweir 	return m_bValue;
47cdf0e10cSrcweir }
48cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAlwaysFalse() const49cdf0e10cSrcweir sal_Bool KabConditionConstant::isAlwaysFalse() const
50cdf0e10cSrcweir {
51cdf0e10cSrcweir 	return !m_bValue;
52cdf0e10cSrcweir }
53cdf0e10cSrcweir // -----------------------------------------------------------------------------
eval(const::KABC::Addressee &) const54cdf0e10cSrcweir sal_Bool KabConditionConstant::eval(const ::KABC::Addressee &) const
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	return m_bValue;
57cdf0e10cSrcweir }
58cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionColumn(const::rtl::OUString & sColumnName)59cdf0e10cSrcweir KabConditionColumn::KabConditionColumn(const ::rtl::OUString &sColumnName) throw(SQLException)
60cdf0e10cSrcweir 	: KabCondition(),
61cdf0e10cSrcweir 	  m_nFieldNumber(findKabField(sColumnName))
62cdf0e10cSrcweir {
63cdf0e10cSrcweir }
64cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAlwaysTrue() const65cdf0e10cSrcweir sal_Bool KabConditionColumn::isAlwaysTrue() const
66cdf0e10cSrcweir {
67cdf0e10cSrcweir 	// Sometimes true, sometimes false
68cdf0e10cSrcweir 	return sal_False;
69cdf0e10cSrcweir }
70cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAlwaysFalse() const71cdf0e10cSrcweir sal_Bool KabConditionColumn::isAlwaysFalse() const
72cdf0e10cSrcweir {
73cdf0e10cSrcweir 	// Sometimes true, sometimes false
74cdf0e10cSrcweir 	return sal_False;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionNull(const::rtl::OUString & sColumnName)77cdf0e10cSrcweir KabConditionNull::KabConditionNull(const ::rtl::OUString &sColumnName) throw(SQLException)
78cdf0e10cSrcweir 	: KabConditionColumn(sColumnName)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir }
81cdf0e10cSrcweir // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const82cdf0e10cSrcweir sal_Bool KabConditionNull::eval(const ::KABC::Addressee &aAddressee) const
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	return aQtName.isNull();
87cdf0e10cSrcweir // KDE address book currently does not use NULL values.
88cdf0e10cSrcweir // But it might do it someday
89cdf0e10cSrcweir }
90cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionNotNull(const::rtl::OUString & sColumnName)91cdf0e10cSrcweir KabConditionNotNull::KabConditionNotNull(const ::rtl::OUString &sColumnName) throw(SQLException)
92cdf0e10cSrcweir 	: KabConditionColumn(sColumnName)
93cdf0e10cSrcweir {
94cdf0e10cSrcweir }
95cdf0e10cSrcweir // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const96cdf0e10cSrcweir sal_Bool KabConditionNotNull::eval(const ::KABC::Addressee &aAddressee) const
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	return !aQtName.isNull();
101cdf0e10cSrcweir // KDE address book currently does not use NULL values.
102cdf0e10cSrcweir // But it might do it someday
103cdf0e10cSrcweir }
104cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionCompare(const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)105cdf0e10cSrcweir KabConditionCompare::KabConditionCompare(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
106cdf0e10cSrcweir     : KabConditionColumn(sColumnName),
107cdf0e10cSrcweir       m_sMatchString(sMatchString)
108cdf0e10cSrcweir {
109cdf0e10cSrcweir }
110cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionEqual(const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)111cdf0e10cSrcweir KabConditionEqual::KabConditionEqual(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
112cdf0e10cSrcweir 	: KabConditionCompare(sColumnName, sMatchString)
113cdf0e10cSrcweir {
114cdf0e10cSrcweir }
115cdf0e10cSrcweir // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const116cdf0e10cSrcweir sal_Bool KabConditionEqual::eval(const ::KABC::Addressee &aAddressee) const
117cdf0e10cSrcweir {
118cdf0e10cSrcweir 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
119cdf0e10cSrcweir // Timestamps should not be compared according to their string value
120cdf0e10cSrcweir // The syntax for such queries should be like
121cdf0e10cSrcweir //	{ts '2004-03-29 12:55:00.000000'}
122cdf0e10cSrcweir // They should also support operators like '<' or '>='
123cdf0e10cSrcweir 
124cdf0e10cSrcweir 	if (aQtName.isNull()) return sal_False;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
127cdf0e10cSrcweir 	return sValue == m_sMatchString;
128cdf0e10cSrcweir }
129cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionDifferent(const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)130cdf0e10cSrcweir KabConditionDifferent::KabConditionDifferent(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
131cdf0e10cSrcweir 	: KabConditionCompare(sColumnName, sMatchString)
132cdf0e10cSrcweir {
133cdf0e10cSrcweir }
134cdf0e10cSrcweir // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const135cdf0e10cSrcweir sal_Bool KabConditionDifferent::eval(const ::KABC::Addressee &aAddressee) const
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	if (aQtName.isNull()) return sal_False;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
142cdf0e10cSrcweir 	return sValue != m_sMatchString;
143cdf0e10cSrcweir }
144cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionSimilar(const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)145cdf0e10cSrcweir KabConditionSimilar::KabConditionSimilar(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
146cdf0e10cSrcweir 	: KabConditionCompare(sColumnName, sMatchString)
147cdf0e10cSrcweir {
148cdf0e10cSrcweir }
149cdf0e10cSrcweir // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const150cdf0e10cSrcweir sal_Bool KabConditionSimilar::eval(const ::KABC::Addressee &aAddressee) const
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	if (aQtName.isNull()) return sal_False;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
157cdf0e10cSrcweir 	return match(m_sMatchString, sValue, '\0');
158cdf0e10cSrcweir }
159cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionBoolean(KabCondition * pLeft,KabCondition * pRight)160cdf0e10cSrcweir KabConditionBoolean::KabConditionBoolean(KabCondition *pLeft, KabCondition *pRight)
161cdf0e10cSrcweir     : KabCondition(),
162cdf0e10cSrcweir       m_pLeft(pLeft),
163cdf0e10cSrcweir       m_pRight(pRight)
164cdf0e10cSrcweir {
165cdf0e10cSrcweir }
166cdf0e10cSrcweir // -----------------------------------------------------------------------------
~KabConditionBoolean()167cdf0e10cSrcweir KabConditionBoolean::~KabConditionBoolean()
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	delete m_pLeft;
170cdf0e10cSrcweir 	delete m_pRight;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionOr(KabCondition * pLeft,KabCondition * pRight)173cdf0e10cSrcweir KabConditionOr::KabConditionOr(KabCondition *pLeft, KabCondition *pRight)
174cdf0e10cSrcweir 	: KabConditionBoolean(pLeft, pRight)
175cdf0e10cSrcweir {
176cdf0e10cSrcweir }
177cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAlwaysTrue() const178cdf0e10cSrcweir sal_Bool KabConditionOr::isAlwaysTrue() const
179cdf0e10cSrcweir {
180cdf0e10cSrcweir 	return m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue();
181cdf0e10cSrcweir }
182cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAlwaysFalse() const183cdf0e10cSrcweir sal_Bool KabConditionOr::isAlwaysFalse() const
184cdf0e10cSrcweir {
185cdf0e10cSrcweir 	return m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse();
186cdf0e10cSrcweir }
187cdf0e10cSrcweir // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const188cdf0e10cSrcweir sal_Bool KabConditionOr::eval(const ::KABC::Addressee &aAddressee) const
189cdf0e10cSrcweir {
190cdf0e10cSrcweir 	// We avoid evaluating terms as much as we can
191cdf0e10cSrcweir 	if (m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue()) return sal_True;
192cdf0e10cSrcweir 	if (m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse()) return sal_False;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	if (m_pLeft->eval(aAddressee)) return sal_True;
195cdf0e10cSrcweir 	if (m_pRight->eval(aAddressee)) return sal_True;
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	return sal_False;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir // -----------------------------------------------------------------------------
KabConditionAnd(KabCondition * pLeft,KabCondition * pRight)200cdf0e10cSrcweir KabConditionAnd::KabConditionAnd(KabCondition *pLeft, KabCondition *pRight)
201cdf0e10cSrcweir 	: KabConditionBoolean(pLeft, pRight)
202cdf0e10cSrcweir {
203cdf0e10cSrcweir }
204cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAlwaysTrue() const205cdf0e10cSrcweir sal_Bool KabConditionAnd::isAlwaysTrue() const
206cdf0e10cSrcweir {
207cdf0e10cSrcweir 	return m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue();
208cdf0e10cSrcweir }
209cdf0e10cSrcweir // -----------------------------------------------------------------------------
isAlwaysFalse() const210cdf0e10cSrcweir sal_Bool KabConditionAnd::isAlwaysFalse() const
211cdf0e10cSrcweir {
212cdf0e10cSrcweir 	return m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse();
213cdf0e10cSrcweir }
214cdf0e10cSrcweir // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const215cdf0e10cSrcweir sal_Bool KabConditionAnd::eval(const ::KABC::Addressee &aAddressee) const
216cdf0e10cSrcweir {
217cdf0e10cSrcweir 	// We avoid evaluating terms as much as we can
218cdf0e10cSrcweir 	if (m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse()) return sal_False;
219cdf0e10cSrcweir 	if (m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue()) return sal_True;
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	if (!m_pLeft->eval(aAddressee)) return sal_False;
222cdf0e10cSrcweir 	if (!m_pRight->eval(aAddressee)) return sal_False;
223cdf0e10cSrcweir 
224cdf0e10cSrcweir 	return sal_True;
225cdf0e10cSrcweir }
226