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/types.hxx>
23 #include <connectivity/dbtools.hxx>
24 #include <com/sun/star/i18n/LocaleData.hpp>
25 #include <com/sun/star/sdbc/DataType.hpp>
26 #include <com/sun/star/sdbc/ColumnValue.hpp>
27 #include <com/sun/star/sdbc/XConnection.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 #include <comphelper/diagnose_ex.hxx>
36 #include <string_view>
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::uno::UNO_QUERY_THROW
;
46 using ::com::sun::star::uno::XComponentContext
;
47 using ::com::sun::star::beans::XPropertySet
;
48 using ::com::sun::star::beans::XPropertySetInfo
;
49 using ::com::sun::star::lang::Locale
;
50 using ::com::sun::star::uno::Exception
;
51 using ::com::sun::star::uno::Reference
;
52 using ::com::sun::star::i18n::LocaleData
;
53 using ::com::sun::star::i18n::LocaleDataItem
;
54 using ::com::sun::star::uno::Any
;
56 using namespace ::com::sun::star::sdbc
;
57 using namespace ::connectivity
;
59 using ::connectivity::OSQLParseNode
;
62 static sal_Unicode
lcl_getSeparatorChar(
63 std::u16string_view _rSeparator
, sal_Unicode _nFallback
)
65 OSL_ENSURE( !_rSeparator
.empty(), "::lcl_getSeparatorChar: invalid separator string!" );
67 sal_Unicode
nReturn( _nFallback
);
68 if ( !_rSeparator
.empty() )
69 nReturn
= _rSeparator
[0];
73 bool OPredicateInputController::getSeparatorChars( const Locale
& _rLocale
, sal_Unicode
& _rDecSep
, sal_Unicode
& _rThdSep
) const
79 LocaleDataItem aLocaleData
;
80 if ( m_xLocaleData
.is() )
82 aLocaleData
= m_xLocaleData
->getLocaleItem( _rLocale
);
83 _rDecSep
= lcl_getSeparatorChar( aLocaleData
.decimalSeparator
, _rDecSep
);
84 _rThdSep
= lcl_getSeparatorChar( aLocaleData
.thousandSeparator
, _rThdSep
);
88 catch( const Exception
& )
90 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OPredicateInputController::getSeparatorChars" );
96 OPredicateInputController::OPredicateInputController(
97 const Reference
< XComponentContext
>& rxContext
, const Reference
< XConnection
>& _rxConnection
, const IParseContext
* _pParseContext
)
98 : m_xConnection( _rxConnection
)
99 ,m_aParser( rxContext
, _pParseContext
)
103 // create a number formatter / number formats supplier pair
104 OSL_ENSURE( rxContext
.is(), "OPredicateInputController::OPredicateInputController: need a service factory!" );
105 if ( rxContext
.is() )
107 m_xFormatter
.set( NumberFormatter::create(rxContext
), UNO_QUERY_THROW
);
110 Reference
< XNumberFormatsSupplier
> xNumberFormats
= ::dbtools::getNumberFormats( m_xConnection
, true );
111 if ( !xNumberFormats
.is() )
112 ::comphelper::disposeComponent( m_xFormatter
);
114 m_xFormatter
->attachNumberFormatsSupplier( xNumberFormats
);
116 // create the locale data
117 if ( rxContext
.is() )
119 m_xLocaleData
= LocaleData::create( rxContext
);
122 catch( const Exception
& )
124 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OPredicateInputController::OPredicateInputController" );
129 std::unique_ptr
<OSQLParseNode
> OPredicateInputController::implPredicateTree(OUString
& _rErrorMessage
, const OUString
& _rStatement
, const Reference
< XPropertySet
> & _rxField
) const
131 std::unique_ptr
<OSQLParseNode
> pReturn
= const_cast< OSQLParser
& >( m_aParser
).predicateTree( _rErrorMessage
, _rStatement
, m_xFormatter
, _rxField
);
133 { // is it a text field ?
134 sal_Int32 nType
= DataType::OTHER
;
135 _rxField
->getPropertyValue(u
"Type"_ustr
) >>= nType
;
137 if ( ( DataType::CHAR
== nType
)
138 || ( DataType::VARCHAR
== nType
)
139 || ( DataType::LONGVARCHAR
== nType
)
140 || ( DataType::CLOB
== nType
)
142 { // yes -> force a quoted text and try again
143 OUString
sQuoted( _rStatement
);
144 if ( !sQuoted
.isEmpty()
145 && ( !sQuoted
.startsWith("'")
146 || !sQuoted
.endsWith("'")
150 sQuoted
= u
"'" + sQuoted
.replaceAll(u
"'", u
"''") + u
"'";
152 pReturn
= const_cast< OSQLParser
& >( m_aParser
).predicateTree( _rErrorMessage
, sQuoted
, m_xFormatter
, _rxField
);
155 // one more fallback: for numeric fields, and value strings containing a decimal/thousands separator
156 // problem which is to be solved with this:
157 // * a system locale "german"
158 // * a column formatted with an english number format
159 // => the output is german (as we use the system locale for this), i.e. "3,4"
160 // => the input does not recognize the german text, as predicateTree uses the number format
161 // of the column to determine the main locale - the locale on the context is only a fallback
162 if ( ( DataType::FLOAT
== nType
)
163 || ( DataType::REAL
== nType
)
164 || ( DataType::DOUBLE
== nType
)
165 || ( DataType::NUMERIC
== nType
)
166 || ( DataType::DECIMAL
== nType
)
169 const IParseContext
& rParseContext
= m_aParser
.getContext();
170 // get the separators for the locale of our parse context
171 sal_Unicode nCtxDecSep
;
172 sal_Unicode nCtxThdSep
;
173 getSeparatorChars( rParseContext
.getPreferredLocale(), nCtxDecSep
, nCtxThdSep
);
175 // determine the locale of the column we're building a predicate string for
176 sal_Unicode
nFmtDecSep( nCtxDecSep
);
177 sal_Unicode
nFmtThdSep( nCtxThdSep
);
180 Reference
< XPropertySetInfo
> xPSI( _rxField
->getPropertySetInfo() );
181 if ( xPSI
.is() && xPSI
->hasPropertyByName(u
"FormatKey"_ustr
) )
183 sal_Int32 nFormatKey
= 0;
184 _rxField
->getPropertyValue(u
"FormatKey"_ustr
) >>= nFormatKey
;
185 if ( nFormatKey
&& m_xFormatter
.is() )
187 Locale aFormatLocale
;
188 ::comphelper::getNumberFormatProperty(
195 if ( !aFormatLocale
.Language
.isEmpty() )
197 getSeparatorChars( aFormatLocale
, nFmtDecSep
, nCtxThdSep
);
202 catch( const Exception
& )
204 TOOLS_WARN_EXCEPTION( "connectivity.commontools", "OPredicateInputController::implPredicateTree: caught an exception while dealing with the formats!" );
207 bool bDecDiffers
= ( nCtxDecSep
!= nFmtDecSep
);
208 bool bFmtDiffers
= ( nCtxThdSep
!= nFmtThdSep
);
209 if ( bDecDiffers
|| bFmtDiffers
)
210 { // okay, at least one differs
211 // "translate" the value into the "format locale"
212 OUString
sTranslated( _rStatement
);
213 const sal_Unicode
nIntermediate( '_' );
214 sTranslated
= sTranslated
.replace( nCtxDecSep
, nIntermediate
);
215 sTranslated
= sTranslated
.replace( nCtxThdSep
, nFmtThdSep
);
216 sTranslated
= sTranslated
.replace( nIntermediate
, nFmtDecSep
);
218 pReturn
= const_cast< OSQLParser
& >( m_aParser
).predicateTree( _rErrorMessage
, sTranslated
, m_xFormatter
, _rxField
);
226 bool OPredicateInputController::normalizePredicateString(
227 OUString
& _rPredicateValue
, const Reference
< XPropertySet
> & _rxField
, OUString
* _pErrorMessage
) const
229 OSL_ENSURE( m_xConnection
.is() && m_xFormatter
.is() && _rxField
.is(),
230 "OPredicateInputController::normalizePredicateString: invalid state or params!" );
232 bool bSuccess
= false;
233 if ( m_xConnection
.is() && m_xFormatter
.is() && _rxField
.is() )
237 OUString
sTransformedText( _rPredicateValue
);
238 std::unique_ptr
<OSQLParseNode
> pParseNode
= implPredicateTree( sError
, sTransformedText
, _rxField
);
239 if ( _pErrorMessage
) *_pErrorMessage
= sError
;
243 const IParseContext
& rParseContext
= m_aParser
.getContext();
244 sal_Unicode nDecSeparator
, nThousandSeparator
;
245 getSeparatorChars( rParseContext
.getPreferredLocale(), nDecSeparator
, nThousandSeparator
);
247 // translate it back into a string
248 sTransformedText
.clear();
249 pParseNode
->parseNodeToPredicateStr(
250 sTransformedText
, m_xConnection
, m_xFormatter
, _rxField
, OUString(),
251 rParseContext
.getPreferredLocale(), OUString(nDecSeparator
), &rParseContext
253 _rPredicateValue
= sTransformedText
;
263 OUString
OPredicateInputController::getPredicateValueStr(
264 const OUString
& _rPredicateValue
, const Reference
< XPropertySet
> & _rxField
) const
266 OSL_ENSURE( _rxField
.is(), "OPredicateInputController::getPredicateValue: invalid params!" );
270 // The following is mostly stolen from the former implementation in the parameter dialog
271 // (dbaccess/source/ui/dlg/paramdialog.cxx). I do not fully understand this...
274 std::unique_ptr
<OSQLParseNode
> pParseNode
= implPredicateTree( sError
, _rPredicateValue
, _rxField
);
276 implParseNode(std::move(pParseNode
), true) >>= sReturn
;
282 OUString
OPredicateInputController::getPredicateValueStr(
283 const OUString
& _sField
, const OUString
& _rPredicateValue
) const
285 OUString sReturn
= _rPredicateValue
;
287 sal_Int32 nIndex
= 0;
288 OUString sField
= _sField
.getToken(0, '(', nIndex
);
291 sal_Int32 nType
= ::connectivity::OSQLParser::getFunctionReturnType(sField
,&m_aParser
.getContext());
292 if ( nType
== DataType::OTHER
|| sField
.isEmpty() )
294 // first try the international version
295 OUString sSql
= "SELECT * FROM x WHERE " + sField
+ _rPredicateValue
;
296 const_cast< OSQLParser
& >( m_aParser
).parseTree( sError
, sSql
, true );
297 nType
= DataType::DOUBLE
;
300 Reference
<XDatabaseMetaData
> xMeta
= m_xConnection
->getMetaData();
301 rtl::Reference
<parse::OParseColumn
> pColumn
= new parse::OParseColumn( sField
,
305 ColumnValue::NULLABLE_UNKNOWN
,
311 xMeta
.is() && xMeta
->supportsMixedCaseQuotedIdentifiers(),
315 Reference
<XPropertySet
> xColumn
= pColumn
;
316 pColumn
->setFunction(true);
317 pColumn
->setRealName(sField
);
319 std::unique_ptr
<OSQLParseNode
> pParseNode
= implPredicateTree( sError
, _rPredicateValue
, xColumn
);
322 implParseNode(std::move(pParseNode
), true) >>= sReturn
;
327 Any
OPredicateInputController::getPredicateValue(
328 const OUString
& _rPredicateValue
, const Reference
< XPropertySet
> & _rxField
) const
330 OSL_ENSURE( _rxField
.is(), "OPredicateInputController::getPredicateValue: invalid params!" );
334 // The following is mostly stolen from the former implementation in the parameter dialog
335 // (dbaccess/source/ui/dlg/paramdialog.cxx). I do not fully understand this...
338 std::unique_ptr
<OSQLParseNode
> pParseNode
= implPredicateTree( sError
, _rPredicateValue
, _rxField
);
340 return implParseNode(std::move(pParseNode
), false);
346 Any
OPredicateInputController::implParseNode(std::unique_ptr
<OSQLParseNode
> pParseNode
, bool _bForStatementUse
) const
353 OSQLParseNode
* pOdbcSpec
= pParseNode
->getByRule( OSQLParseNode::odbc_fct_spec
);
356 if ( _bForStatementUse
)
358 OSQLParseNode
* pFuncSpecParent
= pOdbcSpec
->getParent();
359 OSL_ENSURE( pFuncSpecParent
, "OPredicateInputController::getPredicateValue: an ODBC func spec node without parent?" );
360 if ( pFuncSpecParent
)
361 pFuncSpecParent
->parseNodeToStr(sReturn
, m_xConnection
, &m_aParser
.getContext());
365 OSQLParseNode
* pValueNode
= pOdbcSpec
->getChild(1);
366 if ( SQLNodeType::String
== pValueNode
->getNodeType() )
367 sReturn
= pValueNode
->getTokenValue();
369 pValueNode
->parseNodeToStr(sReturn
, m_xConnection
, &m_aParser
.getContext());
374 if (pParseNode
->getKnownRuleID() == OSQLParseNode::test_for_null
)
376 assert(pParseNode
->count() == 2);
379 // LEM this seems overly permissive as test...
380 else if (pParseNode
->count() >= 3)
382 OSQLParseNode
* pValueNode
= pParseNode
->getChild(2);
383 assert(pValueNode
&& "OPredicateInputController::getPredicateValue: invalid node child!");
384 if ( !_bForStatementUse
)
386 if ( SQLNodeType::String
== pValueNode
->getNodeType() )
387 sReturn
= pValueNode
->getTokenValue();
389 pValueNode
->parseNodeToStr(
390 sReturn
, m_xConnection
, &m_aParser
.getContext()
394 pValueNode
->parseNodeToStr(
395 sReturn
, m_xConnection
, &m_aParser
.getContext()
400 OSL_FAIL( "OPredicateInputController::getPredicateValue: unknown/invalid structure (noodbc)!" );
408 } // namespace dbtools
411 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */