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 "adabas/BKeys.hxx" 31*cdf0e10cSrcweir #include "adabas/BTable.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 <comphelper/types.hxx> 37*cdf0e10cSrcweir #include "adabas/BCatalog.hxx" 38*cdf0e10cSrcweir #include <comphelper/property.hxx> 39*cdf0e10cSrcweir #include <connectivity/TKeys.hxx> 40*cdf0e10cSrcweir #include <connectivity/dbtools.hxx> 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir using namespace ::comphelper; 44*cdf0e10cSrcweir using namespace connectivity; 45*cdf0e10cSrcweir using namespace connectivity::adabas; 46*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 47*cdf0e10cSrcweir using namespace ::com::sun::star::beans; 48*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx; 49*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc; 50*cdf0e10cSrcweir using namespace ::com::sun::star::container; 51*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 52*cdf0e10cSrcweir typedef OKeysHelper OCollection_TYPE; 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir // ------------------------------------------------------------------------- 55*cdf0e10cSrcweir // XAppend 56*cdf0e10cSrcweir sdbcx::ObjectType OKeys::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor ) 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir if ( getTable()->isNew() ) 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir Reference< XPropertySet > xNewDescriptor( cloneDescriptor( descriptor ) ); 61*cdf0e10cSrcweir OKeysHelper::cloneDescriptorColumns( descriptor, xNewDescriptor ); 62*cdf0e10cSrcweir return xNewDescriptor; 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir sal_Int32 nKeyType = getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE))); 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("ALTER TABLE "); 68*cdf0e10cSrcweir const ::rtl::OUString aQuote = getTable()->getConnection()->getMetaData()->getIdentifierQuoteString( ); 69*cdf0e10cSrcweir const ::rtl::OUString& sDot = OAdabasCatalog::getDot(); 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir aSql += composeTableName( getTable()->getConnection()->getMetaData(), getTable(), ::dbtools::eInTableDefinitions, false, false, true ); 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir if(nKeyType == KeyType::PRIMARY) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir aSql = aSql + ::rtl::OUString::createFromAscii(" ALTER PRIMARY KEY ("); 76*cdf0e10cSrcweir } 77*cdf0e10cSrcweir else if(nKeyType == KeyType::FOREIGN) 78*cdf0e10cSrcweir { 79*cdf0e10cSrcweir aSql = aSql + ::rtl::OUString::createFromAscii(" FOREIGN KEY ("); 80*cdf0e10cSrcweir } 81*cdf0e10cSrcweir else 82*cdf0e10cSrcweir throw SQLException(); 83*cdf0e10cSrcweir 84*cdf0e10cSrcweir Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY); 85*cdf0e10cSrcweir Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY); 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir for(sal_Int32 i=0;i<xColumns->getCount();++i) 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir Reference< XPropertySet > xColProp; 90*cdf0e10cSrcweir xColumns->getByIndex(i) >>= xColProp; 91*cdf0e10cSrcweir aSql = aSql + aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote 92*cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(","); 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")")); 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir sal_Int32 nUpdateRule = 0, nDeleteRule = 0; 97*cdf0e10cSrcweir ::rtl::OUString sReferencedName; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir if(nKeyType == KeyType::FOREIGN) 100*cdf0e10cSrcweir { 101*cdf0e10cSrcweir nDeleteRule = getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DELETERULE))); 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir ::rtl::OUString aName,aSchema; 104*cdf0e10cSrcweir sReferencedName = getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REFERENCEDTABLE))); 105*cdf0e10cSrcweir sal_Int32 nLen = sReferencedName.indexOf('.'); 106*cdf0e10cSrcweir aSchema = sReferencedName.copy(0,nLen); 107*cdf0e10cSrcweir aName = sReferencedName.copy(nLen+1); 108*cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" REFERENCES ") 109*cdf0e10cSrcweir + aQuote + aSchema + aQuote + sDot + aQuote + aName + aQuote; 110*cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ("); 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir for(sal_Int32 i=0;i<xColumns->getCount();++i) 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir Reference< XPropertySet > xColProp; 115*cdf0e10cSrcweir xColumns->getByIndex(i) >>= xColProp; 116*cdf0e10cSrcweir aSql = aSql + aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RELATEDCOLUMN))) + aQuote 117*cdf0e10cSrcweir + ::rtl::OUString::createFromAscii(","); 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")")); 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir switch(nDeleteRule) 122*cdf0e10cSrcweir { 123*cdf0e10cSrcweir case KeyRule::CASCADE: 124*cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ON DELETE CASCADE "); 125*cdf0e10cSrcweir break; 126*cdf0e10cSrcweir case KeyRule::RESTRICT: 127*cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ON DELETE RESTRICT "); 128*cdf0e10cSrcweir break; 129*cdf0e10cSrcweir case KeyRule::SET_NULL: 130*cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET NULL "); 131*cdf0e10cSrcweir break; 132*cdf0e10cSrcweir case KeyRule::SET_DEFAULT: 133*cdf0e10cSrcweir aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET DEFAULT "); 134*cdf0e10cSrcweir break; 135*cdf0e10cSrcweir default: 136*cdf0e10cSrcweir ; 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir Reference< XStatement > xStmt = getTable()->getConnection()->createStatement( ); 141*cdf0e10cSrcweir xStmt->execute(aSql); 142*cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt); 143*cdf0e10cSrcweir // find the name which the database gave the new key 144*cdf0e10cSrcweir ::rtl::OUString sNewName( _rForName ); 145*cdf0e10cSrcweir if(nKeyType == KeyType::FOREIGN) 146*cdf0e10cSrcweir { 147*cdf0e10cSrcweir const ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap(); 148*cdf0e10cSrcweir ::rtl::OUString aSchema,aTable; 149*cdf0e10cSrcweir getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema; 150*cdf0e10cSrcweir getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable; 151*cdf0e10cSrcweir Reference< XResultSet > xResult = getTable()->getMetaData()->getImportedKeys( getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME)) 152*cdf0e10cSrcweir ,aSchema 153*cdf0e10cSrcweir ,aTable); 154*cdf0e10cSrcweir if(xResult.is()) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir Reference< XRow > xRow(xResult,UNO_QUERY); 157*cdf0e10cSrcweir while(xResult->next()) 158*cdf0e10cSrcweir { 159*cdf0e10cSrcweir ::rtl::OUString sName = xRow->getString(12); 160*cdf0e10cSrcweir if ( !m_pElements->exists(sName) ) // this name wasn't inserted yet so it must be te new one 161*cdf0e10cSrcweir { 162*cdf0e10cSrcweir descriptor->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sName)); 163*cdf0e10cSrcweir sNewName = sName; 164*cdf0e10cSrcweir break; 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir ::comphelper::disposeComponent(xResult); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir } 170*cdf0e10cSrcweir 171*cdf0e10cSrcweir getTable()->addKey(sNewName,sdbcx::TKeyProperties(new sdbcx::KeyProperties(sReferencedName,nKeyType,nUpdateRule,nDeleteRule))); 172*cdf0e10cSrcweir return createObject( sNewName ); 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir // ------------------------------------------------------------------------- 175*cdf0e10cSrcweir ::rtl::OUString OKeys::getDropForeignKey() const 176*cdf0e10cSrcweir { 177*cdf0e10cSrcweir return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP FOREIGN KEY ")); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 181*cdf0e10cSrcweir 182