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