Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / dbaccess / source / core / api / table.cxx
blob2bb0e7d53a7a6a74ab6132e42b24d99ca9f89fa8
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 .
21 #include <string.h>
23 #include "table.hxx"
24 #include <definitioncolumn.hxx>
25 #include "dbastrings.hrc"
26 #include "core_resource.hxx"
27 #include "core_resource.hrc"
28 #include "CIndexes.hxx"
30 #include <tools/debug.hxx>
31 #include <osl/diagnose.h>
32 #include <cppuhelper/typeprovider.hxx>
33 #include <comphelper/enumhelper.hxx>
34 #include <comphelper/container.hxx>
35 #include <comphelper/sequence.hxx>
36 #include <comphelper/types.hxx>
37 #include <com/sun/star/util/XRefreshListener.hpp>
38 #include <com/sun/star/sdbc/XConnection.hpp>
39 #include <com/sun/star/sdbc/XRow.hpp>
40 #include <com/sun/star/sdbcx/Privilege.hpp>
41 #include <com/sun/star/sdbc/XResultSetMetaData.hpp>
42 #include <com/sun/star/sdbc/XResultSetMetaDataSupplier.hpp>
44 #include <connectivity/TKeys.hxx>
45 #include <connectivity/dbtools.hxx>
46 #include <connectivity/dbexception.hxx>
48 #include "sdbcoretools.hxx"
49 #include "ContainerMediator.hxx"
50 #include <rtl/logfile.hxx>
52 using namespace dbaccess;
53 using namespace connectivity;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::util;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::sdbc;
59 using namespace ::com::sun::star::sdbcx;
60 using namespace ::com::sun::star::container;
61 using namespace ::osl;
62 using namespace ::comphelper;
63 using namespace ::cppu;
65 //==========================================================================
66 //= ODBTable
67 //==========================================================================
68 DBG_NAME(ODBTable)
70 ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables
71 ,const Reference< XConnection >& _rxConn
72 ,const ::rtl::OUString& _rCatalog
73 ,const ::rtl::OUString& _rSchema
74 ,const ::rtl::OUString& _rName
75 ,const ::rtl::OUString& _rType
76 ,const ::rtl::OUString& _rDesc
77 ,const Reference< XNameAccess >& _xColumnDefinitions) throw(SQLException)
78 :OTable_Base(_pTables,_rxConn,_rxConn->getMetaData().is() && _rxConn->getMetaData()->supportsMixedCaseQuotedIdentifiers(), _rName, _rType, _rDesc, _rSchema, _rCatalog )
79 ,m_xColumnDefinitions(_xColumnDefinitions)
80 ,m_nPrivileges(0)
82 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::ODBTable" );
83 DBG_CTOR(ODBTable, NULL);
84 OSL_ENSURE(getMetaData().is(), "ODBTable::ODBTable : invalid conn !");
85 OSL_ENSURE(!_rName.isEmpty(), "ODBTable::ODBTable : name !");
86 // TODO : think about collecting the privileges here, as we can't ensure that in getFastPropertyValue, where
87 // we do this at the moment, the statement needed can be supplied by the connection (for example the SQL-Server
88 // ODBC driver does not allow more than one statement per connection, and in getFastPropertyValue it's more
89 // likely that it's already used up than it's here.)
92 ODBTable::ODBTable(connectivity::sdbcx::OCollection* _pTables
93 ,const Reference< XConnection >& _rxConn)
94 throw(SQLException)
95 :OTable_Base(_pTables,_rxConn, _rxConn->getMetaData().is() && _rxConn->getMetaData()->supportsMixedCaseQuotedIdentifiers())
96 ,m_nPrivileges(-1)
98 DBG_CTOR(ODBTable, NULL);
99 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::ODBTable" );
102 ODBTable::~ODBTable()
104 DBG_DTOR(ODBTable, NULL);
107 IMPLEMENT_FORWARD_REFCOUNT(ODBTable,OTable_Base)
109 OColumn* ODBTable::createColumn(const ::rtl::OUString& _rName) const
111 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::createColumn" );
112 OColumn* pReturn = NULL;
114 Reference<XPropertySet> xProp;
115 if ( m_xDriverColumns.is() && m_xDriverColumns->hasByName(_rName) )
117 xProp.set(m_xDriverColumns->getByName(_rName),UNO_QUERY);
119 else
121 OColumns* pColumns = static_cast<OColumns*>(m_pColumns);
122 xProp.set(pColumns->createBaseObject(_rName),UNO_QUERY);
125 Reference<XPropertySet> xColumnDefintion;
126 if ( m_xColumnDefinitions.is() && m_xColumnDefinitions->hasByName(_rName) )
127 xColumnDefintion.set(m_xColumnDefinitions->getByName(_rName),UNO_QUERY);
128 pReturn = new OTableColumnWrapper( xProp, xColumnDefintion, false );
130 return pReturn;
133 void ODBTable::columnAppended( const Reference< XPropertySet >& /*_rxSourceDescriptor*/ )
135 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::columnAppended" );
136 // not interested in
139 void ODBTable::columnDropped(const ::rtl::OUString& _sName)
141 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::columnDropped" );
142 Reference<XDrop> xDrop(m_xColumnDefinitions,UNO_QUERY);
143 if ( xDrop.is() && m_xColumnDefinitions->hasByName(_sName) )
145 xDrop->dropByName(_sName);
149 Sequence< sal_Int8 > ODBTable::getImplementationId() throw (RuntimeException)
151 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::getImplementationId" );
152 static OImplementationId * pId = 0;
153 if (! pId)
155 MutexGuard aGuard( Mutex::getGlobalMutex() );
156 if (! pId)
158 static OImplementationId aId;
159 pId = &aId;
162 return pId->getImplementationId();
165 // OComponentHelper
166 void SAL_CALL ODBTable::disposing()
168 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::disposing" );
169 OPropertySetHelper::disposing();
170 OTable_Base::disposing();
171 m_xColumnDefinitions = NULL;
172 m_xDriverColumns = NULL;
173 m_pColumnMediator = NULL;
176 void ODBTable::getFastPropertyValue(Any& _rValue, sal_Int32 _nHandle) const
178 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::getFastPropertyValue" );
179 if ((PROPERTY_ID_PRIVILEGES == _nHandle) && (-1 == m_nPrivileges))
180 { // somebody is asking for the privileges an we do not know them, yet
181 const_cast<ODBTable*>(this)->m_nPrivileges = ::dbtools::getTablePrivileges(getMetaData(),m_CatalogName,m_SchemaName, m_Name);
184 OTable_Base::getFastPropertyValue(_rValue, _nHandle);
187 void ODBTable::construct()
189 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::construct" );
190 ::osl::MutexGuard aGuard(m_aMutex);
192 // we don't collect the privileges here, this is potentially expensive. Instead we determine them on request.
193 // (see getFastPropertyValue)
194 m_nPrivileges = -1;
196 OTable_Base::construct();
198 registerProperty(PROPERTY_FILTER, PROPERTY_ID_FILTER, PropertyAttribute::BOUND,
199 &m_sFilter, ::getCppuType(&m_sFilter));
201 registerProperty(PROPERTY_ORDER, PROPERTY_ID_ORDER, PropertyAttribute::BOUND,
202 &m_sOrder, ::getCppuType(&m_sOrder));
204 registerProperty(PROPERTY_APPLYFILTER, PROPERTY_ID_APPLYFILTER, PropertyAttribute::BOUND,
205 &m_bApplyFilter, ::getBooleanCppuType());
207 registerProperty(PROPERTY_FONT, PROPERTY_ID_FONT, PropertyAttribute::BOUND,
208 &m_aFont, ::getCppuType(&m_aFont));
210 registerMayBeVoidProperty(PROPERTY_ROW_HEIGHT, PROPERTY_ID_ROW_HEIGHT, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
211 &m_aRowHeight, ::getCppuType(static_cast<sal_Int32*>(NULL)));
213 registerMayBeVoidProperty(PROPERTY_TEXTCOLOR, PROPERTY_ID_TEXTCOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
214 &m_aTextColor, ::getCppuType(static_cast<sal_Int32*>(NULL)));
216 registerProperty(PROPERTY_PRIVILEGES, PROPERTY_ID_PRIVILEGES, PropertyAttribute::BOUND | PropertyAttribute::READONLY,
217 &m_nPrivileges, ::getCppuType(static_cast<sal_Int32*>(NULL)));
219 registerMayBeVoidProperty(PROPERTY_TEXTLINECOLOR, PROPERTY_ID_TEXTLINECOLOR, PropertyAttribute::BOUND | PropertyAttribute::MAYBEVOID,
220 &m_aTextLineColor, ::getCppuType(static_cast<sal_Int32*>(NULL)));
222 registerProperty(PROPERTY_TEXTEMPHASIS, PROPERTY_ID_TEXTEMPHASIS, PropertyAttribute::BOUND,
223 &m_nFontEmphasis, ::getCppuType(&m_nFontEmphasis));
225 registerProperty(PROPERTY_TEXTRELIEF, PROPERTY_ID_TEXTRELIEF, PropertyAttribute::BOUND,
226 &m_nFontRelief, ::getCppuType(&m_nFontRelief));
228 registerProperty(PROPERTY_FONTNAME, PROPERTY_ID_FONTNAME, PropertyAttribute::BOUND,&m_aFont.Name, ::getCppuType(&m_aFont.Name));
229 registerProperty(PROPERTY_FONTHEIGHT, PROPERTY_ID_FONTHEIGHT, PropertyAttribute::BOUND,&m_aFont.Height, ::getCppuType(&m_aFont.Height));
230 registerProperty(PROPERTY_FONTWIDTH, PROPERTY_ID_FONTWIDTH, PropertyAttribute::BOUND,&m_aFont.Width, ::getCppuType(&m_aFont.Width));
231 registerProperty(PROPERTY_FONTSTYLENAME, PROPERTY_ID_FONTSTYLENAME, PropertyAttribute::BOUND,&m_aFont.StyleName, ::getCppuType(&m_aFont.StyleName));
232 registerProperty(PROPERTY_FONTFAMILY, PROPERTY_ID_FONTFAMILY, PropertyAttribute::BOUND,&m_aFont.Family, ::getCppuType(&m_aFont.Family));
233 registerProperty(PROPERTY_FONTCHARSET, PROPERTY_ID_FONTCHARSET, PropertyAttribute::BOUND,&m_aFont.CharSet, ::getCppuType(&m_aFont.CharSet));
234 registerProperty(PROPERTY_FONTPITCH, PROPERTY_ID_FONTPITCH, PropertyAttribute::BOUND,&m_aFont.Pitch, ::getCppuType(&m_aFont.Pitch));
235 registerProperty(PROPERTY_FONTCHARWIDTH, PROPERTY_ID_FONTCHARWIDTH, PropertyAttribute::BOUND,&m_aFont.CharacterWidth, ::getCppuType(&m_aFont.CharacterWidth));
236 registerProperty(PROPERTY_FONTWEIGHT, PROPERTY_ID_FONTWEIGHT, PropertyAttribute::BOUND,&m_aFont.Weight, ::getCppuType(&m_aFont.Weight));
237 registerProperty(PROPERTY_FONTSLANT, PROPERTY_ID_FONTSLANT, PropertyAttribute::BOUND,&m_aFont.Slant, ::getCppuType(&m_aFont.Slant));
238 registerProperty(PROPERTY_FONTUNDERLINE, PROPERTY_ID_FONTUNDERLINE, PropertyAttribute::BOUND,&m_aFont.Underline, ::getCppuType(&m_aFont.Underline));
239 registerProperty(PROPERTY_FONTSTRIKEOUT, PROPERTY_ID_FONTSTRIKEOUT, PropertyAttribute::BOUND,&m_aFont.Strikeout, ::getCppuType(&m_aFont.Strikeout));
240 registerProperty(PROPERTY_FONTORIENTATION, PROPERTY_ID_FONTORIENTATION, PropertyAttribute::BOUND,&m_aFont.Orientation, ::getCppuType(&m_aFont.Orientation));
241 registerProperty(PROPERTY_FONTKERNING, PROPERTY_ID_FONTKERNING, PropertyAttribute::BOUND,&m_aFont.Kerning, ::getCppuType(&m_aFont.Kerning));
242 registerProperty(PROPERTY_FONTWORDLINEMODE, PROPERTY_ID_FONTWORDLINEMODE,PropertyAttribute::BOUND,&m_aFont.WordLineMode, ::getCppuType(&m_aFont.WordLineMode));
243 registerProperty(PROPERTY_FONTTYPE, PROPERTY_ID_FONTTYPE, PropertyAttribute::BOUND,&m_aFont.Type, ::getCppuType(&m_aFont.Type));
245 refreshColumns();
248 ::cppu::IPropertyArrayHelper* ODBTable::createArrayHelper( sal_Int32 _nId) const
250 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::createArrayHelper" );
251 Sequence< Property > aProps;
252 describeProperties(aProps);
253 if(!_nId)
255 Property* pIter = aProps.getArray();
256 Property* pEnd = pIter + aProps.getLength();
257 for(;pIter != pEnd;++pIter)
259 if (pIter->Name.equalsAsciiL(PROPERTY_CATALOGNAME.ascii, PROPERTY_CATALOGNAME.length))
260 pIter->Attributes = PropertyAttribute::READONLY;
261 else if (pIter->Name.equalsAsciiL(PROPERTY_SCHEMANAME.ascii, PROPERTY_SCHEMANAME.length))
262 pIter->Attributes = PropertyAttribute::READONLY;
263 else if (pIter->Name.equalsAsciiL(PROPERTY_DESCRIPTION.ascii, PROPERTY_DESCRIPTION.length))
264 pIter->Attributes = PropertyAttribute::READONLY;
265 else if (pIter->Name.equalsAsciiL(PROPERTY_NAME.ascii, PROPERTY_NAME.length))
266 pIter->Attributes = PropertyAttribute::READONLY;
270 return new ::cppu::OPropertyArrayHelper(aProps);
273 ::cppu::IPropertyArrayHelper & SAL_CALL ODBTable::getInfoHelper()
275 return *ODBTable_PROP::getArrayHelper(isNew() ? 1 : 0);
278 // XServiceInfo
279 IMPLEMENT_SERVICE_INFO1(ODBTable, "com.sun.star.sdb.dbaccess.ODBTable", SERVICE_SDBCX_TABLE.ascii)
281 Any SAL_CALL ODBTable::queryInterface( const Type & rType ) throw(RuntimeException)
283 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::getInfoHelper" );
284 if(rType == getCppuType( (Reference<XRename>*)0) && !getRenameService().is() )
285 return Any();
286 if(rType == getCppuType( (Reference<XAlterTable>*)0) && !getAlterService().is() )
287 return Any();
288 return OTable_Base::queryInterface( rType);
291 Sequence< Type > SAL_CALL ODBTable::getTypes( ) throw(RuntimeException)
293 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::getTypes" );
294 Type aRenameType = getCppuType( (Reference<XRename>*)0);
295 Type aAlterType = getCppuType( (Reference<XAlterTable>*)0);
297 Sequence< Type > aTypes(OTable_Base::getTypes());
298 ::std::vector<Type> aOwnTypes;
299 aOwnTypes.reserve(aTypes.getLength());
301 const Type* pIter = aTypes.getConstArray();
302 const Type* pEnd = pIter + aTypes.getLength();
303 for(;pIter != pEnd ;++pIter)
305 if( (*pIter != aRenameType || getRenameService().is()) && (*pIter != aAlterType || getAlterService().is()))
306 aOwnTypes.push_back(*pIter);
309 Type* pTypes = aOwnTypes.empty() ? 0 : &aOwnTypes[0];
310 return Sequence< Type >(pTypes, aOwnTypes.size());
313 // XRename,
314 void SAL_CALL ODBTable::rename( const ::rtl::OUString& _rNewName ) throw(SQLException, ElementExistException, RuntimeException)
316 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::rename" );
317 ::osl::MutexGuard aGuard(m_aMutex);
318 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed);
319 if ( !getRenameService().is() )
320 throw SQLException(DBACORE_RESSTRING(RID_STR_NO_TABLE_RENAME),*this,SQLSTATE_GENERAL,1000,Any() );
322 Reference<XPropertySet> xTable(this);
323 getRenameService()->rename(xTable,_rNewName);
324 ::connectivity::OTable_TYPEDEF::rename(_rNewName);
327 // XAlterTable,
328 void SAL_CALL ODBTable::alterColumnByName( const ::rtl::OUString& _rName, const Reference< XPropertySet >& _rxDescriptor ) throw(SQLException, NoSuchElementException, RuntimeException)
330 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::alterColumnByName" );
331 ::osl::MutexGuard aGuard(m_aMutex);
332 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper.bDisposed);
333 if ( !getAlterService().is() )
334 throw SQLException(DBACORE_RESSTRING(RID_STR_NO_TABLE_RENAME),*this,SQLSTATE_GENERAL,1000,Any() );
336 if ( !m_pColumns->hasByName(_rName) )
337 throw SQLException(DBACORE_RESSTRING(RID_STR_COLUMN_NOT_VALID),*this,SQLSTATE_GENERAL,1000,Any() );
339 Reference<XPropertySet> xTable(this);
340 getAlterService()->alterColumnByName(xTable,_rName,_rxDescriptor);
341 m_pColumns->refresh();
344 sal_Int64 SAL_CALL ODBTable::getSomething( const Sequence< sal_Int8 >& rId ) throw(RuntimeException)
346 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::getSomething" );
347 sal_Int64 nRet(0);
348 if (rId.getLength() == 16 && 0 == memcmp(getUnoTunnelImplementationId().getConstArray(), rId.getConstArray(), 16 ) )
349 nRet = reinterpret_cast<sal_Int64>(this);
350 else
351 nRet = OTable_Base::getSomething(rId);
353 return nRet;
356 Sequence< sal_Int8 > ODBTable::getUnoTunnelImplementationId()
358 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::getUnoTunnelImplementationId" );
359 static ::cppu::OImplementationId * pId = 0;
360 if (! pId)
362 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
363 if (! pId)
365 static ::cppu::OImplementationId aId;
366 pId = &aId;
369 return pId->getImplementationId();
372 Reference< XPropertySet > ODBTable::createColumnDescriptor()
374 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::createColumnDescriptor" );
375 return new OTableColumnDescriptor( true );
378 sdbcx::OCollection* ODBTable::createColumns(const TStringVector& _rNames)
380 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::createColumns" );
381 Reference<XDatabaseMetaData> xMeta = getMetaData();
382 OColumns* pCol = new OColumns(*this, m_aMutex, NULL, isCaseSensitive(), _rNames, this,this,
383 getAlterService().is() || (xMeta.is() && xMeta->supportsAlterTableWithAddColumn()),
384 getAlterService().is() || (xMeta.is() && xMeta->supportsAlterTableWithDropColumn()));
385 static_cast<OColumnsHelper*>(pCol)->setParent(this);
386 pCol->setParent(*this);
387 m_pColumnMediator = new OContainerMediator( pCol, m_xColumnDefinitions, getConnection() );
388 pCol->setMediator( m_pColumnMediator.get() );
389 return pCol;
392 sdbcx::OCollection* ODBTable::createKeys(const TStringVector& _rNames)
394 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::createKeys" );
395 return new connectivity::OKeysHelper(this,m_aMutex,_rNames);
398 sdbcx::OCollection* ODBTable::createIndexes(const TStringVector& _rNames)
400 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "dbaccess", "Ocke.Janssen@sun.com", "ODBTable::createIndexes" );
401 return new OIndexes(this,m_aMutex,_rNames,NULL);
403 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */