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