bump product version to 4.1.6.2
[LibreOffice.git] / svx / source / form / ParseContext.cxx
blob49017c41acefa5b57a0729d83c462e10046d3c28
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 "stringlistresource.hxx"
24 #include "svx/fmresids.hrc"
26 #include <svx/dialmgr.hxx>
28 #include <unotools/syslocale.hxx>
29 #include <vcl/svapp.hxx>
30 #include <tools/debug.hxx>
31 #include <osl/mutex.hxx>
33 using namespace svxform;
34 using namespace ::connectivity;
35 //==========================================================================
36 //= OSystemParseContext
37 //==========================================================================
38 DBG_NAME(OSystemParseContext)
39 //-----------------------------------------------------------------------------
40 OSystemParseContext::OSystemParseContext() : IParseContext()
42 DBG_CTOR(OSystemParseContext,NULL);
43 SolarMutexGuard aGuard;
45 ::svx::StringListResource aKeywords( SVX_RES( RID_RSC_SQL_INTERNATIONAL ) );
46 aKeywords.get( m_aLocalizedKeywords );
49 //-----------------------------------------------------------------------------
50 OSystemParseContext::~OSystemParseContext()
52 DBG_DTOR(OSystemParseContext,NULL);
55 //-----------------------------------------------------------------------------
56 ::com::sun::star::lang::Locale OSystemParseContext::getPreferredLocale( ) const
58 return SvtSysLocale().GetLanguageTag().getLocale();
61 //-----------------------------------------------------------------------------
62 OUString OSystemParseContext::getErrorMessage(ErrorCode _eCode) const
64 String aMsg;
65 SolarMutexGuard aGuard;
66 switch (_eCode)
68 case ERROR_GENERAL: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_ERROR); break;
69 case ERROR_VALUE_NO_LIKE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_VALUE_NO_LIKE); break;
70 case ERROR_FIELD_NO_LIKE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_FIELD_NO_LIKE); break;
71 case ERROR_INVALID_COMPARE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_CRIT_NO_COMPARE); break;
72 case ERROR_INVALID_INT_COMPARE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_INT_NO_VALID); break;
73 case ERROR_INVALID_DATE_COMPARE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_ACCESS_DAT_NO_VALID); break;
74 case ERROR_INVALID_REAL_COMPARE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_REAL_NO_VALID); break;
75 case ERROR_INVALID_TABLE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_TABLE); break;
76 case ERROR_INVALID_TABLE_OR_QUERY: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_TABLE_OR_QUERY); break;
77 case ERROR_INVALID_COLUMN: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_COLUMN); break;
78 case ERROR_INVALID_TABLE_EXIST: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_TABLE_EXISTS); break;
79 case ERROR_INVALID_QUERY_EXIST: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_QUERY_EXISTS); break;
80 case ERROR_NONE: break;
82 return aMsg;
85 //-----------------------------------------------------------------------------
86 OString OSystemParseContext::getIntlKeywordAscii(InternationalKeyCode _eKey) const
88 size_t nIndex = 0;
89 switch ( _eKey )
91 case KEY_LIKE: nIndex = 0; break;
92 case KEY_NOT: nIndex = 1; break;
93 case KEY_NULL: nIndex = 2; break;
94 case KEY_TRUE: nIndex = 3; break;
95 case KEY_FALSE: nIndex = 4; break;
96 case KEY_IS: nIndex = 5; break;
97 case KEY_BETWEEN: nIndex = 6; break;
98 case KEY_OR: nIndex = 7; break;
99 case KEY_AND: nIndex = 8; break;
100 case KEY_AVG: nIndex = 9; break;
101 case KEY_COUNT: nIndex = 10; break;
102 case KEY_MAX: nIndex = 11; break;
103 case KEY_MIN: nIndex = 12; break;
104 case KEY_SUM: nIndex = 13; break;
105 case KEY_EVERY: nIndex = 14; break;
106 case KEY_ANY: nIndex = 15; break;
107 case KEY_SOME: nIndex = 16; break;
108 case KEY_STDDEV_POP: nIndex = 17; break;
109 case KEY_STDDEV_SAMP: nIndex = 18; break;
110 case KEY_VAR_SAMP: nIndex = 19; break;
111 case KEY_VAR_POP: nIndex = 20; break;
112 case KEY_COLLECT: nIndex = 21; break;
113 case KEY_FUSION: nIndex = 22; break;
114 case KEY_INTERSECTION: nIndex = 23; break;
115 case KEY_NONE:
116 OSL_FAIL( "OSystemParseContext::getIntlKeywordAscii: illegal argument!" );
117 break;
120 OSL_ENSURE( nIndex < m_aLocalizedKeywords.size(), "OSystemParseContext::getIntlKeywordAscii: invalid index!" );
122 OString sKeyword;
123 if ( nIndex < m_aLocalizedKeywords.size() )
124 sKeyword = OUStringToOString(m_aLocalizedKeywords[nIndex], RTL_TEXTENCODING_UTF8);
125 return sKeyword;
128 // -----------------------------------------------------------------------------
129 IParseContext::InternationalKeyCode OSystemParseContext::getIntlKeyCode(const OString& rToken) const
131 static IParseContext::InternationalKeyCode Intl_TokenID[] =
133 KEY_LIKE, KEY_NOT, KEY_NULL, KEY_TRUE,
134 KEY_FALSE, KEY_IS, KEY_BETWEEN, KEY_OR,
135 KEY_AND, KEY_AVG, KEY_COUNT, KEY_MAX,
136 KEY_MIN, KEY_SUM, KEY_EVERY,
137 KEY_ANY, KEY_SOME, KEY_STDDEV_POP,
138 KEY_STDDEV_SAMP, KEY_VAR_SAMP, KEY_VAR_POP,
139 KEY_COLLECT, KEY_FUSION, KEY_INTERSECTION
142 sal_uInt32 nCount = sizeof Intl_TokenID / sizeof Intl_TokenID[0];
143 for (sal_uInt32 i = 0; i < nCount; i++)
145 OString aKey = getIntlKeywordAscii(Intl_TokenID[i]);
146 if (rToken.equalsIgnoreAsciiCase(aKey))
147 return Intl_TokenID[i];
150 return KEY_NONE;
154 // =============================================================================
155 // =============================================================================
156 namespace
158 // -----------------------------------------------------------------------------
159 ::osl::Mutex& getSafteyMutex()
161 static ::osl::Mutex s_aSafety;
162 return s_aSafety;
164 // -----------------------------------------------------------------------------
165 oslInterlockedCount& getCounter()
167 static oslInterlockedCount s_nCounter;
168 return s_nCounter;
170 // -----------------------------------------------------------------------------
171 OSystemParseContext* getSharedContext(OSystemParseContext* _pContext = NULL,sal_Bool _bSet = sal_False)
173 static OSystemParseContext* s_pSharedContext = NULL;
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;
187 // -----------------------------------------------------------------------------
189 // -----------------------------------------------------------------------------
190 OParseContextClient::OParseContextClient()
192 ::osl::MutexGuard aGuard( getSafteyMutex() );
193 if ( 1 == osl_atomic_increment( &getCounter() ) )
194 { // first instance
195 getSharedContext( new OSystemParseContext );
199 // -----------------------------------------------------------------------------
200 OParseContextClient::~OParseContextClient()
203 ::osl::MutexGuard aGuard( getSafteyMutex() );
204 if ( 0 == osl_atomic_decrement( &getCounter() ) )
205 delete getSharedContext(NULL,sal_True);
208 // -----------------------------------------------------------------------------
209 const OSystemParseContext* OParseContextClient::getParseContext() const
211 return getSharedContext();
213 // -----------------------------------------------------------------------------
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */