1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "precompiled_connectivity.hxx"
24 #include "ParameterSubstitution.hxx"
25 #include "connectivity/sqlparse.hxx"
26 #include <comphelper/sequenceashashmap.hxx>
27 
28 namespace connectivity
29 {
30     using namespace ::com::sun::star::uno;
31     using namespace ::com::sun::star::sdbc;
32     using namespace ::com::sun::star::lang;
33     using namespace ::com::sun::star;
34 
ParameterSubstitution(const::com::sun::star::uno::Reference<::com::sun::star::uno::XComponentContext> & _rxContext)35     ParameterSubstitution::ParameterSubstitution(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext ) : m_xContext(_rxContext)
36     {
37     }
initialize(const uno::Sequence<uno::Any> & _aArguments)38     void SAL_CALL ParameterSubstitution::initialize( const uno::Sequence< uno::Any >& _aArguments ) throw (uno::Exception, uno::RuntimeException)
39     {
40         ::osl::MutexGuard aGuard(m_aMutex);
41         comphelper::SequenceAsHashMap aArgs(_aArguments);
42         uno::Reference< sdbc::XConnection > xConnection;
43         xConnection = aArgs.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ActiveConnection")),xConnection);
44         m_xConnection = xConnection;
45     }
46     //------------------------------------------------------------------------------
getImplementationName_Static()47 	rtl::OUString ParameterSubstitution::getImplementationName_Static(  ) throw(RuntimeException)
48 	{
49 		return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.helper.ParameterSubstitution"));
50 	}
51 	//------------------------------------------------------------------------------
getImplementationName()52 	::rtl::OUString SAL_CALL ParameterSubstitution::getImplementationName(  ) throw(RuntimeException)
53 	{
54 		return getImplementationName_Static();
55 	}
56 	//------------------------------------------------------------------
supportsService(const::rtl::OUString & _rServiceName)57 	sal_Bool SAL_CALL ParameterSubstitution::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
58 	{
59 		Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
60 		const ::rtl::OUString* pSupported = aSupported.getConstArray();
61 		const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
62 		for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
63 			;
64 
65 		return pSupported != pEnd;
66 	}
67 	//------------------------------------------------------------------
getSupportedServiceNames()68 	Sequence< ::rtl::OUString > SAL_CALL ParameterSubstitution::getSupportedServiceNames(  ) throw(RuntimeException)
69 	{
70 		return getSupportedServiceNames_Static();
71 	}
72     //------------------------------------------------------------------
getSupportedServiceNames_Static()73     Sequence< ::rtl::OUString > ParameterSubstitution::getSupportedServiceNames_Static(  ) throw (RuntimeException)
74 	{
75 		Sequence< ::rtl::OUString > aSNS( 1 );
76 		aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdb.ParameterSubstitution");
77 		return aSNS;
78 	}
79 
80 	//------------------------------------------------------------------
create(const Reference<XComponentContext> & _xContext)81     Reference< XInterface >  ParameterSubstitution::create(const Reference< XComponentContext >& _xContext)
82     {
83         return *(new ParameterSubstitution(_xContext));
84     }
85 	//------------------------------------------------------------------
substituteVariables(const::rtl::OUString & _sText,::sal_Bool)86     ::rtl::OUString SAL_CALL ParameterSubstitution::substituteVariables( const ::rtl::OUString& _sText, ::sal_Bool /*bSubstRequired*/ ) throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException)
87     {
88         ::rtl::OUString sRet = _sText;
89         uno::Reference< sdbc::XConnection > xConnection = m_xConnection;
90         if ( xConnection.is() )
91         {
92             try
93 		    {
94                 uno::Reference< XMultiServiceFactory> xFac(m_xContext->getServiceManager(),uno::UNO_QUERY_THROW);
95 			    OSQLParser aParser( xFac );
96 			    ::rtl::OUString sErrorMessage;
97 			    ::rtl::OUString sNewSql;
98 			    OSQLParseNode* pNode = aParser.parseTree(sErrorMessage,_sText);
99 			    if(pNode)
100 			    {	// special handling for parameters
101 				    OSQLParseNode::substituteParameterNames(pNode);
102 				    pNode->parseNodeToStr( sNewSql, xConnection );
103 				    delete pNode;
104 				    sRet = sNewSql;
105 			    }
106 		    }
107 		    catch(const Exception&)
108 		    {
109 		    }
110         }
111         return sRet;
112     }
113 	//------------------------------------------------------------------
reSubstituteVariables(const::rtl::OUString & _sText)114     ::rtl::OUString SAL_CALL ParameterSubstitution::reSubstituteVariables( const ::rtl::OUString& _sText ) throw (::com::sun::star::uno::RuntimeException)
115     {
116         return _sText;
117     }
118 	//------------------------------------------------------------------
getSubstituteVariableValue(const::rtl::OUString &)119     ::rtl::OUString SAL_CALL ParameterSubstitution::getSubstituteVariableValue( const ::rtl::OUString& /*variable*/ ) throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException)
120     {
121         throw container::NoSuchElementException();
122     }
123     //------------------------------------------------------------------
124 
125 
126 // ==================================
127 } // connectivity
128 // ==================================
129