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