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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "adabas/BViews.hxx"
27 #include "adabas/BTables.hxx"
28 #include <com/sun/star/sdbc/XRow.hpp>
29 #include <com/sun/star/sdbc/XResultSet.hpp>
30 #include <com/sun/star/sdbc/ColumnValue.hpp>
31 #include <com/sun/star/sdbc/KeyRule.hpp>
32 #include <com/sun/star/sdbcx/KeyType.hpp>
33 #include <com/sun/star/sdbcx/CheckOption.hpp>
34 #include "adabas/BCatalog.hxx"
35 #include "adabas/BConnection.hxx"
36 #include <comphelper/extract.hxx>
37 #include "connectivity/dbtools.hxx"
38 #include "connectivity/dbexception.hxx"
39 #include <cppuhelper/interfacecontainer.h>
40 #include "connectivity/sdbcx/VView.hxx"
41 #include <comphelper/types.hxx>
42 
43 using namespace ::comphelper;
44 
45 using namespace ::cppu;
46 using namespace connectivity;
47 using namespace connectivity::adabas;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::sdbcx;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::com::sun::star::container;
53 using namespace ::com::sun::star::lang;
54 using namespace dbtools;
55 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
56 
createObject(const::rtl::OUString & _rName)57 sdbcx::ObjectType OViews::createObject(const ::rtl::OUString& _rName)
58 {
59 	::rtl::OUString aName,aSchema;
60 	sal_Int32 nLen = _rName.indexOf('.');
61 	aSchema = _rName.copy(0,nLen);
62 	aName	= _rName.copy(nLen+1);
63 
64 	::rtl::OUString sStmt = ::rtl::OUString::createFromAscii("SELECT DISTINCT * FROM DOMAIN.SHOW_VIEW WHERE ");
65 	if(aSchema.getLength())
66 	{
67 		sStmt += ::rtl::OUString::createFromAscii("OWNER = '");
68 		sStmt += aSchema;
69 		sStmt += ::rtl::OUString::createFromAscii("' AND ");
70 	}
71 	sStmt += ::rtl::OUString::createFromAscii("VIEWNAME = '");
72 	sStmt += aName;
73 	sStmt += ::rtl::OUString::createFromAscii("'");
74 	Reference<XConnection> xConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
75     Reference< XStatement > xStmt = xConnection->createStatement(  );
76 	Reference< XResultSet > xResult = xStmt->executeQuery(sStmt);
77 
78     sdbcx::ObjectType xRet = NULL;
79 	if(xResult.is())
80 	{
81         Reference< XRow > xRow(xResult,UNO_QUERY);
82 		if(xResult->next()) // there can be only one table with this name
83 		{
84 			connectivity::sdbcx::OView* pRet = new connectivity::sdbcx::OView(sal_True,
85 																				aName,
86 																				xConnection->getMetaData(),
87 																				CheckOption::NONE,
88 																				xRow->getString(3),
89 																				aSchema);
90 			xRet = pRet;
91 		}
92 		::comphelper::disposeComponent(xResult);
93 	}
94 	::comphelper::disposeComponent(xStmt);
95 
96 	return xRet;
97 }
98 // -------------------------------------------------------------------------
impl_refresh()99 void OViews::impl_refresh(  ) throw(RuntimeException)
100 {
101 	static_cast<OAdabasCatalog&>(m_rParent).refreshTables();
102 }
103 // -------------------------------------------------------------------------
disposing(void)104 void OViews::disposing(void)
105 {
106 m_xMetaData.clear();
107 	OCollection::disposing();
108 }
109 // -------------------------------------------------------------------------
createDescriptor()110 Reference< XPropertySet > OViews::createDescriptor()
111 {
112 	Reference<XConnection> xConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
113 	return new connectivity::sdbcx::OView(sal_True,xConnection->getMetaData());
114 }
115 // -------------------------------------------------------------------------
116 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)117 sdbcx::ObjectType OViews::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
118 {
119 	createView(descriptor);
120     return createObject( _rForName );
121 }
122 // -------------------------------------------------------------------------
123 // XDrop
dropObject(sal_Int32 _nPos,const::rtl::OUString _sElementName)124 void OViews::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
125 {
126 	if(m_bInDrop)
127 		return;
128 
129     Reference< XInterface > xObject( getObject( _nPos ) );
130     sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
131 	if (!bIsNew)
132 	{
133 		OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
134         Reference< XStatement > xStmt = pConnection->createStatement(  );
135 
136 		::rtl::OUString aName,aSchema;
137 		sal_Int32 nLen = _sElementName.indexOf('.');
138 		aSchema = _sElementName.copy(0,nLen);
139 		aName	= _sElementName.copy(nLen+1);
140 		::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP VIEW");
141 		const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
142 
143 		aSql = aSql + m_xMetaData->getIdentifierQuoteString(  ) + aSchema + m_xMetaData->getIdentifierQuoteString(  );
144 		aSql = aSql + sDot;
145 		aSql = aSql + m_xMetaData->getIdentifierQuoteString(  ) + aName + m_xMetaData->getIdentifierQuoteString(  );
146 		xStmt->execute(aSql);
147 		::comphelper::disposeComponent(xStmt);
148     }
149 }
150 // -----------------------------------------------------------------------------
dropByNameImpl(const::rtl::OUString & elementName)151 void OViews::dropByNameImpl(const ::rtl::OUString& elementName)
152 {
153 	m_bInDrop = sal_True;
154 	OCollection_TYPE::dropByName(elementName);
155 	m_bInDrop = sal_False;
156 }
157 // -----------------------------------------------------------------------------
createView(const Reference<XPropertySet> & descriptor)158 void OViews::createView( const Reference< XPropertySet >& descriptor )
159 {
160 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("CREATE VIEW ");
161 	::rtl::OUString aQuote	= static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getMetaData()->getIdentifierQuoteString(  );
162 	const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
163 	::rtl::OUString sSchema,sCommand;
164 
165 	descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= sSchema;
166 	if(sSchema.getLength())
167 		aSql += ::dbtools::quoteName(aQuote, sSchema) + sDot;
168 	else
169 		descriptor->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME),makeAny(sSchema = static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getMetaData()->getUserName()));
170 
171 	aSql += ::dbtools::quoteName(aQuote, getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))))
172 				+ ::rtl::OUString::createFromAscii(" AS ");
173 	descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_COMMAND)) >>= sCommand;
174 	aSql += sCommand;
175 
176 	OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
177         Reference< XStatement > xStmt = pConnection->createStatement(  );
178 	xStmt->execute(aSql);
179 	::comphelper::disposeComponent(xStmt);
180 
181 	// insert the new view also in the tables collection
182 	OTables* pTables = static_cast<OTables*>(static_cast<OAdabasCatalog&>(m_rParent).getPrivateTables());
183 	if(pTables)
184 	{
185 		::rtl::OUString sName = sSchema;
186 		sName += sDot;
187 		sName += getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)));
188 		pTables->appendNew(sName);
189 	}
190 }
191