LibreOffice Module dbaccess (master) 1
indexcollection.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
20#include <indexcollection.hxx>
22#include <com/sun/star/sdbc/SQLException.hpp>
23#include <com/sun/star/sdbcx/XAppend.hpp>
24#include <com/sun/star/beans/XPropertySet.hpp>
25#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26#include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
28#include <com/sun/star/sdbcx/XDrop.hpp>
29
30namespace dbaui
31{
32
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::container;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::sdbcx;
37 using namespace ::com::sun::star::sdbc;
38
39 // OIndexCollection
41 {
42 }
43
45 {
46 *this = _rSource;
47 }
48
50 {
51 detach();
52 m_xIndexes = _rSource.m_xIndexes;
53 m_aIndexes = _rSource.m_aIndexes;
54 return *this;
55 }
56
58 {
59 implConstructFrom(_rxIndexes);
60 }
61
63 {
64 m_xIndexes.clear();
65 m_aIndexes.clear();
66 }
67
68 Indexes::const_iterator OIndexCollection::find(const OUString& _rName) const
69 {
70 // loop'n'compare
71 return std::find_if(m_aIndexes.cbegin(), m_aIndexes.cend(),
72 [&_rName](const OIndex& rIndex) { return rIndex.sName == _rName; });
73 }
74
75 Indexes::iterator OIndexCollection::find(const OUString& _rName)
76 {
77 // loop'n'compare
78 return std::find_if(m_aIndexes.begin(), m_aIndexes.end(),
79 [&_rName](const OIndex& rIndex) { return rIndex.sName == _rName; });
80 }
81
82 Indexes::const_iterator OIndexCollection::findOriginal(const OUString& _rName) const
83 {
84 // loop'n'compare
85 return std::find_if(m_aIndexes.cbegin(), m_aIndexes.cend(),
86 [&_rName](const OIndex& rIndex) { return rIndex.getOriginalName() == _rName; });
87 }
88
89 Indexes::iterator OIndexCollection::findOriginal(const OUString& _rName)
90 {
91 // loop'n'compare
92 return std::find_if(m_aIndexes.begin(), m_aIndexes.end(),
93 [&_rName](const OIndex& rIndex) { return rIndex.getOriginalName() == _rName; });
94 }
95
96 void OIndexCollection::commitNewIndex(const Indexes::iterator& _rPos)
97 {
98 OSL_ENSURE(_rPos->isNew(), "OIndexCollection::commitNewIndex: index must be new!");
99
100 try
101 {
102 Reference< XDataDescriptorFactory > xIndexFactory(m_xIndexes, UNO_QUERY);
103 Reference< XAppend > xAppendIndex(xIndexFactory, UNO_QUERY);
104 if (!xAppendIndex.is())
105 {
106 OSL_FAIL("OIndexCollection::commitNewIndex: missing an interface of the index container!");
107 return;
108 }
109
110 Reference< XPropertySet > xIndexDescriptor = xIndexFactory->createDataDescriptor();
111 Reference< XColumnsSupplier > xColsSupp(xIndexDescriptor, UNO_QUERY);
113 if (xColsSupp.is())
114 xCols = xColsSupp->getColumns();
115
116 Reference< XDataDescriptorFactory > xColumnFactory(xCols, UNO_QUERY);
117 Reference< XAppend > xAppendCols(xColumnFactory, UNO_QUERY);
118 if (!xAppendCols.is())
119 {
120 OSL_FAIL("OIndexCollection::commitNewIndex: invalid index descriptor returned!");
121 return;
122 }
123
124 // set the properties
125 static constexpr OUStringLiteral s_sNamePropertyName = u"Name";
126 // the index' own props
127 xIndexDescriptor->setPropertyValue("IsUnique", css::uno::Any(_rPos->bUnique));
128 xIndexDescriptor->setPropertyValue(s_sNamePropertyName, Any(_rPos->sName));
129
130 // the fields
131 for (auto const& field : _rPos->aFields)
132 {
133 OSL_ENSURE(!xCols->hasByName(field.sFieldName), "OIndexCollection::commitNewIndex: double column name (need to prevent this outside)!");
134
135 Reference< XPropertySet > xColDescriptor = xColumnFactory->createDataDescriptor();
136 OSL_ENSURE(xColDescriptor.is(), "OIndexCollection::commitNewIndex: invalid column descriptor!");
137 if (xColDescriptor.is())
138 {
139 xColDescriptor->setPropertyValue("IsAscending", css::uno::Any(field.bSortAscending));
140 xColDescriptor->setPropertyValue(s_sNamePropertyName, Any(field.sFieldName));
141 xAppendCols->appendByDescriptor(xColDescriptor);
142 }
143 }
144
145 xAppendIndex->appendByDescriptor(xIndexDescriptor);
146
147 _rPos->flagAsCommitted(GrantIndexAccess());
148 _rPos->clearModified();
149 }
150 catch(SQLException&)
151 { // allowed to pass
152 throw;
153 }
154 catch( const Exception& )
155 {
156 DBG_UNHANDLED_EXCEPTION("dbaccess");
157 }
158 }
159
160 bool OIndexCollection::dropNoRemove(const Indexes::iterator& _rPos)
161 {
162 try
163 {
164 OSL_ENSURE(m_xIndexes->hasByName(_rPos->getOriginalName()), "OIndexCollection::drop: invalid name!");
165
166 Reference< XDrop > xDropIndex(m_xIndexes, UNO_QUERY);
167 if (!xDropIndex.is())
168 {
169 OSL_FAIL("OIndexCollection::drop: no XDrop interface!");
170 return false;
171 }
172
173 xDropIndex->dropByName(_rPos->getOriginalName());
174 }
175 catch(SQLException&)
176 { // allowed to pass
177 throw;
178 }
179 catch( const Exception& )
180 {
181 DBG_UNHANDLED_EXCEPTION("dbaccess");
182 return false;
183 }
184
185 // adjust the OIndex structure
186 Indexes::iterator aDropped = findOriginal(_rPos->getOriginalName());
187 OSL_ENSURE(aDropped != m_aIndexes.end(), "OIndexCollection::drop: invalid original name, but successful commit?!");
188 aDropped->flagAsNew(GrantIndexAccess());
189
190 return true;
191 }
192
193 bool OIndexCollection::drop(const Indexes::iterator& _rPos)
194 {
195 OSL_ENSURE((_rPos >= m_aIndexes.begin()) && (_rPos < m_aIndexes.end()),
196 "OIndexCollection::drop: invalid position (fasten your seatbelt... this will crash)!");
197
198 if (!_rPos->isNew())
199 if (!dropNoRemove(_rPos))
200 return false;
201
202 // adjust the index array
203 m_aIndexes.erase(_rPos);
204 return true;
205 }
206
208 {
209 // get the UNO descriptor for the index
211 m_xIndexes->getByName(_rIndex.getOriginalName()) >>= xIndex;
212 if (!xIndex.is())
213 {
214 OSL_FAIL("OIndexCollection::implFillIndexInfo: got an invalid index object!");
215 }
216 else
217 implFillIndexInfo(_rIndex, xIndex);
218 }
219
220 void OIndexCollection::implFillIndexInfo(OIndex& _rIndex, const Reference< XPropertySet >& _rxDescriptor)
221 {
222 _rIndex.bPrimaryKey = ::cppu::any2bool(_rxDescriptor->getPropertyValue("IsPrimaryKeyIndex"));
223 _rIndex.bUnique = ::cppu::any2bool(_rxDescriptor->getPropertyValue("IsUnique"));
224 _rxDescriptor->getPropertyValue("Catalog") >>= _rIndex.sDescription;
225
226 // the columns
227 Reference< XColumnsSupplier > xSuppCols(_rxDescriptor, UNO_QUERY);
229 if (xSuppCols.is())
230 xCols = xSuppCols->getColumns();
231 OSL_ENSURE(xCols.is(), "OIndexCollection::implFillIndexInfo: the index does not have columns!");
232 if (!xCols.is())
233 return;
234
235 Sequence< OUString > aFieldNames = xCols->getElementNames();
236 _rIndex.aFields.resize(aFieldNames.getLength());
237
238 const OUString* pFieldNames = aFieldNames.getConstArray();
239 const OUString* pFieldNamesEnd = pFieldNames + aFieldNames.getLength();
240 IndexFields::iterator aCopyTo = _rIndex.aFields.begin();
241
242 Reference< XPropertySet > xIndexColumn;
243 for (;pFieldNames < pFieldNamesEnd; ++pFieldNames, ++aCopyTo)
244 {
245 // extract the column
246 xIndexColumn.clear();
247 xCols->getByName(*pFieldNames) >>= xIndexColumn;
248 if (!xIndexColumn.is())
249 {
250 OSL_FAIL("OIndexCollection::implFillIndexInfo: invalid index column!");
251 --aCopyTo;
252 continue;
253 }
254
255 // get the relevant properties
256 aCopyTo->sFieldName = *pFieldNames;
257 aCopyTo->bSortAscending = ::cppu::any2bool(xIndexColumn->getPropertyValue("IsAscending"));
258 }
259
260 _rIndex.aFields.resize(aCopyTo - _rIndex.aFields.begin());
261 // (just in case some fields were invalid ...)
262 }
263
264 void OIndexCollection::resetIndex(const Indexes::iterator& _rPos)
265 {
266 OSL_ENSURE(_rPos >= m_aIndexes.begin() && _rPos < m_aIndexes.end(),
267 "OIndexCollection::resetIndex: invalid position!");
268
269 try
270 {
271 _rPos->sName = _rPos->getOriginalName();
272 implFillIndexInfo(*_rPos);
273
274 _rPos->clearModified();
275 _rPos->flagAsCommitted(GrantIndexAccess());
276 }
277 catch(SQLException&)
278 { // allowed to pass
279 throw;
280 }
281 catch( const Exception& )
282 {
283 DBG_UNHANDLED_EXCEPTION("dbaccess");
284 }
285 }
286
287 Indexes::iterator OIndexCollection::insert(const OUString& _rName)
288 {
289 OSL_ENSURE(end() == find(_rName), "OIndexCollection::insert: invalid new name!");
290 OIndex aNewIndex((OUString())); // the empty string indicates the index is a new one
291 aNewIndex.sName = _rName;
292 m_aIndexes.push_back(aNewIndex);
293 return m_aIndexes.end() - 1; // the last element is the new one ...
294 }
295
297 {
298 detach();
299
300 m_xIndexes = _rxIndexes;
301 if (!m_xIndexes.is())
302 return;
303
304 // loop through all the indexes
305 Sequence< OUString > aNames = m_xIndexes->getElementNames();
306 const OUString* pNames = aNames.getConstArray();
307 const OUString* pEnd = pNames + aNames.getLength();
308 for (; pNames < pEnd; ++pNames)
309 {
310 // extract the index object
312 m_xIndexes->getByName(*pNames) >>= xIndex;
313 if (!xIndex.is())
314 {
315 OSL_FAIL("OIndexCollection::implConstructFrom: got an invalid index object ... ignoring!");
316 continue;
317 }
318
319 // fill the OIndex structure
320 OIndex aCurrentIndex(*pNames);
321 implFillIndexInfo(aCurrentIndex);
322 m_aIndexes.push_back(aCurrentIndex);
323 }
324 }
325
326} // namespace dbaui
327
328/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char *const aFieldNames[]
void attach(const css::uno::Reference< css::container::XNameAccess > &_rxIndexes)
void resetIndex(const Indexes::iterator &_rPos)
Indexes::iterator insert(const OUString &_rName)
OIndexCollection & operator=(const OIndexCollection &_rSource)
css::uno::Reference< css::container::XNameAccess > m_xIndexes
bool drop(const Indexes::iterator &_rPos)
drop an index, and remove it from the collection
void commitNewIndex(const Indexes::iterator &_rPos)
static void implFillIndexInfo(OIndex &_rIndex, const css::uno::Reference< css::beans::XPropertySet > &_rxDescriptor)
void implConstructFrom(const css::uno::Reference< css::container::XNameAccess > &_rxIndexes)
bool dropNoRemove(const Indexes::iterator &_rPos)
simply drop the index described by the name, but don't remove the descriptor from the collection
Indexes::const_iterator findOriginal(const OUString &_rName) const
Indexes::const_iterator find(const OUString &_rName) const
Indexes::const_iterator end() const
get access to the (last + 1st) element of the index collection
#define DBG_UNHANDLED_EXCEPTION(...)
float u
@ Exception
IndexFields aFields
Definition: indexes.hxx:59
bool bUnique
Definition: indexes.hxx:58
OUString sName
Definition: indexes.hxx:55
bool bPrimaryKey
Definition: indexes.hxx:57
const OUString & getOriginalName() const
Definition: indexes.hxx:66
OUString sDescription
Definition: indexes.hxx:56