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