Bump version to 6.4-15
[LibreOffice.git] / svx / source / form / ParseContext.cxx
blobe16b614efba3955b001015133085f53efa2dc3fb
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 <osl/mutex.hxx>
32 #include <fmstring.hrc>
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()
48 css::lang::Locale OSystemParseContext::getPreferredLocale( ) const
50 return SvtSysLocale().GetLanguageTag().getLocale();
53 OUString OSystemParseContext::getErrorMessage(ErrorCode _eCode) const
55 OUString aMsg;
56 SolarMutexGuard aGuard;
57 switch (_eCode)
59 case ErrorCode::General: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_ERROR); break;
60 case ErrorCode::ValueNoLike: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_VALUE_NO_LIKE); break;
61 case ErrorCode::FieldNoLike: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_FIELD_NO_LIKE); break;
62 case ErrorCode::InvalidCompare: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_CRIT_NO_COMPARE); break;
63 case ErrorCode::InvalidIntCompare: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_INT_NO_VALID); break;
64 case ErrorCode::InvalidDateCompare: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_ACCESS_DAT_NO_VALID); break;
65 case ErrorCode::InvalidRealCompare: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_REAL_NO_VALID); break;
66 case ErrorCode::InvalidTableNosuch: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE); break;
67 case ErrorCode::InvalidTableOrQuery: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE_OR_QUERY); break;
68 case ErrorCode::InvalidColumn: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_COLUMN); break;
69 case ErrorCode::InvalidTableExist: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_TABLE_EXISTS); break;
70 case ErrorCode::InvalidQueryExist: aMsg = SvxResId(RID_STR_SVT_SQL_SYNTAX_QUERY_EXISTS); break;
71 default: break;
73 return aMsg;
76 OString OSystemParseContext::getIntlKeywordAscii(InternationalKeyCode _eKey) const
78 size_t nIndex = 0;
79 switch ( _eKey )
81 case InternationalKeyCode::Like: nIndex = 0; break;
82 case InternationalKeyCode::Not: nIndex = 1; break;
83 case InternationalKeyCode::Null: nIndex = 2; break;
84 case InternationalKeyCode::True: nIndex = 3; break;
85 case InternationalKeyCode::False: nIndex = 4; break;
86 case InternationalKeyCode::Is: nIndex = 5; break;
87 case InternationalKeyCode::Between: nIndex = 6; break;
88 case InternationalKeyCode::Or: nIndex = 7; break;
89 case InternationalKeyCode::And: nIndex = 8; break;
90 case InternationalKeyCode::Avg: nIndex = 9; break;
91 case InternationalKeyCode::Count: nIndex = 10; break;
92 case InternationalKeyCode::Max: nIndex = 11; break;
93 case InternationalKeyCode::Min: nIndex = 12; break;
94 case InternationalKeyCode::Sum: nIndex = 13; break;
95 case InternationalKeyCode::Every: nIndex = 14; break;
96 case InternationalKeyCode::Any: nIndex = 15; break;
97 case InternationalKeyCode::Some: nIndex = 16; break;
98 case InternationalKeyCode::StdDevPop: nIndex = 17; break;
99 case InternationalKeyCode::StdDevSamp: nIndex = 18; break;
100 case InternationalKeyCode::VarSamp: nIndex = 19; break;
101 case InternationalKeyCode::VarPop: nIndex = 20; break;
102 case InternationalKeyCode::Collect: nIndex = 21; break;
103 case InternationalKeyCode::Fusion: nIndex = 22; break;
104 case InternationalKeyCode::Intersection: nIndex = 23; break;
105 case InternationalKeyCode::None:
106 OSL_FAIL( "OSystemParseContext::getIntlKeywordAscii: illegal argument!" );
107 break;
110 OSL_ENSURE( nIndex < m_aLocalizedKeywords.size(), "OSystemParseContext::getIntlKeywordAscii: invalid index!" );
112 OString sKeyword;
113 if ( nIndex < m_aLocalizedKeywords.size() )
114 sKeyword = OUStringToOString(m_aLocalizedKeywords[nIndex], RTL_TEXTENCODING_UTF8);
115 return sKeyword;
119 IParseContext::InternationalKeyCode OSystemParseContext::getIntlKeyCode(const OString& rToken) const
121 static const IParseContext::InternationalKeyCode Intl_TokenID[] =
123 InternationalKeyCode::Like, InternationalKeyCode::Not, InternationalKeyCode::Null, InternationalKeyCode::True,
124 InternationalKeyCode::False, InternationalKeyCode::Is, InternationalKeyCode::Between, InternationalKeyCode::Or,
125 InternationalKeyCode::And, InternationalKeyCode::Avg, InternationalKeyCode::Count, InternationalKeyCode::Max,
126 InternationalKeyCode::Min, InternationalKeyCode::Sum, InternationalKeyCode::Every,
127 InternationalKeyCode::Any, InternationalKeyCode::Some, InternationalKeyCode::StdDevPop,
128 InternationalKeyCode::StdDevSamp, InternationalKeyCode::VarSamp, InternationalKeyCode::VarPop,
129 InternationalKeyCode::Collect, InternationalKeyCode::Fusion, InternationalKeyCode::Intersection
132 sal_uInt32 const nCount = SAL_N_ELEMENTS(Intl_TokenID);
133 for (sal_uInt32 i = 0; i < nCount; i++)
135 OString aKey = getIntlKeywordAscii(Intl_TokenID[i]);
136 if (rToken.equalsIgnoreAsciiCase(aKey))
137 return Intl_TokenID[i];
140 return InternationalKeyCode::None;
144 namespace
147 ::osl::Mutex& getSafteyMutex()
149 static ::osl::Mutex s_aSafety;
150 return s_aSafety;
153 oslInterlockedCount& getCounter()
155 static oslInterlockedCount s_nCounter;
156 return s_nCounter;
159 OSystemParseContext* getSharedContext(OSystemParseContext* _pContext, bool _bSet)
161 static OSystemParseContext* s_pSharedContext = nullptr;
162 if ( _pContext && !s_pSharedContext )
164 s_pSharedContext = _pContext;
165 return s_pSharedContext;
167 if ( _bSet )
169 OSystemParseContext* pReturn = _pContext ? _pContext : s_pSharedContext;
170 s_pSharedContext = _pContext;
171 return pReturn;
173 return s_pSharedContext;
178 OParseContextClient::OParseContextClient()
180 ::osl::MutexGuard aGuard( getSafteyMutex() );
181 if ( 1 == osl_atomic_increment( &getCounter() ) )
182 { // first instance
183 getSharedContext( new OSystemParseContext, false );
188 OParseContextClient::~OParseContextClient()
190 ::osl::MutexGuard aGuard( getSafteyMutex() );
191 if ( 0 == osl_atomic_decrement( &getCounter() ) )
192 delete getSharedContext(nullptr,true);
195 const OSystemParseContext* OParseContextClient::getParseContext() const
197 return getSharedContext(nullptr, false);
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */