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 "macabcondition.hxx"
28 #include "MacabHeader.hxx"
29 #include "MacabRecord.hxx"
30 #include "connectivity/CommonTools.hxx"
31 
32 using namespace ::connectivity::macab;
33 using namespace ::com::sun::star::sdbc;
34 // -----------------------------------------------------------------------------
~MacabCondition()35 MacabCondition::~MacabCondition()
36 {
37 }
38 // -----------------------------------------------------------------------------
MacabConditionConstant(const sal_Bool bValue)39 MacabConditionConstant::MacabConditionConstant(const sal_Bool bValue)
40     : MacabCondition(),
41       m_bValue(bValue)
42 {
43 }
44 // -----------------------------------------------------------------------------
isAlwaysTrue() const45 sal_Bool MacabConditionConstant::isAlwaysTrue() const
46 {
47 	return m_bValue;
48 }
49 // -----------------------------------------------------------------------------
isAlwaysFalse() const50 sal_Bool MacabConditionConstant::isAlwaysFalse() const
51 {
52 	return !m_bValue;
53 }
54 // -----------------------------------------------------------------------------
eval(const MacabRecord *) const55 sal_Bool MacabConditionConstant::eval(const MacabRecord *) const
56 {
57 	return m_bValue;
58 }
59 // -----------------------------------------------------------------------------
MacabConditionColumn(const MacabHeader * header,const::rtl::OUString & sColumnName)60 MacabConditionColumn::MacabConditionColumn(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
61 	: MacabCondition(),
62 	  m_nFieldNumber(header->getColumnNumber(sColumnName))
63 {
64 }
65 // -----------------------------------------------------------------------------
isAlwaysTrue() const66 sal_Bool MacabConditionColumn::isAlwaysTrue() const
67 {
68 	// Sometimes true, sometimes false
69 	return sal_False;
70 }
71 // -----------------------------------------------------------------------------
isAlwaysFalse() const72 sal_Bool MacabConditionColumn::isAlwaysFalse() const
73 {
74 	// Sometimes true, sometimes false
75 	return sal_False;
76 }
77 // -----------------------------------------------------------------------------
MacabConditionNull(const MacabHeader * header,const::rtl::OUString & sColumnName)78 MacabConditionNull::MacabConditionNull(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
79 	: MacabConditionColumn(header, sColumnName)
80 {
81 }
82 // -----------------------------------------------------------------------------
eval(const MacabRecord * aRecord) const83 sal_Bool MacabConditionNull::eval(const MacabRecord *aRecord) const
84 {
85 	macabfield *aValue = aRecord->get(m_nFieldNumber);
86 
87 	if(aValue == NULL)
88 		return sal_True;
89 	else if(aValue->value == NULL)
90 		return sal_True;
91 	else
92 		return sal_False;
93 }
94 // -----------------------------------------------------------------------------
MacabConditionNotNull(const MacabHeader * header,const::rtl::OUString & sColumnName)95 MacabConditionNotNull::MacabConditionNotNull(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
96 	: MacabConditionColumn(header, sColumnName)
97 {
98 }
99 // -----------------------------------------------------------------------------
eval(const MacabRecord * aRecord) const100 sal_Bool MacabConditionNotNull::eval(const MacabRecord *aRecord) const
101 {
102 	macabfield *aValue = aRecord->get(m_nFieldNumber);
103 
104 	if(aValue == NULL)
105 		return sal_False;
106 	else if(aValue->value == NULL)
107 		return sal_False;
108 	else
109 		return sal_True;
110 }
111 // -----------------------------------------------------------------------------
MacabConditionCompare(const MacabHeader * header,const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)112 MacabConditionCompare::MacabConditionCompare(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
113     : MacabConditionColumn(header, sColumnName),
114       m_sMatchString(sMatchString)
115 {
116 }
117 // -----------------------------------------------------------------------------
MacabConditionEqual(const MacabHeader * header,const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)118 MacabConditionEqual::MacabConditionEqual(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
119 	: MacabConditionCompare(header, sColumnName, sMatchString)
120 {
121 }
122 // -----------------------------------------------------------------------------
eval(const MacabRecord * aRecord) const123 sal_Bool MacabConditionEqual::eval(const MacabRecord *aRecord) const
124 {
125 	macabfield *aValue = aRecord->get(m_nFieldNumber);
126 
127 	if(aValue == NULL)
128 		return sal_False;
129 
130 	macabfield *aValue2 = MacabRecord::createMacabField(m_sMatchString,aValue->type);
131 
132 	if(aValue2 == NULL)
133 		return sal_False;
134 
135 	sal_Int32 nReturn = MacabRecord::compareFields(aValue, aValue2);
136 
137 	delete aValue2;
138 	return nReturn == 0;
139 }
140 // -----------------------------------------------------------------------------
MacabConditionDifferent(const MacabHeader * header,const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)141 MacabConditionDifferent::MacabConditionDifferent(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
142 	: MacabConditionCompare(header, sColumnName, sMatchString)
143 {
144 }
145 // -----------------------------------------------------------------------------
eval(const MacabRecord * aRecord) const146 sal_Bool MacabConditionDifferent::eval(const MacabRecord *aRecord) const
147 {
148 	macabfield *aValue = aRecord->get(m_nFieldNumber);
149 
150 	if(aValue == NULL)
151 		return sal_False;
152 
153 	macabfield *aValue2 = MacabRecord::createMacabField(m_sMatchString,aValue->type);
154 
155 	if(aValue2 == NULL)
156 		return sal_False;
157 
158 	sal_Int32 nReturn = MacabRecord::compareFields(aValue, aValue2);
159 
160 	delete aValue2;
161 	return nReturn != 0;
162 }
163 // -----------------------------------------------------------------------------
MacabConditionSimilar(const MacabHeader * header,const::rtl::OUString & sColumnName,const::rtl::OUString & sMatchString)164 MacabConditionSimilar::MacabConditionSimilar(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
165 	: MacabConditionCompare(header, sColumnName, sMatchString)
166 {
167 }
168 // -----------------------------------------------------------------------------
eval(const MacabRecord * aRecord) const169 sal_Bool MacabConditionSimilar::eval(const MacabRecord *aRecord) const
170 {
171 	macabfield *aValue = aRecord->get(m_nFieldNumber);
172 
173 	if(aValue == NULL)
174 		return sal_False;
175 
176 	::rtl::OUString sName = MacabRecord::fieldToString(aValue);
177 
178 	return match(m_sMatchString, sName, '\0');
179 }
180 // -----------------------------------------------------------------------------
MacabConditionBoolean(MacabCondition * pLeft,MacabCondition * pRight)181 MacabConditionBoolean::MacabConditionBoolean(MacabCondition *pLeft, MacabCondition *pRight)
182     : MacabCondition(),
183       m_pLeft(pLeft),
184       m_pRight(pRight)
185 {
186 }
187 // -----------------------------------------------------------------------------
~MacabConditionBoolean()188 MacabConditionBoolean::~MacabConditionBoolean()
189 {
190 	delete m_pLeft;
191 	delete m_pRight;
192 }
193 // -----------------------------------------------------------------------------
MacabConditionOr(MacabCondition * pLeft,MacabCondition * pRight)194 MacabConditionOr::MacabConditionOr(MacabCondition *pLeft, MacabCondition *pRight)
195 	: MacabConditionBoolean(pLeft, pRight)
196 {
197 }
198 // -----------------------------------------------------------------------------
isAlwaysTrue() const199 sal_Bool MacabConditionOr::isAlwaysTrue() const
200 {
201 	return m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue();
202 }
203 // -----------------------------------------------------------------------------
isAlwaysFalse() const204 sal_Bool MacabConditionOr::isAlwaysFalse() const
205 {
206 	return m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse();
207 }
208 // -----------------------------------------------------------------------------
eval(const MacabRecord * aRecord) const209 sal_Bool MacabConditionOr::eval(const MacabRecord *aRecord) const
210 {
211 	// We avoid evaluating terms as much as we can
212 	if (m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue()) return sal_True;
213 	if (m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse()) return sal_False;
214 
215 	if (m_pLeft->eval(aRecord)) return sal_True;
216 	if (m_pRight->eval(aRecord)) return sal_True;
217 
218 	return sal_False;
219 }
220 // -----------------------------------------------------------------------------
MacabConditionAnd(MacabCondition * pLeft,MacabCondition * pRight)221 MacabConditionAnd::MacabConditionAnd(MacabCondition *pLeft, MacabCondition *pRight)
222 	: MacabConditionBoolean(pLeft, pRight)
223 {
224 }
225 // -----------------------------------------------------------------------------
isAlwaysTrue() const226 sal_Bool MacabConditionAnd::isAlwaysTrue() const
227 {
228 	return m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue();
229 }
230 // -----------------------------------------------------------------------------
isAlwaysFalse() const231 sal_Bool MacabConditionAnd::isAlwaysFalse() const
232 {
233 	return m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse();
234 }
235 // -----------------------------------------------------------------------------
eval(const MacabRecord * aRecord) const236 sal_Bool MacabConditionAnd::eval(const MacabRecord *aRecord) const
237 {
238 	// We avoid evaluating terms as much as we can
239 	if (m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse()) return sal_False;
240 	if (m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue()) return sal_True;
241 
242 	if (!m_pLeft->eval(aRecord)) return sal_False;
243 	if (!m_pRight->eval(aRecord)) return sal_False;
244 
245 	return sal_True;
246 }
247