merge the formfield patch from ooo-build
[ooovba.git] / connectivity / source / commontools / TIndexes.cxx
blobb4ae00b6031631387bc3896564ce4378dddcced1
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: TIndexes.cxx,v $
10 * $Revision: 1.12.56.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include "connectivity/TIndexes.hxx"
34 #include "connectivity/TIndex.hxx"
35 #include <com/sun/star/sdbc/XRow.hpp>
36 #include <com/sun/star/sdbc/XResultSet.hpp>
37 #include <com/sun/star/sdbc/IndexType.hpp>
38 #include <connectivity/dbtools.hxx>
39 #include "connectivity/TTableHelper.hxx"
40 #include "TConnection.hxx"
41 #include <comphelper/extract.hxx>
42 #include <rtl/ustrbuf.hxx>
43 using namespace connectivity;
44 using namespace connectivity::sdbcx;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::sdbcx;
48 using namespace ::com::sun::star::sdbc;
49 using namespace ::com::sun::star::container;
50 using namespace ::com::sun::star::lang;
51 using namespace cppu;
53 typedef connectivity::sdbcx::OCollection OCollection_TYPE;
54 // -----------------------------------------------------------------------------
55 OIndexesHelper::OIndexesHelper(OTableHelper* _pTable,
56 ::osl::Mutex& _rMutex,
57 const ::std::vector< ::rtl::OUString> &_rVector
59 : OCollection(*_pTable,sal_True,_rMutex,_rVector)
60 ,m_pTable(_pTable)
63 // -----------------------------------------------------------------------------
65 sdbcx::ObjectType OIndexesHelper::createObject(const ::rtl::OUString& _rName)
67 Reference< XConnection> xConnection = m_pTable->getConnection();
68 if ( !xConnection.is() )
69 return NULL;
71 sdbcx::ObjectType xRet;
72 ::rtl::OUString aName,aQualifier;
73 sal_Int32 nLen = _rName.indexOf('.');
74 if ( nLen != -1 )
76 aQualifier = _rName.copy(0,nLen);
77 aName = _rName.copy(nLen+1);
79 else
80 aName = _rName;
82 ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
83 ::rtl::OUString aSchema,aTable;
84 m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
85 m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
87 Any aCatalog = m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME));
88 Reference< XResultSet > xResult = m_pTable->getMetaData()->getIndexInfo(aCatalog,aSchema,aTable,sal_False,sal_False);
90 if ( xResult.is() )
92 Reference< XRow > xRow(xResult,UNO_QUERY);
93 while( xResult->next() )
95 sal_Bool bUnique = !xRow->getBoolean(4);
96 if((!aQualifier.getLength() || xRow->getString(5) == aQualifier ) && xRow->getString(6) == aName)
98 sal_Int32 nClustered = xRow->getShort(7);
99 sal_Bool bPrimarKeyIndex = sal_False;
100 xRow.clear();
101 xResult.clear();
104 xResult = m_pTable->getMetaData()->getPrimaryKeys(aCatalog,aSchema,aTable);
105 xRow.set(xResult,UNO_QUERY);
107 if ( xRow.is() && xResult->next() ) // there can be only one primary key
109 bPrimarKeyIndex = xRow->getString(6) == aName;
112 catch(Exception)
115 OIndexHelper* pRet = new OIndexHelper(m_pTable,aName,aQualifier,bUnique,
116 bPrimarKeyIndex,
117 nClustered == IndexType::CLUSTERED);
118 xRet = pRet;
119 break;
124 return xRet;
126 // -------------------------------------------------------------------------
127 void OIndexesHelper::impl_refresh() throw(RuntimeException)
129 m_pTable->refreshIndexes();
131 // -------------------------------------------------------------------------
132 Reference< XPropertySet > OIndexesHelper::createDescriptor()
134 return new OIndexHelper(m_pTable);
136 // -------------------------------------------------------------------------
137 // XAppend
138 sdbcx::ObjectType OIndexesHelper::appendObject( const ::rtl::OUString& _rForName, const Reference< XPropertySet >& descriptor )
140 Reference< XConnection> xConnection = m_pTable->getConnection();
141 if ( !xConnection.is() )
142 return NULL;
143 if ( m_pTable->isNew() )
144 return cloneDescriptor( descriptor );
146 ::dbtools::OPropertyMap& rPropMap = OMetaConnection::getPropMap();
147 ::rtl::OUStringBuffer aSql( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CREATE ")));
148 ::rtl::OUString aQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
149 ::rtl::OUString aDot = ::rtl::OUString::createFromAscii(".");
151 if(comphelper::getBOOL(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISUNIQUE))))
152 aSql.appendAscii("UNIQUE ");
153 aSql.appendAscii("INDEX ");
156 ::rtl::OUString aCatalog,aSchema,aTable;
157 dbtools::qualifiedNameComponents(m_pTable->getMetaData(),m_pTable->getName(),aCatalog,aSchema,aTable,::dbtools::eInDataManipulation);
158 ::rtl::OUString aComposedName;
160 aComposedName = dbtools::composeTableName(m_pTable->getMetaData(),aCatalog,aSchema,aTable,sal_True,::dbtools::eInIndexDefinitions);
161 if ( _rForName.getLength() )
163 aSql.append( ::dbtools::quoteName( aQuote, _rForName ) );
164 aSql.appendAscii(" ON ");
165 aSql.append(aComposedName);
166 aSql.appendAscii(" ( ");
168 Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
169 Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
170 Reference< XPropertySet > xColProp;
171 sal_Bool bAddIndexAppendix = ::dbtools::getBooleanDataSourceSetting( m_pTable->getConnection(), "AddIndexAppendix" );
172 sal_Int32 nCount = xColumns->getCount();
173 for(sal_Int32 i = 0 ; i < nCount; ++i)
175 xColProp.set(xColumns->getByIndex(i),UNO_QUERY);
176 aSql.append(::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
178 if ( bAddIndexAppendix )
181 aSql.appendAscii(any2bool(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISASCENDING)))
183 " ASC"
185 " DESC");
187 aSql.appendAscii(",");
189 aSql.setCharAt(aSql.getLength()-1,')');
191 else
193 aSql.append(aComposedName);
195 Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
196 Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
197 Reference< XPropertySet > xColProp;
198 if(xColumns->getCount() != 1)
199 throw SQLException();
201 xColumns->getByIndex(0) >>= xColProp;
203 aSql.append(aDot);
204 aSql.append(::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
207 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
208 if ( xStmt.is() )
210 ::rtl::OUString sSql = aSql.makeStringAndClear();
211 xStmt->execute(sSql);
212 ::comphelper::disposeComponent(xStmt);
215 return createObject( _rForName );
217 // -------------------------------------------------------------------------
218 // XDrop
219 void OIndexesHelper::dropObject(sal_Int32 /*_nPos*/,const ::rtl::OUString _sElementName)
221 Reference< XConnection> xConnection = m_pTable->getConnection();
222 if( xConnection.is() && !m_pTable->isNew())
224 ::rtl::OUString aName,aSchema;
225 sal_Int32 nLen = _sElementName.indexOf('.');
226 if(nLen != -1)
227 aSchema = _sElementName.copy(0,nLen);
228 aName = _sElementName.copy(nLen+1);
230 ::rtl::OUString aSql = ::rtl::OUString::createFromAscii("DROP INDEX ");
232 ::rtl::OUString aComposedName = dbtools::composeTableName( m_pTable->getMetaData(), m_pTable, ::dbtools::eInIndexDefinitions, false, false, true );
233 ::rtl::OUString sIndexName,sTemp;
234 sIndexName = dbtools::composeTableName( m_pTable->getMetaData(), sTemp, aSchema, aName, sal_True, ::dbtools::eInIndexDefinitions );
236 aSql += sIndexName
237 + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ON "))
238 + aComposedName;
240 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
241 if ( xStmt.is() )
243 xStmt->execute(aSql);
244 ::comphelper::disposeComponent(xStmt);
248 // -----------------------------------------------------------------------------