Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/form/ParseContext.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
21
#include <sal/macros.h>
22
#include <svx/ParseContext.hxx>
23
#include <svx/strings.hrc>
24
25
#include <svx/dialmgr.hxx>
26
27
#include <i18nlangtag/languagetag.hxx>
28
#include <unotools/syslocale.hxx>
29
#include <vcl/svapp.hxx>
30
#include <osl/diagnose.h>
31
#include <fmstring.hrc>
32
#include <mutex>
33
34
using namespace svxform;
35
using namespace ::connectivity;
36
37
OSystemParseContext::OSystemParseContext()
38
9.78k
    : IParseContext()
39
9.78k
{
40
244k
    for (size_t i = 0; i < SAL_N_ELEMENTS(RID_RSC_SQL_INTERNATIONAL); ++i)
41
234k
        m_aLocalizedKeywords.push_back(SvxResId(RID_RSC_SQL_INTERNATIONAL[i]));
42
9.78k
}
43
44
OSystemParseContext::OSystemParseContext(bool /*bInit*/)
45
9.78k
    : IParseContext()
46
9.78k
{
47
9.78k
}
48
49
OSystemParseContext::~OSystemParseContext()
50
19.5k
{
51
19.5k
}
52
53
css::lang::Locale OSystemParseContext::getPreferredLocale( ) const
54
19.5k
{
55
19.5k
    return SvtSysLocale().GetLanguageTag().getLocale();
56
19.5k
}
57
58
OUString OSystemParseContext::getErrorMessage(ErrorCode _eCode) const
59
0
{
60
0
    OUString aMsg;
61
0
    SolarMutexGuard aGuard;
62
0
    switch (_eCode)
63
0
    {
64
0
        case ErrorCode::General:               aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_ERROR); break;
65
0
        case ErrorCode::ValueNoLike:           aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_VALUE_NO_LIKE); break;
66
0
        case ErrorCode::FieldNoLike:           aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_FIELD_NO_LIKE); break;
67
0
        case ErrorCode::InvalidCompare:        aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_CRIT_NO_COMPARE); break;
68
0
        case ErrorCode::InvalidIntCompare:     aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_INT_NO_VALID); break;
69
0
        case ErrorCode::InvalidDateCompare:    aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_ACCESS_DAT_NO_VALID); break;
70
0
        case ErrorCode::InvalidRealCompare:    aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_REAL_NO_VALID); break;
71
0
        case ErrorCode::InvalidTableNosuch:    aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE); break;
72
0
        case ErrorCode::InvalidTableOrQuery:   aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE_OR_QUERY); break;
73
0
        case ErrorCode::InvalidColumn:         aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_COLUMN); break;
74
0
        case ErrorCode::InvalidTableExist:     aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE_EXISTS); break;
75
0
        case ErrorCode::InvalidQueryExist:     aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_QUERY_EXISTS); break;
76
0
        default: break;
77
0
    }
78
0
    return aMsg;
79
0
}
80
81
OString OSystemParseContext::getIntlKeywordAscii(InternationalKeyCode _eKey) const
82
0
{
83
0
    size_t nIndex = 0;
84
0
    switch ( _eKey )
85
0
    {
86
0
        case InternationalKeyCode::Like:      nIndex = 0; break;
87
0
        case InternationalKeyCode::Not:       nIndex = 1; break;
88
0
        case InternationalKeyCode::Null:      nIndex = 2; break;
89
0
        case InternationalKeyCode::True:      nIndex = 3; break;
90
0
        case InternationalKeyCode::False:     nIndex = 4; break;
91
0
        case InternationalKeyCode::Is:        nIndex = 5; break;
92
0
        case InternationalKeyCode::Between:   nIndex = 6; break;
93
0
        case InternationalKeyCode::Or:        nIndex = 7; break;
94
0
        case InternationalKeyCode::And:       nIndex = 8; break;
95
0
        case InternationalKeyCode::Avg:       nIndex = 9; break;
96
0
        case InternationalKeyCode::Count:     nIndex = 10; break;
97
0
        case InternationalKeyCode::Max:       nIndex = 11; break;
98
0
        case InternationalKeyCode::Min:       nIndex = 12; break;
99
0
        case InternationalKeyCode::Sum:       nIndex = 13; break;
100
0
        case InternationalKeyCode::Every:     nIndex = 14; break;
101
0
        case InternationalKeyCode::Any:       nIndex = 15; break;
102
0
        case InternationalKeyCode::Some:      nIndex = 16; break;
103
0
        case InternationalKeyCode::StdDevPop: nIndex = 17; break;
104
0
        case InternationalKeyCode::StdDevSamp: nIndex = 18; break;
105
0
        case InternationalKeyCode::VarSamp:  nIndex = 19; break;
106
0
        case InternationalKeyCode::VarPop:   nIndex = 20; break;
107
0
        case InternationalKeyCode::Collect:   nIndex = 21; break;
108
0
        case InternationalKeyCode::Fusion:    nIndex = 22; break;
109
0
        case InternationalKeyCode::Intersection: nIndex = 23; break;
110
0
        case InternationalKeyCode::None:
111
0
            OSL_FAIL( "OSystemParseContext::getIntlKeywordAscii: illegal argument!" );
112
0
            break;
113
0
    }
114
115
0
    OSL_ENSURE( nIndex < m_aLocalizedKeywords.size(), "OSystemParseContext::getIntlKeywordAscii: invalid index!" );
116
117
0
    OString sKeyword;
118
0
    if ( nIndex < m_aLocalizedKeywords.size() )
119
0
        sKeyword = OUStringToOString(m_aLocalizedKeywords[nIndex], RTL_TEXTENCODING_UTF8);
120
0
    return sKeyword;
121
0
}
122
123
124
IParseContext::InternationalKeyCode OSystemParseContext::getIntlKeyCode(const OString& rToken) const
125
0
{
126
0
    static const IParseContext::InternationalKeyCode Intl_TokenID[] =
127
0
    {
128
0
        InternationalKeyCode::Like, InternationalKeyCode::Not, InternationalKeyCode::Null, InternationalKeyCode::True,
129
0
        InternationalKeyCode::False, InternationalKeyCode::Is, InternationalKeyCode::Between, InternationalKeyCode::Or,
130
0
        InternationalKeyCode::And, InternationalKeyCode::Avg, InternationalKeyCode::Count, InternationalKeyCode::Max,
131
0
        InternationalKeyCode::Min, InternationalKeyCode::Sum, InternationalKeyCode::Every,
132
0
        InternationalKeyCode::Any, InternationalKeyCode::Some, InternationalKeyCode::StdDevPop,
133
0
        InternationalKeyCode::StdDevSamp, InternationalKeyCode::VarSamp, InternationalKeyCode::VarPop,
134
0
        InternationalKeyCode::Collect, InternationalKeyCode::Fusion, InternationalKeyCode::Intersection
135
0
    };
136
137
0
    sal_uInt32 const nCount = SAL_N_ELEMENTS(Intl_TokenID);
138
0
    for (sal_uInt32 i = 0; i < nCount; i++)
139
0
    {
140
0
        OString aKey = getIntlKeywordAscii(Intl_TokenID[i]);
141
0
        if (rToken.equalsIgnoreAsciiCase(aKey))
142
0
            return Intl_TokenID[i];
143
0
    }
144
145
0
    return InternationalKeyCode::None;
146
0
}
147
148
ONeutralParseContext::ONeutralParseContext()
149
9.78k
    : OSystemParseContext(false)
150
9.78k
{
151
9.78k
    std::locale aLocale = Translate::Create("svx", LanguageTag(u"en-US"_ustr));
152
244k
    for (size_t i = 0; i < SAL_N_ELEMENTS(RID_RSC_SQL_INTERNATIONAL); ++i)
153
234k
        m_aLocalizedKeywords.push_back(Translate::get(RID_RSC_SQL_INTERNATIONAL[i], aLocale));
154
9.78k
}
155
156
ONeutralParseContext::~ONeutralParseContext()
157
{
158
}
159
160
namespace
161
{
162
163
    std::mutex& getSafetyMutex()
164
0
    {
165
0
        static ::std::mutex s_aSafety;
166
0
        return s_aSafety;
167
0
    }
168
169
    int s_nCounter;
170
171
    OSystemParseContext* getSharedContext(OSystemParseContext* _pContext, bool _bSet)
172
0
    {
173
0
        static OSystemParseContext* s_pSharedContext = nullptr;
174
0
        if ( _pContext && !s_pSharedContext )
175
0
        {
176
0
            s_pSharedContext = _pContext;
177
0
            return s_pSharedContext;
178
0
        }
179
0
        if ( _bSet )
180
0
        {
181
0
            OSystemParseContext* pReturn = _pContext ? _pContext : s_pSharedContext;
182
0
            s_pSharedContext = _pContext;
183
0
            return pReturn;
184
0
        }
185
0
        return s_pSharedContext;
186
0
    }
187
188
}
189
190
OParseContextClient::OParseContextClient()
191
0
{
192
0
    std::scoped_lock aGuard( getSafetyMutex() );
193
0
    ++s_nCounter;
194
0
    if ( 1 == s_nCounter )
195
0
    {   // first instance
196
0
        getSharedContext( new OSystemParseContext, false );
197
0
    }
198
0
}
199
200
201
OParseContextClient::~OParseContextClient()
202
0
{
203
0
    std::scoped_lock aGuard( getSafetyMutex() );
204
0
    --s_nCounter;
205
0
    if ( 0 == s_nCounter )
206
0
        delete getSharedContext(nullptr,true);
207
0
}
208
209
const OSystemParseContext* OParseContextClient::getParseContext() const
210
0
{
211
0
    return getSharedContext(nullptr, false);
212
0
}
213
214
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */