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