Line data Source code
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/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"
36 :
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;
48 :
49 2 : sdbcx::ObjectType OTables::createObject(const OUString& _rName)
50 : {
51 4 : OUString sCatalog,sSchema,sTable;
52 2 : ::dbtools::qualifiedNameComponents(m_xMetaData,_rName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
53 :
54 2 : static const OUString s_sTableTypeView("VIEW");
55 2 : static const OUString s_sTableTypeTable("TABLE");
56 2 : static const OUString s_sAll("%");
57 :
58 4 : Sequence< OUString > sTableTypes(3);
59 2 : sTableTypes[0] = s_sTableTypeView;
60 2 : sTableTypes[1] = s_sTableTypeTable;
61 2 : sTableTypes[2] = s_sAll; // just to be sure to include anything else ....
62 :
63 4 : Any aCatalog;
64 2 : if ( !sCatalog.isEmpty() )
65 0 : aCatalog <<= sCatalog;
66 4 : Reference< XResultSet > xResult = m_xMetaData->getTables(aCatalog,sSchema,sTable,sTableTypes);
67 :
68 2 : sdbcx::ObjectType xRet = NULL;
69 2 : if ( xResult.is() )
70 : {
71 2 : Reference< XRow > xRow(xResult,UNO_QUERY);
72 2 : if ( xResult->next() ) // there can be only one table with this name
73 : {
74 2 : sal_Int32 nPrivileges = ::dbtools::getTablePrivileges( m_xMetaData, sCatalog, sSchema, sTable );
75 2 : if ( m_xMetaData->isReadOnly() )
76 0 : nPrivileges &= ~( Privilege::INSERT | Privilege::UPDATE | Privilege::DELETE | Privilege::CREATE | Privilege::ALTER | Privilege::DROP );
77 :
78 : // obtain privileges
79 : OHSQLTable* pRet = new OHSQLTable( this
80 : ,static_cast<OHCatalog&>(m_rParent).getConnection()
81 : ,sTable
82 2 : ,xRow->getString(4)
83 2 : ,xRow->getString(5)
84 : ,sSchema
85 : ,sCatalog
86 6 : ,nPrivileges);
87 2 : xRet = pRet;
88 : }
89 2 : ::comphelper::disposeComponent(xResult);
90 : }
91 :
92 4 : return xRet;
93 : }
94 :
95 0 : void OTables::impl_refresh( ) throw(RuntimeException)
96 : {
97 0 : static_cast<OHCatalog&>(m_rParent).refreshTables();
98 0 : }
99 :
100 2 : void OTables::disposing(void)
101 : {
102 2 : m_xMetaData.clear();
103 2 : OCollection::disposing();
104 2 : }
105 :
106 0 : Reference< XPropertySet > OTables::createDescriptor()
107 : {
108 0 : return new OHSQLTable(this,static_cast<OHCatalog&>(m_rParent).getConnection());
109 : }
110 :
111 : // XAppend
112 0 : sdbcx::ObjectType OTables::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
113 : {
114 0 : createTable(descriptor);
115 0 : return createObject( _rForName );
116 : }
117 :
118 : // XDrop
119 0 : void OTables::dropObject(sal_Int32 _nPos,const OUString& _sElementName)
120 : {
121 0 : Reference< XInterface > xObject( getObject( _nPos ) );
122 0 : bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
123 0 : if (!bIsNew)
124 : {
125 0 : Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
126 :
127 :
128 0 : OUString sCatalog,sSchema,sTable;
129 0 : ::dbtools::qualifiedNameComponents(m_xMetaData,_sElementName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
130 :
131 0 : OUString aSql( "DROP " );
132 :
133 0 : Reference<XPropertySet> xProp(xObject,UNO_QUERY);
134 : bool bIsView;
135 0 : if((bIsView = (xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == "VIEW"))) // here we have a view
136 0 : aSql += "VIEW ";
137 : else
138 0 : aSql += "TABLE ";
139 :
140 : OUString sComposedName(
141 0 : ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, true, ::dbtools::eInDataManipulation ) );
142 0 : aSql += sComposedName;
143 0 : Reference< XStatement > xStmt = xConnection->createStatement( );
144 0 : if ( xStmt.is() )
145 : {
146 0 : xStmt->execute(aSql);
147 0 : ::comphelper::disposeComponent(xStmt);
148 : }
149 : // if no exception was thrown we must delete it from the views
150 0 : if ( bIsView )
151 : {
152 0 : HViews* pViews = static_cast<HViews*>(static_cast<OHCatalog&>(m_rParent).getPrivateViews());
153 0 : if ( pViews && pViews->hasByName(_sElementName) )
154 0 : pViews->dropByNameImpl(_sElementName);
155 0 : }
156 0 : }
157 0 : }
158 :
159 0 : void OTables::createTable( const Reference< XPropertySet >& descriptor )
160 : {
161 0 : Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
162 0 : OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,xConnection);
163 :
164 0 : Reference< XStatement > xStmt = xConnection->createStatement( );
165 0 : if ( xStmt.is() )
166 : {
167 0 : xStmt->execute(aSql);
168 0 : ::comphelper::disposeComponent(xStmt);
169 0 : }
170 0 : }
171 :
172 0 : void OTables::appendNew(const OUString& _rsNewTable)
173 : {
174 0 : insertElement(_rsNewTable,NULL);
175 :
176 : // notify our container listeners
177 0 : ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
178 0 : OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
179 0 : while (aListenerLoop.hasMoreElements())
180 0 : static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
181 0 : }
182 :
183 0 : OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
184 : {
185 : OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
186 0 : return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::eInDataManipulation, false, false, false );
187 : }
188 :
189 :
190 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|