xref: /AOO41X/main/svx/source/form/ParseContext.cxx (revision e761240fe9d2c90d1d0d3ac9106b7f58af465a1c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include "svx/ParseContext.hxx"
28 #include "stringlistresource.hxx"
29 #include "svx/fmresids.hrc"
30 
31 #include <svx/dialmgr.hxx>
32 
33 #include <unotools/syslocale.hxx>
34 #include <vcl/svapp.hxx>
35 #include <tools/debug.hxx>
36 #include <vos/mutex.hxx>
37 
38 using namespace svxform;
39 using namespace ::connectivity;
40 //==========================================================================
41 //= OSystemParseContext
42 //==========================================================================
DBG_NAME(OSystemParseContext)43 DBG_NAME(OSystemParseContext)
44 //-----------------------------------------------------------------------------
45 OSystemParseContext::OSystemParseContext() : IParseContext()
46 {
47     DBG_CTOR(OSystemParseContext,NULL);
48     vos::OGuard aGuard( Application::GetSolarMutex() );
49 
50     ::svx::StringListResource aKeywords( SVX_RES( RID_RSC_SQL_INTERNATIONAL ) );
51     aKeywords.get( m_aLocalizedKeywords );
52 }
53 
54 //-----------------------------------------------------------------------------
~OSystemParseContext()55 OSystemParseContext::~OSystemParseContext()
56 {
57     DBG_DTOR(OSystemParseContext,NULL);
58 }
59 
60 //-----------------------------------------------------------------------------
getPreferredLocale() const61 ::com::sun::star::lang::Locale OSystemParseContext::getPreferredLocale( ) const
62 {
63     return SvtSysLocale().GetLocaleData().getLocale();
64 }
65 
66 //-----------------------------------------------------------------------------
getErrorMessage(ErrorCode _eCode) const67 ::rtl::OUString OSystemParseContext::getErrorMessage(ErrorCode _eCode) const
68 {
69     String aMsg;
70     vos::OGuard aGuard( Application::GetSolarMutex() );
71     switch (_eCode)
72     {
73         case ERROR_GENERAL:                 aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_ERROR); break;
74         case ERROR_VALUE_NO_LIKE:           aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_VALUE_NO_LIKE); break;
75         case ERROR_FIELD_NO_LIKE:           aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_FIELD_NO_LIKE); break;
76         case ERROR_INVALID_COMPARE:         aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_CRIT_NO_COMPARE); break;
77         case ERROR_INVALID_INT_COMPARE:     aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_INT_NO_VALID); break;
78         case ERROR_INVALID_DATE_COMPARE:    aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_ACCESS_DAT_NO_VALID); break;
79         case ERROR_INVALID_REAL_COMPARE:    aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_REAL_NO_VALID); break;
80         case ERROR_INVALID_TABLE:           aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE); break;
81         case ERROR_INVALID_TABLE_OR_QUERY:  aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE_OR_QUERY); break;
82         case ERROR_INVALID_COLUMN:          aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_COLUMN); break;
83         case ERROR_INVALID_TABLE_EXIST:     aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_TABLE_EXISTS); break;
84         case ERROR_INVALID_QUERY_EXIST:     aMsg = SVX_RES(RID_STR_SVT_SQL_SYNTAX_QUERY_EXISTS); break;
85         case ERROR_NONE: break;
86     }
87     return aMsg;
88 }
89 
90 //-----------------------------------------------------------------------------
getIntlKeywordAscii(InternationalKeyCode _eKey) const91 ::rtl::OString OSystemParseContext::getIntlKeywordAscii(InternationalKeyCode _eKey) const
92 {
93     size_t nIndex = 0;
94     switch ( _eKey )
95     {
96         case KEY_LIKE:      nIndex = 0; break;
97         case KEY_NOT:       nIndex = 1; break;
98         case KEY_NULL:      nIndex = 2; break;
99         case KEY_TRUE:      nIndex = 3; break;
100         case KEY_FALSE:     nIndex = 4; break;
101         case KEY_IS:        nIndex = 5; break;
102         case KEY_BETWEEN:   nIndex = 6; break;
103         case KEY_OR:        nIndex = 7; break;
104         case KEY_AND:       nIndex = 8; break;
105         case KEY_AVG:       nIndex = 9; break;
106         case KEY_COUNT:     nIndex = 10; break;
107         case KEY_MAX:       nIndex = 11; break;
108         case KEY_MIN:       nIndex = 12; break;
109         case KEY_SUM:       nIndex = 13; break;
110         case KEY_EVERY:     nIndex = 14; break;
111         case KEY_ANY:       nIndex = 15; break;
112         case KEY_SOME:      nIndex = 16; break;
113         case KEY_STDDEV_POP: nIndex = 17; break;
114         case KEY_STDDEV_SAMP: nIndex = 18; break;
115         case KEY_VAR_SAMP:  nIndex = 19; break;
116         case KEY_VAR_POP:   nIndex = 20; break;
117         case KEY_COLLECT:   nIndex = 21; break;
118         case KEY_FUSION:    nIndex = 22; break;
119         case KEY_INTERSECTION: nIndex = 23; break;
120         case KEY_NONE:
121             DBG_ERROR( "OSystemParseContext::getIntlKeywordAscii: illegal argument!" );
122             break;
123     }
124 
125     OSL_ENSURE( nIndex < m_aLocalizedKeywords.size(), "OSystemParseContext::getIntlKeywordAscii: invalid index!" );
126 
127     ByteString sKeyword;
128     if ( nIndex < m_aLocalizedKeywords.size() )
129         sKeyword = ByteString( m_aLocalizedKeywords[nIndex], RTL_TEXTENCODING_UTF8 );
130     return sKeyword;
131 }
132 
133 //-----------------------------------------------------------------------------
lcl_getSeparatorChar(const String & _rSeparator,sal_Unicode _nFallback)134 static sal_Unicode lcl_getSeparatorChar( const String& _rSeparator, sal_Unicode _nFallback )
135 {
136     DBG_ASSERT( 0 < _rSeparator.Len(), "::lcl_getSeparatorChar: invalid decimal separator!" );
137 
138     sal_Unicode nReturn( _nFallback );
139     if ( _rSeparator.Len() )
140         nReturn = static_cast< sal_Char >( _rSeparator.GetBuffer( )[0] );
141     return nReturn;
142 }
143 
144 //-----------------------------------------------------------------------------
getNumDecimalSep() const145 sal_Unicode OSystemParseContext::getNumDecimalSep( ) const
146 {
147     return lcl_getSeparatorChar( SvtSysLocale().GetLocaleData().getNumDecimalSep(), '.' );
148 }
149 
150 //-----------------------------------------------------------------------------
getNumThousandSep() const151 sal_Unicode OSystemParseContext::getNumThousandSep( ) const
152 {
153     return lcl_getSeparatorChar( SvtSysLocale().GetLocaleData().getNumThousandSep(), ',' );
154 }
155 // -----------------------------------------------------------------------------
getIntlKeyCode(const::rtl::OString & rToken) const156 IParseContext::InternationalKeyCode OSystemParseContext::getIntlKeyCode(const ::rtl::OString& rToken) const
157 {
158     static IParseContext::InternationalKeyCode Intl_TokenID[] =
159     {
160         KEY_LIKE, KEY_NOT, KEY_NULL, KEY_TRUE,
161         KEY_FALSE, KEY_IS, KEY_BETWEEN, KEY_OR,
162         KEY_AND, KEY_AVG, KEY_COUNT, KEY_MAX,
163         KEY_MIN, KEY_SUM, KEY_EVERY,
164         KEY_ANY, KEY_SOME, KEY_STDDEV_POP,
165         KEY_STDDEV_SAMP, KEY_VAR_SAMP, KEY_VAR_POP,
166         KEY_COLLECT, KEY_FUSION, KEY_INTERSECTION
167     };
168 
169     sal_uInt32 nCount = sizeof Intl_TokenID / sizeof Intl_TokenID[0];
170     for (sal_uInt32 i = 0; i < nCount; i++)
171     {
172         ::rtl::OString aKey = getIntlKeywordAscii(Intl_TokenID[i]);
173         if (rToken.equalsIgnoreAsciiCase(aKey))
174             return Intl_TokenID[i];
175     }
176 
177     return KEY_NONE;
178 }
179 
180 
181 // =============================================================================
182 // =============================================================================
183 namespace
184 {
185     // -----------------------------------------------------------------------------
getSafetyMutex()186     ::osl::Mutex& getSafetyMutex()
187     {
188         static ::osl::Mutex s_aSafety;
189         return s_aSafety;
190     }
191     // -----------------------------------------------------------------------------
getCounter()192     oslInterlockedCount& getCounter()
193     {
194         static oslInterlockedCount s_nCounter;
195         return s_nCounter;
196     }
197     // -----------------------------------------------------------------------------
getSharedContext(OSystemParseContext * _pContext=NULL,sal_Bool _bSet=sal_False)198     OSystemParseContext* getSharedContext(OSystemParseContext* _pContext = NULL,sal_Bool _bSet = sal_False)
199     {
200         static OSystemParseContext* s_pSharedContext = NULL;
201         if ( _pContext && !s_pSharedContext )
202         {
203             s_pSharedContext = _pContext;
204             return s_pSharedContext;
205         }
206         if ( _bSet )
207         {
208             OSystemParseContext* pReturn = _pContext ? _pContext : s_pSharedContext;
209             s_pSharedContext = _pContext;
210             return pReturn;
211         }
212         return s_pSharedContext;
213     }
214     // -----------------------------------------------------------------------------
215 }
216 // -----------------------------------------------------------------------------
OParseContextClient()217 OParseContextClient::OParseContextClient()
218 {
219     ::osl::MutexGuard aGuard( getSafetyMutex() );
220     if ( 1 == osl_incrementInterlockedCount( &getCounter() ) )
221     {   // first instance
222         getSharedContext( new OSystemParseContext );
223     }
224 }
225 
226 // -----------------------------------------------------------------------------
~OParseContextClient()227 OParseContextClient::~OParseContextClient()
228 {
229     {
230         ::osl::MutexGuard aGuard( getSafetyMutex() );
231         if ( 0 == osl_decrementInterlockedCount( &getCounter() ) )
232             delete getSharedContext(NULL,sal_True);
233     }
234 }
235 // -----------------------------------------------------------------------------
getParseContext() const236 const OSystemParseContext* OParseContextClient::getParseContext() const
237 {
238     return getSharedContext();
239 }
240 // -----------------------------------------------------------------------------
241