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