Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <connectivity/predicateinput.hxx>
22 : #include <comphelper/processfactory.hxx>
23 : #include <comphelper/types.hxx>
24 : #include <connectivity/dbtools.hxx>
25 : #include <com/sun/star/i18n/LocaleData.hpp>
26 : #include <com/sun/star/sdbc/DataType.hpp>
27 : #include <com/sun/star/sdbc/ColumnValue.hpp>
28 : #include <com/sun/star/util/NumberFormatter.hpp>
29 : #include <osl/diagnose.h>
30 : #include <connectivity/sqlnode.hxx>
31 : #include <connectivity/PColumn.hxx>
32 : #include <comphelper/numbers.hxx>
33 :
34 : #include <boost/shared_ptr.hpp>
35 :
36 : //.........................................................................
37 : namespace dbtools
38 : {
39 : //.........................................................................
40 :
41 : using ::com::sun::star::sdbc::XConnection;
42 : using ::com::sun::star::util::XNumberFormatsSupplier;
43 : using ::com::sun::star::util::NumberFormatter;
44 : using ::com::sun::star::util::XNumberFormatter;
45 : using ::com::sun::star::uno::UNO_QUERY;
46 : using ::com::sun::star::uno::UNO_QUERY_THROW;
47 : using ::com::sun::star::uno::XComponentContext;
48 : using ::com::sun::star::beans::XPropertySet;
49 : using ::com::sun::star::beans::XPropertySetInfo;
50 : using ::com::sun::star::lang::Locale;
51 : using ::com::sun::star::uno::Exception;
52 : using ::com::sun::star::i18n::LocaleData;
53 : using ::com::sun::star::i18n::XLocaleData;
54 : using ::com::sun::star::i18n::LocaleDataItem;
55 :
56 : using namespace ::com::sun::star::sdbc;
57 : using namespace ::connectivity;
58 :
59 : using ::connectivity::OSQLParseNode;
60 :
61 : #define Reference ::com::sun::star::uno::Reference
62 :
63 : //=====================================================================
64 : //---------------------------------------------------------------------
65 0 : static sal_Unicode lcl_getSeparatorChar( const ::rtl::OUString& _rSeparator, sal_Unicode _nFallback )
66 : {
67 : OSL_ENSURE( !_rSeparator.isEmpty(), "::lcl_getSeparatorChar: invalid separator string!" );
68 :
69 0 : sal_Unicode nReturn( _nFallback );
70 0 : if ( !_rSeparator.isEmpty() )
71 0 : nReturn = static_cast< sal_Char >( _rSeparator.getStr()[0] );
72 0 : return nReturn;
73 : }
74 :
75 : //=====================================================================
76 : //= OPredicateInputController
77 : //=====================================================================
78 : //---------------------------------------------------------------------
79 0 : sal_Bool OPredicateInputController::getSeparatorChars( const Locale& _rLocale, sal_Unicode& _rDecSep, sal_Unicode& _rThdSep ) const
80 : {
81 0 : _rDecSep = '.';
82 0 : _rThdSep = ',';
83 : try
84 : {
85 0 : LocaleDataItem aLocaleData;
86 0 : if ( m_xLocaleData.is() )
87 : {
88 0 : aLocaleData = m_xLocaleData->getLocaleItem( _rLocale );
89 0 : _rDecSep = lcl_getSeparatorChar( aLocaleData.decimalSeparator, _rDecSep );
90 0 : _rThdSep = lcl_getSeparatorChar( aLocaleData.decimalSeparator, _rThdSep );
91 0 : return sal_True;
92 0 : }
93 : }
94 0 : catch( const Exception& )
95 : {
96 : OSL_FAIL( "OPredicateInputController::getSeparatorChars: caught an exception!" );
97 : }
98 0 : return sal_False;
99 : }
100 :
101 : //---------------------------------------------------------------------
102 0 : OPredicateInputController::OPredicateInputController(
103 : const Reference< XComponentContext >& rxContext, const Reference< XConnection >& _rxConnection, const IParseContext* _pParseContext )
104 : : m_xConnection( _rxConnection )
105 0 : ,m_aParser( rxContext, _pParseContext )
106 : {
107 : try
108 : {
109 : // create a number formatter / number formats supplier pair
110 : OSL_ENSURE( rxContext.is(), "OPredicateInputController::OPredicateInputController: need a service factory!" );
111 0 : if ( rxContext.is() )
112 : {
113 : m_xFormatter = Reference< XNumberFormatter >(
114 : NumberFormatter::create(rxContext),
115 : UNO_QUERY_THROW
116 0 : );
117 : }
118 :
119 0 : Reference< XNumberFormatsSupplier > xNumberFormats = ::dbtools::getNumberFormats( m_xConnection, sal_True );
120 0 : if ( !xNumberFormats.is() )
121 0 : ::comphelper::disposeComponent( m_xFormatter );
122 : else
123 0 : m_xFormatter->attachNumberFormatsSupplier( xNumberFormats );
124 :
125 : // create the locale data
126 0 : if ( rxContext.is() )
127 : {
128 0 : m_xLocaleData = LocaleData::create( rxContext );
129 0 : }
130 : }
131 0 : catch( const Exception& )
132 : {
133 : OSL_FAIL( "OPredicateInputController::OPredicateInputController: caught an exception!" );
134 : }
135 0 : }
136 :
137 : //---------------------------------------------------------------------
138 0 : OSQLParseNode* OPredicateInputController::implPredicateTree(::rtl::OUString& _rErrorMessage, const ::rtl::OUString& _rStatement, const Reference< XPropertySet > & _rxField) const
139 : {
140 0 : OSQLParseNode* pReturn = const_cast< OSQLParser& >( m_aParser ).predicateTree( _rErrorMessage, _rStatement, m_xFormatter, _rxField );
141 0 : if ( !pReturn )
142 : { // is it a text field ?
143 0 : sal_Int32 nType = DataType::OTHER;
144 0 : _rxField->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "Type" )) ) >>= nType;
145 :
146 0 : if ( ( DataType::CHAR == nType )
147 : || ( DataType::VARCHAR == nType )
148 : || ( DataType::LONGVARCHAR == nType )
149 : || ( DataType::CLOB == nType )
150 : )
151 : { // yes -> force a quoted text and try again
152 0 : ::rtl::OUString sQuoted( _rStatement );
153 0 : if ( !sQuoted.isEmpty()
154 0 : && ( (sQuoted.getStr()[0] != '\'')
155 0 : || (sQuoted.getStr()[ sQuoted.getLength() - 1 ] != '\'' )
156 : )
157 : )
158 : {
159 0 : static const ::rtl::OUString sSingleQuote( RTL_CONSTASCII_USTRINGPARAM( "'" ) );
160 0 : static const ::rtl::OUString sDoubleQuote( RTL_CONSTASCII_USTRINGPARAM( "''" ) );
161 :
162 0 : sal_Int32 nIndex = -1;
163 0 : sal_Int32 nTemp = 0;
164 0 : while ( -1 != ( nIndex = sQuoted.indexOf( '\'',nTemp ) ) )
165 : {
166 0 : sQuoted = sQuoted.replaceAt( nIndex, 1, sDoubleQuote );
167 0 : nTemp = nIndex+2;
168 : }
169 :
170 0 : ::rtl::OUString sTemp( sSingleQuote );
171 0 : ( sTemp += sQuoted ) += sSingleQuote;
172 0 : sQuoted = sTemp;
173 : }
174 0 : pReturn = const_cast< OSQLParser& >( m_aParser ).predicateTree( _rErrorMessage, sQuoted, m_xFormatter, _rxField );
175 : }
176 :
177 : // one more fallback: for numeric fields, and value strings containing a decimal/thousands separator
178 : // problem which is to be solved with this:
179 : // * a system locale "german"
180 : // * a column formatted with an english number format
181 : // => the output is german (as we use the system locale for this), i.e. "3,4"
182 : // => the input does not recognize the german text, as predicateTree uses the number format
183 : // of the column to determine the main locale - the locale on the context is only a fallback
184 0 : if ( ( DataType::FLOAT == nType )
185 : || ( DataType::REAL == nType )
186 : || ( DataType::DOUBLE == nType )
187 : || ( DataType::NUMERIC == nType )
188 : || ( DataType::DECIMAL == nType )
189 : )
190 : {
191 0 : const IParseContext& rParseContext = m_aParser.getContext();
192 : // get the separators for the locale of our parse context
193 : sal_Unicode nCtxDecSep;
194 : sal_Unicode nCtxThdSep;
195 0 : getSeparatorChars( rParseContext.getPreferredLocale(), nCtxDecSep, nCtxThdSep );
196 :
197 : // determine the locale of the column we're building a predicate string for
198 0 : sal_Unicode nFmtDecSep( nCtxDecSep );
199 0 : sal_Unicode nFmtThdSep( nCtxThdSep );
200 : try
201 : {
202 0 : Reference< XPropertySetInfo > xPSI( _rxField->getPropertySetInfo() );
203 0 : if ( xPSI.is() && xPSI->hasPropertyByName( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "FormatKey" )) ) )
204 : {
205 0 : sal_Int32 nFormatKey = 0;
206 0 : _rxField->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( "FormatKey" )) ) >>= nFormatKey;
207 0 : if ( nFormatKey && m_xFormatter.is() )
208 : {
209 0 : Locale aFormatLocale;
210 : ::comphelper::getNumberFormatProperty(
211 : m_xFormatter,
212 : nFormatKey,
213 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Locale" ) )
214 0 : ) >>= aFormatLocale;
215 :
216 : // valid locale
217 0 : if ( !aFormatLocale.Language.isEmpty() )
218 : {
219 0 : getSeparatorChars( aFormatLocale, nFmtDecSep, nCtxThdSep );
220 0 : }
221 : }
222 0 : }
223 : }
224 0 : catch( const Exception& )
225 : {
226 : OSL_FAIL( "OPredicateInputController::implPredicateTree: caught an exception while dealing with the formats!" );
227 : }
228 :
229 0 : sal_Bool bDecDiffers = ( nCtxDecSep != nFmtDecSep );
230 0 : sal_Bool bFmtDiffers = ( nCtxThdSep != nFmtThdSep );
231 0 : if ( bDecDiffers || bFmtDiffers )
232 : { // okay, at least one differs
233 : // "translate" the value into the "format locale"
234 0 : ::rtl::OUString sTranslated( _rStatement );
235 0 : const sal_Unicode nIntermediate( '_' );
236 0 : sTranslated = sTranslated.replace( nCtxDecSep, nIntermediate );
237 0 : sTranslated = sTranslated.replace( nCtxThdSep, nFmtThdSep );
238 0 : sTranslated = sTranslated.replace( nIntermediate, nFmtDecSep );
239 :
240 0 : pReturn = const_cast< OSQLParser& >( m_aParser ).predicateTree( _rErrorMessage, sTranslated, m_xFormatter, _rxField );
241 : }
242 : }
243 : }
244 0 : return pReturn;
245 : }
246 :
247 : //---------------------------------------------------------------------
248 0 : sal_Bool OPredicateInputController::normalizePredicateString(
249 : ::rtl::OUString& _rPredicateValue, const Reference< XPropertySet > & _rxField, ::rtl::OUString* _pErrorMessage ) const
250 : {
251 : OSL_ENSURE( m_xConnection.is() && m_xFormatter.is() && _rxField.is(),
252 : "OPredicateInputController::normalizePredicateString: invalid state or params!" );
253 :
254 0 : sal_Bool bSuccess = sal_False;
255 0 : if ( m_xConnection.is() && m_xFormatter.is() && _rxField.is() )
256 : {
257 : // parse the string
258 0 : ::rtl::OUString sError;
259 0 : ::rtl::OUString sTransformedText( _rPredicateValue );
260 0 : OSQLParseNode* pParseNode = implPredicateTree( sError, sTransformedText, _rxField );
261 0 : if ( _pErrorMessage ) *_pErrorMessage = sError;
262 :
263 0 : if ( pParseNode )
264 : {
265 0 : const IParseContext& rParseContext = m_aParser.getContext();
266 : sal_Unicode nDecSeparator, nThousandSeparator;
267 0 : getSeparatorChars( rParseContext.getPreferredLocale(), nDecSeparator, nThousandSeparator );
268 :
269 : // translate it back into a string
270 0 : sTransformedText = ::rtl::OUString();
271 : pParseNode->parseNodeToPredicateStr(
272 : sTransformedText, m_xConnection, m_xFormatter, _rxField,
273 0 : rParseContext.getPreferredLocale(), (sal_Char)nDecSeparator, &rParseContext
274 0 : );
275 0 : _rPredicateValue = sTransformedText;
276 0 : delete pParseNode;
277 :
278 0 : bSuccess = sal_True;
279 0 : }
280 : }
281 :
282 0 : return bSuccess;
283 : }
284 :
285 : //---------------------------------------------------------------------
286 0 : ::rtl::OUString OPredicateInputController::getPredicateValue(
287 : const ::rtl::OUString& _rPredicateValue, const Reference< XPropertySet > & _rxField,
288 : sal_Bool _bForStatementUse, ::rtl::OUString* _pErrorMessage ) const
289 : {
290 : OSL_ENSURE( _rxField.is(), "OPredicateInputController::getPredicateValue: invalid params!" );
291 0 : ::rtl::OUString sReturn;
292 0 : if ( _rxField.is() )
293 : {
294 0 : ::rtl::OUString sValue( _rPredicateValue );
295 :
296 : // a little problem : if the field is a text field, the normalizePredicateString added two
297 : // '-characters to the text. If we would give this to predicateTree this would add
298 : // two additional '-characters which we don't want. So check the field format.
299 : // FS - 06.01.00 - 71532
300 0 : sal_Bool bValidQuotedText = ( sValue.getLength() >= 2 )
301 0 : && ( sValue.getStr()[0] == '\'' )
302 0 : && ( sValue.getStr()[ sValue.getLength() - 1 ] == '\'' );
303 : // again : as normalizePredicateString always did a conversion on the value text,
304 : // bValidQuotedText == sal_True implies that we have a text field, as no other field
305 : // values will be formatted with the quote characters
306 0 : if ( bValidQuotedText )
307 : {
308 0 : sValue = sValue.copy( 1, sValue.getLength() - 2 );
309 0 : static const ::rtl::OUString sSingleQuote( RTL_CONSTASCII_USTRINGPARAM( "'" ) );
310 0 : static const ::rtl::OUString sDoubleQuote( RTL_CONSTASCII_USTRINGPARAM( "''" ) );
311 :
312 0 : sal_Int32 nIndex = -1;
313 0 : sal_Int32 nTemp = 0;
314 0 : while ( -1 != ( nIndex = sValue.indexOf( sDoubleQuote,nTemp ) ) )
315 : {
316 0 : sValue = sValue.replaceAt( nIndex, 2, sSingleQuote );
317 0 : nTemp = nIndex+2;
318 : }
319 : }
320 :
321 : // The following is mostly stolen from the former implementation in the parameter dialog
322 : // (dbaccess/source/ui/dlg/paramdialog.cxx). I do not fully understand this .....
323 :
324 0 : ::rtl::OUString sError;
325 0 : OSQLParseNode* pParseNode = implPredicateTree( sError, sValue, _rxField );
326 0 : if ( _pErrorMessage )
327 0 : *_pErrorMessage = sError;
328 :
329 0 : sReturn = implParseNode(pParseNode,_bForStatementUse);
330 : }
331 :
332 0 : return sReturn;
333 : }
334 :
335 0 : ::rtl::OUString OPredicateInputController::getPredicateValue(
336 : const ::rtl::OUString& _sField, const ::rtl::OUString& _rPredicateValue, sal_Bool _bForStatementUse, ::rtl::OUString* _pErrorMessage ) const
337 : {
338 0 : ::rtl::OUString sReturn = _rPredicateValue;
339 0 : ::rtl::OUString sError;
340 0 : ::rtl::OUString sField = _sField;
341 0 : sal_Int32 nIndex = 0;
342 0 : sField = sField.getToken(0,'(',nIndex);
343 0 : if(nIndex == -1)
344 0 : sField = _sField;
345 0 : sal_Int32 nType = ::connectivity::OSQLParser::getFunctionReturnType(sField,&m_aParser.getContext());
346 0 : if ( nType == DataType::OTHER || sField.isEmpty() )
347 : {
348 : // first try the international version
349 0 : ::rtl::OUString sSql;
350 0 : sSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SELECT * "));
351 0 : sSql += ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" FROM x WHERE "));
352 0 : sSql += sField;
353 0 : sSql += _rPredicateValue;
354 0 : ::std::auto_ptr<OSQLParseNode> pParseNode( const_cast< OSQLParser& >( m_aParser ).parseTree( sError, sSql, sal_True ) );
355 0 : nType = DataType::DOUBLE;
356 0 : if ( pParseNode.get() )
357 : {
358 0 : OSQLParseNode* pColumnRef = pParseNode->getByRule(OSQLParseNode::column_ref);
359 : if ( pColumnRef )
360 : {
361 : }
362 0 : }
363 : }
364 :
365 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
366 : parse::OParseColumn* pColumn = new parse::OParseColumn( sField,
367 : ::rtl::OUString(),
368 : ::rtl::OUString(),
369 : ::rtl::OUString(),
370 : ColumnValue::NULLABLE_UNKNOWN,
371 : 0,
372 : 0,
373 : nType,
374 : sal_False,
375 : sal_False,
376 0 : xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers(),
377 : ::rtl::OUString(),
378 : ::rtl::OUString(),
379 0 : ::rtl::OUString());
380 0 : Reference<XPropertySet> xColumn = pColumn;
381 0 : pColumn->setFunction(sal_True);
382 0 : pColumn->setRealName(sField);
383 :
384 0 : OSQLParseNode* pParseNode = implPredicateTree( sError, _rPredicateValue, xColumn );
385 0 : if ( _pErrorMessage )
386 0 : *_pErrorMessage = sError;
387 0 : return pParseNode ? implParseNode(pParseNode,_bForStatementUse) : sReturn;
388 : }
389 :
390 0 : ::rtl::OUString OPredicateInputController::implParseNode(OSQLParseNode* pParseNode,sal_Bool _bForStatementUse) const
391 : {
392 0 : ::rtl::OUString sReturn;
393 0 : if ( pParseNode )
394 : {
395 0 : boost::shared_ptr<OSQLParseNode> xTakeOwnership(pParseNode);
396 0 : OSQLParseNode* pOdbcSpec = pParseNode->getByRule( OSQLParseNode::odbc_fct_spec );
397 0 : if ( pOdbcSpec )
398 : {
399 0 : if ( _bForStatementUse )
400 : {
401 0 : OSQLParseNode* pFuncSpecParent = pOdbcSpec->getParent();
402 : OSL_ENSURE( pFuncSpecParent, "OPredicateInputController::getPredicateValue: an ODBC func spec node without parent?" );
403 0 : if ( pFuncSpecParent )
404 0 : pFuncSpecParent->parseNodeToStr(sReturn, m_xConnection, &m_aParser.getContext(), sal_False, sal_True);
405 : }
406 : else
407 : {
408 0 : OSQLParseNode* pValueNode = pOdbcSpec->getChild(1);
409 0 : if ( SQL_NODE_STRING == pValueNode->getNodeType() )
410 0 : sReturn = pValueNode->getTokenValue();
411 : else
412 0 : pValueNode->parseNodeToStr(sReturn, m_xConnection, &m_aParser.getContext(), sal_False, sal_True);
413 : }
414 : }
415 : else
416 : {
417 0 : if ( pParseNode->count() >= 3 )
418 : {
419 0 : OSQLParseNode* pValueNode = pParseNode->getChild(2);
420 : OSL_ENSURE( pValueNode, "OPredicateInputController::getPredicateValue: invalid node child!" );
421 0 : if ( !_bForStatementUse )
422 : {
423 0 : if ( SQL_NODE_STRING == pValueNode->getNodeType() )
424 0 : sReturn = pValueNode->getTokenValue();
425 : else
426 : pValueNode->parseNodeToStr(
427 0 : sReturn, m_xConnection, &m_aParser.getContext(), sal_False, sal_True
428 0 : );
429 : }
430 : else
431 : pValueNode->parseNodeToStr(
432 0 : sReturn, m_xConnection, &m_aParser.getContext(), sal_False, sal_True
433 0 : );
434 : }
435 : else
436 : OSL_FAIL( "OPredicateInputController::getPredicateValue: unknown/invalid structure (noodbc)!" );
437 0 : }
438 : }
439 0 : return sReturn;
440 : }
441 : //.........................................................................
442 : } // namespace dbtools
443 : //.........................................................................
444 :
445 :
446 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|