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 "connectivity/TIndexes.hxx"
27cdf0e10cSrcweir #include "connectivity/TIndex.hxx"
28cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
29cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
30cdf0e10cSrcweir #include <com/sun/star/sdbc/IndexType.hpp>
31cdf0e10cSrcweir #include <connectivity/dbtools.hxx>
32cdf0e10cSrcweir #include "connectivity/TTableHelper.hxx"
33cdf0e10cSrcweir #include "TConnection.hxx"
34cdf0e10cSrcweir #include <comphelper/extract.hxx>
35cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
36cdf0e10cSrcweir using namespace connectivity;
37cdf0e10cSrcweir using namespace connectivity::sdbcx;
38cdf0e10cSrcweir using namespace ::com::sun::star::uno;
39cdf0e10cSrcweir using namespace ::com::sun::star::beans;
40cdf0e10cSrcweir using namespace ::com::sun::star::sdbcx;
41cdf0e10cSrcweir using namespace ::com::sun::star::sdbc;
42cdf0e10cSrcweir using namespace ::com::sun::star::container;
43cdf0e10cSrcweir using namespace ::com::sun::star::lang;
44cdf0e10cSrcweir using namespace cppu;
45cdf0e10cSrcweir
46cdf0e10cSrcweir typedef connectivity::sdbcx::OCollection OCollection_TYPE;
47cdf0e10cSrcweir // -----------------------------------------------------------------------------
OIndexesHelper(OTableHelper * _pTable,::osl::Mutex & _rMutex,const::std::vector<::rtl::OUString> & _rVector)48cdf0e10cSrcweir OIndexesHelper::OIndexesHelper(OTableHelper* _pTable,
49cdf0e10cSrcweir ::osl::Mutex& _rMutex,
50cdf0e10cSrcweir const ::std::vector< ::rtl::OUString> &_rVector
51cdf0e10cSrcweir )
52cdf0e10cSrcweir : OCollection(*_pTable,sal_True,_rMutex,_rVector)
53cdf0e10cSrcweir ,m_pTable(_pTable)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir }
56cdf0e10cSrcweir // -----------------------------------------------------------------------------
57cdf0e10cSrcweir
createObject(const::rtl::OUString & _rName)58cdf0e10cSrcweir sdbcx::ObjectType OIndexesHelper::createObject(const ::rtl::OUString& _rName)
59cdf0e10cSrcweir {
60cdf0e10cSrcweir Reference< XConnection> xConnection = m_pTable->getConnection();
61cdf0e10cSrcweir if ( !xConnection.is() )
62cdf0e10cSrcweir return NULL;
63cdf0e10cSrcweir
64cdf0e10cSrcweir sdbcx::ObjectType xRet;
65cdf0e10cSrcweir ::rtl::OUString aName,aQualifier;
66cdf0e10cSrcweir sal_Int32 nLen = _rName.indexOf('.');
67cdf0e10cSrcweir if ( nLen != -1 )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir aQualifier = _rName.copy(0,nLen);
70cdf0e10cSrcweir aName = _rName.copy(nLen+1);
71cdf0e10cSrcweir }
72cdf0e10cSrcweir else
73cdf0e10cSrcweir aName = _rName;
74cdf0e10cSrcweir
75cdf0e10cSrcweir ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
76cdf0e10cSrcweir ::rtl::OUString aSchema,aTable;
77cdf0e10cSrcweir m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
78cdf0e10cSrcweir m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
79cdf0e10cSrcweir
80cdf0e10cSrcweir Any aCatalog = m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME));
81cdf0e10cSrcweir Reference< XResultSet > xResult = m_pTable->getMetaData()->getIndexInfo(aCatalog,aSchema,aTable,sal_False,sal_False);
82cdf0e10cSrcweir
83cdf0e10cSrcweir if ( xResult.is() )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir Reference< XRow > xRow(xResult,UNO_QUERY);
86cdf0e10cSrcweir while( xResult->next() )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir sal_Bool bUnique = !xRow->getBoolean(4);
89cdf0e10cSrcweir if((!aQualifier.getLength() || xRow->getString(5) == aQualifier ) && xRow->getString(6) == aName)
90cdf0e10cSrcweir {
91cdf0e10cSrcweir sal_Int32 nClustered = xRow->getShort(7);
92cdf0e10cSrcweir sal_Bool bPrimarKeyIndex = sal_False;
93cdf0e10cSrcweir xRow.clear();
94cdf0e10cSrcweir xResult.clear();
95cdf0e10cSrcweir try
96cdf0e10cSrcweir {
97cdf0e10cSrcweir xResult = m_pTable->getMetaData()->getPrimaryKeys(aCatalog,aSchema,aTable);
98cdf0e10cSrcweir xRow.set(xResult,UNO_QUERY);
99cdf0e10cSrcweir
100cdf0e10cSrcweir if ( xRow.is() && xResult->next() ) // there can be only one primary key
101cdf0e10cSrcweir {
102cdf0e10cSrcweir bPrimarKeyIndex = xRow->getString(6) == aName;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir }
105cdf0e10cSrcweir catch(Exception)
106cdf0e10cSrcweir {
107cdf0e10cSrcweir }
108cdf0e10cSrcweir OIndexHelper* pRet = new OIndexHelper(m_pTable,aName,aQualifier,bUnique,
109cdf0e10cSrcweir bPrimarKeyIndex,
110cdf0e10cSrcweir nClustered == IndexType::CLUSTERED);
111cdf0e10cSrcweir xRet = pRet;
112cdf0e10cSrcweir break;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir }
115cdf0e10cSrcweir }
116cdf0e10cSrcweir
117cdf0e10cSrcweir return xRet;
118cdf0e10cSrcweir }
119cdf0e10cSrcweir // -------------------------------------------------------------------------
impl_refresh()120cdf0e10cSrcweir void OIndexesHelper::impl_refresh() throw(RuntimeException)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir m_pTable->refreshIndexes();
123cdf0e10cSrcweir }
124cdf0e10cSrcweir // -------------------------------------------------------------------------
createDescriptor()125cdf0e10cSrcweir Reference< XPropertySet > OIndexesHelper::createDescriptor()
126cdf0e10cSrcweir {
127cdf0e10cSrcweir return new OIndexHelper(m_pTable);
128cdf0e10cSrcweir }
129cdf0e10cSrcweir // -------------------------------------------------------------------------
130cdf0e10cSrcweir // XAppend
appendObject(const::rtl::OUString & _rForName,const Reference<XPropertySet> & descriptor)131cdf0e10cSrcweir sdbcx::ObjectType OIndexesHelper::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir Reference< XConnection> xConnection = m_pTable->getConnection();
134cdf0e10cSrcweir if ( !xConnection.is() )
135cdf0e10cSrcweir return NULL;
136cdf0e10cSrcweir if ( m_pTable->isNew() )
137cdf0e10cSrcweir return cloneDescriptor( descriptor );
138cdf0e10cSrcweir
139cdf0e10cSrcweir if ( m_pTable->getIndexService().is() )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir m_pTable->getIndexService()->addIndex(m_pTable,descriptor);
142cdf0e10cSrcweir }
143cdf0e10cSrcweir else
144cdf0e10cSrcweir {
145cdf0e10cSrcweir ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
146cdf0e10cSrcweir ::rtl::OUStringBuffer aSql( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CREATE ")));
147cdf0e10cSrcweir ::rtl::OUString aQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
148cdf0e10cSrcweir ::rtl::OUString aDot = ::rtl::OUString::createFromAscii(".");
149cdf0e10cSrcweir
150cdf0e10cSrcweir if(comphelper::getBOOL(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISUNIQUE))))
151cdf0e10cSrcweir aSql.appendAscii("UNIQUE ");
152cdf0e10cSrcweir aSql.appendAscii("INDEX ");
153cdf0e10cSrcweir
154cdf0e10cSrcweir
155cdf0e10cSrcweir ::rtl::OUString aCatalog,aSchema,aTable;
156cdf0e10cSrcweir dbtools::qualifiedNameComponents(m_pTable->getMetaData(),m_pTable->getName(),aCatalog,aSchema,aTable,::dbtools::eInDataManipulation);
157cdf0e10cSrcweir ::rtl::OUString aComposedName;
158cdf0e10cSrcweir
159cdf0e10cSrcweir aComposedName = dbtools::composeTableName(m_pTable->getMetaData(),aCatalog,aSchema,aTable,sal_True,::dbtools::eInIndexDefinitions);
160cdf0e10cSrcweir if ( _rForName.getLength() )
161cdf0e10cSrcweir {
162cdf0e10cSrcweir aSql.append( ::dbtools::quoteName( aQuote, _rForName ) );
163cdf0e10cSrcweir aSql.appendAscii(" ON ");
164cdf0e10cSrcweir aSql.append(aComposedName);
165cdf0e10cSrcweir aSql.appendAscii(" ( ");
166cdf0e10cSrcweir
167cdf0e10cSrcweir Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
168cdf0e10cSrcweir Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
169cdf0e10cSrcweir Reference< XPropertySet > xColProp;
170cdf0e10cSrcweir sal_Bool bAddIndexAppendix = ::dbtools::getBooleanDataSourceSetting( m_pTable->getConnection(), "AddIndexAppendix" );
171cdf0e10cSrcweir sal_Int32 nCount = xColumns->getCount();
172cdf0e10cSrcweir for(sal_Int32 i = 0 ; i < nCount; ++i)
173cdf0e10cSrcweir {
174cdf0e10cSrcweir xColProp.set(xColumns->getByIndex(i),UNO_QUERY);
175cdf0e10cSrcweir aSql.append(::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
176cdf0e10cSrcweir
177cdf0e10cSrcweir if ( bAddIndexAppendix )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir
180cdf0e10cSrcweir aSql.appendAscii(any2bool(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISASCENDING)))
181cdf0e10cSrcweir ?
182cdf0e10cSrcweir " ASC"
183cdf0e10cSrcweir :
184cdf0e10cSrcweir " DESC");
185cdf0e10cSrcweir }
186cdf0e10cSrcweir aSql.appendAscii(",");
187cdf0e10cSrcweir }
188cdf0e10cSrcweir aSql.setCharAt(aSql.getLength()-1,')');
189cdf0e10cSrcweir }
190cdf0e10cSrcweir else
191cdf0e10cSrcweir {
192cdf0e10cSrcweir aSql.append(aComposedName);
193cdf0e10cSrcweir
194cdf0e10cSrcweir Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
195cdf0e10cSrcweir Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
196cdf0e10cSrcweir Reference< XPropertySet > xColProp;
197cdf0e10cSrcweir if(xColumns->getCount() != 1)
198cdf0e10cSrcweir throw SQLException();
199cdf0e10cSrcweir
200cdf0e10cSrcweir xColumns->getByIndex(0) >>= xColProp;
201cdf0e10cSrcweir
202cdf0e10cSrcweir aSql.append(aDot);
203cdf0e10cSrcweir aSql.append(::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
204cdf0e10cSrcweir }
205cdf0e10cSrcweir
206cdf0e10cSrcweir Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
207cdf0e10cSrcweir if ( xStmt.is() )
208cdf0e10cSrcweir {
209cdf0e10cSrcweir ::rtl::OUString sSql = aSql.makeStringAndClear();
210cdf0e10cSrcweir xStmt->execute(sSql);
211cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt);
212cdf0e10cSrcweir }
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
215cdf0e10cSrcweir return createObject( _rForName );
216cdf0e10cSrcweir }
217cdf0e10cSrcweir // -------------------------------------------------------------------------
218cdf0e10cSrcweir // XDrop
dropObject(sal_Int32,const::rtl::OUString _sElementName)219cdf0e10cSrcweir void OIndexesHelper::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
220cdf0e10cSrcweir {
221cdf0e10cSrcweir Reference< XConnection> xConnection = m_pTable->getConnection();
222cdf0e10cSrcweir if( xConnection.is() && !m_pTable->isNew())
223cdf0e10cSrcweir {
224cdf0e10cSrcweir if ( m_pTable->getIndexService().is() )
225cdf0e10cSrcweir {
226cdf0e10cSrcweir m_pTable->getIndexService()->dropIndex(m_pTable,_sElementName);
227cdf0e10cSrcweir }
228cdf0e10cSrcweir else
229cdf0e10cSrcweir {
230cdf0e10cSrcweir ::rtl::OUString aName,aSchema;
231cdf0e10cSrcweir sal_Int32 nLen = _sElementName.indexOf('.');
232cdf0e10cSrcweir if(nLen != -1)
233cdf0e10cSrcweir aSchema = _sElementName.copy(0,nLen);
234cdf0e10cSrcweir aName = _sElementName.copy(nLen+1);
235cdf0e10cSrcweir
236cdf0e10cSrcweir ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP INDEX ");
237cdf0e10cSrcweir
238cdf0e10cSrcweir ::rtl::OUString aComposedName = dbtools::composeTableName( m_pTable->getMetaData(), m_pTable, ::dbtools::eInIndexDefinitions, false, false, true );
239cdf0e10cSrcweir ::rtl::OUString sIndexName,sTemp;
240cdf0e10cSrcweir sIndexName = dbtools::composeTableName( m_pTable->getMetaData(), sTemp, aSchema, aName, sal_True, ::dbtools::eInIndexDefinitions );
241cdf0e10cSrcweir
242cdf0e10cSrcweir aSql += sIndexName
243cdf0e10cSrcweir + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ON "))
244cdf0e10cSrcweir + aComposedName;
245cdf0e10cSrcweir
246cdf0e10cSrcweir Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
247cdf0e10cSrcweir if ( xStmt.is() )
248cdf0e10cSrcweir {
249cdf0e10cSrcweir xStmt->execute(aSql);
250cdf0e10cSrcweir ::comphelper::disposeComponent(xStmt);
251cdf0e10cSrcweir }
252cdf0e10cSrcweir }
253cdf0e10cSrcweir }
254cdf0e10cSrcweir }
255cdf0e10cSrcweir // -----------------------------------------------------------------------------
256cdf0e10cSrcweir
257cdf0e10cSrcweir
258cdf0e10cSrcweir
259