update dev300-m58
[ooovba.git] / connectivity / source / drivers / macab / macabcondition.cxx
blob78771cdfe235d5f02bd01b207c9e9d7fe0e6f758
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: macabcondition.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
34 #include "macabcondition.hxx"
35 #include "MacabHeader.hxx"
36 #include "MacabRecord.hxx"
37 #include "connectivity/CommonTools.hxx"
39 using namespace ::connectivity::macab;
40 using namespace ::com::sun::star::sdbc;
41 // -----------------------------------------------------------------------------
42 MacabCondition::~MacabCondition()
45 // -----------------------------------------------------------------------------
46 MacabConditionConstant::MacabConditionConstant(const sal_Bool bValue)
47 : MacabCondition(),
48 m_bValue(bValue)
51 // -----------------------------------------------------------------------------
52 sal_Bool MacabConditionConstant::isAlwaysTrue() const
54 return m_bValue;
56 // -----------------------------------------------------------------------------
57 sal_Bool MacabConditionConstant::isAlwaysFalse() const
59 return !m_bValue;
61 // -----------------------------------------------------------------------------
62 sal_Bool MacabConditionConstant::eval(const MacabRecord *) const
64 return m_bValue;
66 // -----------------------------------------------------------------------------
67 MacabConditionColumn::MacabConditionColumn(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
68 : MacabCondition(),
69 m_nFieldNumber(header->getColumnNumber(sColumnName))
72 // -----------------------------------------------------------------------------
73 sal_Bool MacabConditionColumn::isAlwaysTrue() const
75 // Sometimes true, sometimes false
76 return sal_False;
78 // -----------------------------------------------------------------------------
79 sal_Bool MacabConditionColumn::isAlwaysFalse() const
81 // Sometimes true, sometimes false
82 return sal_False;
84 // -----------------------------------------------------------------------------
85 MacabConditionNull::MacabConditionNull(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
86 : MacabConditionColumn(header, sColumnName)
89 // -----------------------------------------------------------------------------
90 sal_Bool MacabConditionNull::eval(const MacabRecord *aRecord) const
92 macabfield *aValue = aRecord->get(m_nFieldNumber);
94 if(aValue == NULL)
95 return sal_True;
96 else if(aValue->value == NULL)
97 return sal_True;
98 else
99 return sal_False;
101 // -----------------------------------------------------------------------------
102 MacabConditionNotNull::MacabConditionNotNull(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
103 : MacabConditionColumn(header, sColumnName)
106 // -----------------------------------------------------------------------------
107 sal_Bool MacabConditionNotNull::eval(const MacabRecord *aRecord) const
109 macabfield *aValue = aRecord->get(m_nFieldNumber);
111 if(aValue == NULL)
112 return sal_False;
113 else if(aValue->value == NULL)
114 return sal_False;
115 else
116 return sal_True;
118 // -----------------------------------------------------------------------------
119 MacabConditionCompare::MacabConditionCompare(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
120 : MacabConditionColumn(header, sColumnName),
121 m_sMatchString(sMatchString)
124 // -----------------------------------------------------------------------------
125 MacabConditionEqual::MacabConditionEqual(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
126 : MacabConditionCompare(header, sColumnName, sMatchString)
129 // -----------------------------------------------------------------------------
130 sal_Bool MacabConditionEqual::eval(const MacabRecord *aRecord) const
132 macabfield *aValue = aRecord->get(m_nFieldNumber);
134 if(aValue == NULL)
135 return sal_False;
137 macabfield *aValue2 = MacabRecord::createMacabField(m_sMatchString,aValue->type);
139 if(aValue2 == NULL)
140 return sal_False;
142 sal_Int32 nReturn = MacabRecord::compareFields(aValue, aValue2);
144 delete aValue2;
145 return nReturn == 0;
147 // -----------------------------------------------------------------------------
148 MacabConditionDifferent::MacabConditionDifferent(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
149 : MacabConditionCompare(header, sColumnName, sMatchString)
152 // -----------------------------------------------------------------------------
153 sal_Bool MacabConditionDifferent::eval(const MacabRecord *aRecord) const
155 macabfield *aValue = aRecord->get(m_nFieldNumber);
157 if(aValue == NULL)
158 return sal_False;
160 macabfield *aValue2 = MacabRecord::createMacabField(m_sMatchString,aValue->type);
162 if(aValue2 == NULL)
163 return sal_False;
165 sal_Int32 nReturn = MacabRecord::compareFields(aValue, aValue2);
167 delete aValue2;
168 return nReturn != 0;
170 // -----------------------------------------------------------------------------
171 MacabConditionSimilar::MacabConditionSimilar(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
172 : MacabConditionCompare(header, sColumnName, sMatchString)
175 // -----------------------------------------------------------------------------
176 sal_Bool MacabConditionSimilar::eval(const MacabRecord *aRecord) const
178 macabfield *aValue = aRecord->get(m_nFieldNumber);
180 if(aValue == NULL)
181 return sal_False;
183 ::rtl::OUString sName = MacabRecord::fieldToString(aValue);
185 return match(m_sMatchString, sName, '\0');
187 // -----------------------------------------------------------------------------
188 MacabConditionBoolean::MacabConditionBoolean(MacabCondition *pLeft, MacabCondition *pRight)
189 : MacabCondition(),
190 m_pLeft(pLeft),
191 m_pRight(pRight)
194 // -----------------------------------------------------------------------------
195 MacabConditionBoolean::~MacabConditionBoolean()
197 delete m_pLeft;
198 delete m_pRight;
200 // -----------------------------------------------------------------------------
201 MacabConditionOr::MacabConditionOr(MacabCondition *pLeft, MacabCondition *pRight)
202 : MacabConditionBoolean(pLeft, pRight)
205 // -----------------------------------------------------------------------------
206 sal_Bool MacabConditionOr::isAlwaysTrue() const
208 return m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue();
210 // -----------------------------------------------------------------------------
211 sal_Bool MacabConditionOr::isAlwaysFalse() const
213 return m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse();
215 // -----------------------------------------------------------------------------
216 sal_Bool MacabConditionOr::eval(const MacabRecord *aRecord) const
218 // We avoid evaluating terms as much as we can
219 if (m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue()) return sal_True;
220 if (m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse()) return sal_False;
222 if (m_pLeft->eval(aRecord)) return sal_True;
223 if (m_pRight->eval(aRecord)) return sal_True;
225 return sal_False;
227 // -----------------------------------------------------------------------------
228 MacabConditionAnd::MacabConditionAnd(MacabCondition *pLeft, MacabCondition *pRight)
229 : MacabConditionBoolean(pLeft, pRight)
232 // -----------------------------------------------------------------------------
233 sal_Bool MacabConditionAnd::isAlwaysTrue() const
235 return m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue();
237 // -----------------------------------------------------------------------------
238 sal_Bool MacabConditionAnd::isAlwaysFalse() const
240 return m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse();
242 // -----------------------------------------------------------------------------
243 sal_Bool MacabConditionAnd::eval(const MacabRecord *aRecord) const
245 // We avoid evaluating terms as much as we can
246 if (m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse()) return sal_False;
247 if (m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue()) return sal_True;
249 if (!m_pLeft->eval(aRecord)) return sal_False;
250 if (!m_pRight->eval(aRecord)) return sal_False;
252 return sal_True;