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