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