1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ParseContext.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include "ParseContext.hxx"
35 #include "stringlistresource.hxx"
36 #include "fmresids.hrc"
38 #include <svx/dialmgr.hxx>
40 #include <svtools/syslocale.hxx>
41 #include <vcl/svapp.hxx>
42 #include <tools/debug.hxx>
43 #include <vos/mutex.hxx>
45 using namespace svxform
;
46 using namespace ::connectivity
;
47 //==========================================================================
48 //= OSystemParseContext
49 //==========================================================================
50 DBG_NAME(OSystemParseContext
)
51 //-----------------------------------------------------------------------------
52 OSystemParseContext::OSystemParseContext() : IParseContext()
54 DBG_CTOR(OSystemParseContext
,NULL
);
55 vos::OGuard
aGuard( Application::GetSolarMutex() );
57 ::svx::StringListResource
aKeywords( SVX_RES( RID_RSC_SQL_INTERNATIONAL
) );
58 aKeywords
.get( m_aLocalizedKeywords
);
61 //-----------------------------------------------------------------------------
62 OSystemParseContext::~OSystemParseContext()
64 DBG_DTOR(OSystemParseContext
,NULL
);
67 //-----------------------------------------------------------------------------
68 ::com::sun::star::lang::Locale
OSystemParseContext::getPreferredLocale( ) const
70 return SvtSysLocale().GetLocaleData().getLocale();
73 //-----------------------------------------------------------------------------
74 ::rtl::OUString
OSystemParseContext::getErrorMessage(ErrorCode _eCode
) const
77 vos::OGuard
aGuard( Application::GetSolarMutex() );
80 case ERROR_GENERAL
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_ERROR
); break;
81 case ERROR_VALUE_NO_LIKE
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_VALUE_NO_LIKE
); break;
82 case ERROR_FIELD_NO_LIKE
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_FIELD_NO_LIKE
); break;
83 case ERROR_INVALID_COMPARE
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_CRIT_NO_COMPARE
); break;
84 case ERROR_INVALID_INT_COMPARE
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_INT_NO_VALID
); break;
85 case ERROR_INVALID_DATE_COMPARE
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_ACCESS_DAT_NO_VALID
); break;
86 case ERROR_INVALID_REAL_COMPARE
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_REAL_NO_VALID
); break;
87 case ERROR_INVALID_TABLE
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE
); break;
88 case ERROR_INVALID_TABLE_OR_QUERY
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE_OR_QUERY
); break;
89 case ERROR_INVALID_COLUMN
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_COLUMN
); break;
90 case ERROR_INVALID_TABLE_EXIST
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE_EXISTS
); break;
91 case ERROR_INVALID_QUERY_EXIST
: aMsg
= SVX_RES(RID_STR_SVT_SQL_SYNTAX_QUERY_EXISTS
); break;
92 case ERROR_NONE
: break;
97 //-----------------------------------------------------------------------------
98 ::rtl::OString
OSystemParseContext::getIntlKeywordAscii(InternationalKeyCode _eKey
) const
103 case KEY_LIKE
: nIndex
= 0; break;
104 case KEY_NOT
: nIndex
= 1; break;
105 case KEY_NULL
: nIndex
= 2; break;
106 case KEY_TRUE
: nIndex
= 3; break;
107 case KEY_FALSE
: nIndex
= 4; break;
108 case KEY_IS
: nIndex
= 5; break;
109 case KEY_BETWEEN
: nIndex
= 6; break;
110 case KEY_OR
: nIndex
= 7; break;
111 case KEY_AND
: nIndex
= 8; break;
112 case KEY_AVG
: nIndex
= 9; break;
113 case KEY_COUNT
: nIndex
= 10; break;
114 case KEY_MAX
: nIndex
= 11; break;
115 case KEY_MIN
: nIndex
= 12; break;
116 case KEY_SUM
: nIndex
= 13; break;
117 case KEY_EVERY
: nIndex
= 14; break;
118 case KEY_ANY
: nIndex
= 15; break;
119 case KEY_SOME
: nIndex
= 16; break;
120 case KEY_STDDEV_POP
: nIndex
= 17; break;
121 case KEY_STDDEV_SAMP
: nIndex
= 18; break;
122 case KEY_VAR_SAMP
: nIndex
= 19; break;
123 case KEY_VAR_POP
: nIndex
= 20; break;
124 case KEY_COLLECT
: nIndex
= 21; break;
125 case KEY_FUSION
: nIndex
= 22; break;
126 case KEY_INTERSECTION
: nIndex
= 23; break;
128 DBG_ERROR( "OSystemParseContext::getIntlKeywordAscii: illegal argument!" );
132 OSL_ENSURE( nIndex
< m_aLocalizedKeywords
.size(), "OSystemParseContext::getIntlKeywordAscii: invalid index!" );
135 if ( nIndex
< m_aLocalizedKeywords
.size() )
136 sKeyword
= ByteString( m_aLocalizedKeywords
[nIndex
], RTL_TEXTENCODING_UTF8
);
140 //-----------------------------------------------------------------------------
141 static sal_Unicode
lcl_getSeparatorChar( const String
& _rSeparator
, sal_Unicode _nFallback
)
143 DBG_ASSERT( 0 < _rSeparator
.Len(), "::lcl_getSeparatorChar: invalid decimal separator!" );
145 sal_Unicode
nReturn( _nFallback
);
146 if ( _rSeparator
.Len() )
147 nReturn
= static_cast< sal_Char
>( _rSeparator
.GetBuffer( )[0] );
151 //-----------------------------------------------------------------------------
152 sal_Unicode
OSystemParseContext::getNumDecimalSep( ) const
154 return lcl_getSeparatorChar( SvtSysLocale().GetLocaleData().getNumDecimalSep(), '.' );
157 //-----------------------------------------------------------------------------
158 sal_Unicode
OSystemParseContext::getNumThousandSep( ) const
160 return lcl_getSeparatorChar( SvtSysLocale().GetLocaleData().getNumThousandSep(), ',' );
162 // -----------------------------------------------------------------------------
163 IParseContext::InternationalKeyCode
OSystemParseContext::getIntlKeyCode(const ::rtl::OString
& rToken
) const
165 static IParseContext::InternationalKeyCode Intl_TokenID
[] =
167 KEY_LIKE
, KEY_NOT
, KEY_NULL
, KEY_TRUE
,
168 KEY_FALSE
, KEY_IS
, KEY_BETWEEN
, KEY_OR
,
169 KEY_AND
, KEY_AVG
, KEY_COUNT
, KEY_MAX
,
170 KEY_MIN
, KEY_SUM
, KEY_EVERY
,
171 KEY_ANY
, KEY_SOME
, KEY_STDDEV_POP
,
172 KEY_STDDEV_SAMP
, KEY_VAR_SAMP
, KEY_VAR_POP
,
173 KEY_COLLECT
, KEY_FUSION
, KEY_INTERSECTION
176 sal_uInt32 nCount
= sizeof Intl_TokenID
/ sizeof Intl_TokenID
[0];
177 for (sal_uInt32 i
= 0; i
< nCount
; i
++)
179 ::rtl::OString aKey
= getIntlKeywordAscii(Intl_TokenID
[i
]);
180 if (rToken
.equalsIgnoreAsciiCase(aKey
))
181 return Intl_TokenID
[i
];
188 // =============================================================================
189 // =============================================================================
192 // -----------------------------------------------------------------------------
193 ::osl::Mutex
& getSafteyMutex()
195 static ::osl::Mutex s_aSafety
;
198 // -----------------------------------------------------------------------------
199 oslInterlockedCount
& getCounter()
201 static oslInterlockedCount s_nCounter
;
204 // -----------------------------------------------------------------------------
205 OSystemParseContext
* getSharedContext(OSystemParseContext
* _pContext
= NULL
,sal_Bool _bSet
= sal_False
)
207 static OSystemParseContext
* s_pSharedContext
= NULL
;
208 if ( _pContext
&& !s_pSharedContext
)
210 s_pSharedContext
= _pContext
;
211 return s_pSharedContext
;
215 OSystemParseContext
* pReturn
= _pContext
? _pContext
: s_pSharedContext
;
216 s_pSharedContext
= _pContext
;
219 return s_pSharedContext
;
221 // -----------------------------------------------------------------------------
223 // -----------------------------------------------------------------------------
224 OParseContextClient::OParseContextClient()
226 ::osl::MutexGuard
aGuard( getSafteyMutex() );
227 if ( 1 == osl_incrementInterlockedCount( &getCounter() ) )
229 getSharedContext( new OSystemParseContext
);
233 // -----------------------------------------------------------------------------
234 OParseContextClient::~OParseContextClient()
237 ::osl::MutexGuard
aGuard( getSafteyMutex() );
238 if ( 0 == osl_decrementInterlockedCount( &getCounter() ) )
239 delete getSharedContext(NULL
,sal_True
);
242 // -----------------------------------------------------------------------------
243 const OSystemParseContext
* OParseContextClient::getParseContext() const
245 return getSharedContext();
247 // -----------------------------------------------------------------------------