Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / kab / kcondition.cxx
blob1e69494f2a8f4de2bd8ae176d6e3c7a45991bd60
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "kcondition.hxx"
22 #include "kfields.hxx"
23 #include "connectivity/CommonTools.hxx"
25 using namespace ::connectivity::kab;
26 using namespace ::com::sun::star::sdbc;
28 KabCondition::~KabCondition()
32 KabConditionConstant::KabConditionConstant(const sal_Bool bValue)
33 : KabCondition(),
34 m_bValue(bValue)
38 sal_Bool KabConditionConstant::isAlwaysTrue() const
40 return m_bValue;
43 sal_Bool KabConditionConstant::isAlwaysFalse() const
45 return !m_bValue;
48 sal_Bool KabConditionConstant::eval(const ::KABC::Addressee &) const
50 return m_bValue;
53 KabConditionColumn::KabConditionColumn(const OUString &sColumnName) throw(SQLException)
54 : KabCondition(),
55 m_nFieldNumber(findKabField(sColumnName))
59 sal_Bool KabConditionColumn::isAlwaysTrue() const
61 // Sometimes true, sometimes false
62 return sal_False;
65 sal_Bool KabConditionColumn::isAlwaysFalse() const
67 // Sometimes true, sometimes false
68 return sal_False;
71 KabConditionNull::KabConditionNull(const OUString &sColumnName) throw(SQLException)
72 : KabConditionColumn(sColumnName)
76 sal_Bool KabConditionNull::eval(const ::KABC::Addressee &aAddressee) const
78 QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
80 return aQtName.isNull();
81 // KDE address book currently does not use NULL values.
82 // But it might do it someday
85 KabConditionNotNull::KabConditionNotNull(const OUString &sColumnName) throw(SQLException)
86 : KabConditionColumn(sColumnName)
90 sal_Bool KabConditionNotNull::eval(const ::KABC::Addressee &aAddressee) const
92 QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
94 return !aQtName.isNull();
95 // KDE address book currently does not use NULL values.
96 // But it might do it someday
99 KabConditionCompare::KabConditionCompare(const OUString &sColumnName, const OUString &sMatchString) throw(SQLException)
100 : KabConditionColumn(sColumnName),
101 m_sMatchString(sMatchString)
105 KabConditionEqual::KabConditionEqual(const OUString &sColumnName, const OUString &sMatchString) throw(SQLException)
106 : KabConditionCompare(sColumnName, sMatchString)
110 sal_Bool KabConditionEqual::eval(const ::KABC::Addressee &aAddressee) const
112 QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
113 // Timestamps should not be compared according to their string value
114 // The syntax for such queries should be like
115 // {ts '2004-03-29 12:55:00.000000'}
116 // They should also support operators like '<' or '>='
118 if (aQtName.isNull()) return sal_False;
120 OUString sValue((const sal_Unicode *) aQtName.ucs2());
121 return sValue == m_sMatchString;
124 KabConditionDifferent::KabConditionDifferent(const OUString &sColumnName, const OUString &sMatchString) throw(SQLException)
125 : KabConditionCompare(sColumnName, sMatchString)
129 sal_Bool KabConditionDifferent::eval(const ::KABC::Addressee &aAddressee) const
131 QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
133 if (aQtName.isNull()) return sal_False;
135 OUString sValue((const sal_Unicode *) aQtName.ucs2());
136 return sValue != m_sMatchString;
139 KabConditionSimilar::KabConditionSimilar(const OUString &sColumnName, const OUString &sMatchString) throw(SQLException)
140 : KabConditionCompare(sColumnName, sMatchString)
144 sal_Bool KabConditionSimilar::eval(const ::KABC::Addressee &aAddressee) const
146 QString aQtName = valueOfKabField(aAddressee, m_nFieldNumber);
148 if (aQtName.isNull()) return sal_False;
150 OUString sValue((const sal_Unicode *) aQtName.ucs2());
151 return match(m_sMatchString, sValue, '\0');
154 KabConditionBoolean::KabConditionBoolean(KabCondition *pLeft, KabCondition *pRight)
155 : KabCondition(),
156 m_pLeft(pLeft),
157 m_pRight(pRight)
161 KabConditionBoolean::~KabConditionBoolean()
163 delete m_pLeft;
164 delete m_pRight;
167 KabConditionOr::KabConditionOr(KabCondition *pLeft, KabCondition *pRight)
168 : KabConditionBoolean(pLeft, pRight)
172 sal_Bool KabConditionOr::isAlwaysTrue() const
174 return m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue();
177 sal_Bool KabConditionOr::isAlwaysFalse() const
179 return m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse();
182 sal_Bool KabConditionOr::eval(const ::KABC::Addressee &aAddressee) const
184 // We avoid evaluating terms as much as we can
185 if (m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue()) return sal_True;
186 if (m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse()) return sal_False;
188 if (m_pLeft->eval(aAddressee)) return sal_True;
189 if (m_pRight->eval(aAddressee)) return sal_True;
191 return sal_False;
194 KabConditionAnd::KabConditionAnd(KabCondition *pLeft, KabCondition *pRight)
195 : KabConditionBoolean(pLeft, pRight)
199 sal_Bool KabConditionAnd::isAlwaysTrue() const
201 return m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue();
204 sal_Bool KabConditionAnd::isAlwaysFalse() const
206 return m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse();
209 sal_Bool KabConditionAnd::eval(const ::KABC::Addressee &aAddressee) const
211 // We avoid evaluating terms as much as we can
212 if (m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse()) return sal_False;
213 if (m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue()) return sal_True;
215 if (!m_pLeft->eval(aAddressee)) return sal_False;
216 if (!m_pRight->eval(aAddressee)) return sal_False;
218 return sal_True;
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */