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