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