android: Update app icon to new startcenter icon
[LibreOffice.git] / dbaccess / source / ui / misc / indexcollection.cxx
blobbdcdaad09a7fcfe2840f03ae8c7499cc7ddb2d20
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <indexcollection.hxx>
21 #include <comphelper/diagnose_ex.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>
27 #include <comphelper/extract.hxx>
28 #include <com/sun/star/sdbcx/XDrop.hpp>
30 namespace dbaui
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;
39 // OIndexCollection
40 OIndexCollection::OIndexCollection()
44 OIndexCollection::OIndexCollection(const OIndexCollection& _rSource)
46 *this = _rSource;
49 OIndexCollection& OIndexCollection::operator=(const OIndexCollection& _rSource)
51 detach();
52 m_xIndexes = _rSource.m_xIndexes;
53 m_aIndexes = _rSource.m_aIndexes;
54 return *this;
57 void OIndexCollection::attach(const Reference< XNameAccess >& _rxIndexes)
59 implConstructFrom(_rxIndexes);
62 void OIndexCollection::detach()
64 m_xIndexes.clear();
65 m_aIndexes.clear();
68 Indexes::const_iterator OIndexCollection::find(const OUString& _rName) const
70 // loop'n'compare
71 return std::find_if(m_aIndexes.cbegin(), m_aIndexes.cend(),
72 [&_rName](const OIndex& rIndex) { return rIndex.sName == _rName; });
75 Indexes::iterator OIndexCollection::find(const OUString& _rName)
77 // loop'n'compare
78 return std::find_if(m_aIndexes.begin(), m_aIndexes.end(),
79 [&_rName](const OIndex& rIndex) { return rIndex.sName == _rName; });
82 Indexes::const_iterator OIndexCollection::findOriginal(const OUString& _rName) const
84 // loop'n'compare
85 return std::find_if(m_aIndexes.cbegin(), m_aIndexes.cend(),
86 [&_rName](const OIndex& rIndex) { return rIndex.getOriginalName() == _rName; });
89 Indexes::iterator OIndexCollection::findOriginal(const OUString& _rName)
91 // loop'n'compare
92 return std::find_if(m_aIndexes.begin(), m_aIndexes.end(),
93 [&_rName](const OIndex& rIndex) { return rIndex.getOriginalName() == _rName; });
96 void OIndexCollection::commitNewIndex(const Indexes::iterator& _rPos)
98 OSL_ENSURE(_rPos->isNew(), "OIndexCollection::commitNewIndex: index must be new!");
102 Reference< XDataDescriptorFactory > xIndexFactory(m_xIndexes, UNO_QUERY);
103 Reference< XAppend > xAppendIndex(xIndexFactory, UNO_QUERY);
104 if (!xAppendIndex.is())
106 OSL_FAIL("OIndexCollection::commitNewIndex: missing an interface of the index container!");
107 return;
110 Reference< XPropertySet > xIndexDescriptor = xIndexFactory->createDataDescriptor();
111 Reference< XColumnsSupplier > xColsSupp(xIndexDescriptor, UNO_QUERY);
112 Reference< XNameAccess > xCols;
113 if (xColsSupp.is())
114 xCols = xColsSupp->getColumns();
116 Reference< XDataDescriptorFactory > xColumnFactory(xCols, UNO_QUERY);
117 Reference< XAppend > xAppendCols(xColumnFactory, UNO_QUERY);
118 if (!xAppendCols.is())
120 OSL_FAIL("OIndexCollection::commitNewIndex: invalid index descriptor returned!");
121 return;
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));
130 // the fields
131 for (auto const& field : _rPos->aFields)
133 OSL_ENSURE(!xCols->hasByName(field.sFieldName), "OIndexCollection::commitNewIndex: double column name (need to prevent this outside)!");
135 Reference< XPropertySet > xColDescriptor = xColumnFactory->createDataDescriptor();
136 OSL_ENSURE(xColDescriptor.is(), "OIndexCollection::commitNewIndex: invalid column descriptor!");
137 if (xColDescriptor.is())
139 xColDescriptor->setPropertyValue("IsAscending", css::uno::Any(field.bSortAscending));
140 xColDescriptor->setPropertyValue(s_sNamePropertyName, Any(field.sFieldName));
141 xAppendCols->appendByDescriptor(xColDescriptor);
145 xAppendIndex->appendByDescriptor(xIndexDescriptor);
147 _rPos->flagAsCommitted(GrantIndexAccess());
148 _rPos->clearModified();
150 catch(SQLException&)
151 { // allowed to pass
152 throw;
154 catch( const Exception& )
156 DBG_UNHANDLED_EXCEPTION("dbaccess");
160 bool OIndexCollection::dropNoRemove(const Indexes::iterator& _rPos)
164 OSL_ENSURE(m_xIndexes->hasByName(_rPos->getOriginalName()), "OIndexCollection::drop: invalid name!");
166 Reference< XDrop > xDropIndex(m_xIndexes, UNO_QUERY);
167 if (!xDropIndex.is())
169 OSL_FAIL("OIndexCollection::drop: no XDrop interface!");
170 return false;
173 xDropIndex->dropByName(_rPos->getOriginalName());
175 catch(SQLException&)
176 { // allowed to pass
177 throw;
179 catch( const Exception& )
181 DBG_UNHANDLED_EXCEPTION("dbaccess");
182 return false;
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());
190 return true;
193 bool OIndexCollection::drop(const Indexes::iterator& _rPos)
195 OSL_ENSURE((_rPos >= m_aIndexes.begin()) && (_rPos < m_aIndexes.end()),
196 "OIndexCollection::drop: invalid position (fasten your seatbelt... this will crash)!");
198 if (!_rPos->isNew())
199 if (!dropNoRemove(_rPos))
200 return false;
202 // adjust the index array
203 m_aIndexes.erase(_rPos);
204 return true;
207 void OIndexCollection::implFillIndexInfo(OIndex& _rIndex)
209 // get the UNO descriptor for the index
210 Reference< XPropertySet > xIndex;
211 m_xIndexes->getByName(_rIndex.getOriginalName()) >>= xIndex;
212 if (!xIndex.is())
214 OSL_FAIL("OIndexCollection::implFillIndexInfo: got an invalid index object!");
216 else
217 implFillIndexInfo(_rIndex, xIndex);
220 void OIndexCollection::implFillIndexInfo(OIndex& _rIndex, const Reference< XPropertySet >& _rxDescriptor)
222 _rIndex.bPrimaryKey = ::cppu::any2bool(_rxDescriptor->getPropertyValue("IsPrimaryKeyIndex"));
223 _rIndex.bUnique = ::cppu::any2bool(_rxDescriptor->getPropertyValue("IsUnique"));
224 _rxDescriptor->getPropertyValue("Catalog") >>= _rIndex.sDescription;
226 // the columns
227 Reference< XColumnsSupplier > xSuppCols(_rxDescriptor, UNO_QUERY);
228 Reference< XNameAccess > xCols;
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;
235 Sequence< OUString > aFieldNames = xCols->getElementNames();
236 _rIndex.aFields.resize(aFieldNames.getLength());
238 const OUString* pFieldNames = aFieldNames.getConstArray();
239 const OUString* pFieldNamesEnd = pFieldNames + aFieldNames.getLength();
240 IndexFields::iterator aCopyTo = _rIndex.aFields.begin();
242 Reference< XPropertySet > xIndexColumn;
243 for (;pFieldNames < pFieldNamesEnd; ++pFieldNames, ++aCopyTo)
245 // extract the column
246 xIndexColumn.clear();
247 xCols->getByName(*pFieldNames) >>= xIndexColumn;
248 if (!xIndexColumn.is())
250 OSL_FAIL("OIndexCollection::implFillIndexInfo: invalid index column!");
251 --aCopyTo;
252 continue;
255 // get the relevant properties
256 aCopyTo->sFieldName = *pFieldNames;
257 aCopyTo->bSortAscending = ::cppu::any2bool(xIndexColumn->getPropertyValue("IsAscending"));
260 _rIndex.aFields.resize(aCopyTo - _rIndex.aFields.begin());
261 // (just in case some fields were invalid ...)
264 void OIndexCollection::resetIndex(const Indexes::iterator& _rPos)
266 OSL_ENSURE(_rPos >= m_aIndexes.begin() && _rPos < m_aIndexes.end(),
267 "OIndexCollection::resetIndex: invalid position!");
271 _rPos->sName = _rPos->getOriginalName();
272 implFillIndexInfo(*_rPos);
274 _rPos->clearModified();
275 _rPos->flagAsCommitted(GrantIndexAccess());
277 catch(SQLException&)
278 { // allowed to pass
279 throw;
281 catch( const Exception& )
283 DBG_UNHANDLED_EXCEPTION("dbaccess");
287 Indexes::iterator OIndexCollection::insert(const OUString& _rName)
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 ...
296 void OIndexCollection::implConstructFrom(const Reference< XNameAccess >& _rxIndexes)
298 detach();
300 m_xIndexes = _rxIndexes;
301 if (!m_xIndexes.is())
302 return;
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)
310 // extract the index object
311 Reference< XPropertySet > xIndex;
312 m_xIndexes->getByName(*pNames) >>= xIndex;
313 if (!xIndex.is())
315 OSL_FAIL("OIndexCollection::implConstructFrom: got an invalid index object ... ignoring!");
316 continue;
319 // fill the OIndex structure
320 OIndex aCurrentIndex(*pNames);
321 implFillIndexInfo(aCurrentIndex);
322 m_aIndexes.push_back(aCurrentIndex);
326 } // namespace dbaui
328 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */