Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / adabas / BViews.cxx
blobb62f0d12ca0c8159046e227fe3eae5d7039a529d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BViews.cxx,v $
10 * $Revision: 1.21 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "adabas/BViews.hxx"
34 #include "adabas/BTables.hxx"
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include <com/sun/star/sdbc/XResultSet.hpp>
37 #include <com/sun/star/sdbc/ColumnValue.hpp>
38 #include <com/sun/star/sdbc/KeyRule.hpp>
39 #include <com/sun/star/sdbcx/KeyType.hpp>
40 #include <com/sun/star/sdbcx/CheckOption.hpp>
41 #include "adabas/BCatalog.hxx"
42 #include "adabas/BConnection.hxx"
43 #include <comphelper/extract.hxx>
44 #include "connectivity/dbtools.hxx"
45 #include "connectivity/dbexception.hxx"
46 #include <cppuhelper/interfacecontainer.h>
47 #include "connectivity/sdbcx/VView.hxx"
48 #include <comphelper/types.hxx>
50 using namespace ::comphelper;
52 using namespace ::cppu;
53 using namespace connectivity;
54 using namespace connectivity::adabas;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::beans;
57 using namespace ::com::sun::star::sdbcx;
58 using namespace ::com::sun::star::sdbc;
59 using namespace ::com::sun::star::container;
60 using namespace ::com::sun::star::lang;
61 using namespace dbtools;
62 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
64 sdbcx::ObjectType OViews::createObject(const ::rtl::OUString& _rName)
66 ::rtl::OUString aName,aSchema;
67 sal_Int32 nLen = _rName.indexOf('.');
68 aSchema = _rName.copy(0,nLen);
69 aName = _rName.copy(nLen+1);
71 ::rtl::OUString sStmt = ::rtl::OUString::createFromAscii("SELECT DISTINCT * FROM DOMAIN.SHOW_VIEW WHERE ");
72 if(aSchema.getLength())
74 sStmt += ::rtl::OUString::createFromAscii("OWNER = '");
75 sStmt += aSchema;
76 sStmt += ::rtl::OUString::createFromAscii("' AND ");
78 sStmt += ::rtl::OUString::createFromAscii("VIEWNAME = '");
79 sStmt += aName;
80 sStmt += ::rtl::OUString::createFromAscii("'");
81 Reference<XConnection> xConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
82 Reference< XStatement > xStmt = xConnection->createStatement( );
83 Reference< XResultSet > xResult = xStmt->executeQuery(sStmt);
85 sdbcx::ObjectType xRet = NULL;
86 if(xResult.is())
88 Reference< XRow > xRow(xResult,UNO_QUERY);
89 if(xResult->next()) // there can be only one table with this name
91 connectivity::sdbcx::OView* pRet = new connectivity::sdbcx::OView(sal_True,
92 aName,
93 xConnection->getMetaData(),
94 CheckOption::NONE,
95 xRow->getString(3),
96 aSchema);
97 xRet = pRet;
99 ::comphelper::disposeComponent(xResult);
101 ::comphelper::disposeComponent(xStmt);
103 return xRet;
105 // -------------------------------------------------------------------------
106 void OViews::impl_refresh( ) throw(RuntimeException)
108 static_cast<OAdabasCatalog&>(m_rParent).refreshTables();
110 // -------------------------------------------------------------------------
111 void OViews::disposing(void)
113 m_xMetaData.clear();
114 OCollection::disposing();
116 // -------------------------------------------------------------------------
117 Reference< XPropertySet > OViews::createDescriptor()
119 Reference<XConnection> xConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
120 return new connectivity::sdbcx::OView(sal_True,xConnection->getMetaData());
122 // -------------------------------------------------------------------------
123 // XAppend
124 sdbcx::ObjectType OViews::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
126 createView(descriptor);
127 return createObject( _rForName );
129 // -------------------------------------------------------------------------
130 // XDrop
131 void OViews::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
133 if(m_bInDrop)
134 return;
136 Reference< XInterface > xObject( getObject( _nPos ) );
137 sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
138 if (!bIsNew)
140 OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
141 Reference< XStatement > xStmt = pConnection->createStatement( );
143 ::rtl::OUString aName,aSchema;
144 sal_Int32 nLen = _sElementName.indexOf('.');
145 aSchema = _sElementName.copy(0,nLen);
146 aName = _sElementName.copy(nLen+1);
147 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP VIEW");
148 const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
150 aSql = aSql + m_xMetaData->getIdentifierQuoteString( ) + aSchema + m_xMetaData->getIdentifierQuoteString( );
151 aSql = aSql + sDot;
152 aSql = aSql + m_xMetaData->getIdentifierQuoteString( ) + aName + m_xMetaData->getIdentifierQuoteString( );
153 xStmt->execute(aSql);
154 ::comphelper::disposeComponent(xStmt);
157 // -----------------------------------------------------------------------------
158 void OViews::dropByNameImpl(const ::rtl::OUString& elementName)
160 m_bInDrop = sal_True;
161 OCollection_TYPE::dropByName(elementName);
162 m_bInDrop = sal_False;
164 // -----------------------------------------------------------------------------
165 void OViews::createView( const Reference< XPropertySet >& descriptor )
167 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("CREATE VIEW ");
168 ::rtl::OUString aQuote = static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getMetaData()->getIdentifierQuoteString( );
169 const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
170 ::rtl::OUString sSchema,sCommand;
172 descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= sSchema;
173 if(sSchema.getLength())
174 aSql += ::dbtools::quoteName(aQuote, sSchema) + sDot;
175 else
176 descriptor->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_SCHEMANAME),makeAny(sSchema = static_cast<OAdabasCatalog&>(m_rParent).getConnection()->getMetaData()->getUserName()));
178 aSql += ::dbtools::quoteName(aQuote, getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))))
179 + ::rtl::OUString::createFromAscii(" AS ");
180 descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_COMMAND)) >>= sCommand;
181 aSql += sCommand;
183 OAdabasConnection* pConnection = static_cast<OAdabasCatalog&>(m_rParent).getConnection();
184 Reference< XStatement > xStmt = pConnection->createStatement( );
185 xStmt->execute(aSql);
186 ::comphelper::disposeComponent(xStmt);
188 // insert the new view also in the tables collection
189 OTables* pTables = static_cast<OTables*>(static_cast<OAdabasCatalog&>(m_rParent).getPrivateTables());
190 if(pTables)
192 ::rtl::OUString sName = sSchema;
193 sName += sDot;
194 sName += getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME)));
195 pTables->appendNew(sName);