1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
30*cdf0e10cSrcweir #include "connectivity/TIndexes.hxx"
31*cdf0e10cSrcweir #include "connectivity/TIndex.hxx"
32*cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/sdbc/IndexType.hpp>
35*cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
36*cdf0e10cSrcweir #include "connectivity/TTableHelper.hxx"
37*cdf0e10cSrcweir #include "TConnection.hxx"
38*cdf0e10cSrcweir #include <comphelper/extract.hxx>
39*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40*cdf0e10cSrcweir using namespace connectivity;
41*cdf0e10cSrcweir using namespace connectivity::sdbcx;
42*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
43*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
44*cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
45*cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
46*cdf0e10cSrcweir using namespace ::com::sun::star::container;
47*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
48*cdf0e10cSrcweir using namespace cppu;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir typedef connectivity::sdbcx::OCollection OCollection_TYPE;
51*cdf0e10cSrcweir // -----------------------------------------------------------------------------
52*cdf0e10cSrcweir OIndexesHelper::OIndexesHelper(OTableHelper* _pTable,
53*cdf0e10cSrcweir 				 ::osl::Mutex& _rMutex,
54*cdf0e10cSrcweir 			 const ::std::vector< ::rtl::OUString> &_rVector
55*cdf0e10cSrcweir 			 )
56*cdf0e10cSrcweir 	: OCollection(*_pTable,sal_True,_rMutex,_rVector)
57*cdf0e10cSrcweir 	,m_pTable(_pTable)
58*cdf0e10cSrcweir {
59*cdf0e10cSrcweir }
60*cdf0e10cSrcweir // -----------------------------------------------------------------------------
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir sdbcx::ObjectType OIndexesHelper::createObject(const ::rtl::OUString& _rName)
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir     Reference< XConnection> xConnection = m_pTable->getConnection();
65*cdf0e10cSrcweir     if ( !xConnection.is() )
66*cdf0e10cSrcweir         return NULL;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 	sdbcx::ObjectType xRet;
69*cdf0e10cSrcweir 	::rtl::OUString aName,aQualifier;
70*cdf0e10cSrcweir 	sal_Int32 nLen = _rName.indexOf('.');
71*cdf0e10cSrcweir 	if ( nLen != -1 )
72*cdf0e10cSrcweir 	{
73*cdf0e10cSrcweir 		aQualifier	= _rName.copy(0,nLen);
74*cdf0e10cSrcweir 		aName		= _rName.copy(nLen+1);
75*cdf0e10cSrcweir 	}
76*cdf0e10cSrcweir 	else
77*cdf0e10cSrcweir 		aName		= _rName;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
80*cdf0e10cSrcweir 	::rtl::OUString aSchema,aTable;
81*cdf0e10cSrcweir 	m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME))	>>= aSchema;
82*cdf0e10cSrcweir 	m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME))		>>= aTable;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     Any aCatalog = m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME));
85*cdf0e10cSrcweir 	Reference< XResultSet > xResult = m_pTable->getMetaData()->getIndexInfo(aCatalog,aSchema,aTable,sal_False,sal_False);
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	if ( xResult.is() )
88*cdf0e10cSrcweir 	{
89*cdf0e10cSrcweir 		Reference< XRow > xRow(xResult,UNO_QUERY);
90*cdf0e10cSrcweir 		while( xResult->next() )
91*cdf0e10cSrcweir 		{
92*cdf0e10cSrcweir 			sal_Bool bUnique = !xRow->getBoolean(4);
93*cdf0e10cSrcweir 			if((!aQualifier.getLength() || xRow->getString(5) == aQualifier ) && xRow->getString(6) == aName)
94*cdf0e10cSrcweir 			{
95*cdf0e10cSrcweir                 sal_Int32 nClustered = xRow->getShort(7);
96*cdf0e10cSrcweir                 sal_Bool bPrimarKeyIndex = sal_False;
97*cdf0e10cSrcweir                 xRow.clear();
98*cdf0e10cSrcweir                 xResult.clear();
99*cdf0e10cSrcweir                 try
100*cdf0e10cSrcweir                 {
101*cdf0e10cSrcweir                     xResult = m_pTable->getMetaData()->getPrimaryKeys(aCatalog,aSchema,aTable);
102*cdf0e10cSrcweir                     xRow.set(xResult,UNO_QUERY);
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	                if ( xRow.is() && xResult->next() ) // there can be only one primary key
105*cdf0e10cSrcweir 	                {
106*cdf0e10cSrcweir 		                bPrimarKeyIndex = xRow->getString(6) == aName;
107*cdf0e10cSrcweir                     }
108*cdf0e10cSrcweir                 }
109*cdf0e10cSrcweir                 catch(Exception)
110*cdf0e10cSrcweir                 {
111*cdf0e10cSrcweir                 }
112*cdf0e10cSrcweir 				OIndexHelper* pRet = new OIndexHelper(m_pTable,aName,aQualifier,bUnique,
113*cdf0e10cSrcweir 					bPrimarKeyIndex,
114*cdf0e10cSrcweir 					nClustered == IndexType::CLUSTERED);
115*cdf0e10cSrcweir 				xRet = pRet;
116*cdf0e10cSrcweir 				break;
117*cdf0e10cSrcweir 			}
118*cdf0e10cSrcweir 		}
119*cdf0e10cSrcweir 	}
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	return xRet;
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir // -------------------------------------------------------------------------
124*cdf0e10cSrcweir void OIndexesHelper::impl_refresh() throw(RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	m_pTable->refreshIndexes();
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir // -------------------------------------------------------------------------
129*cdf0e10cSrcweir Reference< XPropertySet > OIndexesHelper::createDescriptor()
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	return new OIndexHelper(m_pTable);
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir // -------------------------------------------------------------------------
134*cdf0e10cSrcweir // XAppend
135*cdf0e10cSrcweir sdbcx::ObjectType OIndexesHelper::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     Reference< XConnection> xConnection = m_pTable->getConnection();
138*cdf0e10cSrcweir     if ( !xConnection.is() )
139*cdf0e10cSrcweir         return NULL;
140*cdf0e10cSrcweir 	if ( m_pTable->isNew() )
141*cdf0e10cSrcweir         return cloneDescriptor( descriptor );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir     if ( m_pTable->getIndexService().is() )
144*cdf0e10cSrcweir     {
145*cdf0e10cSrcweir         m_pTable->getIndexService()->addIndex(m_pTable,descriptor);
146*cdf0e10cSrcweir     }
147*cdf0e10cSrcweir     else
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir 	    ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
150*cdf0e10cSrcweir         ::rtl::OUStringBuffer aSql( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CREATE ")));
151*cdf0e10cSrcweir 	    ::rtl::OUString aQuote	= m_pTable->getMetaData()->getIdentifierQuoteString(  );
152*cdf0e10cSrcweir 	    ::rtl::OUString aDot	= ::rtl::OUString::createFromAscii(".");
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	    if(comphelper::getBOOL(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISUNIQUE))))
155*cdf0e10cSrcweir 		    aSql.appendAscii("UNIQUE ");
156*cdf0e10cSrcweir 	    aSql.appendAscii("INDEX ");
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 	    ::rtl::OUString aCatalog,aSchema,aTable;
160*cdf0e10cSrcweir 	    dbtools::qualifiedNameComponents(m_pTable->getMetaData(),m_pTable->getName(),aCatalog,aSchema,aTable,::dbtools::eInDataManipulation);
161*cdf0e10cSrcweir 	    ::rtl::OUString aComposedName;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	    aComposedName = dbtools::composeTableName(m_pTable->getMetaData(),aCatalog,aSchema,aTable,sal_True,::dbtools::eInIndexDefinitions);
164*cdf0e10cSrcweir 	    if ( _rForName.getLength() )
165*cdf0e10cSrcweir 	    {
166*cdf0e10cSrcweir 		    aSql.append( ::dbtools::quoteName( aQuote, _rForName ) );
167*cdf0e10cSrcweir             aSql.appendAscii(" ON ");
168*cdf0e10cSrcweir 		    aSql.append(aComposedName);
169*cdf0e10cSrcweir 		    aSql.appendAscii(" ( ");
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 		    Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
172*cdf0e10cSrcweir 		    Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
173*cdf0e10cSrcweir 		    Reference< XPropertySet > xColProp;
174*cdf0e10cSrcweir             sal_Bool bAddIndexAppendix = ::dbtools::getBooleanDataSourceSetting( m_pTable->getConnection(), "AddIndexAppendix" );
175*cdf0e10cSrcweir             sal_Int32 nCount = xColumns->getCount();
176*cdf0e10cSrcweir 		    for(sal_Int32 i = 0 ; i < nCount; ++i)
177*cdf0e10cSrcweir 		    {
178*cdf0e10cSrcweir                 xColProp.set(xColumns->getByIndex(i),UNO_QUERY);
179*cdf0e10cSrcweir 			    aSql.append(::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 			    if ( bAddIndexAppendix )
182*cdf0e10cSrcweir 			    {
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 				    aSql.appendAscii(any2bool(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISASCENDING)))
185*cdf0e10cSrcweir 											    ?
186*cdf0e10cSrcweir 								    " ASC"
187*cdf0e10cSrcweir 											    :
188*cdf0e10cSrcweir 								    " DESC");
189*cdf0e10cSrcweir 			    }
190*cdf0e10cSrcweir 			    aSql.appendAscii(",");
191*cdf0e10cSrcweir 		    }
192*cdf0e10cSrcweir 		    aSql.setCharAt(aSql.getLength()-1,')');
193*cdf0e10cSrcweir 	    }
194*cdf0e10cSrcweir 	    else
195*cdf0e10cSrcweir 	    {
196*cdf0e10cSrcweir 		    aSql.append(aComposedName);
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 		    Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
199*cdf0e10cSrcweir 		    Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
200*cdf0e10cSrcweir 		    Reference< XPropertySet > xColProp;
201*cdf0e10cSrcweir 		    if(xColumns->getCount() != 1)
202*cdf0e10cSrcweir 			    throw SQLException();
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 		    xColumns->getByIndex(0) >>= xColProp;
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 		    aSql.append(aDot);
207*cdf0e10cSrcweir             aSql.append(::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
208*cdf0e10cSrcweir 	    }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 	    Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement(  );
211*cdf0e10cSrcweir 	    if ( xStmt.is() )
212*cdf0e10cSrcweir 	    {
213*cdf0e10cSrcweir             ::rtl::OUString sSql = aSql.makeStringAndClear();
214*cdf0e10cSrcweir 		    xStmt->execute(sSql);
215*cdf0e10cSrcweir 		    ::comphelper::disposeComponent(xStmt);
216*cdf0e10cSrcweir 	    }
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir     return createObject( _rForName );
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir // -------------------------------------------------------------------------
222*cdf0e10cSrcweir // XDrop
223*cdf0e10cSrcweir void OIndexesHelper::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir     Reference< XConnection> xConnection = m_pTable->getConnection();
226*cdf0e10cSrcweir 	if( xConnection.is() && !m_pTable->isNew())
227*cdf0e10cSrcweir 	{
228*cdf0e10cSrcweir         if ( m_pTable->getIndexService().is() )
229*cdf0e10cSrcweir         {
230*cdf0e10cSrcweir             m_pTable->getIndexService()->dropIndex(m_pTable,_sElementName);
231*cdf0e10cSrcweir         }
232*cdf0e10cSrcweir         else
233*cdf0e10cSrcweir         {
234*cdf0e10cSrcweir 		    ::rtl::OUString aName,aSchema;
235*cdf0e10cSrcweir 		    sal_Int32 nLen = _sElementName.indexOf('.');
236*cdf0e10cSrcweir 		    if(nLen != -1)
237*cdf0e10cSrcweir 			    aSchema = _sElementName.copy(0,nLen);
238*cdf0e10cSrcweir 		    aName	= _sElementName.copy(nLen+1);
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 		    ::rtl::OUString aSql	= ::rtl::OUString::createFromAscii("DROP INDEX ");
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 		    ::rtl::OUString aComposedName = dbtools::composeTableName( m_pTable->getMetaData(), m_pTable, ::dbtools::eInIndexDefinitions, false, false, true );
243*cdf0e10cSrcweir 		    ::rtl::OUString sIndexName,sTemp;
244*cdf0e10cSrcweir 		    sIndexName = dbtools::composeTableName( m_pTable->getMetaData(), sTemp, aSchema, aName, sal_True, ::dbtools::eInIndexDefinitions );
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir 		    aSql += sIndexName
247*cdf0e10cSrcweir 				    + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ON "))
248*cdf0e10cSrcweir 					    + aComposedName;
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 		    Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement(  );
251*cdf0e10cSrcweir 		    if ( xStmt.is() )
252*cdf0e10cSrcweir 		    {
253*cdf0e10cSrcweir 			    xStmt->execute(aSql);
254*cdf0e10cSrcweir 			    ::comphelper::disposeComponent(xStmt);
255*cdf0e10cSrcweir 		    }
256*cdf0e10cSrcweir         }
257*cdf0e10cSrcweir 	}
258*cdf0e10cSrcweir }
259*cdf0e10cSrcweir // -----------------------------------------------------------------------------
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 
263