1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "connectivity/TIndexes.hxx"
30 #include "connectivity/TIndex.hxx"
31 #include <com/sun/star/sdbc/XRow.hpp>
32 #include <com/sun/star/sdbc/XResultSet.hpp>
33 #include <com/sun/star/sdbc/IndexType.hpp>
34 #include <connectivity/dbtools.hxx>
35 #include "connectivity/TTableHelper.hxx"
36 #include "TConnection.hxx"
37 #include <comphelper/extract.hxx>
38 #include <rtl/ustrbuf.hxx>
39 using namespace connectivity
;
40 using namespace connectivity::sdbcx
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::beans
;
43 using namespace ::com::sun::star::sdbcx
;
44 using namespace ::com::sun::star::sdbc
;
45 using namespace ::com::sun::star::container
;
46 using namespace ::com::sun::star::lang
;
49 // -----------------------------------------------------------------------------
50 OIndexesHelper::OIndexesHelper(OTableHelper
* _pTable
,
51 ::osl::Mutex
& _rMutex
,
52 const ::std::vector
< ::rtl::OUString
> &_rVector
54 : OCollection(*_pTable
,sal_True
,_rMutex
,_rVector
)
58 // -----------------------------------------------------------------------------
60 sdbcx::ObjectType
OIndexesHelper::createObject(const ::rtl::OUString
& _rName
)
62 Reference
< XConnection
> xConnection
= m_pTable
->getConnection();
63 if ( !xConnection
.is() )
66 sdbcx::ObjectType xRet
;
67 ::rtl::OUString aName
,aQualifier
;
68 sal_Int32 nLen
= _rName
.indexOf('.');
71 aQualifier
= _rName
.copy(0,nLen
);
72 aName
= _rName
.copy(nLen
+1);
77 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
78 ::rtl::OUString aSchema
,aTable
;
79 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_SCHEMANAME
)) >>= aSchema
;
80 m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)) >>= aTable
;
82 Any aCatalog
= m_pTable
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_CATALOGNAME
));
83 Reference
< XResultSet
> xResult
= m_pTable
->getMetaData()->getIndexInfo(aCatalog
,aSchema
,aTable
,sal_False
,sal_False
);
87 Reference
< XRow
> xRow(xResult
,UNO_QUERY
);
88 while( xResult
->next() )
90 sal_Bool bUnique
= !xRow
->getBoolean(4);
91 if((aQualifier
.isEmpty() || xRow
->getString(5) == aQualifier
) && xRow
->getString(6) == aName
)
93 sal_Int32 nClustered
= xRow
->getShort(7);
94 sal_Bool bPrimarKeyIndex
= sal_False
;
99 xResult
= m_pTable
->getMetaData()->getPrimaryKeys(aCatalog
,aSchema
,aTable
);
100 xRow
.set(xResult
,UNO_QUERY
);
102 if ( xRow
.is() && xResult
->next() ) // there can be only one primary key
104 bPrimarKeyIndex
= xRow
->getString(6) == aName
;
107 catch(const Exception
&)
110 OIndexHelper
* pRet
= new OIndexHelper(m_pTable
,aName
,aQualifier
,bUnique
,
112 nClustered
== IndexType::CLUSTERED
);
121 // -------------------------------------------------------------------------
122 void OIndexesHelper::impl_refresh() throw(RuntimeException
)
124 m_pTable
->refreshIndexes();
126 // -------------------------------------------------------------------------
127 Reference
< XPropertySet
> OIndexesHelper::createDescriptor()
129 return new OIndexHelper(m_pTable
);
131 // -------------------------------------------------------------------------
133 sdbcx::ObjectType
OIndexesHelper::appendObject( const ::rtl::OUString
& _rForName
, const Reference
< XPropertySet
>& descriptor
)
135 Reference
< XConnection
> xConnection
= m_pTable
->getConnection();
136 if ( !xConnection
.is() )
138 if ( m_pTable
->isNew() )
139 return cloneDescriptor( descriptor
);
141 if ( m_pTable
->getIndexService().is() )
143 m_pTable
->getIndexService()->addIndex(m_pTable
,descriptor
);
147 ::dbtools::OPropertyMap
& rPropMap
= OMetaConnection::getPropMap();
148 ::rtl::OUStringBuffer
aSql( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CREATE ")));
149 ::rtl::OUString aQuote
= m_pTable
->getMetaData()->getIdentifierQuoteString( );
150 ::rtl::OUString
aDot( RTL_CONSTASCII_USTRINGPARAM( "." ));
152 if(comphelper::getBOOL(descriptor
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_ISUNIQUE
))))
153 aSql
.appendAscii("UNIQUE ");
154 aSql
.appendAscii("INDEX ");
157 ::rtl::OUString aCatalog
,aSchema
,aTable
;
158 dbtools::qualifiedNameComponents(m_pTable
->getMetaData(),m_pTable
->getName(),aCatalog
,aSchema
,aTable
,::dbtools::eInDataManipulation
);
159 ::rtl::OUString aComposedName
;
161 aComposedName
= dbtools::composeTableName(m_pTable
->getMetaData(),aCatalog
,aSchema
,aTable
,sal_True
,::dbtools::eInIndexDefinitions
);
162 if (!_rForName
.isEmpty() )
164 aSql
.append( ::dbtools::quoteName( aQuote
, _rForName
) );
165 aSql
.appendAscii(" ON ");
166 aSql
.append(aComposedName
);
167 aSql
.appendAscii(" ( ");
169 Reference
<XColumnsSupplier
> xColumnSup(descriptor
,UNO_QUERY
);
170 Reference
<XIndexAccess
> xColumns(xColumnSup
->getColumns(),UNO_QUERY
);
171 Reference
< XPropertySet
> xColProp
;
172 sal_Bool bAddIndexAppendix
= ::dbtools::getBooleanDataSourceSetting( m_pTable
->getConnection(), "AddIndexAppendix" );
173 sal_Int32 nCount
= xColumns
->getCount();
174 for(sal_Int32 i
= 0 ; i
< nCount
; ++i
)
176 xColProp
.set(xColumns
->getByIndex(i
),UNO_QUERY
);
177 aSql
.append(::dbtools::quoteName( aQuote
,comphelper::getString(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)))));
179 if ( bAddIndexAppendix
)
182 aSql
.appendAscii(any2bool(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_ISASCENDING
)))
188 aSql
.appendAscii(",");
190 aSql
[aSql
.getLength() - 1] = ')';
194 aSql
.append(aComposedName
);
196 Reference
<XColumnsSupplier
> xColumnSup(descriptor
,UNO_QUERY
);
197 Reference
<XIndexAccess
> xColumns(xColumnSup
->getColumns(),UNO_QUERY
);
198 Reference
< XPropertySet
> xColProp
;
199 if(xColumns
->getCount() != 1)
200 throw SQLException();
202 xColumns
->getByIndex(0) >>= xColProp
;
205 aSql
.append(::dbtools::quoteName( aQuote
,comphelper::getString(xColProp
->getPropertyValue(rPropMap
.getNameByIndex(PROPERTY_ID_NAME
)))));
208 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
211 ::rtl::OUString sSql
= aSql
.makeStringAndClear();
212 xStmt
->execute(sSql
);
213 ::comphelper::disposeComponent(xStmt
);
217 return createObject( _rForName
);
219 // -------------------------------------------------------------------------
221 void OIndexesHelper::dropObject(sal_Int32
/*_nPos*/,const ::rtl::OUString _sElementName
)
223 Reference
< XConnection
> xConnection
= m_pTable
->getConnection();
224 if( xConnection
.is() && !m_pTable
->isNew())
226 if ( m_pTable
->getIndexService().is() )
228 m_pTable
->getIndexService()->dropIndex(m_pTable
,_sElementName
);
232 ::rtl::OUString aName
,aSchema
;
233 sal_Int32 nLen
= _sElementName
.indexOf('.');
235 aSchema
= _sElementName
.copy(0,nLen
);
236 aName
= _sElementName
.copy(nLen
+1);
238 ::rtl::OUString
aSql( RTL_CONSTASCII_USTRINGPARAM( "DROP INDEX " ));
240 ::rtl::OUString aComposedName
= dbtools::composeTableName( m_pTable
->getMetaData(), m_pTable
, ::dbtools::eInIndexDefinitions
, false, false, true );
241 ::rtl::OUString sIndexName
,sTemp
;
242 sIndexName
= dbtools::composeTableName( m_pTable
->getMetaData(), sTemp
, aSchema
, aName
, sal_True
, ::dbtools::eInIndexDefinitions
);
245 + ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(" ON "))
248 Reference
< XStatement
> xStmt
= m_pTable
->getConnection()->createStatement( );
251 xStmt
->execute(aSql
);
252 ::comphelper::disposeComponent(xStmt
);
257 // -----------------------------------------------------------------------------
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */