xref: /AOO41X/main/connectivity/source/drivers/adabas/BIndexes.cxx (revision 9b5730f6ddef7eb82608ca4d31dc0d7678e652cf)
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/BIndexes.hxx"
27 #include "adabas/BIndex.hxx"
28 #include "adabas/BTable.hxx"
29 #include "connectivity/sdbcx/VIndex.hxx"
30 #include <com/sun/star/sdbc/XRow.hpp>
31 #include <com/sun/star/sdbc/XResultSet.hpp>
32 #include <com/sun/star/sdbc/IndexType.hpp>
33 #include <comphelper/types.hxx>
34 #include <comphelper/types.hxx>
35 #include "adabas/BCatalog.hxx"
36 #include "connectivity/dbexception.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 connectivity::sdbcx::OCollection OCollection_TYPE;
49 
createObject(const::rtl::OUString & _rName)50 sdbcx::ObjectType OIndexes::createObject(const ::rtl::OUString& _rName)
51 {
52     ::rtl::OUString aName,aQualifier;
53     sal_Int32 nLen = _rName.indexOf('.');
54     if(nLen != -1)
55     {
56         aQualifier  = _rName.copy(0,nLen);
57         aName       = _rName.copy(nLen+1);
58     }
59     else
60         aName       = _rName;
61 
62 
63     Reference< XResultSet > xResult = m_pTable->getMetaData()->getIndexInfo(Any(),
64         m_pTable->getSchema(),m_pTable->getTableName(),sal_False,sal_False);
65 
66     sdbcx::ObjectType xRet = NULL;
67     if(xResult.is())
68     {
69         Reference< XRow > xRow(xResult,UNO_QUERY);
70         while(xResult->next())
71         {
72             if(xRow->getString(6) == aName && (!aQualifier.getLength() || xRow->getString(5) == aQualifier ))
73             {
74                 OAdabasIndex* pRet = new OAdabasIndex(m_pTable,aName,aQualifier,!xRow->getBoolean(4),
75                     aName == ::rtl::OUString::createFromAscii("SYSPRIMARYKEYINDEX"),
76                     xRow->getShort(7) == IndexType::CLUSTERED);
77                 xRet = pRet;
78                 break;
79             }
80         }
81         ::comphelper::disposeComponent(xResult);
82     }
83 
84     return xRet;
85 }
86 // -------------------------------------------------------------------------
impl_refresh()87 void OIndexes::impl_refresh() throw(RuntimeException)
88 {
89     m_pTable->refreshIndexes();
90 }
91 // -------------------------------------------------------------------------
createDescriptor()92 Reference< XPropertySet > OIndexes::createDescriptor()
93 {
94     return new OAdabasIndex(m_pTable);
95 }
96 // -------------------------------------------------------------------------
97 // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)98 sdbcx::ObjectType OIndexes::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
99 {
100     if ( m_pTable->isNew() )
101         ::dbtools::throwFunctionSequenceException(static_cast<XTypeProvider*>(this));
102 
103     ::rtl::OUString aSql    = ::rtl::OUString::createFromAscii("CREATE ");
104     ::rtl::OUString aQuote  = m_pTable->getMetaData()->getIdentifierQuoteString(  );
105     const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
106 
107     if(getBOOL(descriptor->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISUNIQUE))))
108         aSql = aSql + ::rtl::OUString::createFromAscii("UNIQUE ");
109     aSql = aSql + ::rtl::OUString::createFromAscii("INDEX ");
110 
111 
112     if(_rForName.getLength())
113     {
114         aSql = aSql + aQuote + _rForName + aQuote
115                     + ::rtl::OUString::createFromAscii(" ON ")
116                     + aQuote + m_pTable->getSchema() + aQuote + sDot
117                     + aQuote + m_pTable->getTableName() + aQuote
118                     + ::rtl::OUString::createFromAscii(" ( ");
119 
120         Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
121         Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
122         Reference< XPropertySet > xColProp;
123         sal_Int32 nCount = xColumns->getCount();
124         for(sal_Int32 i=0;i<nCount;++i)
125         {
126             xColumns->getByIndex(i) >>= xColProp;
127             aSql = aSql + aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote;
128             aSql = aSql +   (getBOOL(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_ISASCENDING)))
129                                         ?
130                             ::rtl::OUString::createFromAscii(" ASC")
131                                         :
132                             ::rtl::OUString::createFromAscii(" DESC"))
133                         +   ::rtl::OUString::createFromAscii(",");
134         }
135         aSql = aSql.replaceAt(aSql.getLength()-1,1,::rtl::OUString::createFromAscii(")"));
136     }
137     else
138     {
139         aSql = aSql + aQuote + m_pTable->getSchema() + aQuote + sDot + aQuote + m_pTable->getTableName() + aQuote;
140 
141         Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
142         Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
143         Reference< XPropertySet > xColProp;
144         if(xColumns->getCount() != 1)
145             throw SQLException();
146 
147         xColumns->getByIndex(0) >>= xColProp;
148 
149         aSql = aSql + sDot + aQuote + getString(xColProp->getPropertyValue(OMetaConnection::getPropMap().getNameByIndex(PROPERTY_ID_NAME))) + aQuote;
150     }
151 
152     Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement(  );
153     xStmt->execute(aSql);
154     ::comphelper::disposeComponent(xStmt);
155 
156     return createObject( _rForName );
157 }
158 // -------------------------------------------------------------------------
159 // XDrop
dropObject(sal_Int32,const::rtl::OUString _sElementName)160 void OIndexes::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
161 {
162     if(!m_pTable->isNew())
163     {
164         ::rtl::OUString aName,aSchema;
165         sal_Int32 nLen = _sElementName.indexOf('.');
166         aSchema = _sElementName.copy(0,nLen);
167         aName   = _sElementName.copy(nLen+1);
168 
169         ::rtl::OUString aSql    = ::rtl::OUString::createFromAscii("DROP INDEX ");
170         ::rtl::OUString aQuote  = m_pTable->getMetaData()->getIdentifierQuoteString(  );
171         const ::rtl::OUString& sDot = OAdabasCatalog::getDot();
172 
173         if (aSchema.getLength())
174             (((aSql += aQuote) += aSchema) += aQuote) += sDot;
175 
176         (((aSql += aQuote) += aName) += aQuote) += ::rtl::OUString::createFromAscii(" ON ");
177 
178         (((aSql += aQuote) += m_pTable->getSchema()) += aQuote) += sDot;
179         ((aSql += aQuote) += m_pTable->getTableName()) += aQuote;
180 
181         Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement(  );
182         xStmt->execute(aSql);
183         ::comphelper::disposeComponent(xStmt);
184     }
185 }
186 // -------------------------------------------------------------------------
187 
188 
189