Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / hsqldb / HTables.cxx
blobcd5c1816c7dcf871b76b822ff36fa8abd4df5180
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/sdbc/ColumnValue.hpp>
26 #include <com/sun/star/sdbcx/Privilege.hpp>
27 #include <com/sun/star/sdbc/KeyRule.hpp>
28 #include <com/sun/star/sdbcx/KeyType.hpp>
29 #include "hsqldb/HCatalog.hxx"
30 #include <comphelper/extract.hxx>
31 #include "connectivity/dbtools.hxx"
32 #include "connectivity/dbexception.hxx"
33 #include <cppuhelper/interfacecontainer.h>
34 #include <comphelper/types.hxx>
35 #include "TConnection.hxx"
37 using namespace ::comphelper;
38 using namespace connectivity;
39 using namespace ::cppu;
40 using namespace connectivity::hsqldb;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::sdbcx;
44 using namespace ::com::sun::star::sdbc;
45 using namespace ::com::sun::star::container;
46 using namespace ::com::sun::star::lang;
47 using namespace dbtools;
49 sdbcx::ObjectType OTables::createObject(const OUString& _rName)
51 OUString sCatalog,sSchema,sTable;
52 ::dbtools::qualifiedNameComponents(m_xMetaData,_rName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
54 static const OUString s_sTableTypeView("VIEW");
55 static const OUString s_sTableTypeTable("TABLE");
56 static const OUString s_sAll("%");
58 Sequence< OUString > sTableTypes(3);
59 sTableTypes[0] = s_sTableTypeView;
60 sTableTypes[1] = s_sTableTypeTable;
61 sTableTypes[2] = s_sAll; // just to be sure to include anything else ....
63 Any aCatalog;
64 if ( !sCatalog.isEmpty() )
65 aCatalog <<= sCatalog;
66 Reference< XResultSet > xResult = m_xMetaData->getTables(aCatalog,sSchema,sTable,sTableTypes);
68 sdbcx::ObjectType xRet = NULL;
69 if ( xResult.is() )
71 Reference< XRow > xRow(xResult,UNO_QUERY);
72 if ( xResult->next() ) // there can be only one table with this name
74 sal_Int32 nPrivileges = ::dbtools::getTablePrivileges( m_xMetaData, sCatalog, sSchema, sTable );
75 if ( m_xMetaData->isReadOnly() )
76 nPrivileges &= ~( Privilege::INSERT | Privilege::UPDATE | Privilege::DELETE | Privilege::CREATE | Privilege::ALTER | Privilege::DROP );
78 // obtain privileges
79 OHSQLTable* pRet = new OHSQLTable( this
80 ,static_cast<OHCatalog&>(m_rParent).getConnection()
81 ,sTable
82 ,xRow->getString(4)
83 ,xRow->getString(5)
84 ,sSchema
85 ,sCatalog
86 ,nPrivileges);
87 xRet = pRet;
89 ::comphelper::disposeComponent(xResult);
92 return xRet;
95 void OTables::impl_refresh( ) throw(RuntimeException)
97 static_cast<OHCatalog&>(m_rParent).refreshTables();
100 void OTables::disposing(void)
102 m_xMetaData.clear();
103 OCollection::disposing();
106 Reference< XPropertySet > OTables::createDescriptor()
108 return new OHSQLTable(this,static_cast<OHCatalog&>(m_rParent).getConnection());
111 // XAppend
112 sdbcx::ObjectType OTables::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
114 createTable(descriptor);
115 return createObject( _rForName );
118 // XDrop
119 void OTables::dropObject(sal_Int32 _nPos,const OUString& _sElementName)
121 Reference< XInterface > xObject( getObject( _nPos ) );
122 bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
123 if (!bIsNew)
125 Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
128 OUString sCatalog,sSchema,sTable;
129 ::dbtools::qualifiedNameComponents(m_xMetaData,_sElementName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
131 OUString aSql( "DROP " );
133 Reference<XPropertySet> xProp(xObject,UNO_QUERY);
134 bool bIsView;
135 if((bIsView = (xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == "VIEW"))) // here we have a view
136 aSql += "VIEW ";
137 else
138 aSql += "TABLE ";
140 OUString sComposedName(
141 ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::eInDataManipulation ) );
142 aSql += sComposedName;
143 Reference< XStatement > xStmt = xConnection->createStatement( );
144 if ( xStmt.is() )
146 xStmt->execute(aSql);
147 ::comphelper::disposeComponent(xStmt);
149 // if no exception was thrown we must delete it from the views
150 if ( bIsView )
152 HViews* pViews = static_cast<HViews*>(static_cast<OHCatalog&>(m_rParent).getPrivateViews());
153 if ( pViews && pViews->hasByName(_sElementName) )
154 pViews->dropByNameImpl(_sElementName);
159 void OTables::createTable( const Reference< XPropertySet >& descriptor )
161 Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
162 OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,xConnection);
164 Reference< XStatement > xStmt = xConnection->createStatement( );
165 if ( xStmt.is() )
167 xStmt->execute(aSql);
168 ::comphelper::disposeComponent(xStmt);
172 void OTables::appendNew(const OUString& _rsNewTable)
174 insertElement(_rsNewTable,NULL);
176 // notify our container listeners
177 ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
178 OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
179 while (aListenerLoop.hasMoreElements())
180 static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
183 OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
185 OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
186 return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::eInDataManipulation, false, false, false );
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */