Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/connectivity/source/drivers/file/fcode.cxx
Line
Count
Source
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
#include <file/fcode.hxx>
21
#include <osl/diagnose.h>
22
#include <sal/log.hxx>
23
#include <connectivity/sqlnode.hxx>
24
#include <sqlbison.hxx>
25
#include <com/sun/star/sdb/SQLFilterOperator.hpp>
26
27
using namespace connectivity;
28
using namespace connectivity::file;
29
using namespace ::com::sun::star::sdbc;
30
using namespace ::com::sun::star::sdb;
31
32
57.3k
OCode::~OCode() = default;
33
34
OOperandRow::OOperandRow(sal_uInt16 _nPos, sal_Int32 _rType)
35
0
    : OOperand(_rType)
36
0
    , m_nRowPos(_nPos)
37
0
{}
38
39
void OOperandRow::bindValue(const OValueRefRow& _pRow)
40
0
{
41
0
    OSL_ENSURE(_pRow.is(),"NO EMPTY row allowed!");
42
0
    m_pRow = _pRow;
43
0
    OSL_ENSURE(m_pRow.is() && m_nRowPos < m_pRow->size(),"Invalid RowPos is >= vector.size()");
44
0
    (*m_pRow)[m_nRowPos]->setBound(true);
45
0
}
46
47
void OOperandRow::setValue(const ORowSetValue& _rVal)
48
0
{
49
0
    OSL_ENSURE(m_pRow.is() && m_nRowPos < m_pRow->size(),"Invalid RowPos is >= vector.size()");
50
0
    (*(*m_pRow)[m_nRowPos]) = _rVal;
51
0
}
52
53
const ORowSetValue& OOperandRow::getValue() const
54
0
{
55
0
    OSL_ENSURE(m_pRow.is() && m_nRowPos < m_pRow->size(),"Invalid RowPos is >= vector.size()");
56
0
    return (*m_pRow)[m_nRowPos]->getValue();
57
0
}
58
59
60
void OOperandValue::setValue(const ORowSetValue& _rVal)
61
0
{
62
0
    m_aValue = _rVal;
63
0
}
64
65
OOperandParam::OOperandParam(sal_Int32 _nPos)
66
0
    : OOperandRow(static_cast<sal_uInt16>(_nPos), DataType::VARCHAR)         // Standard-Type
67
0
{
68
    //TODO: Actually do something here (the current state of OOperandParam appears to be "the
69
    // remains of the very beginnings of a never finished implementation of support for parameters
70
    // in this code", as Lionel put it in the comments at <https://gerrit.libreoffice.org/c/core/+/
71
    // 116839/1#message-7b2bbf3543f559a0b67dc35cd940e2ab8829c274> "-Werror,-Wunused-but-set-variable
72
    // (Clang 13 trunk)").
73
0
}
74
75
76
const ORowSetValue& OOperandValue::getValue() const
77
0
{
78
0
    return m_aValue;
79
0
}
80
81
82
OOperandConst::OOperandConst(const OSQLParseNode& rColumnRef, const OUString& aStrValue)
83
38.2k
{
84
38.2k
    switch (rColumnRef.getNodeType())
85
38.2k
    {
86
0
    case SQLNodeType::String:
87
0
        m_aValue = aStrValue;
88
0
        m_eDBType = DataType::VARCHAR;
89
0
        m_aValue.setBound(true);
90
0
        return;
91
38.2k
    case SQLNodeType::IntNum:
92
38.2k
    case SQLNodeType::ApproxNum:
93
38.2k
        m_aValue = aStrValue.toDouble();
94
38.2k
        m_eDBType = DataType::DOUBLE;
95
38.2k
        m_aValue.setBound(true);
96
38.2k
        return;
97
0
    default:
98
0
        break;
99
38.2k
    }
100
101
0
    if (SQL_ISTOKEN(&rColumnRef, TRUE))
102
0
    {
103
0
        m_aValue = 1.0;
104
0
        m_eDBType = DataType::BIT;
105
0
    }
106
0
    else if (SQL_ISTOKEN(&rColumnRef, FALSE))
107
0
    {
108
0
        m_aValue = 0.0;
109
0
        m_eDBType = DataType::BIT;
110
0
    }
111
0
    else
112
0
    {
113
0
        SAL_WARN( "connectivity.drivers", "Parse Error");
114
0
    }
115
0
    m_aValue.setBound(true);
116
0
}
117
118
119
// Implementation of the operators
120
121
122
bool OBoolOperator::operate(const OOperand*, const OOperand*) const
123
0
{
124
0
    return false;
125
0
}
126
127
128
void OBoolOperator::Exec(OCodeStack& rCodeStack)
129
0
{
130
0
    OOperand  *pRight   = rCodeStack.top();
131
0
    rCodeStack.pop();
132
0
    OOperand  *pLeft    = rCodeStack.top();
133
0
    rCodeStack.pop();
134
135
0
    rCodeStack.push(new OOperandResultBOOL(operate(pLeft, pRight)));
136
0
    if( typeid(OOperandResult) == typeid(*pLeft))
137
0
        delete pLeft;
138
0
    if( typeid(OOperandResult) == typeid(*pRight))
139
0
        delete pRight;
140
0
}
141
142
bool OOp_NOT::operate(const OOperand* pLeft, const OOperand* ) const
143
0
{
144
0
    return !pLeft->isValid();
145
0
}
146
147
void OOp_NOT::Exec(OCodeStack& rCodeStack)
148
0
{
149
0
    OOperand* pOperand = rCodeStack.top();
150
0
    rCodeStack.pop();
151
152
0
    rCodeStack.push(new OOperandResultBOOL(operate(pOperand, nullptr)));
153
154
0
    if( typeid(OOperandResult) == typeid(*pOperand))
155
0
        delete pOperand;
156
0
}
157
158
bool OOp_AND::operate(const OOperand* pLeft, const OOperand* pRight) const
159
0
{
160
0
    return pLeft->isValid() && pRight->isValid();
161
0
}
162
163
164
bool OOp_OR::operate(const OOperand* pLeft, const OOperand* pRight) const
165
0
{
166
0
    return pLeft->isValid() || pRight->isValid();
167
0
}
168
169
170
void OOp_ISNULL::Exec(OCodeStack& rCodeStack)
171
0
{
172
0
    OOperand* pOperand = rCodeStack.top();
173
0
    rCodeStack.pop();
174
175
0
    rCodeStack.push(new OOperandResultBOOL(operate(pOperand, nullptr)));
176
0
    if( typeid(OOperandResult) == typeid(*pOperand))
177
0
        delete pOperand;
178
0
}
179
180
181
bool OOp_ISNULL::operate(const OOperand* pOperand, const OOperand*) const
182
0
{
183
0
    return pOperand->getValue().isNull();
184
0
}
185
186
187
bool OOp_ISNOTNULL::operate(const OOperand* pOperand, const OOperand*) const
188
0
{
189
0
    return !OOp_ISNULL::operate(pOperand, nullptr);
190
0
}
191
192
193
bool OOp_LIKE::operate(const OOperand* pLeft, const OOperand* pRight) const
194
0
{
195
0
    bool bMatch;
196
0
    const ORowSetValue& aLH(pLeft->getValue());
197
0
    const ORowSetValue& aRH(pRight->getValue());
198
199
0
    if (aLH.isNull() || aRH.isNull())
200
0
        bMatch = false;
201
0
    else
202
0
    {
203
0
        bMatch = match(aRH.getString(), aLH.getString(), cEscape);
204
0
    }
205
0
    return bMatch;
206
0
}
207
208
209
bool OOp_NOTLIKE::operate(const OOperand* pLeft, const OOperand* pRight) const
210
0
{
211
0
    return !OOp_LIKE::operate(pLeft, pRight);
212
0
}
213
214
215
bool OOp_COMPARE::operate(const OOperand* pLeft, const OOperand* pRight) const
216
0
{
217
0
    const ORowSetValue& aLH(pLeft->getValue());
218
0
    const ORowSetValue& aRH(pRight->getValue());
219
220
0
    if (aLH.isNull() || aRH.isNull()) // if (!aLH.getValue() || !aRH.getValue())
221
0
        return false;
222
223
0
    bool bResult = false;
224
0
    sal_Int32 eDBType = pLeft->getDBType();
225
226
    // Comparison (depending on Data-type):
227
0
    switch (eDBType)
228
0
    {
229
0
        case DataType::CHAR:
230
0
        case DataType::VARCHAR:
231
0
        case DataType::LONGVARCHAR:
232
0
        {
233
0
            OUString sLH = aLH.getString(), sRH = aRH.getString();
234
0
            sal_Int32 nRes = sLH.compareToIgnoreAsciiCase(sRH);
235
0
            switch(aPredicateType)
236
0
            {
237
0
                case SQLFilterOperator::EQUAL:          bResult = (nRes == 0); break;
238
0
                case SQLFilterOperator::NOT_EQUAL:          bResult = (nRes != 0); break;
239
0
                case SQLFilterOperator::LESS:               bResult = (nRes <  0); break;
240
0
                case SQLFilterOperator::LESS_EQUAL:     bResult = (nRes <= 0); break;
241
0
                case SQLFilterOperator::GREATER:            bResult = (nRes >  0); break;
242
0
                case SQLFilterOperator::GREATER_EQUAL:  bResult = (nRes >= 0); break;
243
0
                default:                        bResult = false;
244
0
            }
245
0
        } break;
246
0
        case DataType::TINYINT:
247
0
        case DataType::SMALLINT:
248
0
        case DataType::INTEGER:
249
0
        case DataType::DECIMAL:
250
0
        case DataType::NUMERIC:
251
0
        case DataType::REAL:
252
0
        case DataType::DOUBLE:
253
0
        case DataType::BIT:
254
0
        case DataType::TIMESTAMP:
255
0
        case DataType::DATE:
256
0
        case DataType::TIME:
257
0
        {
258
0
            double n = aLH.getDouble(), m = aRH.getDouble();
259
260
0
            switch (aPredicateType)
261
0
            {
262
0
                case SQLFilterOperator::EQUAL:          bResult = (n == m); break;
263
0
                case SQLFilterOperator::LIKE:               bResult = (n == m); break;
264
0
                case SQLFilterOperator::NOT_EQUAL:          bResult = (n != m); break;
265
0
                case SQLFilterOperator::NOT_LIKE:           bResult = (n != m); break;
266
0
                case SQLFilterOperator::LESS:               bResult = (n < m); break;
267
0
                case SQLFilterOperator::LESS_EQUAL:     bResult = (n <= m); break;
268
0
                case SQLFilterOperator::GREATER:            bResult = (n > m); break;
269
0
                case SQLFilterOperator::GREATER_EQUAL:  bResult = (n >= m); break;
270
0
                default:                        bResult = false;
271
0
            }
272
0
        } break;
273
0
        default:
274
0
            bResult = aLH == aRH;
275
0
    }
276
0
    return bResult;
277
0
}
278
279
280
void ONumOperator::Exec(OCodeStack& rCodeStack)
281
0
{
282
0
    OOperand  *pRight   = rCodeStack.top();
283
0
    rCodeStack.pop();
284
0
    OOperand  *pLeft    = rCodeStack.top();
285
0
    rCodeStack.pop();
286
287
0
    rCodeStack.push(new OOperandResultNUM(operate(pLeft->getValue().getDouble(), pRight->getValue().getDouble())));
288
0
    if( typeid(OOperandResult) == typeid(*pLeft))
289
0
        delete pLeft;
290
0
    if( typeid(OOperandResult) == typeid(*pRight))
291
0
        delete pRight;
292
0
}
293
294
double OOp_ADD::operate(const double& fLeft,const double& fRight) const
295
0
{
296
0
    return fLeft + fRight;
297
0
}
298
299
300
double OOp_SUB::operate(const double& fLeft,const double& fRight) const
301
0
{
302
0
    return fLeft - fRight;
303
0
}
304
305
306
double OOp_MUL::operate(const double& fLeft,const double& fRight) const
307
0
{
308
0
    return fLeft * fRight;
309
0
}
310
311
312
double OOp_DIV::operate(const double& fLeft,const double& fRight) const
313
0
{
314
0
    return fLeft / fRight;
315
0
}
316
317
void ONthOperator::Exec(OCodeStack& rCodeStack)
318
0
{
319
0
    std::vector<ORowSetValue> aValues;
320
0
    std::vector<OOperand*> aOperands;
321
0
    OOperand* pOperand;
322
0
    do
323
0
    {
324
0
        OSL_ENSURE(!rCodeStack.empty(),"Stack must be none empty!");
325
0
        pOperand    = rCodeStack.top();
326
0
        rCodeStack.pop();
327
0
        assert(pOperand);
328
0
        if (pOperand && typeid(OStopOperand) != typeid(*pOperand))
329
0
            aValues.push_back( pOperand->getValue() );
330
0
        aOperands.push_back( pOperand );
331
0
    }
332
0
    while (pOperand && typeid(OStopOperand) != typeid(*pOperand));
333
334
0
    rCodeStack.push(new OOperandResult(operate(aValues)));
335
336
0
    for (const auto& rpOperand : aOperands)
337
0
    {
338
0
        if (typeid(OOperandResult) == typeid(*rpOperand))
339
0
            delete rpOperand;
340
0
    }
341
0
}
342
343
void OBinaryOperator::Exec(OCodeStack& rCodeStack)
344
0
{
345
0
    OOperand  *pRight   = rCodeStack.top();
346
0
    rCodeStack.pop();
347
0
    OOperand  *pLeft    = rCodeStack.top();
348
0
    rCodeStack.pop();
349
350
0
    if ( !rCodeStack.empty() && typeid(OStopOperand) == typeid(*rCodeStack.top()) )
351
0
        rCodeStack.pop();
352
353
0
    rCodeStack.push(new OOperandResult(operate(pLeft->getValue(),pRight->getValue())));
354
0
    if(typeid(OOperandResult) == typeid(*pRight))
355
0
        delete pRight;
356
0
    if(typeid(OOperandResult) == typeid(*pLeft))
357
0
        delete pLeft;
358
0
}
359
360
void OUnaryOperator::Exec(OCodeStack& rCodeStack)
361
0
{
362
0
    OSL_ENSURE(!rCodeStack.empty(),"Stack is empty!");
363
0
    OOperand* pOperand = rCodeStack.top();
364
0
    rCodeStack.pop();
365
366
0
    rCodeStack.push(new OOperandResult(operate(pOperand->getValue())));
367
0
    if (typeid(OOperandResult) == typeid(*pOperand))
368
0
        delete pOperand;
369
0
}
370
371
372
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */