update dev300-m58
[ooovba.git] / connectivity / source / drivers / adabas / BKeys.cxx
blob2d5ff147597b6f02587572a76d0598e85c045fb9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: BKeys.cxx,v $
10 * $Revision: 1.25 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "adabas/BKeys.hxx"
34 #include "adabas/BTable.hxx"
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include <com/sun/star/sdbc/XResultSet.hpp>
37 #include <com/sun/star/sdbcx/KeyType.hpp>
38 #include <com/sun/star/sdbc/KeyRule.hpp>
39 #include <comphelper/types.hxx>
40 #include "adabas/BCatalog.hxx"
41 #include <comphelper/property.hxx>
42 #include <connectivity/TKeys.hxx>
43 #include <connectivity/dbtools.hxx>
46 using namespace ::comphelper;
47 using namespace connectivity;
48 using namespace connectivity::adabas;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::sdbcx;
52 using namespace ::com::sun::star::sdbc;
53 using namespace ::com::sun::star::container;
54 using namespace ::com::sun::star::lang;
55 typedef OKeysHelper OCollection_TYPE;
57 // -------------------------------------------------------------------------
58 // XAppend
59 sdbcx::ObjectType OKeys::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
61 if ( getTable()->isNew() )
63 Reference< XPropertySet > xNewDescriptor( cloneDescriptor( descriptor ) );
64 OKeysHelper::cloneDescriptorColumns( descriptor, xNewDescriptor );
65 return xNewDescriptor;
68 sal_Int32 nKeyType = getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)));
70 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("ALTER TABLE ");
71 const ::rtl::OUString aQuote = getTable()->getConnection()->getMetaData()->getIdentifierQuoteString( );
72 const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
74 aSql += composeTableName( getTable()->getConnection()->getMetaData(), getTable(), ::dbtools::eInTableDefinitions, false, false, true );
76 if(nKeyType == KeyType::PRIMARY)
78 aSql = aSql + ::rtl::OUString::createFromAscii(" ALTER PRIMARY KEY (");
80 else if(nKeyType == KeyType::FOREIGN)
82 aSql = aSql + ::rtl::OUString::createFromAscii(" FOREIGN KEY (");
84 else
85 throw SQLException();
87 Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
88 Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
90 for(sal_Int32 i=0;i<xColumns->getCount();++i)
92 Reference< XPropertySet > xColProp;
93 xColumns->getByIndex(i) >>= xColProp;
94 aSql = aSql + aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote
95 + ::rtl::OUString::createFromAscii(",");
97 aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
99 sal_Int32 nUpdateRule = 0, nDeleteRule = 0;
100 ::rtl::OUString sReferencedName;
102 if(nKeyType == KeyType::FOREIGN)
104 nDeleteRule = getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DELETERULE)));
106 ::rtl::OUString aName,aSchema;
107 sReferencedName = getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REFERENCEDTABLE)));
108 sal_Int32 nLen = sReferencedName.indexOf('.');
109 aSchema = sReferencedName.copy(0,nLen);
110 aName = sReferencedName.copy(nLen+1);
111 aSql += ::rtl::OUString::createFromAscii(" REFERENCES ")
112 + aQuote + aSchema + aQuote + sDot + aQuote + aName + aQuote;
113 aSql += ::rtl::OUString::createFromAscii(" (");
115 for(sal_Int32 i=0;i<xColumns->getCount();++i)
117 Reference< XPropertySet > xColProp;
118 xColumns->getByIndex(i) >>= xColProp;
119 aSql = aSql + aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RELATEDCOLUMN))) + aQuote
120 + ::rtl::OUString::createFromAscii(",");
122 aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
124 switch(nDeleteRule)
126 case KeyRule::CASCADE:
127 aSql += ::rtl::OUString::createFromAscii(" ON DELETE CASCADE ");
128 break;
129 case KeyRule::RESTRICT:
130 aSql += ::rtl::OUString::createFromAscii(" ON DELETE RESTRICT ");
131 break;
132 case KeyRule::SET_NULL:
133 aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET NULL ");
134 break;
135 case KeyRule::SET_DEFAULT:
136 aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET DEFAULT ");
137 break;
138 default:
143 Reference< XStatement > xStmt = getTable()->getConnection()->createStatement( );
144 xStmt->execute(aSql);
145 ::comphelper::disposeComponent(xStmt);
146 // find the name which the database gave the new key
147 ::rtl::OUString sNewName( _rForName );
148 if(nKeyType == KeyType::FOREIGN)
150 const ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
151 ::rtl::OUString aSchema,aTable;
152 getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
153 getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
154 Reference< XResultSet > xResult = getTable()->getMetaData()->getImportedKeys( getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME))
155 ,aSchema
156 ,aTable);
157 if(xResult.is())
159 Reference< XRow > xRow(xResult,UNO_QUERY);
160 while(xResult->next())
162 ::rtl::OUString sName = xRow->getString(12);
163 if ( !m_pElements->exists(sName) ) // this name wasn't inserted yet so it must be te new one
165 descriptor->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sName));
166 sNewName = sName;
167 break;
170 ::comphelper::disposeComponent(xResult);
174 getTable()->addKey(sNewName,sdbcx::TKeyProperties(new sdbcx::KeyProperties(sReferencedName,nKeyType,nUpdateRule,nDeleteRule)));
175 return createObject( sNewName );
177 // -------------------------------------------------------------------------
178 ::rtl::OUString OKeys::getDropForeignKey() const
180 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP FOREIGN KEY "));
183 // -----------------------------------------------------------------------------