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>
27 #include <connectivity/dbtools.hxx>
28 #include <comphelper/types.hxx>
29 #include <TConnection.hxx>
30 
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;
42 
createObject(const OUString & _rName)43 sdbcx::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 
55     sdbcx::ObjectType xRet;
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
72                                                 ,sCatalog
73                                                 ,nPrivileges);
74         }
75         ::comphelper::disposeComponent(xResult);
76     }
77 
78     return xRet;
79 }
80 
impl_refresh()81 void OTables::impl_refresh(  )
82 {
83     static_cast<OHCatalog&>(m_rParent).refreshTables();
84 }
85 
disposing()86 void OTables::disposing()
87 {
88     m_xMetaData.clear();
89     OCollection::disposing();
90 }
91 
createDescriptor()92 Reference< XPropertySet > OTables::createDescriptor()
93 {
94     return new OHSQLTable(this,static_cast<OHCatalog&>(m_rParent).getConnection());
95 }
96 
97 // XAppend
appendObject(const OUString & _rForName,const Reference<XPropertySet> & descriptor)98 sdbcx::ObjectType OTables::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
99 {
100     createTable(descriptor);
101     return createObject( _rForName );
102 }
103 
104 // XDrop
dropObject(sal_Int32 _nPos,const OUString & _sElementName)105 void 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 
createTable(const Reference<XPropertySet> & descriptor)145 void 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 
appendNew(const OUString & _rsNewTable)158 void OTables::appendNew(const OUString& _rsNewTable)
159 {
160     insertElement(_rsNewTable,nullptr);
161 
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);
167 }
168 
getNameForObject(const sdbcx::ObjectType & _xObject)169 OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
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: */
177