LibreOffice Module connectivity (master) 1
ParameterSubstitution.cxx
Go to the documentation of this file.
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 */
23#include <utility>
24
25namespace connectivity
26{
27 using namespace ::com::sun::star::uno;
28 using namespace ::com::sun::star::sdbc;
29 using namespace ::com::sun::star::lang;
30 using namespace ::com::sun::star;
31
32 ParameterSubstitution::ParameterSubstitution(css::uno::Reference< css::uno::XComponentContext > _xContext ) : m_xContext(std::move(_xContext))
33 {
34 }
35 void SAL_CALL ParameterSubstitution::initialize( const uno::Sequence< uno::Any >& _aArguments )
36 {
37 ::osl::MutexGuard aGuard(m_aMutex);
38 comphelper::SequenceAsHashMap aArgs(_aArguments);
39 uno::Reference< sdbc::XConnection > xConnection;
40 xConnection = aArgs.getUnpackedValueOrDefault("ActiveConnection",xConnection);
41 m_xConnection = xConnection;
42 }
43
45 {
46 return "org.openoffice.comp.helper.ParameterSubstitution";
47 }
48
49 sal_Bool SAL_CALL ParameterSubstitution::supportsService( const OUString& _rServiceName )
50 {
51 return cppu::supportsService(this, _rServiceName);
52 }
53
54 Sequence< OUString > SAL_CALL ParameterSubstitution::getSupportedServiceNames( )
55 {
56 return { "com.sun.star.sdb.ParameterSubstitution" };
57 }
58
59
60 OUString SAL_CALL ParameterSubstitution::substituteVariables( const OUString& _sText, sal_Bool /*bSubstRequired*/ )
61 {
62 OUString sRet = _sText;
63 uno::Reference< sdbc::XConnection > xConnection = m_xConnection;
64 if ( xConnection.is() )
65 {
66 try
67 {
68 OSQLParser aParser( m_xContext );
69 OUString sErrorMessage;
70 std::unique_ptr<OSQLParseNode> pNode = aParser.parseTree(sErrorMessage,_sText);
71 if(pNode)
72 { // special handling for parameters
74 OUString sNewSql;
75 pNode->parseNodeToStr( sNewSql, xConnection );
76 sRet = sNewSql;
77 }
78 }
79 catch(const Exception&)
80 {
81 }
82 }
83 return sRet;
84 }
85
86 OUString SAL_CALL ParameterSubstitution::reSubstituteVariables( const OUString& _sText )
87 {
88 return _sText;
89 }
90
91 OUString SAL_CALL ParameterSubstitution::getSubstituteVariableValue( const OUString& /*variable*/ )
92 {
93 throw container::NoSuchElementException();
94 }
95
96
97} // connectivity
98
99extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
101 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
102{
103 return cppu::acquire(new connectivity::ParameterSubstitution(context));
104}
105
106/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * connectivity_dbtools_ParameterSubstitution_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
static void substituteParameterNames(OSQLParseNode const *_pNode)
Definition: sqlnode.cxx:1492
Parser for SQL92.
Definition: sqlparse.hxx:110
std::unique_ptr< OSQLParseNode > parseTree(OUString &rErrorMessage, const OUString &rStatement, bool bInternational=false)
css::uno::WeakReference< css::sdbc::XConnection > m_xConnection
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual OUString SAL_CALL getImplementationName() override
virtual OUString SAL_CALL reSubstituteVariables(const OUString &aText) override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &aArguments) override
virtual OUString SAL_CALL substituteVariables(const OUString &aText, sal_Bool bSubstRequired) override
ParameterSubstitution(const ParameterSubstitution &)=delete
virtual OUString SAL_CALL getSubstituteVariableValue(const OUString &variable) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
@ Exception
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool
Reference< XComponentContext > _xContext