1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include "adabas/BViews.hxx"
27cdf0e10cSrcweir #include "adabas/BTables.hxx"
28cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
29cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
30cdf0e10cSrcweir #include <com/sun/star/sdbc/ColumnValue.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sdbc/KeyRule.hpp>
32cdf0e10cSrcweir #include <com/sun/star/sdbcx/KeyType.hpp>
33cdf0e10cSrcweir #include <com/sun/star/sdbcx/CheckOption.hpp>
34cdf0e10cSrcweir #include "adabas/BCatalog.hxx"
35cdf0e10cSrcweir #include "adabas/BConnection.hxx"
36cdf0e10cSrcweir #include <comphelper/extract.hxx>
37cdf0e10cSrcweir #include "connectivity/dbtools.hxx"
38cdf0e10cSrcweir #include "connectivity/dbexception.hxx"
39cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.h>
40cdf0e10cSrcweir #include "connectivity/sdbcx/VView.hxx"
41cdf0e10cSrcweir #include <comphelper/types.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace ::comphelper;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace ::cppu;
46cdf0e10cSrcweir using namespace connectivity;
47cdf0e10cSrcweir using namespace connectivity::adabas;
48cdf0e10cSrcweir using namespace ::com::sun::star::uno;
49cdf0e10cSrcweir using namespace ::com::sun::star::beans;
50cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
51cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
52cdf0e10cSrcweir using namespace ::com::sun::star::container;
53cdf0e10cSrcweir using namespace ::com::sun::star::lang;
54cdf0e10cSrcweir using namespace dbtools;
55cdf0e10cSrcweir typedef connectivity::sdbcx::OCollection OCollection_TYPE;
56cdf0e10cSrcweir 
createObject(const::rtl::OUString & _rName)57cdf0e10cSrcweir sdbcx::ObjectType OViews::createObject(const ::rtl::OUString& _rName)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	::rtl::OUString aName,aSchema;
60cdf0e10cSrcweir 	sal_Int32 nLen = _rName.indexOf('.');
61cdf0e10cSrcweir 	aSchema = _rName.copy(0,nLen);
62cdf0e10cSrcweir 	aName	= _rName.copy(nLen+1);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	::rtl::OUString sStmt = ::rtl::OUString::createFromAscii("SELECT DISTINCT * FROM DOMAIN.SHOW_VIEW WHERE ");
65cdf0e10cSrcweir 	if(aSchema.getLength())
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir 		sStmt += ::rtl::OUString::createFromAscii("OWNER = '");
68cdf0e10cSrcweir 		sStmt += aSchema;
69cdf0e10cSrcweir 		sStmt += ::rtl::OUString::createFromAscii("' AND ");
70cdf0e10cSrcweir 	}
71cdf0e10cSrcweir 	sStmt += ::rtl::OUString::createFromAscii("VIEWNAME = '");
72cdf0e10cSrcweir 	sStmt += aName;
73cdf0e10cSrcweir 	sStmt += ::rtl::OUString::createFromAscii("'");
74cdf0e10cSrcweir 	Reference<XConnection> xConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
75cdf0e10cSrcweir     Reference< XStatement > xStmt = xConnection->createStatement(  );
76cdf0e10cSrcweir 	Reference< XResultSet > xResult = xStmt->executeQuery(sStmt);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     sdbcx::ObjectType xRet = NULL;
79cdf0e10cSrcweir 	if(xResult.is())
80cdf0e10cSrcweir 	{
81cdf0e10cSrcweir         Reference< XRow > xRow(xResult,UNO_QUERY);
82cdf0e10cSrcweir 		if(xResult->next()) // there can be only one table with this name
83cdf0e10cSrcweir 		{
84cdf0e10cSrcweir 			connectivity::sdbcx::OView* pRet = new connectivity::sdbcx::OView(sal_True,
85cdf0e10cSrcweir 																				aName,
86cdf0e10cSrcweir 																				xConnection->getMetaData(),
87cdf0e10cSrcweir 																				CheckOption::NONE,
88cdf0e10cSrcweir 																				xRow->getString(3),
89cdf0e10cSrcweir 																				aSchema);
90cdf0e10cSrcweir 			xRet = pRet;
91cdf0e10cSrcweir 		}
92cdf0e10cSrcweir 		::comphelper::disposeComponent(xResult);
93cdf0e10cSrcweir 	}
94cdf0e10cSrcweir 	::comphelper::disposeComponent(xStmt);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	return xRet;
97cdf0e10cSrcweir }
98cdf0e10cSrcweir // -------------------------------------------------------------------------
impl_refresh()99cdf0e10cSrcweir void OViews::impl_refresh(  ) throw(RuntimeException)
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	static_cast<OAdabasCatalog&>(m_rParent).refreshTables();
102cdf0e10cSrcweir }
103cdf0e10cSrcweir // -------------------------------------------------------------------------
disposing(void)104cdf0e10cSrcweir void OViews::disposing(void)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir m_xMetaData.clear();
107cdf0e10cSrcweir 	OCollection::disposing();
108cdf0e10cSrcweir }
109cdf0e10cSrcweir // -------------------------------------------------------------------------
createDescriptor()110cdf0e10cSrcweir Reference< XPropertySet > OViews::createDescriptor()
111cdf0e10cSrcweir {
112cdf0e10cSrcweir 	Reference<XConnection> xConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
113cdf0e10cSrcweir 	return new connectivity::sdbcx::OView(sal_True,xConnection->getMetaData());
114cdf0e10cSrcweir }
115cdf0e10cSrcweir // -------------------------------------------------------------------------
116cdf0e10cSrcweir // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)117cdf0e10cSrcweir sdbcx::ObjectType OViews::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
118cdf0e10cSrcweir {
119cdf0e10cSrcweir 	createView(descriptor);
120cdf0e10cSrcweir     return createObject( _rForName );
121cdf0e10cSrcweir }
122cdf0e10cSrcweir // -------------------------------------------------------------------------
123cdf0e10cSrcweir // XDrop
dropObject(sal_Int32 _nPos,const::rtl::OUString _sElementName)124cdf0e10cSrcweir void OViews::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir 	if(m_bInDrop)
127cdf0e10cSrcweir 		return;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     Reference< XInterface > xObject( getObject( _nPos ) );
130cdf0e10cSrcweir     sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
131cdf0e10cSrcweir 	if (!bIsNew)
132cdf0e10cSrcweir 	{
133cdf0e10cSrcweir 		OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
134cdf0e10cSrcweir         Reference< XStatement > xStmt = pConnection->createStatement(  );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 		::rtl::OUString aName,aSchema;
137cdf0e10cSrcweir 		sal_Int32 nLen = _sElementName.indexOf('.');
138cdf0e10cSrcweir 		aSchema = _sElementName.copy(0,nLen);
139cdf0e10cSrcweir 		aName	= _sElementName.copy(nLen+1);
140cdf0e10cSrcweir 		::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP VIEW");
141cdf0e10cSrcweir 		const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 		aSql = aSql + m_xMetaData->getIdentifierQuoteString(  ) + aSchema + m_xMetaData->getIdentifierQuoteString(  );
144cdf0e10cSrcweir 		aSql = aSql + sDot;
145cdf0e10cSrcweir 		aSql = aSql + m_xMetaData->getIdentifierQuoteString(  ) + aName + m_xMetaData->getIdentifierQuoteString(  );
146cdf0e10cSrcweir 		xStmt->execute(aSql);
147cdf0e10cSrcweir 		::comphelper::disposeComponent(xStmt);
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir }
150cdf0e10cSrcweir // -----------------------------------------------------------------------------
dropByNameImpl(const::rtl::OUString & elementName)151cdf0e10cSrcweir void OViews::dropByNameImpl(const ::rtl::OUString& elementName)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir 	m_bInDrop = sal_True;
154cdf0e10cSrcweir 	OCollection_TYPE::dropByName(elementName);
155cdf0e10cSrcweir 	m_bInDrop = sal_False;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir // -----------------------------------------------------------------------------
createView(const Reference<XPropertySet> & descriptor)158cdf0e10cSrcweir void OViews::createView( const Reference< XPropertySet >& descriptor )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("CREATE VIEW ");
161cdf0e10cSrcweir 	::rtl::OUString aQuote	= static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getMetaData()->getIdentifierQuoteString(  );
162cdf0e10cSrcweir 	const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
163cdf0e10cSrcweir 	::rtl::OUString sSchema,sCommand;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= sSchema;
166cdf0e10cSrcweir 	if(sSchema.getLength())
167cdf0e10cSrcweir 		aSql += ::dbtools::quoteName(aQuote, sSchema) + sDot;
168cdf0e10cSrcweir 	else
169cdf0e10cSrcweir 		descriptor->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME),makeAny(sSchema = static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getMetaData()->getUserName()));
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	aSql += ::dbtools::quoteName(aQuote, getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))))
172cdf0e10cSrcweir 				+ ::rtl::OUString::createFromAscii(" AS ");
173cdf0e10cSrcweir 	descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_COMMAND)) >>= sCommand;
174cdf0e10cSrcweir 	aSql += sCommand;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 	OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
177cdf0e10cSrcweir         Reference< XStatement > xStmt = pConnection->createStatement(  );
178cdf0e10cSrcweir 	xStmt->execute(aSql);
179cdf0e10cSrcweir 	::comphelper::disposeComponent(xStmt);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	// insert the new view also in the tables collection
182cdf0e10cSrcweir 	OTables* pTables = static_cast<OTables*>(static_cast<OAdabasCatalog&>(m_rParent).getPrivateTables());
183cdf0e10cSrcweir 	if(pTables)
184cdf0e10cSrcweir 	{
185cdf0e10cSrcweir 		::rtl::OUString sName = sSchema;
186cdf0e10cSrcweir 		sName += sDot;
187cdf0e10cSrcweir 		sName += getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)));
188cdf0e10cSrcweir 		pTables->appendNew(sName);
189cdf0e10cSrcweir 	}
190cdf0e10cSrcweir }
191