1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include "adabas/BKeys.hxx"
27 #include "adabas/BTable.hxx"
28 #include <com/sun/star/sdbc/XRow.hpp>
29 #include <com/sun/star/sdbc/XResultSet.hpp>
30 #include <com/sun/star/sdbcx/KeyType.hpp>
31 #include <com/sun/star/sdbc/KeyRule.hpp>
32 #include <comphelper/types.hxx>
33 #include "adabas/BCatalog.hxx"
34 #include <comphelper/property.hxx>
35 #include <connectivity/TKeys.hxx>
36 #include <connectivity/dbtools.hxx>
37
38
39 using namespace ::comphelper;
40 using namespace connectivity;
41 using namespace connectivity::adabas;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::sdbcx;
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::lang;
48 typedef OKeysHelper OCollection_TYPE;
49
50 // -------------------------------------------------------------------------
51 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)52 sdbcx::ObjectType OKeys::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
53 {
54 if ( getTable()->isNew() )
55 {
56 Reference< XPropertySet > xNewDescriptor( cloneDescriptor( descriptor ) );
57 OKeysHelper::cloneDescriptorColumns( descriptor, xNewDescriptor );
58 return xNewDescriptor;
59 }
60
61 sal_Int32 nKeyType = getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_TYPE)));
62
63 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("ALTER TABLE ");
64 const ::rtl::OUString aQuote = getTable()->getConnection()->getMetaData()->getIdentifierQuoteString( );
65 const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
66
67 aSql += composeTableName( getTable()->getConnection()->getMetaData(), getTable(), ::dbtools::eInTableDefinitions, false, false, true );
68
69 if(nKeyType == KeyType::PRIMARY)
70 {
71 aSql = aSql + ::rtl::OUString::createFromAscii(" ALTER PRIMARY KEY (");
72 }
73 else if(nKeyType == KeyType::FOREIGN)
74 {
75 aSql = aSql + ::rtl::OUString::createFromAscii(" FOREIGN KEY (");
76 }
77 else
78 throw SQLException();
79
80 Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
81 Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
82
83 for(sal_Int32 i=0;i<xColumns->getCount();++i)
84 {
85 Reference< XPropertySet > xColProp;
86 xColumns->getByIndex(i) >>= xColProp;
87 aSql = aSql + aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote
88 + ::rtl::OUString::createFromAscii(",");
89 }
90 aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
91
92 sal_Int32 nUpdateRule = 0, nDeleteRule = 0;
93 ::rtl::OUString sReferencedName;
94
95 if(nKeyType == KeyType::FOREIGN)
96 {
97 nDeleteRule = getINT32(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_DELETERULE)));
98
99 ::rtl::OUString aName,aSchema;
100 sReferencedName = getString(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_REFERENCEDTABLE)));
101 sal_Int32 nLen = sReferencedName.indexOf('.');
102 aSchema = sReferencedName.copy(0,nLen);
103 aName = sReferencedName.copy(nLen+1);
104 aSql += ::rtl::OUString::createFromAscii(" REFERENCES ")
105 + aQuote + aSchema + aQuote + sDot + aQuote + aName + aQuote;
106 aSql += ::rtl::OUString::createFromAscii(" (");
107
108 for(sal_Int32 i=0;i<xColumns->getCount();++i)
109 {
110 Reference< XPropertySet > xColProp;
111 xColumns->getByIndex(i) >>= xColProp;
112 aSql = aSql + aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_RELATEDCOLUMN))) + aQuote
113 + ::rtl::OUString::createFromAscii(",");
114 }
115 aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
116
117 switch(nDeleteRule)
118 {
119 case KeyRule::CASCADE:
120 aSql += ::rtl::OUString::createFromAscii(" ON DELETE CASCADE ");
121 break;
122 case KeyRule::RESTRICT:
123 aSql += ::rtl::OUString::createFromAscii(" ON DELETE RESTRICT ");
124 break;
125 case KeyRule::SET_NULL:
126 aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET NULL ");
127 break;
128 case KeyRule::SET_DEFAULT:
129 aSql += ::rtl::OUString::createFromAscii(" ON DELETE SET DEFAULT ");
130 break;
131 default:
132 ;
133 }
134 }
135
136 Reference< XStatement > xStmt = getTable()->getConnection()->createStatement( );
137 xStmt->execute(aSql);
138 ::comphelper::disposeComponent(xStmt);
139 // find the name which the database gave the new key
140 ::rtl::OUString sNewName( _rForName );
141 if(nKeyType == KeyType::FOREIGN)
142 {
143 const ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
144 ::rtl::OUString aSchema,aTable;
145 getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
146 getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
147 Reference< XResultSet > xResult = getTable()->getMetaData()->getImportedKeys( getTable()->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME))
148 ,aSchema
149 ,aTable);
150 if(xResult.is())
151 {
152 Reference< XRow > xRow(xResult,UNO_QUERY);
153 while(xResult->next())
154 {
155 ::rtl::OUString sName = xRow->getString(12);
156 if ( !m_pElements->exists(sName) ) // this name wasn't inserted yet so it must be te new one
157 {
158 descriptor->setPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME),makeAny(sName));
159 sNewName = sName;
160 break;
161 }
162 }
163 ::comphelper::disposeComponent(xResult);
164 }
165 }
166
167 getTable()->addKey(sNewName,sdbcx::TKeyProperties(new sdbcx::KeyProperties(sReferencedName,nKeyType,nUpdateRule,nDeleteRule)));
168 return createObject( sNewName );
169 }
170 // -------------------------------------------------------------------------
getDropForeignKey() const171 ::rtl::OUString OKeys::getDropForeignKey() const
172 {
173 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" DROP FOREIGN KEY "));
174 }
175
176 // -----------------------------------------------------------------------------
177
178