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