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 "connectivity/TKeys.hxx" 31*cdf0e10cSrcweir #include "connectivity/TKey.hxx" 32*cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp> 33*cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/sdbcx/KeyType.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/sdbc/KeyRule.hpp> 36*cdf0e10cSrcweir #include "connectivity/dbtools.hxx" 37*cdf0e10cSrcweir #include <comphelper/extract.hxx> 38*cdf0e10cSrcweir #include <comphelper/types.hxx> 39*cdf0e10cSrcweir #include <comphelper/property.hxx> 40*cdf0e10cSrcweir #include "TConnection.hxx" 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir namespace connectivity 43*cdf0e10cSrcweir { 44*cdf0e10cSrcweir using namespace comphelper; 45*cdf0e10cSrcweir using namespace connectivity::sdbcx; 46*cdf0e10cSrcweir using namespace dbtools; 47*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 48*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 49*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx; 50*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc; 51*cdf0e10cSrcweir using namespace ::com::sun::star::container; 52*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir OKeysHelper::OKeysHelper( OTableHelper* _pTable, 57*cdf0e10cSrcweir ::osl::Mutex& _rMutex, 58*cdf0e10cSrcweir const TStringVector& _rVector 59*cdf0e10cSrcweir ) : OKeys_BASE(*_pTable,sal_True,_rMutex,_rVector,sal_True) 60*cdf0e10cSrcweir ,m_pTable(_pTable) 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir } 63*cdf0e10cSrcweir // ------------------------------------------------------------------------- 64*cdf0e10cSrcweir sdbcx::ObjectType OKeysHelper::createObject(const ::rtl::OUString& _rName) 65*cdf0e10cSrcweir { 66*cdf0e10cSrcweir sdbcx::ObjectType xRet = NULL; 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir if(_rName.getLength()) 69*cdf0e10cSrcweir { 70*cdf0e10cSrcweir OTableKeyHelper* pRet = new OTableKeyHelper(m_pTable,_rName,m_pTable->getKeyProperties(_rName)); 71*cdf0e10cSrcweir xRet = pRet; 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir if(!xRet.is()) // we have a primary key with a system name 75*cdf0e10cSrcweir { 76*cdf0e10cSrcweir OTableKeyHelper* pRet = new OTableKeyHelper(m_pTable,_rName,m_pTable->getKeyProperties(_rName)); 77*cdf0e10cSrcweir xRet = pRet; 78*cdf0e10cSrcweir } 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir return xRet; 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir // ------------------------------------------------------------------------- 83*cdf0e10cSrcweir void OKeysHelper::impl_refresh() throw(RuntimeException) 84*cdf0e10cSrcweir { 85*cdf0e10cSrcweir m_pTable->refreshKeys(); 86*cdf0e10cSrcweir } 87*cdf0e10cSrcweir // ------------------------------------------------------------------------- 88*cdf0e10cSrcweir Reference< XPropertySet > OKeysHelper::createDescriptor() 89*cdf0e10cSrcweir { 90*cdf0e10cSrcweir return new OTableKeyHelper(m_pTable); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 93*cdf0e10cSrcweir /** returns the keyrule string for the primary key 94*cdf0e10cSrcweir */ 95*cdf0e10cSrcweir ::rtl::OUString getKeyRuleString(sal_Bool _bUpdate,sal_Int32 _nKeyRule) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir const char* pKeyRule = NULL; 98*cdf0e10cSrcweir switch ( _nKeyRule ) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir case KeyRule::CASCADE: 101*cdf0e10cSrcweir pKeyRule = _bUpdate ? " ON UPDATE CASCADE " : " ON DELETE CASCADE "; 102*cdf0e10cSrcweir break; 103*cdf0e10cSrcweir case KeyRule::RESTRICT: 104*cdf0e10cSrcweir pKeyRule = _bUpdate ? " ON UPDATE RESTRICT " : " ON DELETE RESTRICT "; 105*cdf0e10cSrcweir break; 106*cdf0e10cSrcweir case KeyRule::SET_NULL: 107*cdf0e10cSrcweir pKeyRule = _bUpdate ? " ON UPDATE SET NULL " : " ON DELETE SET NULL "; 108*cdf0e10cSrcweir break; 109*cdf0e10cSrcweir case KeyRule::SET_DEFAULT: 110*cdf0e10cSrcweir pKeyRule = _bUpdate ? " ON UPDATE SET DEFAULT " : " ON DELETE SET DEFAULT "; 111*cdf0e10cSrcweir break; 112*cdf0e10cSrcweir default: 113*cdf0e10cSrcweir ; 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir ::rtl::OUString sRet; 116*cdf0e10cSrcweir if ( pKeyRule ) 117*cdf0e10cSrcweir sRet = ::rtl::OUString::createFromAscii(pKeyRule); 118*cdf0e10cSrcweir return sRet; 119*cdf0e10cSrcweir } 120*cdf0e10cSrcweir // ------------------------------------------------------------------------- 121*cdf0e10cSrcweir void OKeysHelper::cloneDescriptorColumns( const sdbcx::ObjectType& _rSourceDescriptor, const sdbcx::ObjectType& _rDestDescriptor ) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir Reference< XColumnsSupplier > xColSupp( _rSourceDescriptor, UNO_QUERY_THROW ); 124*cdf0e10cSrcweir Reference< XIndexAccess > xSourceCols( xColSupp->getColumns(), UNO_QUERY_THROW ); 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir xColSupp.set( _rDestDescriptor, UNO_QUERY_THROW ); 127*cdf0e10cSrcweir Reference< XAppend > xDestAppend( xColSupp->getColumns(), UNO_QUERY_THROW ); 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir sal_Int32 nCount = xSourceCols->getCount(); 130*cdf0e10cSrcweir for ( sal_Int32 i=0; i< nCount; ++i ) 131*cdf0e10cSrcweir { 132*cdf0e10cSrcweir Reference< XPropertySet > xColProp( xSourceCols->getByIndex(i), UNO_QUERY ); 133*cdf0e10cSrcweir xDestAppend->appendByDescriptor( xColProp ); 134*cdf0e10cSrcweir } 135*cdf0e10cSrcweir } 136*cdf0e10cSrcweir // ------------------------------------------------------------------------- 137*cdf0e10cSrcweir // XAppend 138*cdf0e10cSrcweir sdbcx::ObjectType OKeysHelper::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir Reference< XConnection> xConnection = m_pTable->getConnection(); 141*cdf0e10cSrcweir if ( !xConnection.is() ) 142*cdf0e10cSrcweir return NULL; 143*cdf0e10cSrcweir if ( m_pTable->isNew() ) 144*cdf0e10cSrcweir { 145*cdf0e10cSrcweir Reference< XPropertySet > xNewDescriptor( cloneDescriptor( descriptor ) ); 146*cdf0e10cSrcweir cloneDescriptorColumns( descriptor, xNewDescriptor ); 147*cdf0e10cSrcweir return xNewDescriptor; 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir const ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap(); 151*cdf0e10cSrcweir sal_Int32 nKeyType = getINT32(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE))); 152*cdf0e10cSrcweir sal_Int32 nUpdateRule = 0, nDeleteRule = 0; 153*cdf0e10cSrcweir ::rtl::OUString sReferencedName; 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir if ( nKeyType == KeyType::FOREIGN ) 156*cdf0e10cSrcweir { 157*cdf0e10cSrcweir descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_REFERENCEDTABLE)) >>= sReferencedName; 158*cdf0e10cSrcweir descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_UPDATERULE)) >>= nUpdateRule; 159*cdf0e10cSrcweir descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_DELETERULE)) >>= nDeleteRule; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir if ( m_pTable->getKeyService().is() ) 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir m_pTable->getKeyService()->addKey(m_pTable,descriptor); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir else 167*cdf0e10cSrcweir { 168*cdf0e10cSrcweir // if we're here, we belong to a table which is not new, i.e. already exists in the database. 169*cdf0e10cSrcweir // In this case, really append the new index. 170*cdf0e10cSrcweir ::rtl::OUStringBuffer aSql; 171*cdf0e10cSrcweir aSql.appendAscii("ALTER TABLE "); 172*cdf0e10cSrcweir ::rtl::OUString aQuote = m_pTable->getConnection()->getMetaData()->getIdentifierQuoteString( ); 173*cdf0e10cSrcweir ::rtl::OUString aDot = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(".")); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir aSql.append(composeTableName( m_pTable->getConnection()->getMetaData(), m_pTable, ::dbtools::eInTableDefinitions, false, false, true )); 176*cdf0e10cSrcweir aSql.appendAscii(" ADD "); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir if ( nKeyType == KeyType::PRIMARY ) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir aSql.appendAscii(" PRIMARY KEY ("); 181*cdf0e10cSrcweir } 182*cdf0e10cSrcweir else if ( nKeyType == KeyType::FOREIGN ) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir aSql.appendAscii(" FOREIGN KEY ("); 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir else 187*cdf0e10cSrcweir throw SQLException(); 188*cdf0e10cSrcweir 189*cdf0e10cSrcweir Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY); 190*cdf0e10cSrcweir Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY); 191*cdf0e10cSrcweir Reference< XPropertySet > xColProp; 192*cdf0e10cSrcweir for(sal_Int32 i = 0 ; i < xColumns->getCount() ; ++i) 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir if ( i > 0 ) 195*cdf0e10cSrcweir aSql.appendAscii(","); 196*cdf0e10cSrcweir ::cppu::extractInterface(xColProp,xColumns->getByIndex(i)); 197*cdf0e10cSrcweir aSql.append( ::dbtools::quoteName( aQuote,getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))) ); 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir aSql.appendAscii(")"); 201*cdf0e10cSrcweir 202*cdf0e10cSrcweir if ( nKeyType == KeyType::FOREIGN ) 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir aSql.appendAscii(" REFERENCES "); 205*cdf0e10cSrcweir aSql.append(::dbtools::quoteTableName(m_pTable->getConnection()->getMetaData(),sReferencedName,::dbtools::eInTableDefinitions)); 206*cdf0e10cSrcweir aSql.appendAscii(" ("); 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir for(sal_Int32 i=0;i<xColumns->getCount();++i) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir if ( i > 0 ) 211*cdf0e10cSrcweir aSql.appendAscii(","); 212*cdf0e10cSrcweir xColumns->getByIndex(i) >>= xColProp; 213*cdf0e10cSrcweir aSql.append(::dbtools::quoteName( aQuote,getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_RELATEDCOLUMN))))); 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir } 216*cdf0e10cSrcweir aSql.appendAscii(")"); 217*cdf0e10cSrcweir aSql.append(getKeyRuleString(sal_True ,nUpdateRule)); 218*cdf0e10cSrcweir aSql.append(getKeyRuleString(sal_False ,nDeleteRule)); 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir 221*cdf0e10cSrcweir Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( ); 222*cdf0e10cSrcweir xStmt->execute(aSql.makeStringAndClear()); 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir // find the name which the database gave the new key 225*cdf0e10cSrcweir ::rtl::OUString sNewName( _rForName ); 226*cdf0e10cSrcweir try 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir ::rtl::OUString aSchema,aTable; 229*cdf0e10cSrcweir m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema; 230*cdf0e10cSrcweir m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable; 231*cdf0e10cSrcweir Reference< XResultSet > xResult; 232*cdf0e10cSrcweir sal_Int32 nColumn = 12; 233*cdf0e10cSrcweir if ( nKeyType == KeyType::FOREIGN ) 234*cdf0e10cSrcweir xResult = m_pTable->getMetaData()->getImportedKeys( m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)) 235*cdf0e10cSrcweir ,aSchema 236*cdf0e10cSrcweir ,aTable); 237*cdf0e10cSrcweir else 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir xResult = m_pTable->getMetaData()->getPrimaryKeys( m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)) 240*cdf0e10cSrcweir ,aSchema 241*cdf0e10cSrcweir ,aTable); 242*cdf0e10cSrcweir nColumn = 6; 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir if ( xResult.is() ) 245*cdf0e10cSrcweir { 246*cdf0e10cSrcweir Reference< XRow > xRow(xResult,UNO_QUERY); 247*cdf0e10cSrcweir while( xResult->next() ) 248*cdf0e10cSrcweir { 249*cdf0e10cSrcweir ::rtl::OUString sName = xRow->getString(nColumn); 250*cdf0e10cSrcweir if ( !m_pElements->exists(sName) ) // this name wasn't inserted yet so it must be te new one 251*cdf0e10cSrcweir { 252*cdf0e10cSrcweir descriptor->setPropertyValue( rPropMap.getNameByIndex( PROPERTY_ID_NAME ), makeAny( sName ) ); 253*cdf0e10cSrcweir sNewName = sName; 254*cdf0e10cSrcweir break; 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir } 257*cdf0e10cSrcweir ::comphelper::disposeComponent(xResult); 258*cdf0e10cSrcweir } 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir catch(const SQLException&) 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir } 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir m_pTable->addKey(sNewName,sdbcx::TKeyProperties(new sdbcx::KeyProperties(sReferencedName,nKeyType,nUpdateRule,nDeleteRule))); 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir return createObject( sNewName ); 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 269*cdf0e10cSrcweir ::rtl::OUString OKeysHelper::getDropForeignKey() const 270*cdf0e10cSrcweir { 271*cdf0e10cSrcweir return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP CONSTRAINT ")); 272*cdf0e10cSrcweir } 273*cdf0e10cSrcweir // ------------------------------------------------------------------------- 274*cdf0e10cSrcweir // XDrop 275*cdf0e10cSrcweir void OKeysHelper::dropObject(sal_Int32 _nPos,const ::rtl::OUString _sElementName) 276*cdf0e10cSrcweir { 277*cdf0e10cSrcweir Reference< XConnection> xConnection = m_pTable->getConnection(); 278*cdf0e10cSrcweir if ( xConnection.is() && !m_pTable->isNew() ) 279*cdf0e10cSrcweir { 280*cdf0e10cSrcweir Reference<XPropertySet> xKey(getObject(_nPos),UNO_QUERY); 281*cdf0e10cSrcweir if ( m_pTable->getKeyService().is() ) 282*cdf0e10cSrcweir { 283*cdf0e10cSrcweir m_pTable->getKeyService()->dropKey(m_pTable,xKey); 284*cdf0e10cSrcweir } 285*cdf0e10cSrcweir else 286*cdf0e10cSrcweir { 287*cdf0e10cSrcweir ::rtl::OUStringBuffer aSql; 288*cdf0e10cSrcweir aSql.appendAscii("ALTER TABLE "); 289*cdf0e10cSrcweir 290*cdf0e10cSrcweir aSql.append( composeTableName( m_pTable->getConnection()->getMetaData(), m_pTable,::dbtools::eInTableDefinitions, false, false, true )); 291*cdf0e10cSrcweir 292*cdf0e10cSrcweir sal_Int32 nKeyType = KeyType::PRIMARY; 293*cdf0e10cSrcweir if ( xKey.is() ) 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap(); 296*cdf0e10cSrcweir xKey->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_TYPE)) >>= nKeyType; 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir if ( KeyType::PRIMARY == nKeyType ) 299*cdf0e10cSrcweir { 300*cdf0e10cSrcweir aSql.appendAscii(" DROP PRIMARY KEY"); 301*cdf0e10cSrcweir } 302*cdf0e10cSrcweir else 303*cdf0e10cSrcweir { 304*cdf0e10cSrcweir aSql.append(getDropForeignKey()); 305*cdf0e10cSrcweir const ::rtl::OUString aQuote = m_pTable->getConnection()->getMetaData()->getIdentifierQuoteString(); 306*cdf0e10cSrcweir aSql.append( ::dbtools::quoteName( aQuote,_sElementName) ); 307*cdf0e10cSrcweir } 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( ); 310*cdf0e10cSrcweir if ( xStmt.is() ) 311*cdf0e10cSrcweir { 312*cdf0e10cSrcweir xStmt->execute(aSql.makeStringAndClear()); 313*cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt); 314*cdf0e10cSrcweir } 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir } 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 319*cdf0e10cSrcweir } // namespace connectivity 320*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 321