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/scoped_ptr.hpp>
35 : #include <boost/shared_ptr.hpp>
36 :
37 :
38 : namespace dbtools
39 : {
40 :
41 :
42 : using ::com::sun::star::sdbc::XConnection;
43 : using ::com::sun::star::util::XNumberFormatsSupplier;
44 : using ::com::sun::star::util::NumberFormatter;
45 : using ::com::sun::star::util::XNumberFormatter;
46 : using ::com::sun::star::uno::UNO_QUERY;
47 : using ::com::sun::star::uno::UNO_QUERY_THROW;
48 : using ::com::sun::star::uno::XComponentContext;
49 : using ::com::sun::star::beans::XPropertySet;
50 : using ::com::sun::star::beans::XPropertySetInfo;
51 : using ::com::sun::star::lang::Locale;
52 : using ::com::sun::star::uno::Exception;
53 : using ::com::sun::star::uno::Reference;
54 : using ::com::sun::star::i18n::LocaleData;
55 : using ::com::sun::star::i18n::XLocaleData;
56 : using ::com::sun::star::i18n::LocaleDataItem;
57 :
58 : using namespace ::com::sun::star::sdbc;
59 : using namespace ::connectivity;
60 :
61 : using ::connectivity::OSQLParseNode;
62 :
63 :
64 :
65 0 : static sal_Unicode lcl_getSeparatorChar( const 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[0] );
72 0 : return nReturn;
73 : }
74 :
75 :
76 : //= OPredicateInputController
77 :
78 :
79 0 : 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 true;
92 0 : }
93 : }
94 0 : catch( const Exception& )
95 : {
96 : OSL_FAIL( "OPredicateInputController::getSeparatorChars: caught an exception!" );
97 : }
98 0 : return 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 0 : 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, 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(OUString& _rErrorMessage, const 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("Type") >>= nType;
145 :
146 0 : if ( ( DataType::CHAR == nType )
147 0 : || ( DataType::VARCHAR == nType )
148 0 : || ( DataType::LONGVARCHAR == nType )
149 0 : || ( DataType::CLOB == nType )
150 : )
151 : { // yes -> force a quoted text and try again
152 0 : OUString sQuoted( _rStatement );
153 0 : if ( !sQuoted.isEmpty()
154 0 : && ( !sQuoted.startsWith("'")
155 0 : || !sQuoted.endsWith("'")
156 : )
157 : )
158 : {
159 0 : static const OUString sSingleQuote( "'" );
160 0 : static const OUString sDoubleQuote( "''" );
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 : 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 0 : || ( DataType::REAL == nType )
186 0 : || ( DataType::DOUBLE == nType )
187 0 : || ( DataType::NUMERIC == nType )
188 0 : || ( 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("FormatKey") )
204 : {
205 0 : sal_Int32 nFormatKey = 0;
206 0 : _rxField->getPropertyValue("FormatKey") >>= nFormatKey;
207 0 : if ( nFormatKey && m_xFormatter.is() )
208 : {
209 0 : Locale aFormatLocale;
210 : ::comphelper::getNumberFormatProperty(
211 : m_xFormatter,
212 : nFormatKey,
213 : OUString( "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 : 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 : bool OPredicateInputController::normalizePredicateString(
249 : OUString& _rPredicateValue, const Reference< XPropertySet > & _rxField, 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 : bool bSuccess = false;
255 0 : if ( m_xConnection.is() && m_xFormatter.is() && _rxField.is() )
256 : {
257 : // parse the string
258 0 : OUString sError;
259 0 : 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 = OUString();
271 : pParseNode->parseNodeToPredicateStr(
272 : sTransformedText, m_xConnection, m_xFormatter, _rxField, OUString(),
273 0 : rParseContext.getPreferredLocale(), (sal_Char)nDecSeparator, &rParseContext
274 0 : );
275 0 : _rPredicateValue = sTransformedText;
276 0 : delete pParseNode;
277 :
278 0 : bSuccess = true;
279 0 : }
280 : }
281 :
282 0 : return bSuccess;
283 : }
284 :
285 :
286 0 : OUString OPredicateInputController::getPredicateValue(
287 : const OUString& _rPredicateValue, const Reference< XPropertySet > & _rxField,
288 : bool _bForStatementUse, OUString* _pErrorMessage ) const
289 : {
290 : OSL_ENSURE( _rxField.is(), "OPredicateInputController::getPredicateValue: invalid params!" );
291 0 : OUString sReturn;
292 0 : if ( _rxField.is() )
293 : {
294 0 : 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.startsWith("'") && sValue.endsWith("'");
301 : // again : as normalizePredicateString always did a conversion on the value text,
302 : // bValidQuotedText == sal_True implies that we have a text field, as no other field
303 : // values will be formatted with the quote characters
304 0 : if ( bValidQuotedText )
305 : {
306 0 : sValue = sValue.copy( 1, sValue.getLength() - 2 );
307 0 : static const OUString sSingleQuote( "'" );
308 0 : static const OUString sDoubleQuote( "''" );
309 :
310 0 : sal_Int32 nIndex = -1;
311 0 : sal_Int32 nTemp = 0;
312 0 : while ( -1 != ( nIndex = sValue.indexOf( sDoubleQuote,nTemp ) ) )
313 : {
314 0 : sValue = sValue.replaceAt( nIndex, 2, sSingleQuote );
315 0 : nTemp = nIndex+2;
316 : }
317 : }
318 :
319 : // The following is mostly stolen from the former implementation in the parameter dialog
320 : // (dbaccess/source/ui/dlg/paramdialog.cxx). I do not fully understand this .....
321 :
322 0 : OUString sError;
323 0 : OSQLParseNode* pParseNode = implPredicateTree( sError, sValue, _rxField );
324 0 : if ( _pErrorMessage )
325 0 : *_pErrorMessage = sError;
326 :
327 0 : sReturn = implParseNode(pParseNode,_bForStatementUse);
328 : }
329 :
330 0 : return sReturn;
331 : }
332 :
333 0 : OUString OPredicateInputController::getPredicateValue(
334 : const OUString& _sField, const OUString& _rPredicateValue, bool _bForStatementUse, OUString* _pErrorMessage ) const
335 : {
336 0 : OUString sReturn = _rPredicateValue;
337 0 : OUString sError;
338 0 : OUString sField = _sField;
339 0 : sal_Int32 nIndex = 0;
340 0 : sField = sField.getToken(0,'(',nIndex);
341 0 : if(nIndex == -1)
342 0 : sField = _sField;
343 0 : sal_Int32 nType = ::connectivity::OSQLParser::getFunctionReturnType(sField,&m_aParser.getContext());
344 0 : if ( nType == DataType::OTHER || sField.isEmpty() )
345 : {
346 : // first try the international version
347 0 : OUString sSql = "SELECT * FROM x WHERE " + sField + _rPredicateValue;
348 0 : boost::scoped_ptr<OSQLParseNode> pParseNode( const_cast< OSQLParser& >( m_aParser ).parseTree( sError, sSql, true ) );
349 0 : nType = DataType::DOUBLE;
350 0 : if ( pParseNode.get() )
351 : {
352 0 : OSQLParseNode* pColumnRef = pParseNode->getByRule(OSQLParseNode::column_ref);
353 : if ( pColumnRef )
354 : {
355 : }
356 0 : }
357 : }
358 :
359 0 : Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
360 : parse::OParseColumn* pColumn = new parse::OParseColumn( sField,
361 : OUString(),
362 : OUString(),
363 : OUString(),
364 : ColumnValue::NULLABLE_UNKNOWN,
365 : 0,
366 : 0,
367 : nType,
368 : false,
369 : false,
370 0 : xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers(),
371 : OUString(),
372 : OUString(),
373 0 : OUString());
374 0 : Reference<XPropertySet> xColumn = pColumn;
375 0 : pColumn->setFunction(true);
376 0 : pColumn->setRealName(sField);
377 :
378 0 : OSQLParseNode* pParseNode = implPredicateTree( sError, _rPredicateValue, xColumn );
379 0 : if ( _pErrorMessage )
380 0 : *_pErrorMessage = sError;
381 0 : return pParseNode ? implParseNode(pParseNode,_bForStatementUse) : sReturn;
382 : }
383 :
384 0 : OUString OPredicateInputController::implParseNode(OSQLParseNode* pParseNode, bool _bForStatementUse) const
385 : {
386 0 : OUString sReturn;
387 0 : if ( pParseNode )
388 : {
389 0 : boost::shared_ptr<OSQLParseNode> xTakeOwnership(pParseNode);
390 0 : OSQLParseNode* pOdbcSpec = pParseNode->getByRule( OSQLParseNode::odbc_fct_spec );
391 0 : if ( pOdbcSpec )
392 : {
393 0 : if ( _bForStatementUse )
394 : {
395 0 : OSQLParseNode* pFuncSpecParent = pOdbcSpec->getParent();
396 : OSL_ENSURE( pFuncSpecParent, "OPredicateInputController::getPredicateValue: an ODBC func spec node without parent?" );
397 0 : if ( pFuncSpecParent )
398 0 : pFuncSpecParent->parseNodeToStr(sReturn, m_xConnection, &m_aParser.getContext(), false, true);
399 : }
400 : else
401 : {
402 0 : OSQLParseNode* pValueNode = pOdbcSpec->getChild(1);
403 0 : if ( SQL_NODE_STRING == pValueNode->getNodeType() )
404 0 : sReturn = pValueNode->getTokenValue();
405 : else
406 0 : pValueNode->parseNodeToStr(sReturn, m_xConnection, &m_aParser.getContext(), false, true);
407 : }
408 : }
409 : else
410 : {
411 0 : if ( pParseNode->count() >= 3 )
412 : {
413 0 : OSQLParseNode* pValueNode = pParseNode->getChild(2);
414 : OSL_ENSURE( pValueNode, "OPredicateInputController::getPredicateValue: invalid node child!" );
415 0 : if ( !_bForStatementUse )
416 : {
417 0 : if ( SQL_NODE_STRING == pValueNode->getNodeType() )
418 0 : sReturn = pValueNode->getTokenValue();
419 : else
420 : pValueNode->parseNodeToStr(
421 0 : sReturn, m_xConnection, &m_aParser.getContext(), false, true
422 0 : );
423 : }
424 : else
425 : pValueNode->parseNodeToStr(
426 0 : sReturn, m_xConnection, &m_aParser.getContext(), false, true
427 0 : );
428 : }
429 : else
430 : OSL_FAIL( "OPredicateInputController::getPredicateValue: unknown/invalid structure (noodbc)!" );
431 0 : }
432 : }
433 0 : return sReturn;
434 : }
435 :
436 : } // namespace dbtools
437 :
438 :
439 :
440 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|