1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "hsqldb/HTables.hxx"
27 #include "hsqldb/HViews.hxx"
28 #include "hsqldb/HTable.hxx"
29 #include <com/sun/star/sdbc/XRow.hpp>
30 #include <com/sun/star/sdbc/XResultSet.hpp>
31 #include <com/sun/star/sdbc/ColumnValue.hpp>
32 #include <com/sun/star/sdbcx/Privilege.hpp>
33 #include <com/sun/star/sdbc/KeyRule.hpp>
34 #include <com/sun/star/sdbcx/KeyType.hpp>
35 #include "hsqldb/HCatalog.hxx"
36 #include <comphelper/extract.hxx>
37 #include "connectivity/dbtools.hxx"
38 #include "connectivity/dbexception.hxx"
39 #include <cppuhelper/interfacecontainer.h>
40 #include <comphelper/types.hxx>
41 #include "TConnection.hxx"
42
43 using namespace ::comphelper;
44 using namespace connectivity;
45 using namespace ::cppu;
46 using namespace connectivity::hsqldb;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::sdbcx;
50 using namespace ::com::sun::star::sdbc;
51 using namespace ::com::sun::star::container;
52 using namespace ::com::sun::star::lang;
53 using namespace dbtools;
54 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
55
createObject(const::rtl::OUString & _rName)56 sdbcx::ObjectType OTables::createObject(const ::rtl::OUString& _rName)
57 {
58 ::rtl::OUString sCatalog,sSchema,sTable;
59 ::dbtools::qualifiedNameComponents(m_xMetaData,_rName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
60
61 static const ::rtl::OUString s_sTableTypeView(RTL_CONSTASCII_USTRINGPARAM("VIEW"));
62 static const ::rtl::OUString s_sTableTypeTable(RTL_CONSTASCII_USTRINGPARAM("TABLE"));
63 static const ::rtl::OUString s_sAll(RTL_CONSTASCII_USTRINGPARAM("%"));
64
65 Sequence< ::rtl::OUString > sTableTypes(3);
66 sTableTypes[0] = s_sTableTypeView;
67 sTableTypes[1] = s_sTableTypeTable;
68 sTableTypes[2] = s_sAll; // just to be sure to include anything else ....
69
70 Any aCatalog;
71 if ( sCatalog.getLength() )
72 aCatalog <<= sCatalog;
73 Reference< XResultSet > xResult = m_xMetaData->getTables(aCatalog,sSchema,sTable,sTableTypes);
74
75 sdbcx::ObjectType xRet = NULL;
76 if ( xResult.is() )
77 {
78 Reference< XRow > xRow(xResult,UNO_QUERY);
79 if ( xResult->next() ) // there can be only one table with this name
80 {
81 sal_Int32 nPrivileges = ::dbtools::getTablePrivileges( m_xMetaData, sCatalog, sSchema, sTable );
82 if ( m_xMetaData->isReadOnly() )
83 nPrivileges &= ~( Privilege::INSERT | Privilege::UPDATE | Privilege::DELETE | Privilege::CREATE | Privilege::ALTER | Privilege::DROP );
84
85 // obtain privileges
86 OHSQLTable* pRet = new OHSQLTable( this
87 ,static_cast<OHCatalog&>(m_rParent).getConnection()
88 ,sTable
89 ,xRow->getString(4)
90 ,xRow->getString(5)
91 ,sSchema
92 ,sCatalog
93 ,nPrivileges);
94 xRet = pRet;
95 }
96 ::comphelper::disposeComponent(xResult);
97 }
98
99 return xRet;
100 }
101 // -------------------------------------------------------------------------
impl_refresh()102 void OTables::impl_refresh( ) throw(RuntimeException)
103 {
104 static_cast<OHCatalog&>(m_rParent).refreshTables();
105 }
106 // -------------------------------------------------------------------------
disposing(void)107 void OTables::disposing(void)
108 {
109 m_xMetaData.clear();
110 OCollection::disposing();
111 }
112 // -------------------------------------------------------------------------
createDescriptor()113 Reference< XPropertySet > OTables::createDescriptor()
114 {
115 return new OHSQLTable(this,static_cast<OHCatalog&>(m_rParent).getConnection());
116 }
117 // -------------------------------------------------------------------------
118 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)119 sdbcx::ObjectType OTables::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
120 {
121 createTable(descriptor);
122 return createObject( _rForName );
123 }
124 // -------------------------------------------------------------------------
125 // XDrop
dropObject(sal_Int32 _nPos,const::rtl::OUString _sElementName)126 void OTables::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName)
127 {
128 Reference< XInterface > xObject( getObject( _nPos ) );
129 sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
130 if (!bIsNew)
131 {
132 Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
133
134
135 ::rtl::OUString sCatalog,sSchema,sTable;
136 ::dbtools::qualifiedNameComponents(m_xMetaData,_sElementName,sCatalog,sSchema,sTable,::dbtools::eInDataManipulation);
137
138 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP ");
139
140 Reference<XPropertySet> xProp(xObject,UNO_QUERY);
141 sal_Bool bIsView;
142 if((bIsView = (xProp.is() && ::comphelper::getString(xProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))) == ::rtl::OUString::createFromAscii("VIEW")))) // here we have a view
143 aSql += ::rtl::OUString::createFromAscii("VIEW ");
144 else
145 aSql += ::rtl::OUString::createFromAscii("TABLE ");
146
147 ::rtl::OUString sComposedName(
148 ::dbtools::composeTableName( m_xMetaData, sCatalog, sSchema, sTable, sal_True, ::dbtools::eInDataManipulation ) );
149 aSql += sComposedName;
150 Reference< XStatement > xStmt = xConnection->createStatement( );
151 if ( xStmt.is() )
152 {
153 xStmt->execute(aSql);
154 ::comphelper::disposeComponent(xStmt);
155 }
156 // if no exception was thrown we must delete it from the views
157 if ( bIsView )
158 {
159 HViews* pViews = static_cast<HViews*>(static_cast<OHCatalog&>(m_rParent).getPrivateViews());
160 if ( pViews && pViews->hasByName(_sElementName) )
161 pViews->dropByNameImpl(_sElementName);
162 }
163 }
164 }
165 // -------------------------------------------------------------------------
createTable(const Reference<XPropertySet> & descriptor)166 void OTables::createTable( const Reference< XPropertySet >& descriptor )
167 {
168 Reference< XConnection > xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
169 ::rtl::OUString aSql = ::dbtools::createSqlCreateTableStatement(descriptor,xConnection);
170
171 Reference< XStatement > xStmt = xConnection->createStatement( );
172 if ( xStmt.is() )
173 {
174 xStmt->execute(aSql);
175 ::comphelper::disposeComponent(xStmt);
176 }
177 }
178 // -----------------------------------------------------------------------------
appendNew(const::rtl::OUString & _rsNewTable)179 void OTables::appendNew(const ::rtl::OUString& _rsNewTable)
180 {
181 insertElement(_rsNewTable,NULL);
182
183 // notify our container listeners
184 ContainerEvent aEvent(static_cast<XContainer*>(this), makeAny(_rsNewTable), Any(), Any());
185 OInterfaceIteratorHelper aListenerLoop(m_aContainerListeners);
186 while (aListenerLoop.hasMoreElements())
187 static_cast<XContainerListener*>(aListenerLoop.next())->elementInserted(aEvent);
188 }
189 // -----------------------------------------------------------------------------
getNameForObject(const sdbcx::ObjectType & _xObject)190 ::rtl::OUString OTables::getNameForObject(const sdbcx::ObjectType& _xObject)
191 {
192 OSL_ENSURE(_xObject.is(),"OTables::getNameForObject: Object is NULL!");
193 return ::dbtools::composeTableName( m_xMetaData, _xObject, ::dbtools::eInDataManipulation, false, false, false );
194 }
195 // -----------------------------------------------------------------------------
196
197