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