LibreOffice Module connectivity (master) 1
TIndexes.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21#include <TIndex.hxx>
23#include <com/sun/star/sdb/tools/XIndexAlteration.hpp>
24#include <com/sun/star/sdbc/XRow.hpp>
25#include <com/sun/star/sdbc/XResultSet.hpp>
26#include <com/sun/star/sdbc/IndexType.hpp>
27#include <com/sun/star/sdbc/SQLException.hpp>
29#include <TConnection.hxx>
31#include <comphelper/types.hxx>
32#include <rtl/ustrbuf.hxx>
33using namespace connectivity;
34using namespace connectivity::sdbcx;
35using namespace ::com::sun::star::uno;
36using namespace ::com::sun::star::beans;
37using namespace ::com::sun::star::sdbcx;
38using namespace ::com::sun::star::sdbc;
39using namespace ::com::sun::star::container;
40using namespace ::com::sun::star::lang;
41using namespace cppu;
42
43
44OIndexesHelper::OIndexesHelper(OTableHelper* _pTable,
45 ::osl::Mutex& _rMutex,
46 const std::vector< OUString> &_rVector
47 )
48 : OCollection(*_pTable,true,_rMutex,_rVector)
49 ,m_pTable(_pTable)
50{
51}
52
53
55{
56 Reference< XConnection> xConnection = m_pTable->getConnection();
57 if ( !xConnection.is() )
58 return nullptr;
59
61 OUString aName,aQualifier;
62 sal_Int32 nLen = _rName.indexOf('.');
63 if ( nLen != -1 )
64 {
65 aQualifier = _rName.copy(0,nLen);
66 aName = _rName.copy(nLen+1);
67 }
68 else
69 aName = _rName;
70
72 OUString aSchema,aTable;
73 m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_SCHEMANAME)) >>= aSchema;
74 m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)) >>= aTable;
75
76 Any aCatalog = m_pTable->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_CATALOGNAME));
77 Reference< XResultSet > xResult = m_pTable->getMetaData()->getIndexInfo(aCatalog,aSchema,aTable,false,false);
78
79 if ( xResult.is() )
80 {
81 Reference< XRow > xRow(xResult,UNO_QUERY);
82 while( xResult->next() )
83 {
84 bool bUnique = !xRow->getBoolean(4);
85 if((aQualifier.isEmpty() || xRow->getString(5) == aQualifier ) && xRow->getString(6) == aName)
86 {
87 sal_Int32 nClustered = xRow->getShort(7);
88 bool bPrimarKeyIndex = false;
89 xRow.clear();
90 xResult.clear();
91 try
92 {
93 xResult = m_pTable->getMetaData()->getPrimaryKeys(aCatalog,aSchema,aTable);
94 xRow.set(xResult,UNO_QUERY);
95
96 if ( xRow.is() && xResult->next() ) // there can be only one primary key
97 {
98 bPrimarKeyIndex = xRow->getString(6) == aName;
99 }
100 }
101 catch(const Exception&)
102 {
103 }
104 xRet = new OIndexHelper(m_pTable,aName,aQualifier,bUnique,
105 bPrimarKeyIndex,
106 nClustered == IndexType::CLUSTERED);
107 break;
108 }
109 }
110 }
111
112 return xRet;
113}
114
116{
118}
119
120Reference< XPropertySet > OIndexesHelper::createDescriptor()
121{
122 return new OIndexHelper(m_pTable);
123}
124
125// XAppend
126sdbcx::ObjectType OIndexesHelper::appendObject( const OUString& _rForName, const Reference< XPropertySet >& descriptor )
127{
128 Reference< XConnection> xConnection = m_pTable->getConnection();
129 if ( !xConnection.is() )
130 return nullptr;
131 if ( m_pTable->isNew() )
132 return cloneDescriptor( descriptor );
133
134 if ( m_pTable->getIndexService().is() )
135 {
136 m_pTable->getIndexService()->addIndex(m_pTable,descriptor);
137 }
138 else
139 {
141 OUStringBuffer aSql( "CREATE " );
142 OUString aQuote = m_pTable->getMetaData()->getIdentifierQuoteString( );
143
144 if(comphelper::getBOOL(descriptor->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISUNIQUE))))
145 aSql.append("UNIQUE ");
146 aSql.append("INDEX ");
147
148
149 OUString aCatalog,aSchema,aTable;
150 dbtools::qualifiedNameComponents(m_pTable->getMetaData(),m_pTable->getName(),aCatalog,aSchema,aTable,::dbtools::EComposeRule::InDataManipulation);
151
152 OUString aComposedName = dbtools::composeTableName(m_pTable->getMetaData(),aCatalog,aSchema,aTable, true, ::dbtools::EComposeRule::InIndexDefinitions);
153 if (!_rForName.isEmpty() )
154 {
155 aSql.append( ::dbtools::quoteName( aQuote, _rForName )
156 + " ON "
157 + aComposedName
158 + " ( ");
159
160 Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
161 Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
162 Reference< XPropertySet > xColProp;
163 bool bAddIndexAppendix = ::dbtools::getBooleanDataSourceSetting( m_pTable->getConnection(), "AddIndexAppendix" );
164 sal_Int32 nCount = xColumns->getCount();
165 for(sal_Int32 i = 0 ; i < nCount; ++i)
166 {
167 xColProp.set(xColumns->getByIndex(i),UNO_QUERY);
168 aSql.append(::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
169
170 if ( bAddIndexAppendix )
171 {
172
173 aSql.appendAscii(any2bool(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_ISASCENDING)))
174 ?
175 " ASC"
176 :
177 " DESC");
178 }
179 aSql.append(",");
180 }
181 aSql[aSql.getLength() - 1] = ')';
182 }
183 else
184 {
185 aSql.append(aComposedName);
186
187 Reference<XColumnsSupplier> xColumnSup(descriptor,UNO_QUERY);
188 Reference<XIndexAccess> xColumns(xColumnSup->getColumns(),UNO_QUERY);
189 Reference< XPropertySet > xColProp;
190 if(xColumns->getCount() != 1)
191 throw SQLException();
192
193 xColumns->getByIndex(0) >>= xColProp;
194
195 aSql.append("."
196 + ::dbtools::quoteName( aQuote,comphelper::getString(xColProp->getPropertyValue(rPropMap.getNameByIndex(PROPERTY_ID_NAME)))));
197 }
198
199 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
200 if ( xStmt.is() )
201 {
202 OUString sSql = aSql.makeStringAndClear();
203 xStmt->execute(sSql);
204 ::comphelper::disposeComponent(xStmt);
205 }
206 }
207
208 return createObject( _rForName );
209}
210
211// XDrop
212void OIndexesHelper::dropObject(sal_Int32 /*_nPos*/,const OUString& _sElementName)
213{
214 Reference< XConnection> xConnection = m_pTable->getConnection();
215 if( !xConnection.is() || m_pTable->isNew() )
216 return;
217
218 if ( m_pTable->getIndexService().is() )
219 {
220 m_pTable->getIndexService()->dropIndex(m_pTable,_sElementName);
221 }
222 else
223 {
224 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);
229
230 OUString aSql( "DROP INDEX " );
231
232 OUString aComposedName = dbtools::composeTableName( m_pTable->getMetaData(), m_pTable, ::dbtools::EComposeRule::InIndexDefinitions, true );
233 OUString sIndexName = dbtools::composeTableName( m_pTable->getMetaData(), OUString(), aSchema, aName, true, ::dbtools::EComposeRule::InIndexDefinitions );
234
235 aSql += sIndexName + " ON " + aComposedName;
236
237 Reference< XStatement > xStmt = m_pTable->getConnection()->createStatement( );
238 if ( xStmt.is() )
239 {
240 xStmt->execute(aSql);
241 ::comphelper::disposeComponent(xStmt);
242 }
243 }
244}
245
246
247/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void dropObject(sal_Int32 _nPos, const OUString &_sElementName) override
Definition: TIndexes.cxx:212
virtual css::uno::Reference< css::beans::XPropertySet > createDescriptor() override
Definition: TIndexes.cxx:120
virtual sdbcx::ObjectType createObject(const OUString &_rName) override
Definition: TIndexes.cxx:54
virtual sdbcx::ObjectType appendObject(const OUString &_rForName, const css::uno::Reference< css::beans::XPropertySet > &descriptor) override
appends an object described by a descriptor, under a given name
Definition: TIndexes.cxx:126
virtual void impl_refresh() override
Definition: TIndexes.cxx:115
::dbtools::OPropertyMap & getPropMap()
Definition: TConnection.cxx:68
virtual void refreshIndexes() override
css::uno::Reference< css::sdbc::XConnection > const & getConnection() const
virtual css::uno::Reference< css::sdbc::XDatabaseMetaData > getMetaData() const override
css::uno::Reference< css::sdb::tools::XIndexAlteration > const & getIndexService() const
virtual OUString SAL_CALL getName() override
ObjectType cloneDescriptor(const ObjectType &_descriptor)
clones the given descriptor
const OUString & getNameByIndex(sal_Int32 _nIndex) const
Definition: propertyids.cxx:95
int nCount
OUString aName
@ Exception
bool getBOOL(const Any &_rAny)
OUString getString(const Any &_rAny)
css::uno::Reference< css::beans::XPropertySet > ObjectType
Definition: VCollection.hxx:59
bool any2bool(const css::uno::Any &rAny)
OUString composeTableName(const Reference< XDatabaseMetaData > &_rxMetaData, const OUString &_rCatalog, const OUString &_rSchema, const OUString &_rName, bool _bQuote, EComposeRule _eComposeRule)
Definition: dbtools.cxx:1286
void qualifiedNameComponents(const Reference< XDatabaseMetaData > &_rxConnMetaData, const OUString &_rQualifiedName, OUString &_rCatalog, OUString &_rSchema, OUString &_rName, EComposeRule _eComposeRule)
Definition: dbtools.cxx:862
OUString quoteName(std::u16string_view _rQuote, const OUString &_rName)
quote the given name with the given quote string.
bool getBooleanDataSourceSetting(const Reference< XConnection > &_rxConnection, const char *_pAsciiSettingName)
Definition: dbtools2.cxx:566
int i
#define PROPERTY_ID_NAME
Definition: propertyids.hxx:50
#define PROPERTY_ID_ISUNIQUE
Definition: propertyids.hxx:65
#define PROPERTY_ID_CATALOGNAME
Definition: propertyids.hxx:70
#define PROPERTY_ID_ISASCENDING
Definition: propertyids.hxx:68
#define PROPERTY_ID_SCHEMANAME
Definition: propertyids.hxx:69