1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "precompiled_connectivity.hxx"
28 #include "ParameterSubstitution.hxx"
29 #include "connectivity/sqlparse.hxx"
30 #include <comphelper/sequenceashashmap.hxx>
31 
32 namespace connectivity
33 {
34     using namespace ::com::sun::star::uno;
35     using namespace ::com::sun::star::sdbc;
36     using namespace ::com::sun::star::lang;
37     using namespace ::com::sun::star;
38 
39     ParameterSubstitution::ParameterSubstitution(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& _rxContext ) : m_xContext(_rxContext)
40     {
41     }
42     void SAL_CALL ParameterSubstitution::initialize( const uno::Sequence< uno::Any >& _aArguments ) throw (uno::Exception, uno::RuntimeException)
43     {
44         ::osl::MutexGuard aGuard(m_aMutex);
45         comphelper::SequenceAsHashMap aArgs(_aArguments);
46         uno::Reference< sdbc::XConnection > xConnection;
47         xConnection = aArgs.getUnpackedValueOrDefault(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ActiveConnection")),xConnection);
48         m_xConnection = xConnection;
49     }
50     //------------------------------------------------------------------------------
51 	rtl::OUString ParameterSubstitution::getImplementationName_Static(  ) throw(RuntimeException)
52 	{
53 		return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.helper.ParameterSubstitution"));
54 	}
55 	//------------------------------------------------------------------------------
56 	::rtl::OUString SAL_CALL ParameterSubstitution::getImplementationName(  ) throw(RuntimeException)
57 	{
58 		return getImplementationName_Static();
59 	}
60 	//------------------------------------------------------------------
61 	sal_Bool SAL_CALL ParameterSubstitution::supportsService( const ::rtl::OUString& _rServiceName ) throw(RuntimeException)
62 	{
63 		Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());
64 		const ::rtl::OUString* pSupported = aSupported.getConstArray();
65 		const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();
66 		for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)
67 			;
68 
69 		return pSupported != pEnd;
70 	}
71 	//------------------------------------------------------------------
72 	Sequence< ::rtl::OUString > SAL_CALL ParameterSubstitution::getSupportedServiceNames(  ) throw(RuntimeException)
73 	{
74 		return getSupportedServiceNames_Static();
75 	}
76     //------------------------------------------------------------------
77     Sequence< ::rtl::OUString > ParameterSubstitution::getSupportedServiceNames_Static(  ) throw (RuntimeException)
78 	{
79 		Sequence< ::rtl::OUString > aSNS( 1 );
80 		aSNS[0] = ::rtl::OUString::createFromAscii("com.sun.star.sdb.ParameterSubstitution");
81 		return aSNS;
82 	}
83 
84 	//------------------------------------------------------------------
85     Reference< XInterface >  ParameterSubstitution::create(const Reference< XComponentContext >& _xContext)
86     {
87         return *(new ParameterSubstitution(_xContext));
88     }
89 	//------------------------------------------------------------------
90     ::rtl::OUString SAL_CALL ParameterSubstitution::substituteVariables( const ::rtl::OUString& _sText, ::sal_Bool /*bSubstRequired*/ ) throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException)
91     {
92         ::rtl::OUString sRet = _sText;
93         uno::Reference< sdbc::XConnection > xConnection = m_xConnection;
94         if ( xConnection.is() )
95         {
96             try
97 		    {
98                 uno::Reference< XMultiServiceFactory> xFac(m_xContext->getServiceManager(),uno::UNO_QUERY_THROW);
99 			    OSQLParser aParser( xFac );
100 			    ::rtl::OUString sErrorMessage;
101 			    ::rtl::OUString sNewSql;
102 			    OSQLParseNode* pNode = aParser.parseTree(sErrorMessage,_sText);
103 			    if(pNode)
104 			    {	// special handling for parameters
105 				    OSQLParseNode::substituteParameterNames(pNode);
106 				    pNode->parseNodeToStr( sNewSql, xConnection );
107 				    delete pNode;
108 				    sRet = sNewSql;
109 			    }
110 		    }
111 		    catch(const Exception&)
112 		    {
113 		    }
114         }
115         return sRet;
116     }
117 	//------------------------------------------------------------------
118     ::rtl::OUString SAL_CALL ParameterSubstitution::reSubstituteVariables( const ::rtl::OUString& _sText ) throw (::com::sun::star::uno::RuntimeException)
119     {
120         return _sText;
121     }
122 	//------------------------------------------------------------------
123     ::rtl::OUString SAL_CALL ParameterSubstitution::getSubstituteVariableValue( const ::rtl::OUString& /*variable*/ ) throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::uno::RuntimeException)
124     {
125         throw container::NoSuchElementException();
126     }
127     //------------------------------------------------------------------
128 
129 
130 // ==================================
131 } // connectivity
132 // ==================================
133