svx: add class "ONeutralParseContext"
[LibreOffice.git] / svx / source / form / ParseContext.cxx
blobd231bfb3cd52130533ffe420de9884baf5acffb2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <sal/macros.h>
22 #include <svx/ParseContext.hxx>
23 #include <svx/strings.hrc>
25 #include <svx/dialmgr.hxx>
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>
34 using namespace svxform;
35 using namespace ::connectivity;
37 OSystemParseContext::OSystemParseContext()
38 : IParseContext()
40 for (size_t i = 0; i < SAL_N_ELEMENTS(RID_RSC_SQL_INTERNATIONAL); ++i)
41 m_aLocalizedKeywords.push_back(SvxResId(RID_RSC_SQL_INTERNATIONAL[i]));
44 OSystemParseContext::OSystemParseContext(bool /*bInit*/)
45 : IParseContext()
49 OSystemParseContext::~OSystemParseContext()
53 css::lang::Locale OSystemParseContext::getPreferredLocale( ) const
55 return SvtSysLocale().GetLanguageTag().getLocale();
58 OUString OSystemParseContext::getErrorMessage(ErrorCode _eCode) const
60 OUString aMsg;
61 SolarMutexGuard aGuard;
62 switch (_eCode)
64 case ErrorCode::General: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_ERROR); break;
65 case ErrorCode::ValueNoLike: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_VALUE_NO_LIKE); break;
66 case ErrorCode::FieldNoLike: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_FIELD_NO_LIKE); break;
67 case ErrorCode::InvalidCompare: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_CRIT_NO_COMPARE); break;
68 case ErrorCode::InvalidIntCompare: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_INT_NO_VALID); break;
69 case ErrorCode::InvalidDateCompare: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_ACCESS_DAT_NO_VALID); break;
70 case ErrorCode::InvalidRealCompare: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_REAL_NO_VALID); break;
71 case ErrorCode::InvalidTableNosuch: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE); break;
72 case ErrorCode::InvalidTableOrQuery: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE_OR_QUERY); break;
73 case ErrorCode::InvalidColumn: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_COLUMN); break;
74 case ErrorCode::InvalidTableExist: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE_EXISTS); break;
75 case ErrorCode::InvalidQueryExist: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_QUERY_EXISTS); break;
76 default: break;
78 return aMsg;
81 OString OSystemParseContext::getIntlKeywordAscii(InternationalKeyCode _eKey) const
83 size_t nIndex = 0;
84 switch ( _eKey )
86 case InternationalKeyCode::Like: nIndex = 0; break;
87 case InternationalKeyCode::Not: nIndex = 1; break;
88 case InternationalKeyCode::Null: nIndex = 2; break;
89 case InternationalKeyCode::True: nIndex = 3; break;
90 case InternationalKeyCode::False: nIndex = 4; break;
91 case InternationalKeyCode::Is: nIndex = 5; break;
92 case InternationalKeyCode::Between: nIndex = 6; break;
93 case InternationalKeyCode::Or: nIndex = 7; break;
94 case InternationalKeyCode::And: nIndex = 8; break;
95 case InternationalKeyCode::Avg: nIndex = 9; break;
96 case InternationalKeyCode::Count: nIndex = 10; break;
97 case InternationalKeyCode::Max: nIndex = 11; break;
98 case InternationalKeyCode::Min: nIndex = 12; break;
99 case InternationalKeyCode::Sum: nIndex = 13; break;
100 case InternationalKeyCode::Every: nIndex = 14; break;
101 case InternationalKeyCode::Any: nIndex = 15; break;
102 case InternationalKeyCode::Some: nIndex = 16; break;
103 case InternationalKeyCode::StdDevPop: nIndex = 17; break;
104 case InternationalKeyCode::StdDevSamp: nIndex = 18; break;
105 case InternationalKeyCode::VarSamp: nIndex = 19; break;
106 case InternationalKeyCode::VarPop: nIndex = 20; break;
107 case InternationalKeyCode::Collect: nIndex = 21; break;
108 case InternationalKeyCode::Fusion: nIndex = 22; break;
109 case InternationalKeyCode::Intersection: nIndex = 23; break;
110 case InternationalKeyCode::None:
111 OSL_FAIL( "OSystemParseContext::getIntlKeywordAscii: illegal argument!" );
112 break;
115 OSL_ENSURE( nIndex < m_aLocalizedKeywords.size(), "OSystemParseContext::getIntlKeywordAscii: invalid index!" );
117 OString sKeyword;
118 if ( nIndex < m_aLocalizedKeywords.size() )
119 sKeyword = OUStringToOString(m_aLocalizedKeywords[nIndex], RTL_TEXTENCODING_UTF8);
120 return sKeyword;
124 IParseContext::InternationalKeyCode OSystemParseContext::getIntlKeyCode(const OString& rToken) const
126 static const IParseContext::InternationalKeyCode Intl_TokenID[] =
128 InternationalKeyCode::Like, InternationalKeyCode::Not, InternationalKeyCode::Null, InternationalKeyCode::True,
129 InternationalKeyCode::False, InternationalKeyCode::Is, InternationalKeyCode::Between, InternationalKeyCode::Or,
130 InternationalKeyCode::And, InternationalKeyCode::Avg, InternationalKeyCode::Count, InternationalKeyCode::Max,
131 InternationalKeyCode::Min, InternationalKeyCode::Sum, InternationalKeyCode::Every,
132 InternationalKeyCode::Any, InternationalKeyCode::Some, InternationalKeyCode::StdDevPop,
133 InternationalKeyCode::StdDevSamp, InternationalKeyCode::VarSamp, InternationalKeyCode::VarPop,
134 InternationalKeyCode::Collect, InternationalKeyCode::Fusion, InternationalKeyCode::Intersection
137 sal_uInt32 const nCount = SAL_N_ELEMENTS(Intl_TokenID);
138 for (sal_uInt32 i = 0; i < nCount; i++)
140 OString aKey = getIntlKeywordAscii(Intl_TokenID[i]);
141 if (rToken.equalsIgnoreAsciiCase(aKey))
142 return Intl_TokenID[i];
145 return InternationalKeyCode::None;
148 ONeutralParseContext::ONeutralParseContext()
149 : OSystemParseContext(false)
151 std::locale aLocale = Translate::Create("svx", LanguageTag("en-US"));
152 for (size_t i = 0; i < SAL_N_ELEMENTS(RID_RSC_SQL_INTERNATIONAL); ++i)
153 m_aLocalizedKeywords.push_back(Translate::get(RID_RSC_SQL_INTERNATIONAL[i], aLocale));
156 ONeutralParseContext::~ONeutralParseContext()
160 namespace
163 std::mutex& getSafetyMutex()
165 static ::std::mutex s_aSafety;
166 return s_aSafety;
169 int s_nCounter;
171 OSystemParseContext* getSharedContext(OSystemParseContext* _pContext, bool _bSet)
173 static OSystemParseContext* s_pSharedContext = nullptr;
174 if ( _pContext && !s_pSharedContext )
176 s_pSharedContext = _pContext;
177 return s_pSharedContext;
179 if ( _bSet )
181 OSystemParseContext* pReturn = _pContext ? _pContext : s_pSharedContext;
182 s_pSharedContext = _pContext;
183 return pReturn;
185 return s_pSharedContext;
190 OParseContextClient::OParseContextClient()
192 std::scoped_lock aGuard( getSafetyMutex() );
193 ++s_nCounter;
194 if ( 1 == s_nCounter )
195 { // first instance
196 getSharedContext( new OSystemParseContext, false );
201 OParseContextClient::~OParseContextClient()
203 std::scoped_lock aGuard( getSafetyMutex() );
204 --s_nCounter;
205 if ( 0 == s_nCounter )
206 delete getSharedContext(nullptr,true);
209 const OSystemParseContext* OParseContextClient::getParseContext() const
211 return getSharedContext(nullptr, false);
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */