1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_connectivity.hxx"
30 
31 #include "macabcondition.hxx"
32 #include "MacabHeader.hxx"
33 #include "MacabRecord.hxx"
34 #include "connectivity/CommonTools.hxx"
35 
36 using namespace ::connectivity::macab;
37 using namespace ::com::sun::star::sdbc;
38 // -----------------------------------------------------------------------------
39 MacabCondition::~MacabCondition()
40 {
41 }
42 // -----------------------------------------------------------------------------
43 MacabConditionConstant::MacabConditionConstant(const sal_Bool bValue)
44     : MacabCondition(),
45       m_bValue(bValue)
46 {
47 }
48 // -----------------------------------------------------------------------------
49 sal_Bool MacabConditionConstant::isAlwaysTrue() const
50 {
51 	return m_bValue;
52 }
53 // -----------------------------------------------------------------------------
54 sal_Bool MacabConditionConstant::isAlwaysFalse() const
55 {
56 	return !m_bValue;
57 }
58 // -----------------------------------------------------------------------------
59 sal_Bool MacabConditionConstant::eval(const MacabRecord *) const
60 {
61 	return m_bValue;
62 }
63 // -----------------------------------------------------------------------------
64 MacabConditionColumn::MacabConditionColumn(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
65 	: MacabCondition(),
66 	  m_nFieldNumber(header->getColumnNumber(sColumnName))
67 {
68 }
69 // -----------------------------------------------------------------------------
70 sal_Bool MacabConditionColumn::isAlwaysTrue() const
71 {
72 	// Sometimes true, sometimes false
73 	return sal_False;
74 }
75 // -----------------------------------------------------------------------------
76 sal_Bool MacabConditionColumn::isAlwaysFalse() const
77 {
78 	// Sometimes true, sometimes false
79 	return sal_False;
80 }
81 // -----------------------------------------------------------------------------
82 MacabConditionNull::MacabConditionNull(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
83 	: MacabConditionColumn(header, sColumnName)
84 {
85 }
86 // -----------------------------------------------------------------------------
87 sal_Bool MacabConditionNull::eval(const MacabRecord *aRecord) const
88 {
89 	macabfield *aValue = aRecord->get(m_nFieldNumber);
90 
91 	if(aValue == NULL)
92 		return sal_True;
93 	else if(aValue->value == NULL)
94 		return sal_True;
95 	else
96 		return sal_False;
97 }
98 // -----------------------------------------------------------------------------
99 MacabConditionNotNull::MacabConditionNotNull(const MacabHeader *header, const ::rtl::OUString &sColumnName) throw(SQLException)
100 	: MacabConditionColumn(header, sColumnName)
101 {
102 }
103 // -----------------------------------------------------------------------------
104 sal_Bool MacabConditionNotNull::eval(const MacabRecord *aRecord) const
105 {
106 	macabfield *aValue = aRecord->get(m_nFieldNumber);
107 
108 	if(aValue == NULL)
109 		return sal_False;
110 	else if(aValue->value == NULL)
111 		return sal_False;
112 	else
113 		return sal_True;
114 }
115 // -----------------------------------------------------------------------------
116 MacabConditionCompare::MacabConditionCompare(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
117     : MacabConditionColumn(header, sColumnName),
118       m_sMatchString(sMatchString)
119 {
120 }
121 // -----------------------------------------------------------------------------
122 MacabConditionEqual::MacabConditionEqual(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
123 	: MacabConditionCompare(header, sColumnName, sMatchString)
124 {
125 }
126 // -----------------------------------------------------------------------------
127 sal_Bool MacabConditionEqual::eval(const MacabRecord *aRecord) const
128 {
129 	macabfield *aValue = aRecord->get(m_nFieldNumber);
130 
131 	if(aValue == NULL)
132 		return sal_False;
133 
134 	macabfield *aValue2 = MacabRecord::createMacabField(m_sMatchString,aValue->type);
135 
136 	if(aValue2 == NULL)
137 		return sal_False;
138 
139 	sal_Int32 nReturn = MacabRecord::compareFields(aValue, aValue2);
140 
141 	delete aValue2;
142 	return nReturn == 0;
143 }
144 // -----------------------------------------------------------------------------
145 MacabConditionDifferent::MacabConditionDifferent(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
146 	: MacabConditionCompare(header, sColumnName, sMatchString)
147 {
148 }
149 // -----------------------------------------------------------------------------
150 sal_Bool MacabConditionDifferent::eval(const MacabRecord *aRecord) const
151 {
152 	macabfield *aValue = aRecord->get(m_nFieldNumber);
153 
154 	if(aValue == NULL)
155 		return sal_False;
156 
157 	macabfield *aValue2 = MacabRecord::createMacabField(m_sMatchString,aValue->type);
158 
159 	if(aValue2 == NULL)
160 		return sal_False;
161 
162 	sal_Int32 nReturn = MacabRecord::compareFields(aValue, aValue2);
163 
164 	delete aValue2;
165 	return nReturn != 0;
166 }
167 // -----------------------------------------------------------------------------
168 MacabConditionSimilar::MacabConditionSimilar(const MacabHeader *header, const ::rtl::OUString &sColumnName, const ::rtl::OUString &sMatchString) throw(SQLException)
169 	: MacabConditionCompare(header, sColumnName, sMatchString)
170 {
171 }
172 // -----------------------------------------------------------------------------
173 sal_Bool MacabConditionSimilar::eval(const MacabRecord *aRecord) const
174 {
175 	macabfield *aValue = aRecord->get(m_nFieldNumber);
176 
177 	if(aValue == NULL)
178 		return sal_False;
179 
180 	::rtl::OUString sName = MacabRecord::fieldToString(aValue);
181 
182 	return match(m_sMatchString, sName, '\0');
183 }
184 // -----------------------------------------------------------------------------
185 MacabConditionBoolean::MacabConditionBoolean(MacabCondition *pLeft, MacabCondition *pRight)
186     : MacabCondition(),
187       m_pLeft(pLeft),
188       m_pRight(pRight)
189 {
190 }
191 // -----------------------------------------------------------------------------
192 MacabConditionBoolean::~MacabConditionBoolean()
193 {
194 	delete m_pLeft;
195 	delete m_pRight;
196 }
197 // -----------------------------------------------------------------------------
198 MacabConditionOr::MacabConditionOr(MacabCondition *pLeft, MacabCondition *pRight)
199 	: MacabConditionBoolean(pLeft, pRight)
200 {
201 }
202 // -----------------------------------------------------------------------------
203 sal_Bool MacabConditionOr::isAlwaysTrue() const
204 {
205 	return m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue();
206 }
207 // -----------------------------------------------------------------------------
208 sal_Bool MacabConditionOr::isAlwaysFalse() const
209 {
210 	return m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse();
211 }
212 // -----------------------------------------------------------------------------
213 sal_Bool MacabConditionOr::eval(const MacabRecord *aRecord) const
214 {
215 	// We avoid evaluating terms as much as we can
216 	if (m_pLeft->isAlwaysTrue() || m_pRight->isAlwaysTrue()) return sal_True;
217 	if (m_pLeft->isAlwaysFalse() && m_pRight->isAlwaysFalse()) return sal_False;
218 
219 	if (m_pLeft->eval(aRecord)) return sal_True;
220 	if (m_pRight->eval(aRecord)) return sal_True;
221 
222 	return sal_False;
223 }
224 // -----------------------------------------------------------------------------
225 MacabConditionAnd::MacabConditionAnd(MacabCondition *pLeft, MacabCondition *pRight)
226 	: MacabConditionBoolean(pLeft, pRight)
227 {
228 }
229 // -----------------------------------------------------------------------------
230 sal_Bool MacabConditionAnd::isAlwaysTrue() const
231 {
232 	return m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue();
233 }
234 // -----------------------------------------------------------------------------
235 sal_Bool MacabConditionAnd::isAlwaysFalse() const
236 {
237 	return m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse();
238 }
239 // -----------------------------------------------------------------------------
240 sal_Bool MacabConditionAnd::eval(const MacabRecord *aRecord) const
241 {
242 	// We avoid evaluating terms as much as we can
243 	if (m_pLeft->isAlwaysFalse() || m_pRight->isAlwaysFalse()) return sal_False;
244 	if (m_pLeft->isAlwaysTrue() && m_pRight->isAlwaysTrue()) return sal_True;
245 
246 	if (!m_pLeft->eval(aRecord)) return sal_False;
247 	if (!m_pRight->eval(aRecord)) return sal_False;
248 
249 	return sal_True;
250 }
251