Update ooo320-m1
[ooovba.git] / connectivity / source / drivers / hsqldb / HTables.cxx
blob9b3c08aa282a75d84440c55be5655b54675eedea
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: HTables.cxx,v $
10 * $Revision: 1.10 $
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 "hsqldb/HTables.hxx"
34 #include "hsqldb/HViews.hxx"
35 #include "hsqldb/HTable.hxx"
36 #include <com/sun/star/sdbc/XRow.hpp>
37 #include <com/sun/star/sdbc/XResultSet.hpp>
38 #include <com/sun/star/sdbc/ColumnValue.hpp>
39 #include <com/sun/star/sdbcx/Privilege.hpp>
40 #include <com/sun/star/sdbc/KeyRule.hpp>
41 #include <com/sun/star/sdbcx/KeyType.hpp>
42 #include "hsqldb/HCatalog.hxx"
43 #include <comphelper/extract.hxx>
44 #include "connectivity/dbtools.hxx"
45 #include "connectivity/dbexception.hxx"
46 #include <cppuhelper/interfacecontainer.h>
47 #include <comphelper/types.hxx>
48 #include "TConnection.hxx"
50 using namespace ::comphelper;
51 using namespace connectivity;
52 using namespace ::cppu;
53 using namespace connectivity::hsqldb;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::sdbcx;
57 using namespace ::com::sun::star::sdbc;
58 using namespace ::com::sun::star::container;
59 using namespace ::com::sun::star::lang;
60 using namespace dbtools;
61 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
63 sdbcx::ObjectType OTables::createObject(const ::rtl::OUString& _rName)
65 ::rtl::OUString sCatalog,sSchema,sTable;
66 ::dbtools::qualifiedNameComponents(m_xMetaData,_rName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
68 static const ::rtl::OUString s_sTableTypeView(RTL_CONSTASCII_USTRINGPARAM("VIEW"));
69 static const ::rtl::OUString s_sTableTypeTable(RTL_CONSTASCII_USTRINGPARAM("TABLE"));
70 static const ::rtl::OUString s_sAll(RTL_CONSTASCII_USTRINGPARAM("%"));
72 Sequence< ::rtl::OUString > sTableTypes(3);
73 sTableTypes[0] = s_sTableTypeView;
74 sTableTypes[1] = s_sTableTypeTable;
75 sTableTypes[2] = s_sAll; // just to be sure to include anything else ....
77 Any aCatalog;
78 if ( sCatalog.getLength() )
79 aCatalog <<= sCatalog;
80 Reference< XResultSet > xResult = m_xMetaData->getTables(aCatalog,sSchema,sTable,sTableTypes);
82 sdbcx::ObjectType xRet = NULL;
83 if ( xResult.is() )
85 Reference< XRow > xRow(xResult,UNO_QUERY);
86 if ( xResult->next() ) // there can be only one table with this name
88 sal_Int32 nPrivileges = ::dbtools::getTablePrivileges( m_xMetaData, sCatalog, sSchema, sTable );
89 if ( m_xMetaData->isReadOnly() )
90 nPrivileges &= ~( Privilege::INSERT | Privilege::UPDATE | Privilege::DELETE | Privilege::CREATE | Privilege::ALTER | Privilege::DROP );
92 // obtain privileges
93 OHSQLTable* pRet = new OHSQLTable( this
94 ,static_cast<OHCatalog&>(m_rParent).getConnection()
95 ,sTable
96 ,xRow->getString(4)
97 ,xRow->getString(5)
98 ,sSchema
99 ,sCatalog
100 ,nPrivileges);
101 xRet = pRet;
103 ::comphelper::disposeComponent(xResult);
106 return xRet;
108 // -------------------------------------------------------------------------
109 void OTables::impl_refresh( ) throw(RuntimeException)
111 static_cast<OHCatalog&>(m_rParent).refreshTables();
113 // -------------------------------------------------------------------------
114 void OTables::disposing(void)
116 m_xMetaData.clear();
117 OCollection::disposing();
119 // -------------------------------------------------------------------------
120 Reference< XPropertySet > OTables::createDescriptor()
122 return new OHSQLTable(this,static_cast<OHCatalog&>(m_rParent).getConnection());
124 // -------------------------------------------------------------------------
125 // XAppend
126 sdbcx::ObjectType OTables::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
128 createTable(descriptor);
129 return createObject( _rForName );
131 // -------------------------------------------------------------------------
132 // XDrop
133 void OTables::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
135 Reference< XInterface > xObject( getObject( _nPos ) );
136 sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
137 if (!bIsNew)
139 Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
142 ::rtl::OUString sCatalog,sSchema,sTable;
143 ::dbtools::qualifiedNameComponents(m_xMetaData,_sElementName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
145 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP ");
147 Reference<XPropertySet> xProp(xObject,UNO_QUERY);
148 sal_Bool bIsView;
149 if((bIsView = (xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == ::rtl::OUString::createFromAscii("VIEW")))) // here we have a view
150 aSql += ::rtl::OUString::createFromAscii("VIEW ");
151 else
152 aSql += ::rtl::OUString::createFromAscii("TABLE ");
154 ::rtl::OUString sComposedName(
155 ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, sal_True, ::dbtools::eInDataManipulation ) );
156 aSql += sComposedName;
157 Reference< XStatement > xStmt = xConnection->createStatement( );
158 if ( xStmt.is() )
160 xStmt->execute(aSql);
161 ::comphelper::disposeComponent(xStmt);
163 // if no exception was thrown we must delete it from the views
164 if ( bIsView )
166 HViews* pViews = static_cast<HViews*>(static_cast<OHCatalog&>(m_rParent).getPrivateViews());
167 if ( pViews && pViews->hasByName(_sElementName) )
168 pViews->dropByNameImpl(_sElementName);
172 // -------------------------------------------------------------------------
173 void OTables::createTable( const Reference< XPropertySet >& descriptor )
175 Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
176 ::rtl::OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,xConnection);
178 Reference< XStatement > xStmt = xConnection->createStatement( );
179 if ( xStmt.is() )
181 xStmt->execute(aSql);
182 ::comphelper::disposeComponent(xStmt);
185 // -----------------------------------------------------------------------------
186 void OTables::appendNew(const ::rtl::OUString& _rsNewTable)
188 insertElement(_rsNewTable,NULL);
190 // notify our container listeners
191 ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
192 OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
193 while (aListenerLoop.hasMoreElements())
194 static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
196 // -----------------------------------------------------------------------------
197 ::rtl::OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
199 OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
200 return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::eInDataManipulation, false, false, false );
202 // -----------------------------------------------------------------------------