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_connectivity.hxx"
26 
27 #include "kcondition.hxx"
28 #include "kfields.hxx"
29 #include "connectivity/CommonTools.hxx"
30 
31 using namespace ::connectivity::kab;
32 using namespace ::com::sun::star::sdbc;
33 // -----------------------------------------------------------------------------
~KabCondition()34 KabCondition::~KabCondition()
35 {
36 }
37 // -----------------------------------------------------------------------------
KabConditionConstant(const sal_Bool bValue)38 KabConditionConstant::KabConditionConstant(const sal_Bool bValue)
39     : KabCondition(),
40       m_bValue(bValue)
41 {
42 }
43 // -----------------------------------------------------------------------------
isAlwaysTrue() const44 sal_Bool KabConditionConstant::isAlwaysTrue() const
45 {
46 	return m_bValue;
47 }
48 // -----------------------------------------------------------------------------
isAlwaysFalse() const49 sal_Bool KabConditionConstant::isAlwaysFalse() const
50 {
51 	return !m_bValue;
52 }
53 // -----------------------------------------------------------------------------
eval(const::KABC::Addressee &) const54 sal_Bool KabConditionConstant::eval(const ::KABC::Addressee &) const
55 {
56 	return m_bValue;
57 }
58 // -----------------------------------------------------------------------------
KabConditionColumn(const::rtl::OUString & sColumnName)59 KabConditionColumn::KabConditionColumn(const ::rtl::OUString &sColumnName) throw(SQLException)
60 	: KabCondition(),
61 	  m_nFieldNumber(findKabField(sColumnName))
62 {
63 }
64 // -----------------------------------------------------------------------------
isAlwaysTrue() const65 sal_Bool KabConditionColumn::isAlwaysTrue() const
66 {
67 	// Sometimes true, sometimes false
68 	return sal_False;
69 }
70 // -----------------------------------------------------------------------------
isAlwaysFalse() const71 sal_Bool KabConditionColumn::isAlwaysFalse() const
72 {
73 	// Sometimes true, sometimes false
74 	return sal_False;
75 }
76 // -----------------------------------------------------------------------------
KabConditionNull(const::rtl::OUString & sColumnName)77 KabConditionNull::KabConditionNull(const ::rtl::OUString &sColumnName) throw(SQLException)
78 	: KabConditionColumn(sColumnName)
79 {
80 }
81 // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const82 sal_Bool KabConditionNull::eval(const ::KABC::Addressee &aAddressee) const
83 {
84 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
85 
86 	return aQtName.isNull();
87 // KDE address book currently does not use NULL values.
88 // But it might do it someday
89 }
90 // -----------------------------------------------------------------------------
KabConditionNotNull(const::rtl::OUString & sColumnName)91 KabConditionNotNull::KabConditionNotNull(const ::rtl::OUString &sColumnName) throw(SQLException)
92 	: KabConditionColumn(sColumnName)
93 {
94 }
95 // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const96 sal_Bool KabConditionNotNull::eval(const ::KABC::Addressee &aAddressee) const
97 {
98 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
99 
100 	return !aQtName.isNull();
101 // KDE address book currently does not use NULL values.
102 // But it might do it someday
103 }
104 // -----------------------------------------------------------------------------
KabConditionCompare(const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)105 KabConditionCompare::KabConditionCompare(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
106     : KabConditionColumn(sColumnName),
107       m_sMatchString(sMatchString)
108 {
109 }
110 // -----------------------------------------------------------------------------
KabConditionEqual(const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)111 KabConditionEqual::KabConditionEqual(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
112 	: KabConditionCompare(sColumnName, sMatchString)
113 {
114 }
115 // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const116 sal_Bool KabConditionEqual::eval(const ::KABC::Addressee &aAddressee) const
117 {
118 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
119 // Timestamps should not be compared according to their string value
120 // The syntax for such queries should be like
121 //	{ts '2004-03-29 12:55:00.000000'}
122 // They should also support operators like '<' or '>='
123 
124 	if (aQtName.isNull()) return sal_False;
125 
126 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
127 	return sValue == m_sMatchString;
128 }
129 // -----------------------------------------------------------------------------
KabConditionDifferent(const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)130 KabConditionDifferent::KabConditionDifferent(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
131 	: KabConditionCompare(sColumnName, sMatchString)
132 {
133 }
134 // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const135 sal_Bool KabConditionDifferent::eval(const ::KABC::Addressee &aAddressee) const
136 {
137 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
138 
139 	if (aQtName.isNull()) return sal_False;
140 
141 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
142 	return sValue != m_sMatchString;
143 }
144 // -----------------------------------------------------------------------------
KabConditionSimilar(const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)145 KabConditionSimilar::KabConditionSimilar(const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
146 	: KabConditionCompare(sColumnName, sMatchString)
147 {
148 }
149 // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const150 sal_Bool KabConditionSimilar::eval(const ::KABC::Addressee &aAddressee) const
151 {
152 	QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
153 
154 	if (aQtName.isNull()) return sal_False;
155 
156 	::rtl::OUString sValue((const sal_Unicode *) aQtName.ucs2());
157 	return match(m_sMatchString, sValue, '\0');
158 }
159 // -----------------------------------------------------------------------------
KabConditionBoolean(KabCondition * pLeft,KabCondition * pRight)160 KabConditionBoolean::KabConditionBoolean(KabCondition *pLeft, KabCondition *pRight)
161     : KabCondition(),
162       m_pLeft(pLeft),
163       m_pRight(pRight)
164 {
165 }
166 // -----------------------------------------------------------------------------
~KabConditionBoolean()167 KabConditionBoolean::~KabConditionBoolean()
168 {
169 	delete m_pLeft;
170 	delete m_pRight;
171 }
172 // -----------------------------------------------------------------------------
KabConditionOr(KabCondition * pLeft,KabCondition * pRight)173 KabConditionOr::KabConditionOr(KabCondition *pLeft, KabCondition *pRight)
174 	: KabConditionBoolean(pLeft, pRight)
175 {
176 }
177 // -----------------------------------------------------------------------------
isAlwaysTrue() const178 sal_Bool KabConditionOr::isAlwaysTrue() const
179 {
180 	return m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue();
181 }
182 // -----------------------------------------------------------------------------
isAlwaysFalse() const183 sal_Bool KabConditionOr::isAlwaysFalse() const
184 {
185 	return m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse();
186 }
187 // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const188 sal_Bool KabConditionOr::eval(const ::KABC::Addressee &aAddressee) const
189 {
190 	// We avoid evaluating terms as much as we can
191 	if (m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue()) return sal_True;
192 	if (m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse()) return sal_False;
193 
194 	if (m_pLeft->eval(aAddressee)) return sal_True;
195 	if (m_pRight->eval(aAddressee)) return sal_True;
196 
197 	return sal_False;
198 }
199 // -----------------------------------------------------------------------------
KabConditionAnd(KabCondition * pLeft,KabCondition * pRight)200 KabConditionAnd::KabConditionAnd(KabCondition *pLeft, KabCondition *pRight)
201 	: KabConditionBoolean(pLeft, pRight)
202 {
203 }
204 // -----------------------------------------------------------------------------
isAlwaysTrue() const205 sal_Bool KabConditionAnd::isAlwaysTrue() const
206 {
207 	return m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue();
208 }
209 // -----------------------------------------------------------------------------
isAlwaysFalse() const210 sal_Bool KabConditionAnd::isAlwaysFalse() const
211 {
212 	return m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse();
213 }
214 // -----------------------------------------------------------------------------
eval(const::KABC::Addressee & aAddressee) const215 sal_Bool KabConditionAnd::eval(const ::KABC::Addressee &aAddressee) const
216 {
217 	// We avoid evaluating terms as much as we can
218 	if (m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse()) return sal_False;
219 	if (m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue()) return sal_True;
220 
221 	if (!m_pLeft->eval(aAddressee)) return sal_False;
222 	if (!m_pRight->eval(aAddressee)) return sal_False;
223 
224 	return sal_True;
225 }
226