calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / dbaccess / source / core / api / table.cxx
blobf9a1dffb956ae2ba9f6e05b05d1bfaf47d7f75ee
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 <table.hxx>
21 #include <definitioncolumn.hxx>
22 #include <stringconstants.hxx>
23 #include <strings.hxx>
24 #include <core_resource.hxx>
25 #include <strings.hrc>
26 #include "CIndexes.hxx"
28 #include <osl/diagnose.h>
29 #include <comphelper/servicehelper.hxx>
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <com/sun/star/sdbc/SQLException.hpp>
32 #include <com/sun/star/sdb/tools/XTableRename.hpp>
33 #include <com/sun/star/sdb/tools/XTableAlteration.hpp>
35 #include <connectivity/TKeys.hxx>
36 #include <connectivity/dbtools.hxx>
38 #include <ContainerMediator.hxx>
40 using namespace dbaccess;
41 using namespace connectivity;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::util;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::sdbc;
47 using namespace ::com::sun::star::sdbcx;
48 using namespace ::com::sun::star::container;
49 using namespace ::osl;
50 using namespace ::comphelper;
51 using namespace ::cppu;
53 // ODBTable
55 ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables
56 ,const Reference< XConnection >& _rxConn
57 ,const OUString& _rCatalog
58 ,const OUString& _rSchema
59 ,const OUString& _rName
60 ,const OUString& _rType
61 ,const OUString& _rDesc
62 ,const Reference< XNameAccess >& _xColumnDefinitions)
63 :OTable_Base(_pTables,_rxConn,_rxConn->getMetaData().is() && _rxConn->getMetaData()->supportsMixedCaseQuotedIdentifiers(), _rName, _rType, _rDesc, _rSchema, _rCatalog )
64 ,m_xColumnDefinitions(_xColumnDefinitions)
65 ,m_nPrivileges(0)
67 OSL_ENSURE(getMetaData().is(), "ODBTable::ODBTable : invalid conn !");
68 OSL_ENSURE(!_rName.isEmpty(), "ODBTable::ODBTable : name !");
69 // TODO : think about collecting the privileges here, as we can't ensure that in getFastPropertyValue, where
70 // we do this at the moment, the statement needed can be supplied by the connection (for example the SQL-Server
71 // ODBC driver does not allow more than one statement per connection, and in getFastPropertyValue it's more
72 // likely that it's already used up than it's here.)
75 ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables
76 ,const Reference< XConnection >& _rxConn)
77 :OTable_Base(_pTables,_rxConn, _rxConn->getMetaData().is() && _rxConn->getMetaData()->supportsMixedCaseQuotedIdentifiers())
78 ,m_nPrivileges(-1)
82 ODBTable::~ODBTable()
86 rtl::Reference<OColumn> ODBTable::createColumn(const OUString& _rName) const
88 Reference<XPropertySet> xProp;
89 if ( m_xDriverColumns.is() && m_xDriverColumns->hasByName(_rName) )
91 xProp.set(m_xDriverColumns->getByName(_rName),UNO_QUERY);
93 else
95 OColumns* pColumns = static_cast<OColumns*>(m_xColumns.get());
96 xProp.set(pColumns->createBaseObject(_rName),UNO_QUERY);
99 Reference<XPropertySet> xColumnDefinition;
100 if ( m_xColumnDefinitions.is() && m_xColumnDefinitions->hasByName(_rName) )
101 xColumnDefinition.set(m_xColumnDefinitions->getByName(_rName),UNO_QUERY);
102 return new OTableColumnWrapper( xProp, xColumnDefinition, false );
105 void ODBTable::columnAppended( const Reference< XPropertySet >& /*_rxSourceDescriptor*/ )
107 // not interested in
110 void ODBTable::columnDropped(const OUString& _sName)
112 Reference<XDrop> xDrop(m_xColumnDefinitions,UNO_QUERY);
113 if ( xDrop.is() && m_xColumnDefinitions->hasByName(_sName) )
115 xDrop->dropByName(_sName);
119 Sequence< sal_Int8 > ODBTable::getImplementationId()
121 return css::uno::Sequence<sal_Int8>();
124 // OComponentHelper
125 void SAL_CALL ODBTable::disposing()
127 OPropertySetHelper::disposing();
128 OTable_Base::disposing();
129 m_xColumnDefinitions = nullptr;
130 m_xDriverColumns = nullptr;
131 m_pColumnMediator = nullptr;
134 void ODBTable::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
136 if ((PROPERTY_ID_PRIVILEGES == _nHandle) && (-1 == m_nPrivileges))
137 { // somebody is asking for the privileges and we do not know them, yet
138 const_cast<ODBTable*>(this)->m_nPrivileges = ::dbtools::getTablePrivileges(getMetaData(),m_CatalogName,m_SchemaName, m_Name);
141 OTable_Base::getFastPropertyValue(_rValue, _nHandle);
144 void ODBTable::construct()
146 ::osl::MutexGuard aGuard(m_aMutex);
148 // we don't collect the privileges here, this is potentially expensive. Instead we determine them on request.
149 // (see getFastPropertyValue)
150 m_nPrivileges = -1;
152 OTable_Base::construct();
154 registerProperty(PROPERTY_FILTER, PROPERTY_ID_FILTER, PropertyAttribute::BOUND,
155 &m_sFilter, cppu::UnoType<OUString>::get());
157 registerProperty(PROPERTY_ORDER, PROPERTY_ID_ORDER, PropertyAttribute::BOUND,
158 &m_sOrder, cppu::UnoType<OUString>::get());
160 registerProperty(PROPERTY_APPLYFILTER, PROPERTY_ID_APPLYFILTER, PropertyAttribute::BOUND,
161 &m_bApplyFilter, cppu::UnoType<bool>::get());
163 registerProperty(PROPERTY_FONT, PROPERTY_ID_FONT, PropertyAttribute::BOUND,
164 &m_aFont, cppu::UnoType<css::awt::FontDescriptor>::get());
166 registerMayBeVoidProperty(PROPERTY_ROW_HEIGHT, PROPERTY_ID_ROW_HEIGHT, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
167 &m_aRowHeight, cppu::UnoType<sal_Int32>::get());
169 registerProperty(PROPERTY_AUTOGROW, PROPERTY_ID_AUTOGROW, PropertyAttribute::BOUND,
170 &m_bAutoGrow, cppu::UnoType<bool>::get());
172 registerMayBeVoidProperty(PROPERTY_TEXTCOLOR, PROPERTY_ID_TEXTCOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
173 &m_aTextColor, cppu::UnoType<sal_Int32>::get());
175 registerProperty(PROPERTY_PRIVILEGES, PROPERTY_ID_PRIVILEGES, PropertyAttribute::BOUND | PropertyAttribute::READONLY,
176 &m_nPrivileges, cppu::UnoType<sal_Int32>::get());
178 registerMayBeVoidProperty(PROPERTY_TEXTLINECOLOR, PROPERTY_ID_TEXTLINECOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
179 &m_aTextLineColor, cppu::UnoType<sal_Int32>::get());
181 registerProperty(PROPERTY_TEXTEMPHASIS, PROPERTY_ID_TEXTEMPHASIS, PropertyAttribute::BOUND,
182 &m_nFontEmphasis, cppu::UnoType<sal_Int16>::get());
184 registerProperty(PROPERTY_TEXTRELIEF, PROPERTY_ID_TEXTRELIEF, PropertyAttribute::BOUND,
185 &m_nFontRelief, cppu::UnoType<sal_Int16>::get());
187 registerProperty(PROPERTY_FONTNAME, PROPERTY_ID_FONTNAME, PropertyAttribute::BOUND,&m_aFont.Name, cppu::UnoType<OUString>::get());
188 registerProperty(PROPERTY_FONTHEIGHT, PROPERTY_ID_FONTHEIGHT, PropertyAttribute::BOUND,&m_aFont.Height, cppu::UnoType<sal_Int16>::get());
189 registerProperty(PROPERTY_FONTWIDTH, PROPERTY_ID_FONTWIDTH, PropertyAttribute::BOUND,&m_aFont.Width, cppu::UnoType<sal_Int16>::get());
190 registerProperty(PROPERTY_FONTSTYLENAME, PROPERTY_ID_FONTSTYLENAME, PropertyAttribute::BOUND,&m_aFont.StyleName, cppu::UnoType<OUString>::get());
191 registerProperty(PROPERTY_FONTFAMILY, PROPERTY_ID_FONTFAMILY, PropertyAttribute::BOUND,&m_aFont.Family, cppu::UnoType<sal_Int16>::get());
192 registerProperty(PROPERTY_FONTCHARSET, PROPERTY_ID_FONTCHARSET, PropertyAttribute::BOUND,&m_aFont.CharSet, cppu::UnoType<sal_Int16>::get());
193 registerProperty(PROPERTY_FONTPITCH, PROPERTY_ID_FONTPITCH, PropertyAttribute::BOUND,&m_aFont.Pitch, cppu::UnoType<sal_Int16>::get());
194 registerProperty(PROPERTY_FONTCHARWIDTH, PROPERTY_ID_FONTCHARWIDTH, PropertyAttribute::BOUND,&m_aFont.CharacterWidth, cppu::UnoType<float>::get());
195 registerProperty(PROPERTY_FONTWEIGHT, PROPERTY_ID_FONTWEIGHT, PropertyAttribute::BOUND,&m_aFont.Weight, cppu::UnoType<float>::get());
196 registerProperty(PROPERTY_FONTSLANT, PROPERTY_ID_FONTSLANT, PropertyAttribute::BOUND,&m_aFont.Slant, cppu::UnoType<css::awt::FontSlant>::get());
197 registerProperty(PROPERTY_FONTUNDERLINE, PROPERTY_ID_FONTUNDERLINE, PropertyAttribute::BOUND,&m_aFont.Underline, cppu::UnoType<sal_Int16>::get());
198 registerProperty(PROPERTY_FONTSTRIKEOUT, PROPERTY_ID_FONTSTRIKEOUT, PropertyAttribute::BOUND,&m_aFont.Strikeout, cppu::UnoType<sal_Int16>::get());
199 registerProperty(PROPERTY_FONTORIENTATION, PROPERTY_ID_FONTORIENTATION, PropertyAttribute::BOUND,&m_aFont.Orientation, cppu::UnoType<float>::get());
200 registerProperty(PROPERTY_FONTKERNING, PROPERTY_ID_FONTKERNING, PropertyAttribute::BOUND,&m_aFont.Kerning, cppu::UnoType<sal_Bool>::get());
201 registerProperty(PROPERTY_FONTWORDLINEMODE, PROPERTY_ID_FONTWORDLINEMODE,PropertyAttribute::BOUND,&m_aFont.WordLineMode, cppu::UnoType<sal_Bool>::get());
202 registerProperty(PROPERTY_FONTTYPE, PROPERTY_ID_FONTTYPE, PropertyAttribute::BOUND,&m_aFont.Type, cppu::UnoType<sal_Int16>::get());
204 refreshColumns();
207 ::cppu::IPropertyArrayHelper* ODBTable::createArrayHelper( sal_Int32 _nId) const
209 Sequence< Property > aProps;
210 describeProperties(aProps);
211 if(!_nId)
213 for(Property & prop : asNonConstRange(aProps))
215 if (prop.Name == PROPERTY_CATALOGNAME)
216 prop.Attributes = PropertyAttribute::READONLY;
217 else if (prop.Name == PROPERTY_SCHEMANAME)
218 prop.Attributes = PropertyAttribute::READONLY;
219 else if (prop.Name == PROPERTY_DESCRIPTION)
220 prop.Attributes = PropertyAttribute::READONLY;
221 else if (prop.Name == PROPERTY_NAME)
222 prop.Attributes = PropertyAttribute::READONLY;
226 return new ::cppu::OPropertyArrayHelper(aProps);
229 ::cppu::IPropertyArrayHelper & SAL_CALL ODBTable::getInfoHelper()
231 return *ODBTable_PROP::getArrayHelper(isNew() ? 1 : 0);
234 // XServiceInfo
235 OUString SAL_CALL ODBTable::getImplementationName()
237 return "com.sun.star.sdb.dbaccess.ODBTable";
239 sal_Bool SAL_CALL ODBTable::supportsService(const OUString& _rServiceName)
241 const css::uno::Sequence< OUString > aSupported(getSupportedServiceNames());
242 for (const OUString& s : aSupported)
243 if (s == _rServiceName)
244 return true;
246 return false;
248 css::uno::Sequence< OUString > SAL_CALL ODBTable::getSupportedServiceNames()
250 return { SERVICE_SDBCX_TABLE };
254 Any SAL_CALL ODBTable::queryInterface( const Type & rType )
256 if(rType == cppu::UnoType<XRename>::get()&& !getRenameService().is() )
257 return Any();
258 if(rType == cppu::UnoType<XAlterTable>::get()&& !getAlterService().is() )
259 return Any();
260 return OTable_Base::queryInterface( rType);
263 Sequence< Type > SAL_CALL ODBTable::getTypes( )
265 Type aRenameType = cppu::UnoType<XRename>::get();
266 Type aAlterType = cppu::UnoType<XAlterTable>::get();
268 Sequence< Type > aTypes(OTable_Base::getTypes());
269 std::vector<Type> aOwnTypes;
270 aOwnTypes.reserve(aTypes.getLength());
272 const Type* pIter = aTypes.getConstArray();
273 const Type* pEnd = pIter + aTypes.getLength();
274 for(;pIter != pEnd ;++pIter)
276 if( (*pIter != aRenameType || getRenameService().is()) && (*pIter != aAlterType || getAlterService().is()))
277 aOwnTypes.push_back(*pIter);
280 return Sequence< Type >(aOwnTypes.data(), aOwnTypes.size());
283 // XRename,
284 void SAL_CALL ODBTable::rename( const OUString& _rNewName )
286 ::osl::MutexGuard aGuard(m_aMutex);
287 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed);
288 if ( !getRenameService().is() )
289 throw SQLException(DBA_RES(RID_STR_NO_TABLE_RENAME),*this,SQLSTATE_GENERAL,1000,Any() );
291 Reference<XPropertySet> xTable(this);
292 getRenameService()->rename(xTable,_rNewName);
293 ::connectivity::OTable_TYPEDEF::rename(_rNewName);
296 // XAlterTable,
297 void SAL_CALL ODBTable::alterColumnByName( const OUString& _rName, const Reference< XPropertySet >& _rxDescriptor )
299 ::osl::MutexGuard aGuard(m_aMutex);
300 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed);
301 if ( !getAlterService().is() )
302 throw SQLException(DBA_RES(RID_STR_NO_TABLE_RENAME),*this,SQLSTATE_GENERAL,1000,Any() );
304 if ( !m_xColumns->hasByName(_rName) )
305 throw SQLException(DBA_RES(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any() );
307 Reference<XPropertySet> xTable(this);
308 getAlterService()->alterColumnByName(xTable,_rName,_rxDescriptor);
309 m_xColumns->refresh();
312 sal_Int64 SAL_CALL ODBTable::getSomething( const Sequence< sal_Int8 >& rId )
314 return comphelper::getSomethingImpl(rId, this,
315 comphelper::FallbackToGetSomethingOf<OTable_Base>{});
318 const Sequence< sal_Int8 > & ODBTable::getUnoTunnelId()
320 static const comphelper::UnoIdInit s_Id;
321 return s_Id.getSeq();
324 Reference< XPropertySet > ODBTable::createColumnDescriptor()
326 return new OTableColumnDescriptor( true );
329 sdbcx::OCollection* ODBTable::createColumns(const ::std::vector< OUString>& _rNames)
331 Reference<XDatabaseMetaData> xMeta = getMetaData();
332 OColumns* pCol = new OColumns(*this, m_aMutex, nullptr, isCaseSensitive(), _rNames, this,this,
333 getAlterService().is() || (xMeta.is() && xMeta->supportsAlterTableWithAddColumn()),
334 getAlterService().is() || (xMeta.is() && xMeta->supportsAlterTableWithDropColumn()));
335 static_cast<OColumnsHelper*>(pCol)->setParent(this);
336 pCol->setParent(*this);
337 m_pColumnMediator = new OContainerMediator( pCol, m_xColumnDefinitions );
338 pCol->setMediator( m_pColumnMediator.get() );
339 return pCol;
342 sdbcx::OCollection* ODBTable::createKeys(const ::std::vector< OUString>& _rNames)
344 return new connectivity::OKeysHelper(this,m_aMutex,_rNames);
347 sdbcx::OCollection* ODBTable::createIndexes(const ::std::vector< OUString>& _rNames)
349 return new OIndexes(this,m_aMutex,_rNames,nullptr);
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */