LibreOffice Module connectivity (master) 1
HTables.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
28#include <comphelper/types.hxx>
29#include <TConnection.hxx>
30
31using namespace ::comphelper;
32using namespace connectivity;
33using namespace ::cppu;
34using namespace connectivity::hsqldb;
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star::beans;
37using namespace ::com::sun::star::sdbcx;
38using namespace ::com::sun::star::sdbc;
39using namespace ::com::sun::star::container;
40using namespace ::com::sun::star::lang;
41using namespace dbtools;
42
43sdbcx::ObjectType OTables::createObject(const OUString& _rName)
44{
45 OUString sCatalog,sSchema,sTable;
46 ::dbtools::qualifiedNameComponents(m_xMetaData,_rName,sCatalog,sSchema,sTable,::dbtools::EComposeRule::InDataManipulation);
47
48 Sequence< OUString > sTableTypes {"VIEW", "TABLE", "%"}; // this last one just to be sure to include anything else...
49
50 Any aCatalog;
51 if ( !sCatalog.isEmpty() )
52 aCatalog <<= sCatalog;
53 Reference< XResultSet > xResult = m_xMetaData->getTables(aCatalog,sSchema,sTable,sTableTypes);
54
56 if ( xResult.is() )
57 {
58 Reference< XRow > xRow(xResult,UNO_QUERY);
59 if ( xResult->next() ) // there can be only one table with this name
60 {
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 );
64
65 // obtain privileges
66 xRet = new OHSQLTable( this
67 ,static_cast<OHCatalog&>(m_rParent).getConnection()
68 ,sTable
69 ,xRow->getString(4)
70 ,xRow->getString(5)
71 ,sSchema
73 ,nPrivileges);
74 }
75 ::comphelper::disposeComponent(xResult);
76 }
77
78 return xRet;
79}
80
82{
83 static_cast<OHCatalog&>(m_rParent).refreshTables();
84}
85
87{
88 m_xMetaData.clear();
90}
91
92Reference< XPropertySet > OTables::createDescriptor()
93{
94 return new OHSQLTable(this,static_cast<OHCatalog&>(m_rParent).getConnection());
95}
96
97// XAppend
98sdbcx::ObjectType OTables::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
99{
100 createTable(descriptor);
101 return createObject( _rForName );
102}
103
104// XDrop
105void OTables::dropObject(sal_Int32 _nPos,const OUString& _sElementName)
106{
107 Reference< XInterface > xObject( getObject( _nPos ) );
108 bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
109 if (bIsNew)
110 return;
111
112 Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
113
114
115 OUString sCatalog,sSchema,sTable;
116 ::dbtools::qualifiedNameComponents(m_xMetaData,_sElementName,sCatalog,sSchema,sTable,::dbtools::EComposeRule::InDataManipulation);
117
118 OUString aSql( "DROP " );
119
120 Reference<XPropertySet> xProp(xObject,UNO_QUERY);
121 bool bIsView;
122 if((bIsView = (xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == "VIEW"))) // here we have a view
123 aSql += "VIEW ";
124 else
125 aSql += "TABLE ";
126
127 OUString sComposedName(
128 ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::EComposeRule::InDataManipulation ) );
129 aSql += sComposedName;
130 Reference< XStatement > xStmt = xConnection->createStatement( );
131 if ( xStmt.is() )
132 {
133 xStmt->execute(aSql);
134 ::comphelper::disposeComponent(xStmt);
135 }
136 // if no exception was thrown we must delete it from the views
137 if ( bIsView )
138 {
139 HViews* pViews = static_cast<HViews*>(static_cast<OHCatalog&>(m_rParent).getPrivateViews());
140 if ( pViews && pViews->hasByName(_sElementName) )
141 pViews->dropByNameImpl(_sElementName);
142 }
143}
144
145void OTables::createTable( const Reference< XPropertySet >& descriptor )
146{
147 Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
148 OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,xConnection);
149
150 Reference< XStatement > xStmt = xConnection->createStatement( );
151 if ( xStmt.is() )
152 {
153 xStmt->execute(aSql);
154 ::comphelper::disposeComponent(xStmt);
155 }
156}
157
158void OTables::appendNew(const OUString& _rsNewTable)
159{
160 insertElement(_rsNewTable,nullptr);
161
162 // notify our container listeners
163 ContainerEvent aEvent(static_cast<XContainer*>(this), Any(_rsNewTable), Any(), Any());
165 while (aListenerLoop.hasMoreElements())
166 aListenerLoop.next()->elementInserted(aEvent);
167}
168
170{
171 OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
172 return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::EComposeRule::InDataManipulation, false );
173}
174
175
176/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sSchema
OptionalString sCatalog
OptionalString sComposedName
AnyEventRef aEvent
css::uno::Reference< ListenerT > const & next()
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
void dropByNameImpl(const OUString &elementName)
Definition: HViews.cxx:113
css::uno::Reference< css::sdbc::XDatabaseMetaData > m_xMetaData
Definition: HTables.hxx:28
void createTable(const css::uno::Reference< css::beans::XPropertySet > &descriptor)
virtual void disposing() override
Definition: HTables.cxx:86
virtual css::uno::Reference< css::beans::XPropertySet > createDescriptor() override
Definition: HTables.cxx:92
virtual void impl_refresh() override
Definition: HTables.cxx:81
virtual void dropObject(sal_Int32 _nPos, const OUString &_sElementName) override
Definition: HTables.cxx:105
virtual sdbcx::ObjectType appendObject(const OUString &_rForName, const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
appends an object described by a descriptor, under a given name
void appendNew(const OUString &_rsNewTable)
Definition: HTables.cxx:158
virtual OUString getNameForObject(const sdbcx::ObjectType &_xObject) override
returns the name for the object.
Definition: HTables.cxx:169
virtual sdbcx::ObjectType createObject(const OUString &_rName) override
Definition: HTables.cxx:43
void insertElement(const OUString &_sElementName, const ObjectType &_xElement)
insert a new element into the collection
ObjectType getObject(sal_Int32 _nIndex)
return the object, if not existent it creates it.
::cppu::OWeakObject & m_rParent
Definition: VCollection.hxx:97
virtual sal_Bool SAL_CALL hasByName(const OUString &aName) override
::comphelper::OInterfaceContainerHelper3< css::container::XContainerListener > m_aContainerListeners
Definition: VCollection.hxx:93
const OUString & getNameByIndex(sal_Int32 _nIndex) const
Definition: propertyids.cxx:95
css::uno::Reference< css::beans::XPropertySet > ObjectType
Definition: VCollection.hxx:59
sal_Int32 getTablePrivileges(const Reference< XDatabaseMetaData > &_xMetaData, const OUString &_sCatalog, const OUString &_sSchema, const OUString &_sTable)
Definition: dbtools2.cxx:676
OUString createSqlCreateTableStatement(const Reference< XPropertySet > &descriptor, const Reference< XConnection > &_xConnection)
Definition: dbtools2.cxx:371
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
Definition: dbtools.cxx:1286
Reference< XConnection > getConnection(const Reference< XRowSet > &_rxRowSet)
Definition: dbtools.cxx:348
void qualifiedNameComponents(const Reference< XDatabaseMetaData > &_rxConnMetaData, const OUString &_rQualifiedName, OUString &_rCatalog, OUString &_rSchema, OUString &_rName, EComposeRule _eComposeRule)
Definition: dbtools.cxx:862
#define PROPERTY_ID_TYPE
Definition: propertyids.hxx:51
sal_Int32 _nPos