1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <apitools.hxx>
22 #include <definitioncolumn.hxx>
23 #include <stringconstants.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/sdbc/XConnection.hpp>
33 #include <com/sun/star/sdb/tools/XTableRename.hpp>
34 #include <com/sun/star/sdb/tools/XTableAlteration.hpp>
36 #include <connectivity/TKeys.hxx>
37 #include <connectivity/dbtools.hxx>
39 #include <ContainerMediator.hxx>
41 using namespace dbaccess
;
42 using namespace connectivity
;
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::util
;
45 using namespace ::com::sun::star::lang
;
46 using namespace ::com::sun::star::beans
;
47 using namespace ::com::sun::star::sdbc
;
48 using namespace ::com::sun::star::sdbcx
;
49 using namespace ::com::sun::star::container
;
50 using namespace ::osl
;
51 using namespace ::comphelper
;
52 using namespace ::cppu
;
56 ODBTable::ODBTable(connectivity::sdbcx::OCollection
* _pTables
57 ,const Reference
< XConnection
>& _rxConn
58 ,const OUString
& _rCatalog
59 ,const OUString
& _rSchema
60 ,const OUString
& _rName
61 ,const OUString
& _rType
62 ,const OUString
& _rDesc
63 ,const Reference
< XNameAccess
>& _xColumnDefinitions
)
64 :OTable_Base(_pTables
,_rxConn
,_rxConn
->getMetaData().is() && _rxConn
->getMetaData()->supportsMixedCaseQuotedIdentifiers(), _rName
, _rType
, _rDesc
, _rSchema
, _rCatalog
)
65 ,m_xColumnDefinitions(_xColumnDefinitions
)
68 OSL_ENSURE(getMetaData().is(), "ODBTable::ODBTable : invalid conn !");
69 OSL_ENSURE(!_rName
.isEmpty(), "ODBTable::ODBTable : name !");
70 // TODO : think about collecting the privileges here, as we can't ensure that in getFastPropertyValue, where
71 // we do this at the moment, the statement needed can be supplied by the connection (for example the SQL-Server
72 // ODBC driver does not allow more than one statement per connection, and in getFastPropertyValue it's more
73 // likely that it's already used up than it's here.)
76 ODBTable::ODBTable(connectivity::sdbcx::OCollection
* _pTables
77 ,const Reference
< XConnection
>& _rxConn
)
78 :OTable_Base(_pTables
,_rxConn
, _rxConn
->getMetaData().is() && _rxConn
->getMetaData()->supportsMixedCaseQuotedIdentifiers())
87 rtl::Reference
<OColumn
> ODBTable::createColumn(const OUString
& _rName
) const
89 Reference
<XPropertySet
> xProp
;
90 if ( m_xDriverColumns
.is() && m_xDriverColumns
->hasByName(_rName
) )
92 xProp
.set(m_xDriverColumns
->getByName(_rName
),UNO_QUERY
);
96 OColumns
* pColumns
= static_cast<OColumns
*>(m_xColumns
.get());
97 xProp
.set(pColumns
->createBaseObject(_rName
),UNO_QUERY
);
100 Reference
<XPropertySet
> xColumnDefinition
;
101 if ( m_xColumnDefinitions
.is() && m_xColumnDefinitions
->hasByName(_rName
) )
102 xColumnDefinition
.set(m_xColumnDefinitions
->getByName(_rName
),UNO_QUERY
);
103 return new OTableColumnWrapper( xProp
, xColumnDefinition
, false );
106 void ODBTable::columnAppended( const Reference
< XPropertySet
>& /*_rxSourceDescriptor*/ )
111 void ODBTable::columnDropped(const OUString
& _sName
)
113 Reference
<XDrop
> xDrop(m_xColumnDefinitions
,UNO_QUERY
);
114 if ( xDrop
.is() && m_xColumnDefinitions
->hasByName(_sName
) )
116 xDrop
->dropByName(_sName
);
120 Sequence
< sal_Int8
> ODBTable::getImplementationId()
122 return css::uno::Sequence
<sal_Int8
>();
126 void SAL_CALL
ODBTable::disposing()
128 OPropertySetHelper::disposing();
129 OTable_Base::disposing();
130 m_xColumnDefinitions
= nullptr;
131 m_xDriverColumns
= nullptr;
132 m_pColumnMediator
= nullptr;
135 void ODBTable::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
137 if ((PROPERTY_ID_PRIVILEGES
== _nHandle
) && (-1 == m_nPrivileges
))
138 { // somebody is asking for the privileges and we do not know them, yet
139 const_cast<ODBTable
*>(this)->m_nPrivileges
= ::dbtools::getTablePrivileges(getMetaData(),m_CatalogName
,m_SchemaName
, m_Name
);
142 OTable_Base::getFastPropertyValue(_rValue
, _nHandle
);
145 void ODBTable::construct()
147 ::osl::MutexGuard
aGuard(m_aMutex
);
149 // we don't collect the privileges here, this is potentially expensive. Instead we determine them on request.
150 // (see getFastPropertyValue)
153 OTable_Base::construct();
155 registerProperty(PROPERTY_FILTER
, PROPERTY_ID_FILTER
, PropertyAttribute::BOUND
,
156 &m_sFilter
, cppu::UnoType
<OUString
>::get());
158 registerProperty(PROPERTY_ORDER
, PROPERTY_ID_ORDER
, PropertyAttribute::BOUND
,
159 &m_sOrder
, cppu::UnoType
<OUString
>::get());
161 registerProperty(PROPERTY_APPLYFILTER
, PROPERTY_ID_APPLYFILTER
, PropertyAttribute::BOUND
,
162 &m_bApplyFilter
, cppu::UnoType
<bool>::get());
164 registerProperty(PROPERTY_FONT
, PROPERTY_ID_FONT
, PropertyAttribute::BOUND
,
165 &m_aFont
, cppu::UnoType
<css::awt::FontDescriptor
>::get());
167 registerMayBeVoidProperty(PROPERTY_ROW_HEIGHT
, PROPERTY_ID_ROW_HEIGHT
, PropertyAttribute::BOUND
| PropertyAttribute::MAYBEVOID
,
168 &m_aRowHeight
, cppu::UnoType
<sal_Int32
>::get());
170 registerProperty(PROPERTY_AUTOGROW
, PROPERTY_ID_AUTOGROW
, PropertyAttribute::BOUND
,
171 &m_bAutoGrow
, cppu::UnoType
<bool>::get());
173 registerMayBeVoidProperty(PROPERTY_TEXTCOLOR
, PROPERTY_ID_TEXTCOLOR
, PropertyAttribute::BOUND
| PropertyAttribute::MAYBEVOID
,
174 &m_aTextColor
, cppu::UnoType
<sal_Int32
>::get());
176 registerProperty(PROPERTY_PRIVILEGES
, PROPERTY_ID_PRIVILEGES
, PropertyAttribute::BOUND
| PropertyAttribute::READONLY
,
177 &m_nPrivileges
, cppu::UnoType
<sal_Int32
>::get());
179 registerMayBeVoidProperty(PROPERTY_TEXTLINECOLOR
, PROPERTY_ID_TEXTLINECOLOR
, PropertyAttribute::BOUND
| PropertyAttribute::MAYBEVOID
,
180 &m_aTextLineColor
, cppu::UnoType
<sal_Int32
>::get());
182 registerProperty(PROPERTY_TEXTEMPHASIS
, PROPERTY_ID_TEXTEMPHASIS
, PropertyAttribute::BOUND
,
183 &m_nFontEmphasis
, cppu::UnoType
<sal_Int16
>::get());
185 registerProperty(PROPERTY_TEXTRELIEF
, PROPERTY_ID_TEXTRELIEF
, PropertyAttribute::BOUND
,
186 &m_nFontRelief
, cppu::UnoType
<sal_Int16
>::get());
188 registerProperty(PROPERTY_FONTNAME
, PROPERTY_ID_FONTNAME
, PropertyAttribute::BOUND
,&m_aFont
.Name
, cppu::UnoType
<OUString
>::get());
189 registerProperty(PROPERTY_FONTHEIGHT
, PROPERTY_ID_FONTHEIGHT
, PropertyAttribute::BOUND
,&m_aFont
.Height
, cppu::UnoType
<sal_Int16
>::get());
190 registerProperty(PROPERTY_FONTWIDTH
, PROPERTY_ID_FONTWIDTH
, PropertyAttribute::BOUND
,&m_aFont
.Width
, cppu::UnoType
<sal_Int16
>::get());
191 registerProperty(PROPERTY_FONTSTYLENAME
, PROPERTY_ID_FONTSTYLENAME
, PropertyAttribute::BOUND
,&m_aFont
.StyleName
, cppu::UnoType
<OUString
>::get());
192 registerProperty(PROPERTY_FONTFAMILY
, PROPERTY_ID_FONTFAMILY
, PropertyAttribute::BOUND
,&m_aFont
.Family
, cppu::UnoType
<sal_Int16
>::get());
193 registerProperty(PROPERTY_FONTCHARSET
, PROPERTY_ID_FONTCHARSET
, PropertyAttribute::BOUND
,&m_aFont
.CharSet
, cppu::UnoType
<sal_Int16
>::get());
194 registerProperty(PROPERTY_FONTPITCH
, PROPERTY_ID_FONTPITCH
, PropertyAttribute::BOUND
,&m_aFont
.Pitch
, cppu::UnoType
<sal_Int16
>::get());
195 registerProperty(PROPERTY_FONTCHARWIDTH
, PROPERTY_ID_FONTCHARWIDTH
, PropertyAttribute::BOUND
,&m_aFont
.CharacterWidth
, cppu::UnoType
<float>::get());
196 registerProperty(PROPERTY_FONTWEIGHT
, PROPERTY_ID_FONTWEIGHT
, PropertyAttribute::BOUND
,&m_aFont
.Weight
, cppu::UnoType
<float>::get());
197 registerProperty(PROPERTY_FONTSLANT
, PROPERTY_ID_FONTSLANT
, PropertyAttribute::BOUND
,&m_aFont
.Slant
, cppu::UnoType
<css::awt::FontSlant
>::get());
198 registerProperty(PROPERTY_FONTUNDERLINE
, PROPERTY_ID_FONTUNDERLINE
, PropertyAttribute::BOUND
,&m_aFont
.Underline
, cppu::UnoType
<sal_Int16
>::get());
199 registerProperty(PROPERTY_FONTSTRIKEOUT
, PROPERTY_ID_FONTSTRIKEOUT
, PropertyAttribute::BOUND
,&m_aFont
.Strikeout
, cppu::UnoType
<sal_Int16
>::get());
200 registerProperty(PROPERTY_FONTORIENTATION
, PROPERTY_ID_FONTORIENTATION
, PropertyAttribute::BOUND
,&m_aFont
.Orientation
, cppu::UnoType
<float>::get());
201 registerProperty(PROPERTY_FONTKERNING
, PROPERTY_ID_FONTKERNING
, PropertyAttribute::BOUND
,&m_aFont
.Kerning
, cppu::UnoType
<sal_Bool
>::get());
202 registerProperty(PROPERTY_FONTWORDLINEMODE
, PROPERTY_ID_FONTWORDLINEMODE
,PropertyAttribute::BOUND
,&m_aFont
.WordLineMode
, cppu::UnoType
<sal_Bool
>::get());
203 registerProperty(PROPERTY_FONTTYPE
, PROPERTY_ID_FONTTYPE
, PropertyAttribute::BOUND
,&m_aFont
.Type
, cppu::UnoType
<sal_Int16
>::get());
208 ::cppu::IPropertyArrayHelper
* ODBTable::createArrayHelper( sal_Int32 _nId
) const
210 Sequence
< Property
> aProps
;
211 describeProperties(aProps
);
214 for(Property
& prop
: asNonConstRange(aProps
))
216 if (prop
.Name
== PROPERTY_CATALOGNAME
)
217 prop
.Attributes
= PropertyAttribute::READONLY
;
218 else if (prop
.Name
== PROPERTY_SCHEMANAME
)
219 prop
.Attributes
= PropertyAttribute::READONLY
;
220 else if (prop
.Name
== PROPERTY_DESCRIPTION
)
221 prop
.Attributes
= PropertyAttribute::READONLY
;
222 else if (prop
.Name
== PROPERTY_NAME
)
223 prop
.Attributes
= PropertyAttribute::READONLY
;
227 return new ::cppu::OPropertyArrayHelper(aProps
);
230 ::cppu::IPropertyArrayHelper
& SAL_CALL
ODBTable::getInfoHelper()
232 return *ODBTable_PROP::getArrayHelper(isNew() ? 1 : 0);
236 IMPLEMENT_SERVICE_INFO1(ODBTable
, "com.sun.star.sdb.dbaccess.ODBTable", SERVICE_SDBCX_TABLE
)
238 Any SAL_CALL
ODBTable::queryInterface( const Type
& rType
)
240 if(rType
== cppu::UnoType
<XRename
>::get()&& !getRenameService().is() )
242 if(rType
== cppu::UnoType
<XAlterTable
>::get()&& !getAlterService().is() )
244 return OTable_Base::queryInterface( rType
);
247 Sequence
< Type
> SAL_CALL
ODBTable::getTypes( )
249 Type aRenameType
= cppu::UnoType
<XRename
>::get();
250 Type aAlterType
= cppu::UnoType
<XAlterTable
>::get();
252 Sequence
< Type
> aTypes(OTable_Base::getTypes());
253 std::vector
<Type
> aOwnTypes
;
254 aOwnTypes
.reserve(aTypes
.getLength());
256 const Type
* pIter
= aTypes
.getConstArray();
257 const Type
* pEnd
= pIter
+ aTypes
.getLength();
258 for(;pIter
!= pEnd
;++pIter
)
260 if( (*pIter
!= aRenameType
|| getRenameService().is()) && (*pIter
!= aAlterType
|| getAlterService().is()))
261 aOwnTypes
.push_back(*pIter
);
264 return Sequence
< Type
>(aOwnTypes
.data(), aOwnTypes
.size());
268 void SAL_CALL
ODBTable::rename( const OUString
& _rNewName
)
270 ::osl::MutexGuard
aGuard(m_aMutex
);
271 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper
.bDisposed
);
272 if ( !getRenameService().is() )
273 throw SQLException(DBA_RES(RID_STR_NO_TABLE_RENAME
),*this,SQLSTATE_GENERAL
,1000,Any() );
275 Reference
<XPropertySet
> xTable(this);
276 getRenameService()->rename(xTable
,_rNewName
);
277 ::connectivity::OTable_TYPEDEF::rename(_rNewName
);
281 void SAL_CALL
ODBTable::alterColumnByName( const OUString
& _rName
, const Reference
< XPropertySet
>& _rxDescriptor
)
283 ::osl::MutexGuard
aGuard(m_aMutex
);
284 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper
.bDisposed
);
285 if ( !getAlterService().is() )
286 throw SQLException(DBA_RES(RID_STR_NO_TABLE_RENAME
),*this,SQLSTATE_GENERAL
,1000,Any() );
288 if ( !m_xColumns
->hasByName(_rName
) )
289 throw SQLException(DBA_RES(RID_STR_COLUMN_NOT_VALID
),*this,SQLSTATE_GENERAL
,1000,Any() );
291 Reference
<XPropertySet
> xTable(this);
292 getAlterService()->alterColumnByName(xTable
,_rName
,_rxDescriptor
);
293 m_xColumns
->refresh();
296 sal_Int64 SAL_CALL
ODBTable::getSomething( const Sequence
< sal_Int8
>& rId
)
298 return comphelper::getSomethingImpl(rId
, this,
299 comphelper::FallbackToGetSomethingOf
<OTable_Base
>{});
302 Sequence
< sal_Int8
> ODBTable::getUnoTunnelId()
304 static const comphelper::UnoIdInit s_Id
;
305 return s_Id
.getSeq();
308 Reference
< XPropertySet
> ODBTable::createColumnDescriptor()
310 return new OTableColumnDescriptor( true );
313 sdbcx::OCollection
* ODBTable::createColumns(const ::std::vector
< OUString
>& _rNames
)
315 Reference
<XDatabaseMetaData
> xMeta
= getMetaData();
316 OColumns
* pCol
= new OColumns(*this, m_aMutex
, nullptr, isCaseSensitive(), _rNames
, this,this,
317 getAlterService().is() || (xMeta
.is() && xMeta
->supportsAlterTableWithAddColumn()),
318 getAlterService().is() || (xMeta
.is() && xMeta
->supportsAlterTableWithDropColumn()));
319 static_cast<OColumnsHelper
*>(pCol
)->setParent(this);
320 pCol
->setParent(*this);
321 m_pColumnMediator
= new OContainerMediator( pCol
, m_xColumnDefinitions
);
322 pCol
->setMediator( m_pColumnMediator
.get() );
326 sdbcx::OCollection
* ODBTable::createKeys(const ::std::vector
< OUString
>& _rNames
)
328 return new connectivity::OKeysHelper(this,m_aMutex
,_rNames
);
331 sdbcx::OCollection
* ODBTable::createIndexes(const ::std::vector
< OUString
>& _rNames
)
333 return new OIndexes(this,m_aMutex
,_rNames
,nullptr);
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */