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