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 
27 #include "hsqldb/HTables.hxx"
28 #include "hsqldb/HViews.hxx"
29 #include "hsqldb/HView.hxx"
30 #include <com/sun/star/sdbc/XRow.hpp>
31 #include <com/sun/star/sdbc/XResultSet.hpp>
32 #include <com/sun/star/sdbc/ColumnValue.hpp>
33 #include <com/sun/star/sdbc/KeyRule.hpp>
34 #include <com/sun/star/sdbcx/KeyType.hpp>
35 #include <com/sun/star/sdbcx/CheckOption.hpp>
36 #include "hsqldb/HCatalog.hxx"
37 #include <comphelper/extract.hxx>
38 #include "connectivity/dbtools.hxx"
39 #include "connectivity/dbexception.hxx"
40 #include <cppuhelper/interfacecontainer.h>
41 #include <comphelper/types.hxx>
42 #include "TConnection.hxx"
43 
44 using namespace ::comphelper;
45 
46 using namespace ::cppu;
47 using namespace connectivity;
48 using namespace connectivity::hsqldb;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::sdbcx;
52 using namespace ::com::sun::star::sdbc;
53 using namespace ::com::sun::star::container;
54 using namespace ::com::sun::star::lang;
55 using namespace dbtools;
56 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
57 
58 // -------------------------------------------------------------------------
HViews(const Reference<XConnection> & _rxConnection,::cppu::OWeakObject & _rParent,::osl::Mutex & _rMutex,const TStringVector & _rVector)59 HViews::HViews( const Reference< XConnection >& _rxConnection, ::cppu::OWeakObject& _rParent, ::osl::Mutex& _rMutex,
60     const TStringVector &_rVector )
61     :sdbcx::OCollection( _rParent, sal_True, _rMutex, _rVector )
62     ,m_xConnection( _rxConnection )
63     ,m_xMetaData( _rxConnection->getMetaData() )
64     ,m_bInDrop( sal_False )
65 {
66 }
67 
68 // -------------------------------------------------------------------------
createObject(const::rtl::OUString & _rName)69 sdbcx::ObjectType HViews::createObject(const ::rtl::OUString& _rName)
70 {
71 	::rtl::OUString sCatalog,sSchema,sTable;
72 	::dbtools::qualifiedNameComponents(m_xMetaData,
73 										_rName,
74 										sCatalog,
75 										sSchema,
76 										sTable,
77 										::dbtools::eInDataManipulation);
78     return new HView( m_xConnection, isCaseSensitive(), sSchema, sTable );
79 }
80 
81 // -------------------------------------------------------------------------
impl_refresh()82 void HViews::impl_refresh(  ) throw(RuntimeException)
83 {
84 	static_cast<OHCatalog&>(m_rParent).refreshTables();
85 }
86 // -------------------------------------------------------------------------
disposing(void)87 void HViews::disposing(void)
88 {
89 m_xMetaData.clear();
90 	OCollection::disposing();
91 }
92 // -------------------------------------------------------------------------
createDescriptor()93 Reference< XPropertySet > HViews::createDescriptor()
94 {
95 	Reference<XConnection> xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
96 	connectivity::sdbcx::OView* pNew = new connectivity::sdbcx::OView(sal_True,xConnection->getMetaData());
97 	return pNew;
98 }
99 // -------------------------------------------------------------------------
100 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)101 sdbcx::ObjectType HViews::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
102 {
103 	createView(descriptor);
104     return createObject( _rForName );
105 }
106 // -------------------------------------------------------------------------
107 // XDrop
dropObject(sal_Int32 _nPos,const::rtl::OUString)108 void HViews::dropObject(sal_Int32 _nPos,const ::rtl::OUString /*_sElementName*/)
109 {
110 	if ( m_bInDrop )
111 		return;
112 
113     Reference< XInterface > xObject( getObject( _nPos ) );
114     sal_Bool bIsNew = connectivity::sdbcx::ODescriptor::isNew( xObject );
115 	if (!bIsNew)
116 	{
117 		::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP VIEW");
118 
119         Reference<XPropertySet> xProp(xObject,UNO_QUERY);
120 		aSql += ::dbtools::composeTableName( m_xMetaData, xProp, ::dbtools::eInTableDefinitions, false, false, true );
121 
122 		Reference<XConnection> xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
123 		Reference< XStatement > xStmt = xConnection->createStatement(  );
124 		xStmt->execute(aSql);
125 		::comphelper::disposeComponent(xStmt);
126     }
127 }
128 // -----------------------------------------------------------------------------
dropByNameImpl(const::rtl::OUString & elementName)129 void HViews::dropByNameImpl(const ::rtl::OUString& elementName)
130 {
131 	m_bInDrop = sal_True;
132 	OCollection_TYPE::dropByName(elementName);
133 	m_bInDrop = sal_False;
134 }
135 // -----------------------------------------------------------------------------
createView(const Reference<XPropertySet> & descriptor)136 void HViews::createView( const Reference< XPropertySet >& descriptor )
137 {
138 	Reference<XConnection> xConnection = static_cast<OHCatalog&>(m_rParent).getConnection();
139 
140 	::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("CREATE VIEW ");
141 	::rtl::OUString aQuote	= xConnection->getMetaData()->getIdentifierQuoteString(  );
142 	::rtl::OUString sSchema,sCommand;
143 
144 	aSql += ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::eInTableDefinitions, false, false, true );
145 
146 	aSql += ::rtl::OUString::createFromAscii(" AS ");
147 	descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_COMMAND)) >>= sCommand;
148 	aSql += sCommand;
149 
150     Reference< XStatement > xStmt = xConnection->createStatement(  );
151 	if ( xStmt.is() )
152 	{
153 		xStmt->execute(aSql);
154 		::comphelper::disposeComponent(xStmt);
155 	}
156 
157 	// insert the new view also in the tables collection
158 	OTables* pTables = static_cast<OTables*>(static_cast<OHCatalog&>(m_rParent).getPrivateTables());
159 	if ( pTables )
160 	{
161 		::rtl::OUString sName = ::dbtools::composeTableName( m_xMetaData, descriptor, ::dbtools::eInDataManipulation, false, false, false );
162 		pTables->appendNew(sName);
163 	}
164 }
165 // -----------------------------------------------------------------------------
166