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