xref: /aoo41x/main/svx/source/form/ParseContext.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 
31 #include "svx/ParseContext.hxx"
32 #include "stringlistresource.hxx"
33 #include "svx/fmresids.hrc"
34 
35 #include <svx/dialmgr.hxx>
36 
37 #include <unotools/syslocale.hxx>
38 #include <vcl/svapp.hxx>
39 #include <tools/debug.hxx>
40 #include <vos/mutex.hxx>
41 
42 using namespace svxform;
43 using namespace ::connectivity;
44 //==========================================================================
45 //= OSystemParseContext
46 //==========================================================================
47 DBG_NAME(OSystemParseContext)
48 //-----------------------------------------------------------------------------
49 OSystemParseContext::OSystemParseContext() : IParseContext()
50 {
51 	DBG_CTOR(OSystemParseContext,NULL);
52 	vos::OGuard aGuard( Application::GetSolarMutex() );
53 
54     ::svx::StringListResource aKeywords( SVX_RES( RID_RSC_SQL_INTERNATIONAL ) );
55     aKeywords.get( m_aLocalizedKeywords );
56 }
57 
58 //-----------------------------------------------------------------------------
59 OSystemParseContext::~OSystemParseContext()
60 {
61 	DBG_DTOR(OSystemParseContext,NULL);
62 }
63 
64 //-----------------------------------------------------------------------------
65 ::com::sun::star::lang::Locale OSystemParseContext::getPreferredLocale( ) const
66 {
67 	return SvtSysLocale().GetLocaleData().getLocale();
68 }
69 
70 //-----------------------------------------------------------------------------
71 ::rtl::OUString OSystemParseContext::getErrorMessage(ErrorCode _eCode) const
72 {
73 	String aMsg;
74 	vos::OGuard aGuard( Application::GetSolarMutex() );
75 	switch (_eCode)
76 	{
77 		case ERROR_GENERAL:					aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_ERROR); break;
78 		case ERROR_VALUE_NO_LIKE:			aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_VALUE_NO_LIKE); break;
79 		case ERROR_FIELD_NO_LIKE:			aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_FIELD_NO_LIKE); break;
80 		case ERROR_INVALID_COMPARE:			aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_CRIT_NO_COMPARE); break;
81 		case ERROR_INVALID_INT_COMPARE:		aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_INT_NO_VALID); break;
82 		case ERROR_INVALID_DATE_COMPARE:	aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_ACCESS_DAT_NO_VALID); break;
83 		case ERROR_INVALID_REAL_COMPARE:	aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_REAL_NO_VALID); break;
84 		case ERROR_INVALID_TABLE:			aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE); break;
85 		case ERROR_INVALID_TABLE_OR_QUERY:	aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE_OR_QUERY); break;
86 		case ERROR_INVALID_COLUMN:			aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_COLUMN); break;
87         case ERROR_INVALID_TABLE_EXIST:     aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE_EXISTS); break;
88         case ERROR_INVALID_QUERY_EXIST:     aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_QUERY_EXISTS); break;
89         case ERROR_NONE: break;
90 	}
91 	return aMsg;
92 }
93 
94 //-----------------------------------------------------------------------------
95 ::rtl::OString OSystemParseContext::getIntlKeywordAscii(InternationalKeyCode _eKey) const
96 {
97     size_t nIndex = 0;
98 	switch ( _eKey )
99 	{
100 		case KEY_LIKE:		nIndex = 0; break;
101 		case KEY_NOT:		nIndex = 1; break;
102 		case KEY_NULL:		nIndex = 2; break;
103 		case KEY_TRUE:		nIndex = 3; break;
104 		case KEY_FALSE:		nIndex = 4; break;
105 		case KEY_IS:		nIndex = 5; break;
106 		case KEY_BETWEEN:	nIndex = 6; break;
107 		case KEY_OR:		nIndex = 7; break;
108 		case KEY_AND:		nIndex = 8; break;
109 		case KEY_AVG:		nIndex = 9; break;
110 		case KEY_COUNT:		nIndex = 10; break;
111 		case KEY_MAX:		nIndex = 11; break;
112 		case KEY_MIN:		nIndex = 12; break;
113 		case KEY_SUM:		nIndex = 13; break;
114         case KEY_EVERY:     nIndex = 14; break;
115         case KEY_ANY:       nIndex = 15; break;
116         case KEY_SOME:      nIndex = 16; break;
117         case KEY_STDDEV_POP: nIndex = 17; break;
118         case KEY_STDDEV_SAMP: nIndex = 18; break;
119         case KEY_VAR_SAMP:  nIndex = 19; break;
120         case KEY_VAR_POP:   nIndex = 20; break;
121         case KEY_COLLECT:   nIndex = 21; break;
122         case KEY_FUSION:    nIndex = 22; break;
123         case KEY_INTERSECTION: nIndex = 23; break;
124         case KEY_NONE:
125             DBG_ERROR( "OSystemParseContext::getIntlKeywordAscii: illegal argument!" );
126             break;
127 	}
128 
129     OSL_ENSURE( nIndex < m_aLocalizedKeywords.size(), "OSystemParseContext::getIntlKeywordAscii: invalid index!" );
130 
131 	ByteString sKeyword;
132     if ( nIndex < m_aLocalizedKeywords.size() )
133         sKeyword = ByteString( m_aLocalizedKeywords[nIndex], RTL_TEXTENCODING_UTF8 );
134 	return sKeyword;
135 }
136 
137 //-----------------------------------------------------------------------------
138 static sal_Unicode lcl_getSeparatorChar( const String& _rSeparator, sal_Unicode _nFallback )
139 {
140 	DBG_ASSERT( 0 < _rSeparator.Len(), "::lcl_getSeparatorChar: invalid decimal separator!" );
141 
142 	sal_Unicode nReturn( _nFallback );
143 	if ( _rSeparator.Len() )
144 		nReturn = static_cast< sal_Char >( _rSeparator.GetBuffer( )[0] );
145 	return nReturn;
146 }
147 
148 //-----------------------------------------------------------------------------
149 sal_Unicode OSystemParseContext::getNumDecimalSep( ) const
150 {
151 	return lcl_getSeparatorChar( SvtSysLocale().GetLocaleData().getNumDecimalSep(), '.' );
152 }
153 
154 //-----------------------------------------------------------------------------
155 sal_Unicode OSystemParseContext::getNumThousandSep( ) const
156 {
157 	return lcl_getSeparatorChar( SvtSysLocale().GetLocaleData().getNumThousandSep(), ',' );
158 }
159 // -----------------------------------------------------------------------------
160 IParseContext::InternationalKeyCode OSystemParseContext::getIntlKeyCode(const ::rtl::OString& rToken) const
161 {
162 	static IParseContext::InternationalKeyCode Intl_TokenID[] =
163 	{
164 		KEY_LIKE, KEY_NOT, KEY_NULL, KEY_TRUE,
165 		KEY_FALSE, KEY_IS, KEY_BETWEEN, KEY_OR,
166 		KEY_AND, KEY_AVG, KEY_COUNT, KEY_MAX,
167 		KEY_MIN, KEY_SUM, KEY_EVERY,
168         KEY_ANY, KEY_SOME, KEY_STDDEV_POP,
169         KEY_STDDEV_SAMP, KEY_VAR_SAMP, KEY_VAR_POP,
170         KEY_COLLECT, KEY_FUSION, KEY_INTERSECTION
171 	};
172 
173 	sal_uInt32 nCount = sizeof Intl_TokenID / sizeof Intl_TokenID[0];
174 	for (sal_uInt32 i = 0; i < nCount; i++)
175 	{
176 		::rtl::OString aKey = getIntlKeywordAscii(Intl_TokenID[i]);
177 		if (rToken.equalsIgnoreAsciiCase(aKey))
178 			return Intl_TokenID[i];
179 	}
180 
181 	return KEY_NONE;
182 }
183 
184 
185 // =============================================================================
186 // =============================================================================
187 namespace
188 {
189 	// -----------------------------------------------------------------------------
190 	::osl::Mutex& getSafteyMutex()
191 	{
192 		static ::osl::Mutex s_aSafety;
193 		return s_aSafety;
194 	}
195 	// -----------------------------------------------------------------------------
196 	oslInterlockedCount& getCounter()
197 	{
198 		static oslInterlockedCount s_nCounter;
199 		return s_nCounter;
200 	}
201 	// -----------------------------------------------------------------------------
202 	OSystemParseContext* getSharedContext(OSystemParseContext* _pContext = NULL,sal_Bool _bSet = sal_False)
203 	{
204 		static OSystemParseContext* s_pSharedContext = NULL;
205 		if ( _pContext && !s_pSharedContext )
206         {
207 			s_pSharedContext = _pContext;
208             return s_pSharedContext;
209         }
210         if ( _bSet )
211         {
212             OSystemParseContext* pReturn = _pContext ? _pContext : s_pSharedContext;
213             s_pSharedContext = _pContext;
214             return pReturn;
215         }
216 		return s_pSharedContext;
217 	}
218 	// -----------------------------------------------------------------------------
219 }
220 // -----------------------------------------------------------------------------
221 OParseContextClient::OParseContextClient()
222 {
223 	::osl::MutexGuard aGuard( getSafteyMutex() );
224 	if ( 1 == osl_incrementInterlockedCount( &getCounter() ) )
225 	{	// first instance
226 		getSharedContext( new OSystemParseContext );
227 	}
228 }
229 
230 // -----------------------------------------------------------------------------
231 OParseContextClient::~OParseContextClient()
232 {
233 	{
234 		::osl::MutexGuard aGuard( getSafteyMutex() );
235 		if ( 0 == osl_decrementInterlockedCount( &getCounter() ) )
236 			delete getSharedContext(NULL,sal_True);
237 	}
238 }
239 // -----------------------------------------------------------------------------
240 const OSystemParseContext* OParseContextClient::getParseContext() const
241 {
242 	return getSharedContext();
243 }
244 // -----------------------------------------------------------------------------
245 
246 
247 
248 
249 
250