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