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