Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / form / ParseContext.cxx
bloba17f12b2ee667ffa42646355e017bcbfe7e2faac
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;
36 //= OSystemParseContext
39 OSystemParseContext::OSystemParseContext() : IParseContext()
41 SolarMutexGuard aGuard;
43 ::svx::StringListResource aKeywords( SVX_RES( RID_RSC_SQL_INTERNATIONAL ) );
44 aKeywords.get( m_aLocalizedKeywords );
48 OSystemParseContext::~OSystemParseContext()
53 ::com::sun::star::lang::Locale OSystemParseContext::getPreferredLocale( ) const
55 return SvtSysLocale().GetLanguageTag().getLocale();
59 OUString OSystemParseContext::getErrorMessage(ErrorCode _eCode) const
61 OUString aMsg;
62 SolarMutexGuard aGuard;
63 switch (_eCode)
65 case ERROR_GENERAL: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_ERROR); break;
66 case ERROR_VALUE_NO_LIKE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_VALUE_NO_LIKE); break;
67 case ERROR_FIELD_NO_LIKE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_FIELD_NO_LIKE); break;
68 case ERROR_INVALID_COMPARE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_CRIT_NO_COMPARE); break;
69 case ERROR_INVALID_INT_COMPARE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_INT_NO_VALID); break;
70 case ERROR_INVALID_DATE_COMPARE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_ACCESS_DAT_NO_VALID); break;
71 case ERROR_INVALID_REAL_COMPARE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_REAL_NO_VALID); break;
72 case ERROR_INVALID_TABLE: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_TABLE); break;
73 case ERROR_INVALID_TABLE_OR_QUERY: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_TABLE_OR_QUERY); break;
74 case ERROR_INVALID_COLUMN: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_COLUMN); break;
75 case ERROR_INVALID_TABLE_EXIST: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_TABLE_EXISTS); break;
76 case ERROR_INVALID_QUERY_EXIST: aMsg = SVX_RESSTR(RID_STR_SVT_SQL_SYNTAX_QUERY_EXISTS); break;
77 case ERROR_NONE: break;
79 return aMsg;
83 OString OSystemParseContext::getIntlKeywordAscii(InternationalKeyCode _eKey) const
85 size_t nIndex = 0;
86 switch ( _eKey )
88 case KEY_LIKE: nIndex = 0; break;
89 case KEY_NOT: nIndex = 1; break;
90 case KEY_NULL: nIndex = 2; break;
91 case KEY_TRUE: nIndex = 3; break;
92 case KEY_FALSE: nIndex = 4; break;
93 case KEY_IS: nIndex = 5; break;
94 case KEY_BETWEEN: nIndex = 6; break;
95 case KEY_OR: nIndex = 7; break;
96 case KEY_AND: nIndex = 8; break;
97 case KEY_AVG: nIndex = 9; break;
98 case KEY_COUNT: nIndex = 10; break;
99 case KEY_MAX: nIndex = 11; break;
100 case KEY_MIN: nIndex = 12; break;
101 case KEY_SUM: nIndex = 13; break;
102 case KEY_EVERY: nIndex = 14; break;
103 case KEY_ANY: nIndex = 15; break;
104 case KEY_SOME: nIndex = 16; break;
105 case KEY_STDDEV_POP: nIndex = 17; break;
106 case KEY_STDDEV_SAMP: nIndex = 18; break;
107 case KEY_VAR_SAMP: nIndex = 19; break;
108 case KEY_VAR_POP: nIndex = 20; break;
109 case KEY_COLLECT: nIndex = 21; break;
110 case KEY_FUSION: nIndex = 22; break;
111 case KEY_INTERSECTION: nIndex = 23; break;
112 case KEY_NONE:
113 OSL_FAIL( "OSystemParseContext::getIntlKeywordAscii: illegal argument!" );
114 break;
117 OSL_ENSURE( nIndex < m_aLocalizedKeywords.size(), "OSystemParseContext::getIntlKeywordAscii: invalid index!" );
119 OString sKeyword;
120 if ( nIndex < m_aLocalizedKeywords.size() )
121 sKeyword = OUStringToOString(m_aLocalizedKeywords[nIndex], RTL_TEXTENCODING_UTF8);
122 return sKeyword;
126 IParseContext::InternationalKeyCode OSystemParseContext::getIntlKeyCode(const OString& rToken) const
128 static const IParseContext::InternationalKeyCode Intl_TokenID[] =
130 KEY_LIKE, KEY_NOT, KEY_NULL, KEY_TRUE,
131 KEY_FALSE, KEY_IS, KEY_BETWEEN, KEY_OR,
132 KEY_AND, KEY_AVG, KEY_COUNT, KEY_MAX,
133 KEY_MIN, KEY_SUM, KEY_EVERY,
134 KEY_ANY, KEY_SOME, KEY_STDDEV_POP,
135 KEY_STDDEV_SAMP, KEY_VAR_SAMP, KEY_VAR_POP,
136 KEY_COLLECT, KEY_FUSION, KEY_INTERSECTION
139 sal_uInt32 nCount = sizeof Intl_TokenID / sizeof Intl_TokenID[0];
140 for (sal_uInt32 i = 0; i < nCount; i++)
142 OString aKey = getIntlKeywordAscii(Intl_TokenID[i]);
143 if (rToken.equalsIgnoreAsciiCase(aKey))
144 return Intl_TokenID[i];
147 return KEY_NONE;
153 namespace
156 ::osl::Mutex& getSafteyMutex()
158 static ::osl::Mutex s_aSafety;
159 return s_aSafety;
162 oslInterlockedCount& getCounter()
164 static oslInterlockedCount s_nCounter;
165 return s_nCounter;
168 OSystemParseContext* getSharedContext(OSystemParseContext* _pContext = NULL,bool _bSet = false)
170 static OSystemParseContext* s_pSharedContext = NULL;
171 if ( _pContext && !s_pSharedContext )
173 s_pSharedContext = _pContext;
174 return s_pSharedContext;
176 if ( _bSet )
178 OSystemParseContext* pReturn = _pContext ? _pContext : s_pSharedContext;
179 s_pSharedContext = _pContext;
180 return pReturn;
182 return s_pSharedContext;
187 OParseContextClient::OParseContextClient()
189 ::osl::MutexGuard aGuard( getSafteyMutex() );
190 if ( 1 == osl_atomic_increment( &getCounter() ) )
191 { // first instance
192 getSharedContext( new OSystemParseContext );
197 OParseContextClient::~OParseContextClient()
200 ::osl::MutexGuard aGuard( getSafteyMutex() );
201 if ( 0 == osl_atomic_decrement( &getCounter() ) )
202 delete getSharedContext(NULL,true);
206 const OSystemParseContext* OParseContextClient::getParseContext() const
208 return getSharedContext();
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */