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