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 
21 #include "macabcondition.hxx"
22 #include "MacabHeader.hxx"
23 #include "MacabRecord.hxx"
24 #include <connectivity/CommonTools.hxx>
25 
26 using namespace ::connectivity::macab;
27 using namespace ::com::sun::star::sdbc;
28 
~MacabCondition()29 MacabCondition::~MacabCondition()
30 {
31 }
32 
MacabConditionConstant(const bool bValue)33 MacabConditionConstant::MacabConditionConstant(const bool bValue)
34     : MacabCondition(),
35       m_bValue(bValue)
36 {
37 }
38 
isAlwaysTrue() const39 bool MacabConditionConstant::isAlwaysTrue() const
40 {
41     return m_bValue;
42 }
43 
isAlwaysFalse() const44 bool MacabConditionConstant::isAlwaysFalse() const
45 {
46     return !m_bValue;
47 }
48 
eval(const MacabRecord *) const49 bool MacabConditionConstant::eval(const MacabRecord *) const
50 {
51     return m_bValue;
52 }
53 
MacabConditionColumn(const MacabHeader * header,std::u16string_view sColumnName)54 MacabConditionColumn::MacabConditionColumn(
55     const MacabHeader *header, std::u16string_view sColumnName)
56     : MacabCondition(),
57       m_nFieldNumber(header->getColumnNumber(sColumnName))
58 {
59 }
60 
isAlwaysTrue() const61 bool MacabConditionColumn::isAlwaysTrue() const
62 {
63     // Sometimes true, sometimes false
64     return false;
65 }
66 
isAlwaysFalse() const67 bool MacabConditionColumn::isAlwaysFalse() const
68 {
69     // Sometimes true, sometimes false
70     return false;
71 }
72 
MacabConditionNull(const MacabHeader * header,std::u16string_view sColumnName)73 MacabConditionNull::MacabConditionNull(const MacabHeader *header, std::u16string_view sColumnName)
74     : MacabConditionColumn(header, sColumnName)
75 {
76 }
77 
eval(const MacabRecord * aRecord) const78 bool MacabConditionNull::eval(const MacabRecord *aRecord) const
79 {
80     macabfield *aValue = aRecord->get(m_nFieldNumber);
81 
82     if(aValue == nullptr)
83         return true;
84     else if(aValue->value == nullptr)
85         return true;
86     else
87         return false;
88 }
89 
MacabConditionNotNull(const MacabHeader * header,std::u16string_view sColumnName)90 MacabConditionNotNull::MacabConditionNotNull(
91     const MacabHeader *header, std::u16string_view sColumnName)
92     : MacabConditionColumn(header, sColumnName)
93 {
94 }
95 
eval(const MacabRecord * aRecord) const96 bool MacabConditionNotNull::eval(const MacabRecord *aRecord) const
97 {
98     macabfield *aValue = aRecord->get(m_nFieldNumber);
99 
100     if(aValue == nullptr)
101         return false;
102     else if(aValue->value == nullptr)
103         return false;
104     else
105         return true;
106 }
107 
MacabConditionCompare(const MacabHeader * header,std::u16string_view sColumnName,const OUString & sMatchString)108 MacabConditionCompare::MacabConditionCompare(const MacabHeader *header, std::u16string_view sColumnName, const OUString &sMatchString)
109     : MacabConditionColumn(header, sColumnName),
110       m_sMatchString(sMatchString)
111 {
112 }
113 
MacabConditionEqual(const MacabHeader * header,std::u16string_view sColumnName,const OUString & sMatchString)114 MacabConditionEqual::MacabConditionEqual(const MacabHeader *header, std::u16string_view sColumnName, const OUString &sMatchString)
115     : MacabConditionCompare(header, sColumnName, sMatchString)
116 {
117 }
118 
eval(const MacabRecord * aRecord) const119 bool MacabConditionEqual::eval(const MacabRecord *aRecord) const
120 {
121     macabfield *aValue = aRecord->get(m_nFieldNumber);
122 
123     if(aValue == nullptr)
124         return false;
125 
126     macabfield *aValue2 = MacabRecord::createMacabField(m_sMatchString,aValue->type);
127 
128     if(aValue2 == nullptr)
129         return false;
130 
131     sal_Int32 nReturn = MacabRecord::compareFields(aValue, aValue2);
132 
133     delete aValue2;
134     return nReturn == 0;
135 }
136 
MacabConditionDifferent(const MacabHeader * header,std::u16string_view sColumnName,const OUString & sMatchString)137 MacabConditionDifferent::MacabConditionDifferent(const MacabHeader *header, std::u16string_view sColumnName, const OUString &sMatchString)
138     : MacabConditionCompare(header, sColumnName, sMatchString)
139 {
140 }
141 
eval(const MacabRecord * aRecord) const142 bool MacabConditionDifferent::eval(const MacabRecord *aRecord) const
143 {
144     macabfield *aValue = aRecord->get(m_nFieldNumber);
145 
146     if(aValue == nullptr)
147         return false;
148 
149     macabfield *aValue2 = MacabRecord::createMacabField(m_sMatchString,aValue->type);
150 
151     if(aValue2 == nullptr)
152         return false;
153 
154     sal_Int32 nReturn = MacabRecord::compareFields(aValue, aValue2);
155 
156     delete aValue2;
157     return nReturn != 0;
158 }
159 
MacabConditionSimilar(const MacabHeader * header,std::u16string_view sColumnName,const OUString & sMatchString)160 MacabConditionSimilar::MacabConditionSimilar(const MacabHeader *header, std::u16string_view sColumnName, const OUString &sMatchString)
161     : MacabConditionCompare(header, sColumnName, sMatchString)
162 {
163 }
164 
eval(const MacabRecord * aRecord) const165 bool MacabConditionSimilar::eval(const MacabRecord *aRecord) const
166 {
167     macabfield *aValue = aRecord->get(m_nFieldNumber);
168 
169     if(aValue == nullptr)
170         return false;
171 
172     OUString sName = MacabRecord::fieldToString(aValue);
173 
174     return match(m_sMatchString, sName, '\0');
175 }
176 
MacabConditionBoolean(MacabCondition * pLeft,MacabCondition * pRight)177 MacabConditionBoolean::MacabConditionBoolean(MacabCondition *pLeft, MacabCondition *pRight)
178     : MacabCondition(),
179       m_pLeft(pLeft),
180       m_pRight(pRight)
181 {
182 }
183 
~MacabConditionBoolean()184 MacabConditionBoolean::~MacabConditionBoolean()
185 {
186     delete m_pLeft;
187     delete m_pRight;
188 }
189 
MacabConditionOr(MacabCondition * pLeft,MacabCondition * pRight)190 MacabConditionOr::MacabConditionOr(MacabCondition *pLeft, MacabCondition *pRight)
191     : MacabConditionBoolean(pLeft, pRight)
192 {
193 }
194 
isAlwaysTrue() const195 bool MacabConditionOr::isAlwaysTrue() const
196 {
197     return m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue();
198 }
199 
isAlwaysFalse() const200 bool MacabConditionOr::isAlwaysFalse() const
201 {
202     return m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse();
203 }
204 
eval(const MacabRecord * aRecord) const205 bool MacabConditionOr::eval(const MacabRecord *aRecord) const
206 {
207     // We avoid evaluating terms as much as we can
208     if (m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue()) return true;
209     if (m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse()) return false;
210 
211     if (m_pLeft->eval(aRecord)) return true;
212     if (m_pRight->eval(aRecord)) return true;
213 
214     return false;
215 }
216 
MacabConditionAnd(MacabCondition * pLeft,MacabCondition * pRight)217 MacabConditionAnd::MacabConditionAnd(MacabCondition *pLeft, MacabCondition *pRight)
218     : MacabConditionBoolean(pLeft, pRight)
219 {
220 }
221 
isAlwaysTrue() const222 bool MacabConditionAnd::isAlwaysTrue() const
223 {
224     return m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue();
225 }
226 
isAlwaysFalse() const227 bool MacabConditionAnd::isAlwaysFalse() const
228 {
229     return m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse();
230 }
231 
eval(const MacabRecord * aRecord) const232 bool MacabConditionAnd::eval(const MacabRecord *aRecord) const
233 {
234     // We avoid evaluating terms as much as we can
235     if (m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse()) return false;
236     if (m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue()) return true;
237 
238     if (!m_pLeft->eval(aRecord)) return false;
239     if (!m_pRight->eval(aRecord)) return false;
240 
241     return true;
242 }
243 
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
245