1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
34 #include <boost/scoped_ptr.hpp>
35 #include <boost/shared_ptr.hpp>
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
;
58 using namespace ::com::sun::star::sdbc
;
59 using namespace ::connectivity
;
61 using ::connectivity::OSQLParseNode
;
65 static sal_Unicode
lcl_getSeparatorChar( const OUString
& _rSeparator
, sal_Unicode _nFallback
)
67 OSL_ENSURE( !_rSeparator
.isEmpty(), "::lcl_getSeparatorChar: invalid separator string!" );
69 sal_Unicode
nReturn( _nFallback
);
70 if ( !_rSeparator
.isEmpty() )
71 nReturn
= static_cast< sal_Char
>( _rSeparator
[0] );
76 //= OPredicateInputController
79 bool OPredicateInputController::getSeparatorChars( const Locale
& _rLocale
, sal_Unicode
& _rDecSep
, sal_Unicode
& _rThdSep
) const
85 LocaleDataItem aLocaleData
;
86 if ( m_xLocaleData
.is() )
88 aLocaleData
= m_xLocaleData
->getLocaleItem( _rLocale
);
89 _rDecSep
= lcl_getSeparatorChar( aLocaleData
.decimalSeparator
, _rDecSep
);
90 _rThdSep
= lcl_getSeparatorChar( aLocaleData
.decimalSeparator
, _rThdSep
);
94 catch( const Exception
& )
96 OSL_FAIL( "OPredicateInputController::getSeparatorChars: caught an exception!" );
102 OPredicateInputController::OPredicateInputController(
103 const Reference
< XComponentContext
>& rxContext
, const Reference
< XConnection
>& _rxConnection
, const IParseContext
* _pParseContext
)
104 : m_xConnection( _rxConnection
)
105 ,m_aParser( rxContext
, _pParseContext
)
109 // create a number formatter / number formats supplier pair
110 OSL_ENSURE( rxContext
.is(), "OPredicateInputController::OPredicateInputController: need a service factory!" );
111 if ( rxContext
.is() )
113 m_xFormatter
= Reference
< XNumberFormatter
>(
114 NumberFormatter::create(rxContext
),
119 Reference
< XNumberFormatsSupplier
> xNumberFormats
= ::dbtools::getNumberFormats( m_xConnection
, true );
120 if ( !xNumberFormats
.is() )
121 ::comphelper::disposeComponent( m_xFormatter
);
123 m_xFormatter
->attachNumberFormatsSupplier( xNumberFormats
);
125 // create the locale data
126 if ( rxContext
.is() )
128 m_xLocaleData
= LocaleData::create( rxContext
);
131 catch( const Exception
& )
133 OSL_FAIL( "OPredicateInputController::OPredicateInputController: caught an exception!" );
138 OSQLParseNode
* OPredicateInputController::implPredicateTree(OUString
& _rErrorMessage
, const OUString
& _rStatement
, const Reference
< XPropertySet
> & _rxField
) const
140 OSQLParseNode
* pReturn
= const_cast< OSQLParser
& >( m_aParser
).predicateTree( _rErrorMessage
, _rStatement
, m_xFormatter
, _rxField
);
142 { // is it a text field ?
143 sal_Int32 nType
= DataType::OTHER
;
144 _rxField
->getPropertyValue("Type") >>= nType
;
146 if ( ( DataType::CHAR
== nType
)
147 || ( DataType::VARCHAR
== nType
)
148 || ( DataType::LONGVARCHAR
== nType
)
149 || ( DataType::CLOB
== nType
)
151 { // yes -> force a quoted text and try again
152 OUString
sQuoted( _rStatement
);
153 if ( !sQuoted
.isEmpty()
154 && ( !sQuoted
.startsWith("'")
155 || !sQuoted
.endsWith("'")
159 static const OUString
sSingleQuote( "'" );
160 static const OUString
sDoubleQuote( "''" );
162 sal_Int32 nIndex
= -1;
164 while ( -1 != ( nIndex
= sQuoted
.indexOf( '\'',nTemp
) ) )
166 sQuoted
= sQuoted
.replaceAt( nIndex
, 1, sDoubleQuote
);
170 OUString
sTemp( sSingleQuote
);
171 ( sTemp
+= sQuoted
) += sSingleQuote
;
174 pReturn
= const_cast< OSQLParser
& >( m_aParser
).predicateTree( _rErrorMessage
, sQuoted
, m_xFormatter
, _rxField
);
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 if ( ( DataType::FLOAT
== nType
)
185 || ( DataType::REAL
== nType
)
186 || ( DataType::DOUBLE
== nType
)
187 || ( DataType::NUMERIC
== nType
)
188 || ( DataType::DECIMAL
== nType
)
191 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 getSeparatorChars( rParseContext
.getPreferredLocale(), nCtxDecSep
, nCtxThdSep
);
197 // determine the locale of the column we're building a predicate string for
198 sal_Unicode
nFmtDecSep( nCtxDecSep
);
199 sal_Unicode
nFmtThdSep( nCtxThdSep
);
202 Reference
< XPropertySetInfo
> xPSI( _rxField
->getPropertySetInfo() );
203 if ( xPSI
.is() && xPSI
->hasPropertyByName("FormatKey") )
205 sal_Int32 nFormatKey
= 0;
206 _rxField
->getPropertyValue("FormatKey") >>= nFormatKey
;
207 if ( nFormatKey
&& m_xFormatter
.is() )
209 Locale aFormatLocale
;
210 ::comphelper::getNumberFormatProperty(
217 if ( !aFormatLocale
.Language
.isEmpty() )
219 getSeparatorChars( aFormatLocale
, nFmtDecSep
, nCtxThdSep
);
224 catch( const Exception
& )
226 OSL_FAIL( "OPredicateInputController::implPredicateTree: caught an exception while dealing with the formats!" );
229 bool bDecDiffers
= ( nCtxDecSep
!= nFmtDecSep
);
230 bool bFmtDiffers
= ( nCtxThdSep
!= nFmtThdSep
);
231 if ( bDecDiffers
|| bFmtDiffers
)
232 { // okay, at least one differs
233 // "translate" the value into the "format locale"
234 OUString
sTranslated( _rStatement
);
235 const sal_Unicode
nIntermediate( '_' );
236 sTranslated
= sTranslated
.replace( nCtxDecSep
, nIntermediate
);
237 sTranslated
= sTranslated
.replace( nCtxThdSep
, nFmtThdSep
);
238 sTranslated
= sTranslated
.replace( nIntermediate
, nFmtDecSep
);
240 pReturn
= const_cast< OSQLParser
& >( m_aParser
).predicateTree( _rErrorMessage
, sTranslated
, m_xFormatter
, _rxField
);
248 bool OPredicateInputController::normalizePredicateString(
249 OUString
& _rPredicateValue
, const Reference
< XPropertySet
> & _rxField
, OUString
* _pErrorMessage
) const
251 OSL_ENSURE( m_xConnection
.is() && m_xFormatter
.is() && _rxField
.is(),
252 "OPredicateInputController::normalizePredicateString: invalid state or params!" );
254 bool bSuccess
= false;
255 if ( m_xConnection
.is() && m_xFormatter
.is() && _rxField
.is() )
259 OUString
sTransformedText( _rPredicateValue
);
260 OSQLParseNode
* pParseNode
= implPredicateTree( sError
, sTransformedText
, _rxField
);
261 if ( _pErrorMessage
) *_pErrorMessage
= sError
;
265 const IParseContext
& rParseContext
= m_aParser
.getContext();
266 sal_Unicode nDecSeparator
, nThousandSeparator
;
267 getSeparatorChars( rParseContext
.getPreferredLocale(), nDecSeparator
, nThousandSeparator
);
269 // translate it back into a string
270 sTransformedText
= OUString();
271 pParseNode
->parseNodeToPredicateStr(
272 sTransformedText
, m_xConnection
, m_xFormatter
, _rxField
, OUString(),
273 rParseContext
.getPreferredLocale(), (sal_Char
)nDecSeparator
, &rParseContext
275 _rPredicateValue
= sTransformedText
;
286 OUString
OPredicateInputController::getPredicateValue(
287 const OUString
& _rPredicateValue
, const Reference
< XPropertySet
> & _rxField
,
288 bool _bForStatementUse
, OUString
* _pErrorMessage
) const
290 OSL_ENSURE( _rxField
.is(), "OPredicateInputController::getPredicateValue: invalid params!" );
294 OUString
sValue( _rPredicateValue
);
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 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 if ( bValidQuotedText
)
306 sValue
= sValue
.copy( 1, sValue
.getLength() - 2 );
307 static const OUString
sSingleQuote( "'" );
308 static const OUString
sDoubleQuote( "''" );
310 sal_Int32 nIndex
= -1;
312 while ( -1 != ( nIndex
= sValue
.indexOf( sDoubleQuote
,nTemp
) ) )
314 sValue
= sValue
.replaceAt( nIndex
, 2, sSingleQuote
);
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 .....
323 OSQLParseNode
* pParseNode
= implPredicateTree( sError
, sValue
, _rxField
);
324 if ( _pErrorMessage
)
325 *_pErrorMessage
= sError
;
327 sReturn
= implParseNode(pParseNode
,_bForStatementUse
);
333 OUString
OPredicateInputController::getPredicateValue(
334 const OUString
& _sField
, const OUString
& _rPredicateValue
, bool _bForStatementUse
, OUString
* _pErrorMessage
) const
336 OUString sReturn
= _rPredicateValue
;
338 OUString sField
= _sField
;
339 sal_Int32 nIndex
= 0;
340 sField
= sField
.getToken(0,'(',nIndex
);
343 sal_Int32 nType
= ::connectivity::OSQLParser::getFunctionReturnType(sField
,&m_aParser
.getContext());
344 if ( nType
== DataType::OTHER
|| sField
.isEmpty() )
346 // first try the international version
347 OUString sSql
= "SELECT * FROM x WHERE " + sField
+ _rPredicateValue
;
348 boost::scoped_ptr
<OSQLParseNode
> pParseNode( const_cast< OSQLParser
& >( m_aParser
).parseTree( sError
, sSql
, true ) );
349 nType
= DataType::DOUBLE
;
350 if ( pParseNode
.get() )
352 OSQLParseNode
* pColumnRef
= pParseNode
->getByRule(OSQLParseNode::column_ref
);
359 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
360 parse::OParseColumn
* pColumn
= new parse::OParseColumn( sField
,
364 ColumnValue::NULLABLE_UNKNOWN
,
370 xMeta
.is() && xMeta
->supportsMixedCaseQuotedIdentifiers(),
374 Reference
<XPropertySet
> xColumn
= pColumn
;
375 pColumn
->setFunction(true);
376 pColumn
->setRealName(sField
);
378 OSQLParseNode
* pParseNode
= implPredicateTree( sError
, _rPredicateValue
, xColumn
);
379 if ( _pErrorMessage
)
380 *_pErrorMessage
= sError
;
381 return pParseNode
? implParseNode(pParseNode
,_bForStatementUse
) : sReturn
;
384 OUString
OPredicateInputController::implParseNode(OSQLParseNode
* pParseNode
, bool _bForStatementUse
) const
389 boost::shared_ptr
<OSQLParseNode
> xTakeOwnership(pParseNode
);
390 OSQLParseNode
* pOdbcSpec
= pParseNode
->getByRule( OSQLParseNode::odbc_fct_spec
);
393 if ( _bForStatementUse
)
395 OSQLParseNode
* pFuncSpecParent
= pOdbcSpec
->getParent();
396 OSL_ENSURE( pFuncSpecParent
, "OPredicateInputController::getPredicateValue: an ODBC func spec node without parent?" );
397 if ( pFuncSpecParent
)
398 pFuncSpecParent
->parseNodeToStr(sReturn
, m_xConnection
, &m_aParser
.getContext(), false, true);
402 OSQLParseNode
* pValueNode
= pOdbcSpec
->getChild(1);
403 if ( SQL_NODE_STRING
== pValueNode
->getNodeType() )
404 sReturn
= pValueNode
->getTokenValue();
406 pValueNode
->parseNodeToStr(sReturn
, m_xConnection
, &m_aParser
.getContext(), false, true);
411 if ( pParseNode
->count() >= 3 )
413 OSQLParseNode
* pValueNode
= pParseNode
->getChild(2);
414 OSL_ENSURE( pValueNode
, "OPredicateInputController::getPredicateValue: invalid node child!" );
415 if ( !_bForStatementUse
)
417 if ( SQL_NODE_STRING
== pValueNode
->getNodeType() )
418 sReturn
= pValueNode
->getTokenValue();
420 pValueNode
->parseNodeToStr(
421 sReturn
, m_xConnection
, &m_aParser
.getContext(), false, true
425 pValueNode
->parseNodeToStr(
426 sReturn
, m_xConnection
, &m_aParser
.getContext(), false, true
430 OSL_FAIL( "OPredicateInputController::getPredicateValue: unknown/invalid structure (noodbc)!" );
436 } // namespace dbtools
440 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */