1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <hsqldb/HTables.hxx>
21 #include <hsqldb/HViews.hxx>
22 #include <hsqldb/HTable.hxx>
23 #include <com/sun/star/sdbc/XRow.hpp>
24 #include <com/sun/star/sdbc/XResultSet.hpp>
25 #include <com/sun/star/sdbcx/Privilege.hpp>
26 #include <hsqldb/HCatalog.hxx>
27 #include <connectivity/dbtools.hxx>
28 #include <comphelper/types.hxx>
29 #include <TConnection.hxx>
31 using namespace ::comphelper
;
32 using namespace connectivity
;
33 using namespace ::cppu
;
34 using namespace connectivity::hsqldb
;
35 using namespace ::com::sun::star::uno
;
36 using namespace ::com::sun::star::beans
;
37 using namespace ::com::sun::star::sdbcx
;
38 using namespace ::com::sun::star::sdbc
;
39 using namespace ::com::sun::star::container
;
40 using namespace ::com::sun::star::lang
;
41 using namespace dbtools
;
43 sdbcx::ObjectType
OTables::createObject(const OUString
& _rName
)
45 OUString sCatalog
,sSchema
,sTable
;
46 ::dbtools::qualifiedNameComponents(m_xMetaData
,_rName
,sCatalog
,sSchema
,sTable
,::dbtools::EComposeRule::InDataManipulation
);
48 Sequence
< OUString
> sTableTypes
{"VIEW", "TABLE", "%"}; // this last one just to be sure to include anything else...
51 if ( !sCatalog
.isEmpty() )
52 aCatalog
<<= sCatalog
;
53 Reference
< XResultSet
> xResult
= m_xMetaData
->getTables(aCatalog
,sSchema
,sTable
,sTableTypes
);
55 sdbcx::ObjectType xRet
;
58 Reference
< XRow
> xRow(xResult
,UNO_QUERY
);
59 if ( xResult
->next() ) // there can be only one table with this name
61 sal_Int32 nPrivileges
= ::dbtools::getTablePrivileges( m_xMetaData
, sCatalog
, sSchema
, sTable
);
62 if ( m_xMetaData
->isReadOnly() )
63 nPrivileges
&= ~( Privilege::INSERT
| Privilege::UPDATE
| Privilege::DELETE
| Privilege::CREATE
| Privilege::ALTER
| Privilege::DROP
);
66 xRet
= new OHSQLTable( this
67 ,static_cast<OHCatalog
&>(m_rParent
).getConnection()
75 ::comphelper::disposeComponent(xResult
);
81 void OTables::impl_refresh( )
83 static_cast<OHCatalog
&>(m_rParent
).refreshTables();
86 void OTables::disposing()
89 OCollection::disposing();
92 Reference
< XPropertySet
> OTables::createDescriptor()
94 return new OHSQLTable(this,static_cast<OHCatalog
&>(m_rParent
).getConnection());
98 sdbcx::ObjectType
OTables::appendObject( const OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
100 createTable(descriptor
);
101 return createObject( _rForName
);
105 void OTables::dropObject(sal_Int32 _nPos
,const OUString
& _sElementName
)
107 Reference
< XInterface
> xObject( getObject( _nPos
) );
108 bool bIsNew
= connectivity::sdbcx::ODescriptor::isNew( xObject
);
112 Reference
< XConnection
> xConnection
= static_cast<OHCatalog
&>(m_rParent
).getConnection();
115 OUString sCatalog
,sSchema
,sTable
;
116 ::dbtools::qualifiedNameComponents(m_xMetaData
,_sElementName
,sCatalog
,sSchema
,sTable
,::dbtools::EComposeRule::InDataManipulation
);
118 OUString
aSql( "DROP " );
120 Reference
<XPropertySet
> xProp(xObject
,UNO_QUERY
);
122 if((bIsView
= (xProp
.is() && ::comphelper::getString(xProp
->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE
))) == "VIEW"))) // here we have a view
127 OUString
sComposedName(
128 ::dbtools::composeTableName( m_xMetaData
, sCatalog
, sSchema
, sTable
, true, ::dbtools::EComposeRule::InDataManipulation
) );
129 aSql
+= sComposedName
;
130 Reference
< XStatement
> xStmt
= xConnection
->createStatement( );
133 xStmt
->execute(aSql
);
134 ::comphelper::disposeComponent(xStmt
);
136 // if no exception was thrown we must delete it from the views
139 HViews
* pViews
= static_cast<HViews
*>(static_cast<OHCatalog
&>(m_rParent
).getPrivateViews());
140 if ( pViews
&& pViews
->hasByName(_sElementName
) )
141 pViews
->dropByNameImpl(_sElementName
);
145 void OTables::createTable( const Reference
< XPropertySet
>& descriptor
)
147 Reference
< XConnection
> xConnection
= static_cast<OHCatalog
&>(m_rParent
).getConnection();
148 OUString aSql
= ::dbtools::createSqlCreateTableStatement(descriptor
,xConnection
);
150 Reference
< XStatement
> xStmt
= xConnection
->createStatement( );
153 xStmt
->execute(aSql
);
154 ::comphelper::disposeComponent(xStmt
);
158 void OTables::appendNew(const OUString
& _rsNewTable
)
160 insertElement(_rsNewTable
,nullptr);
162 // notify our container listeners
163 ContainerEvent
aEvent(static_cast<XContainer
*>(this), makeAny(_rsNewTable
), Any(), Any());
164 OInterfaceIteratorHelper2
aListenerLoop(m_aContainerListeners
);
165 while (aListenerLoop
.hasMoreElements())
166 static_cast<XContainerListener
*>(aListenerLoop
.next())->elementInserted(aEvent
);
169 OUString
OTables::getNameForObject(const sdbcx::ObjectType
& _xObject
)
171 OSL_ENSURE(_xObject
.is(),"OTables::getNameForObject: Object is NULL!");
172 return ::dbtools::composeTableName( m_xMetaData
, _xObject
, ::dbtools::EComposeRule::InDataManipulation
, false );
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */