Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / core / api / table.cxx
blob6c2ac8e039abe60288c27b29cc119f6f426f3437
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 <string.h>
22 #include "table.hxx"
23 #include <definitioncolumn.hxx>
24 #include "dbastrings.hrc"
25 #include "core_resource.hxx"
26 #include "core_resource.hrc"
27 #include "CIndexes.hxx"
29 #include <tools/debug.hxx>
30 #include <osl/diagnose.h>
31 #include <cppuhelper/typeprovider.hxx>
32 #include <comphelper/enumhelper.hxx>
33 #include <comphelper/container.hxx>
34 #include <comphelper/sequence.hxx>
35 #include <comphelper/types.hxx>
36 #include <com/sun/star/util/XRefreshListener.hpp>
37 #include <com/sun/star/sdbc/XConnection.hpp>
38 #include <com/sun/star/sdbc/XRow.hpp>
39 #include <com/sun/star/sdbcx/Privilege.hpp>
40 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
41 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
43 #include <connectivity/TKeys.hxx>
44 #include <connectivity/dbtools.hxx>
45 #include <connectivity/dbexception.hxx>
47 #include "sdbcoretools.hxx"
48 #include "ContainerMediator.hxx"
50 using namespace dbaccess;
51 using namespace connectivity;
52 using namespace ::com::sun::star::uno;
53 using namespace ::com::sun::star::util;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::sdbc;
57 using namespace ::com::sun::star::sdbcx;
58 using namespace ::com::sun::star::container;
59 using namespace ::osl;
60 using namespace ::comphelper;
61 using namespace ::cppu;
63 // ODBTable
65 ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables
66 ,const Reference< XConnection >& _rxConn
67 ,const OUString& _rCatalog
68 ,const OUString& _rSchema
69 ,const OUString& _rName
70 ,const OUString& _rType
71 ,const OUString& _rDesc
72 ,const Reference< XNameAccess >& _xColumnDefinitions) throw(SQLException)
73 :OTable_Base(_pTables,_rxConn,_rxConn->getMetaData().is() && _rxConn->getMetaData()->supportsMixedCaseQuotedIdentifiers(), _rName, _rType, _rDesc, _rSchema, _rCatalog )
74 ,m_xColumnDefinitions(_xColumnDefinitions)
75 ,m_nPrivileges(0)
77 OSL_ENSURE(getMetaData().is(), "ODBTable::ODBTable : invalid conn !");
78 OSL_ENSURE(!_rName.isEmpty(), "ODBTable::ODBTable : name !");
79 // TODO : think about collecting the privileges here, as we can't ensure that in getFastPropertyValue, where
80 // we do this at the moment, the statement needed can be supplied by the connection (for example the SQL-Server
81 // ODBC driver does not allow more than one statement per connection, and in getFastPropertyValue it's more
82 // likely that it's already used up than it's here.)
85 ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables
86 ,const Reference< XConnection >& _rxConn)
87 throw(SQLException)
88 :OTable_Base(_pTables,_rxConn, _rxConn->getMetaData().is() && _rxConn->getMetaData()->supportsMixedCaseQuotedIdentifiers())
89 ,m_nPrivileges(-1)
93 ODBTable::~ODBTable()
97 IMPLEMENT_FORWARD_REFCOUNT(ODBTable,OTable_Base)
99 OColumn* ODBTable::createColumn(const OUString& _rName) const
101 OColumn* pReturn = NULL;
103 Reference<XPropertySet> xProp;
104 if ( m_xDriverColumns.is() && m_xDriverColumns->hasByName(_rName) )
106 xProp.set(m_xDriverColumns->getByName(_rName),UNO_QUERY);
108 else
110 OColumns* pColumns = static_cast<OColumns*>(m_pColumns);
111 xProp.set(pColumns->createBaseObject(_rName),UNO_QUERY);
114 Reference<XPropertySet> xColumnDefintion;
115 if ( m_xColumnDefinitions.is() && m_xColumnDefinitions->hasByName(_rName) )
116 xColumnDefintion.set(m_xColumnDefinitions->getByName(_rName),UNO_QUERY);
117 pReturn = new OTableColumnWrapper( xProp, xColumnDefintion, false );
119 return pReturn;
122 void ODBTable::columnAppended( const Reference< XPropertySet >& /*_rxSourceDescriptor*/ )
124 // not interested in
127 void ODBTable::columnDropped(const OUString& _sName)
129 Reference<XDrop> xDrop(m_xColumnDefinitions,UNO_QUERY);
130 if ( xDrop.is() && m_xColumnDefinitions->hasByName(_sName) )
132 xDrop->dropByName(_sName);
136 Sequence< sal_Int8 > ODBTable::getImplementationId() throw (RuntimeException, std::exception)
138 return css::uno::Sequence<sal_Int8>();
141 // OComponentHelper
142 void SAL_CALL ODBTable::disposing()
144 OPropertySetHelper::disposing();
145 OTable_Base::disposing();
146 m_xColumnDefinitions = NULL;
147 m_xDriverColumns = NULL;
148 m_pColumnMediator = NULL;
151 void ODBTable::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
153 if ((PROPERTY_ID_PRIVILEGES == _nHandle) && (-1 == m_nPrivileges))
154 { // somebody is asking for the privileges an we do not know them, yet
155 const_cast<ODBTable*>(this)->m_nPrivileges = ::dbtools::getTablePrivileges(getMetaData(),m_CatalogName,m_SchemaName, m_Name);
158 OTable_Base::getFastPropertyValue(_rValue, _nHandle);
161 void ODBTable::construct()
163 ::osl::MutexGuard aGuard(m_aMutex);
165 // we don't collect the privileges here, this is potentially expensive. Instead we determine them on request.
166 // (see getFastPropertyValue)
167 m_nPrivileges = -1;
169 OTable_Base::construct();
171 registerProperty(PROPERTY_FILTER, PROPERTY_ID_FILTER, PropertyAttribute::BOUND,
172 &m_sFilter, cppu::UnoType<OUString>::get());
174 registerProperty(PROPERTY_ORDER, PROPERTY_ID_ORDER, PropertyAttribute::BOUND,
175 &m_sOrder, cppu::UnoType<OUString>::get());
177 registerProperty(PROPERTY_APPLYFILTER, PROPERTY_ID_APPLYFILTER, PropertyAttribute::BOUND,
178 &m_bApplyFilter, cppu::UnoType<bool>::get());
180 registerProperty(PROPERTY_FONT, PROPERTY_ID_FONT, PropertyAttribute::BOUND,
181 &m_aFont, cppu::UnoType<com::sun::star::awt::FontDescriptor>::get());
183 registerMayBeVoidProperty(PROPERTY_ROW_HEIGHT, PROPERTY_ID_ROW_HEIGHT, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
184 &m_aRowHeight, cppu::UnoType<sal_Int32>::get());
186 registerMayBeVoidProperty(PROPERTY_TEXTCOLOR, PROPERTY_ID_TEXTCOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
187 &m_aTextColor, cppu::UnoType<sal_Int32>::get());
189 registerProperty(PROPERTY_PRIVILEGES, PROPERTY_ID_PRIVILEGES, PropertyAttribute::BOUND | PropertyAttribute::READONLY,
190 &m_nPrivileges, cppu::UnoType<sal_Int32>::get());
192 registerMayBeVoidProperty(PROPERTY_TEXTLINECOLOR, PROPERTY_ID_TEXTLINECOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
193 &m_aTextLineColor, cppu::UnoType<sal_Int32>::get());
195 registerProperty(PROPERTY_TEXTEMPHASIS, PROPERTY_ID_TEXTEMPHASIS, PropertyAttribute::BOUND,
196 &m_nFontEmphasis, cppu::UnoType<sal_Int16>::get());
198 registerProperty(PROPERTY_TEXTRELIEF, PROPERTY_ID_TEXTRELIEF, PropertyAttribute::BOUND,
199 &m_nFontRelief, cppu::UnoType<sal_Int16>::get());
201 registerProperty(PROPERTY_FONTNAME, PROPERTY_ID_FONTNAME, PropertyAttribute::BOUND,&m_aFont.Name, cppu::UnoType<OUString>::get());
202 registerProperty(PROPERTY_FONTHEIGHT, PROPERTY_ID_FONTHEIGHT, PropertyAttribute::BOUND,&m_aFont.Height, cppu::UnoType<sal_Int16>::get());
203 registerProperty(PROPERTY_FONTWIDTH, PROPERTY_ID_FONTWIDTH, PropertyAttribute::BOUND,&m_aFont.Width, cppu::UnoType<sal_Int16>::get());
204 registerProperty(PROPERTY_FONTSTYLENAME, PROPERTY_ID_FONTSTYLENAME, PropertyAttribute::BOUND,&m_aFont.StyleName, cppu::UnoType<OUString>::get());
205 registerProperty(PROPERTY_FONTFAMILY, PROPERTY_ID_FONTFAMILY, PropertyAttribute::BOUND,&m_aFont.Family, cppu::UnoType<sal_Int16>::get());
206 registerProperty(PROPERTY_FONTCHARSET, PROPERTY_ID_FONTCHARSET, PropertyAttribute::BOUND,&m_aFont.CharSet, cppu::UnoType<sal_Int16>::get());
207 registerProperty(PROPERTY_FONTPITCH, PROPERTY_ID_FONTPITCH, PropertyAttribute::BOUND,&m_aFont.Pitch, cppu::UnoType<sal_Int16>::get());
208 registerProperty(PROPERTY_FONTCHARWIDTH, PROPERTY_ID_FONTCHARWIDTH, PropertyAttribute::BOUND,&m_aFont.CharacterWidth, cppu::UnoType<float>::get());
209 registerProperty(PROPERTY_FONTWEIGHT, PROPERTY_ID_FONTWEIGHT, PropertyAttribute::BOUND,&m_aFont.Weight, cppu::UnoType<float>::get());
210 registerProperty(PROPERTY_FONTSLANT, PROPERTY_ID_FONTSLANT, PropertyAttribute::BOUND,&m_aFont.Slant, cppu::UnoType<css::awt::FontSlant>::get());
211 registerProperty(PROPERTY_FONTUNDERLINE, PROPERTY_ID_FONTUNDERLINE, PropertyAttribute::BOUND,&m_aFont.Underline, cppu::UnoType<sal_Int16>::get());
212 registerProperty(PROPERTY_FONTSTRIKEOUT, PROPERTY_ID_FONTSTRIKEOUT, PropertyAttribute::BOUND,&m_aFont.Strikeout, cppu::UnoType<sal_Int16>::get());
213 registerProperty(PROPERTY_FONTORIENTATION, PROPERTY_ID_FONTORIENTATION, PropertyAttribute::BOUND,&m_aFont.Orientation, cppu::UnoType<float>::get());
214 registerProperty(PROPERTY_FONTKERNING, PROPERTY_ID_FONTKERNING, PropertyAttribute::BOUND,&m_aFont.Kerning, cppu::UnoType<sal_Bool>::get());
215 registerProperty(PROPERTY_FONTWORDLINEMODE, PROPERTY_ID_FONTWORDLINEMODE,PropertyAttribute::BOUND,&m_aFont.WordLineMode, cppu::UnoType<sal_Bool>::get());
216 registerProperty(PROPERTY_FONTTYPE, PROPERTY_ID_FONTTYPE, PropertyAttribute::BOUND,&m_aFont.Type, cppu::UnoType<sal_Int16>::get());
218 refreshColumns();
221 ::cppu::IPropertyArrayHelper* ODBTable::createArrayHelper( sal_Int32 _nId) const
223 Sequence< Property > aProps;
224 describeProperties(aProps);
225 if(!_nId)
227 Property* pIter = aProps.getArray();
228 Property* pEnd = pIter + aProps.getLength();
229 for(;pIter != pEnd;++pIter)
231 if (pIter->Name == PROPERTY_CATALOGNAME)
232 pIter->Attributes = PropertyAttribute::READONLY;
233 else if (pIter->Name == PROPERTY_SCHEMANAME)
234 pIter->Attributes = PropertyAttribute::READONLY;
235 else if (pIter->Name == PROPERTY_DESCRIPTION)
236 pIter->Attributes = PropertyAttribute::READONLY;
237 else if (pIter->Name == PROPERTY_NAME)
238 pIter->Attributes = PropertyAttribute::READONLY;
242 return new ::cppu::OPropertyArrayHelper(aProps);
245 ::cppu::IPropertyArrayHelper & SAL_CALL ODBTable::getInfoHelper()
247 return *ODBTable_PROP::getArrayHelper(isNew() ? 1 : 0);
250 // XServiceInfo
251 IMPLEMENT_SERVICE_INFO1(ODBTable, "com.sun.star.sdb.dbaccess.ODBTable", SERVICE_SDBCX_TABLE)
253 Any SAL_CALL ODBTable::queryInterface( const Type & rType ) throw(RuntimeException, std::exception)
255 if(rType == cppu::UnoType<XRename>::get()&& !getRenameService().is() )
256 return Any();
257 if(rType == cppu::UnoType<XAlterTable>::get()&& !getAlterService().is() )
258 return Any();
259 return OTable_Base::queryInterface( rType);
262 Sequence< Type > SAL_CALL ODBTable::getTypes( ) throw(RuntimeException, std::exception)
264 Type aRenameType = cppu::UnoType<XRename>::get();
265 Type aAlterType = cppu::UnoType<XAlterTable>::get();
267 Sequence< Type > aTypes(OTable_Base::getTypes());
268 ::std::vector<Type> aOwnTypes;
269 aOwnTypes.reserve(aTypes.getLength());
271 const Type* pIter = aTypes.getConstArray();
272 const Type* pEnd = pIter + aTypes.getLength();
273 for(;pIter != pEnd ;++pIter)
275 if( (*pIter != aRenameType || getRenameService().is()) && (*pIter != aAlterType || getAlterService().is()))
276 aOwnTypes.push_back(*pIter);
279 return Sequence< Type >(aOwnTypes.data(), aOwnTypes.size());
282 // XRename,
283 void SAL_CALL ODBTable::rename( const OUString& _rNewName ) throw(SQLException, ElementExistException, RuntimeException, std::exception)
285 ::osl::MutexGuard aGuard(m_aMutex);
286 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed);
287 if ( !getRenameService().is() )
288 throw SQLException(DBACORE_RESSTRING(RID_STR_NO_TABLE_RENAME),*this,SQLSTATE_GENERAL,1000,Any() );
290 Reference<XPropertySet> xTable(this);
291 getRenameService()->rename(xTable,_rNewName);
292 ::connectivity::OTable_TYPEDEF::rename(_rNewName);
295 // XAlterTable,
296 void SAL_CALL ODBTable::alterColumnByName( const OUString& _rName, const Reference< XPropertySet >& _rxDescriptor ) throw(SQLException, NoSuchElementException, RuntimeException, std::exception)
298 ::osl::MutexGuard aGuard(m_aMutex);
299 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed);
300 if ( !getAlterService().is() )
301 throw SQLException(DBACORE_RESSTRING(RID_STR_NO_TABLE_RENAME),*this,SQLSTATE_GENERAL,1000,Any() );
303 if ( !m_pColumns->hasByName(_rName) )
304 throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any() );
306 Reference<XPropertySet> xTable(this);
307 getAlterService()->alterColumnByName(xTable,_rName,_rxDescriptor);
308 m_pColumns->refresh();
311 sal_Int64 SAL_CALL ODBTable::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException, std::exception)
313 sal_Int64 nRet(0);
314 if (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
315 nRet = reinterpret_cast<sal_Int64>(this);
316 else
317 nRet = OTable_Base::getSomething(rId);
319 return nRet;
322 Sequence< sal_Int8 > ODBTable::getUnoTunnelImplementationId()
324 static ::cppu::OImplementationId * pId = 0;
325 if (! pId)
327 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
328 if (! pId)
330 static ::cppu::OImplementationId aId;
331 pId = &aId;
334 return pId->getImplementationId();
337 Reference< XPropertySet > ODBTable::createColumnDescriptor()
339 return new OTableColumnDescriptor( true );
342 sdbcx::OCollection* ODBTable::createColumns(const TStringVector& _rNames)
344 Reference<XDatabaseMetaData> xMeta = getMetaData();
345 OColumns* pCol = new OColumns(*this, m_aMutex, NULL, isCaseSensitive(), _rNames, this,this,
346 getAlterService().is() || (xMeta.is() && xMeta->supportsAlterTableWithAddColumn()),
347 getAlterService().is() || (xMeta.is() && xMeta->supportsAlterTableWithDropColumn()));
348 static_cast<OColumnsHelper*>(pCol)->setParent(this);
349 pCol->setParent(*this);
350 m_pColumnMediator = new OContainerMediator( pCol, m_xColumnDefinitions, getConnection() );
351 pCol->setMediator( m_pColumnMediator.get() );
352 return pCol;
355 sdbcx::OCollection* ODBTable::createKeys(const TStringVector& _rNames)
357 return new connectivity::OKeysHelper(this,m_aMutex,_rNames);
360 sdbcx::OCollection* ODBTable::createIndexes(const TStringVector& _rNames)
362 return new OIndexes(this,m_aMutex,_rNames,NULL);
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */