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 <cppuhelper/typeprovider.hxx>
30 #include <comphelper/servicehelper.hxx>
31 #include <com/sun/star/beans/PropertyAttribute.hpp>
32 #include <com/sun/star/sdbc/SQLException.hpp>
33 #include <com/sun/star/sdbc/XConnection.hpp>
34 #include <com/sun/star/sdb/tools/XTableRename.hpp>
35 #include <com/sun/star/sdb/tools/XTableAlteration.hpp>
37 #include <connectivity/TKeys.hxx>
38 #include <connectivity/dbtools.hxx>
40 #include <ContainerMediator.hxx>
42 using namespace dbaccess
;
43 using namespace connectivity
;
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::util
;
46 using namespace ::com::sun::star::lang
;
47 using namespace ::com::sun::star::beans
;
48 using namespace ::com::sun::star::sdbc
;
49 using namespace ::com::sun::star::sdbcx
;
50 using namespace ::com::sun::star::container
;
51 using namespace ::osl
;
52 using namespace ::comphelper
;
53 using namespace ::cppu
;
57 ODBTable::ODBTable(connectivity::sdbcx::OCollection
* _pTables
58 ,const Reference
< XConnection
>& _rxConn
59 ,const OUString
& _rCatalog
60 ,const OUString
& _rSchema
61 ,const OUString
& _rName
62 ,const OUString
& _rType
63 ,const OUString
& _rDesc
64 ,const Reference
< XNameAccess
>& _xColumnDefinitions
)
65 :OTable_Base(_pTables
,_rxConn
,_rxConn
->getMetaData().is() && _rxConn
->getMetaData()->supportsMixedCaseQuotedIdentifiers(), _rName
, _rType
, _rDesc
, _rSchema
, _rCatalog
)
66 ,m_xColumnDefinitions(_xColumnDefinitions
)
69 OSL_ENSURE(getMetaData().is(), "ODBTable::ODBTable : invalid conn !");
70 OSL_ENSURE(!_rName
.isEmpty(), "ODBTable::ODBTable : name !");
71 // TODO : think about collecting the privileges here, as we can't ensure that in getFastPropertyValue, where
72 // we do this at the moment, the statement needed can be supplied by the connection (for example the SQL-Server
73 // ODBC driver does not allow more than one statement per connection, and in getFastPropertyValue it's more
74 // likely that it's already used up than it's here.)
77 ODBTable::ODBTable(connectivity::sdbcx::OCollection
* _pTables
78 ,const Reference
< XConnection
>& _rxConn
)
79 :OTable_Base(_pTables
,_rxConn
, _rxConn
->getMetaData().is() && _rxConn
->getMetaData()->supportsMixedCaseQuotedIdentifiers())
88 OColumn
* ODBTable::createColumn(const OUString
& _rName
) const
90 OColumn
* pReturn
= nullptr;
92 Reference
<XPropertySet
> xProp
;
93 if ( m_xDriverColumns
.is() && m_xDriverColumns
->hasByName(_rName
) )
95 xProp
.set(m_xDriverColumns
->getByName(_rName
),UNO_QUERY
);
99 OColumns
* pColumns
= static_cast<OColumns
*>(m_xColumns
.get());
100 xProp
.set(pColumns
->createBaseObject(_rName
),UNO_QUERY
);
103 Reference
<XPropertySet
> xColumnDefinition
;
104 if ( m_xColumnDefinitions
.is() && m_xColumnDefinitions
->hasByName(_rName
) )
105 xColumnDefinition
.set(m_xColumnDefinitions
->getByName(_rName
),UNO_QUERY
);
106 pReturn
= new OTableColumnWrapper( xProp
, xColumnDefinition
, false );
111 void ODBTable::columnAppended( const Reference
< XPropertySet
>& /*_rxSourceDescriptor*/ )
116 void ODBTable::columnDropped(const OUString
& _sName
)
118 Reference
<XDrop
> xDrop(m_xColumnDefinitions
,UNO_QUERY
);
119 if ( xDrop
.is() && m_xColumnDefinitions
->hasByName(_sName
) )
121 xDrop
->dropByName(_sName
);
125 Sequence
< sal_Int8
> ODBTable::getImplementationId()
127 return css::uno::Sequence
<sal_Int8
>();
131 void SAL_CALL
ODBTable::disposing()
133 OPropertySetHelper::disposing();
134 OTable_Base::disposing();
135 m_xColumnDefinitions
= nullptr;
136 m_xDriverColumns
= nullptr;
137 m_pColumnMediator
= nullptr;
140 void ODBTable::getFastPropertyValue(Any
& _rValue
, sal_Int32 _nHandle
) const
142 if ((PROPERTY_ID_PRIVILEGES
== _nHandle
) && (-1 == m_nPrivileges
))
143 { // somebody is asking for the privileges and we do not know them, yet
144 const_cast<ODBTable
*>(this)->m_nPrivileges
= ::dbtools::getTablePrivileges(getMetaData(),m_CatalogName
,m_SchemaName
, m_Name
);
147 OTable_Base::getFastPropertyValue(_rValue
, _nHandle
);
150 void ODBTable::construct()
152 ::osl::MutexGuard
aGuard(m_aMutex
);
154 // we don't collect the privileges here, this is potentially expensive. Instead we determine them on request.
155 // (see getFastPropertyValue)
158 OTable_Base::construct();
160 registerProperty(PROPERTY_FILTER
, PROPERTY_ID_FILTER
, PropertyAttribute::BOUND
,
161 &m_sFilter
, cppu::UnoType
<OUString
>::get());
163 registerProperty(PROPERTY_ORDER
, PROPERTY_ID_ORDER
, PropertyAttribute::BOUND
,
164 &m_sOrder
, cppu::UnoType
<OUString
>::get());
166 registerProperty(PROPERTY_APPLYFILTER
, PROPERTY_ID_APPLYFILTER
, PropertyAttribute::BOUND
,
167 &m_bApplyFilter
, cppu::UnoType
<bool>::get());
169 registerProperty(PROPERTY_FONT
, PROPERTY_ID_FONT
, PropertyAttribute::BOUND
,
170 &m_aFont
, cppu::UnoType
<css::awt::FontDescriptor
>::get());
172 registerMayBeVoidProperty(PROPERTY_ROW_HEIGHT
, PROPERTY_ID_ROW_HEIGHT
, PropertyAttribute::BOUND
| PropertyAttribute::MAYBEVOID
,
173 &m_aRowHeight
, cppu::UnoType
<sal_Int32
>::get());
175 registerProperty(PROPERTY_AUTOGROW
, PROPERTY_ID_AUTOGROW
, PropertyAttribute::BOUND
,
176 &m_bAutoGrow
, cppu::UnoType
<bool>::get());
178 registerMayBeVoidProperty(PROPERTY_TEXTCOLOR
, PROPERTY_ID_TEXTCOLOR
, PropertyAttribute::BOUND
| PropertyAttribute::MAYBEVOID
,
179 &m_aTextColor
, cppu::UnoType
<sal_Int32
>::get());
181 registerProperty(PROPERTY_PRIVILEGES
, PROPERTY_ID_PRIVILEGES
, PropertyAttribute::BOUND
| PropertyAttribute::READONLY
,
182 &m_nPrivileges
, cppu::UnoType
<sal_Int32
>::get());
184 registerMayBeVoidProperty(PROPERTY_TEXTLINECOLOR
, PROPERTY_ID_TEXTLINECOLOR
, PropertyAttribute::BOUND
| PropertyAttribute::MAYBEVOID
,
185 &m_aTextLineColor
, cppu::UnoType
<sal_Int32
>::get());
187 registerProperty(PROPERTY_TEXTEMPHASIS
, PROPERTY_ID_TEXTEMPHASIS
, PropertyAttribute::BOUND
,
188 &m_nFontEmphasis
, cppu::UnoType
<sal_Int16
>::get());
190 registerProperty(PROPERTY_TEXTRELIEF
, PROPERTY_ID_TEXTRELIEF
, PropertyAttribute::BOUND
,
191 &m_nFontRelief
, cppu::UnoType
<sal_Int16
>::get());
193 registerProperty(PROPERTY_FONTNAME
, PROPERTY_ID_FONTNAME
, PropertyAttribute::BOUND
,&m_aFont
.Name
, cppu::UnoType
<OUString
>::get());
194 registerProperty(PROPERTY_FONTHEIGHT
, PROPERTY_ID_FONTHEIGHT
, PropertyAttribute::BOUND
,&m_aFont
.Height
, cppu::UnoType
<sal_Int16
>::get());
195 registerProperty(PROPERTY_FONTWIDTH
, PROPERTY_ID_FONTWIDTH
, PropertyAttribute::BOUND
,&m_aFont
.Width
, cppu::UnoType
<sal_Int16
>::get());
196 registerProperty(PROPERTY_FONTSTYLENAME
, PROPERTY_ID_FONTSTYLENAME
, PropertyAttribute::BOUND
,&m_aFont
.StyleName
, cppu::UnoType
<OUString
>::get());
197 registerProperty(PROPERTY_FONTFAMILY
, PROPERTY_ID_FONTFAMILY
, PropertyAttribute::BOUND
,&m_aFont
.Family
, cppu::UnoType
<sal_Int16
>::get());
198 registerProperty(PROPERTY_FONTCHARSET
, PROPERTY_ID_FONTCHARSET
, PropertyAttribute::BOUND
,&m_aFont
.CharSet
, cppu::UnoType
<sal_Int16
>::get());
199 registerProperty(PROPERTY_FONTPITCH
, PROPERTY_ID_FONTPITCH
, PropertyAttribute::BOUND
,&m_aFont
.Pitch
, cppu::UnoType
<sal_Int16
>::get());
200 registerProperty(PROPERTY_FONTCHARWIDTH
, PROPERTY_ID_FONTCHARWIDTH
, PropertyAttribute::BOUND
,&m_aFont
.CharacterWidth
, cppu::UnoType
<float>::get());
201 registerProperty(PROPERTY_FONTWEIGHT
, PROPERTY_ID_FONTWEIGHT
, PropertyAttribute::BOUND
,&m_aFont
.Weight
, cppu::UnoType
<float>::get());
202 registerProperty(PROPERTY_FONTSLANT
, PROPERTY_ID_FONTSLANT
, PropertyAttribute::BOUND
,&m_aFont
.Slant
, cppu::UnoType
<css::awt::FontSlant
>::get());
203 registerProperty(PROPERTY_FONTUNDERLINE
, PROPERTY_ID_FONTUNDERLINE
, PropertyAttribute::BOUND
,&m_aFont
.Underline
, cppu::UnoType
<sal_Int16
>::get());
204 registerProperty(PROPERTY_FONTSTRIKEOUT
, PROPERTY_ID_FONTSTRIKEOUT
, PropertyAttribute::BOUND
,&m_aFont
.Strikeout
, cppu::UnoType
<sal_Int16
>::get());
205 registerProperty(PROPERTY_FONTORIENTATION
, PROPERTY_ID_FONTORIENTATION
, PropertyAttribute::BOUND
,&m_aFont
.Orientation
, cppu::UnoType
<float>::get());
206 registerProperty(PROPERTY_FONTKERNING
, PROPERTY_ID_FONTKERNING
, PropertyAttribute::BOUND
,&m_aFont
.Kerning
, cppu::UnoType
<sal_Bool
>::get());
207 registerProperty(PROPERTY_FONTWORDLINEMODE
, PROPERTY_ID_FONTWORDLINEMODE
,PropertyAttribute::BOUND
,&m_aFont
.WordLineMode
, cppu::UnoType
<sal_Bool
>::get());
208 registerProperty(PROPERTY_FONTTYPE
, PROPERTY_ID_FONTTYPE
, PropertyAttribute::BOUND
,&m_aFont
.Type
, cppu::UnoType
<sal_Int16
>::get());
213 ::cppu::IPropertyArrayHelper
* ODBTable::createArrayHelper( sal_Int32 _nId
) const
215 Sequence
< Property
> aProps
;
216 describeProperties(aProps
);
219 for(Property
& prop
: aProps
)
221 if (prop
.Name
== PROPERTY_CATALOGNAME
)
222 prop
.Attributes
= PropertyAttribute::READONLY
;
223 else if (prop
.Name
== PROPERTY_SCHEMANAME
)
224 prop
.Attributes
= PropertyAttribute::READONLY
;
225 else if (prop
.Name
== PROPERTY_DESCRIPTION
)
226 prop
.Attributes
= PropertyAttribute::READONLY
;
227 else if (prop
.Name
== PROPERTY_NAME
)
228 prop
.Attributes
= PropertyAttribute::READONLY
;
232 return new ::cppu::OPropertyArrayHelper(aProps
);
235 ::cppu::IPropertyArrayHelper
& SAL_CALL
ODBTable::getInfoHelper()
237 return *ODBTable_PROP::getArrayHelper(isNew() ? 1 : 0);
241 IMPLEMENT_SERVICE_INFO1(ODBTable
, "com.sun.star.sdb.dbaccess.ODBTable", SERVICE_SDBCX_TABLE
)
243 Any SAL_CALL
ODBTable::queryInterface( const Type
& rType
)
245 if(rType
== cppu::UnoType
<XRename
>::get()&& !getRenameService().is() )
247 if(rType
== cppu::UnoType
<XAlterTable
>::get()&& !getAlterService().is() )
249 return OTable_Base::queryInterface( rType
);
252 Sequence
< Type
> SAL_CALL
ODBTable::getTypes( )
254 Type aRenameType
= cppu::UnoType
<XRename
>::get();
255 Type aAlterType
= cppu::UnoType
<XAlterTable
>::get();
257 Sequence
< Type
> aTypes(OTable_Base::getTypes());
258 std::vector
<Type
> aOwnTypes
;
259 aOwnTypes
.reserve(aTypes
.getLength());
261 const Type
* pIter
= aTypes
.getConstArray();
262 const Type
* pEnd
= pIter
+ aTypes
.getLength();
263 for(;pIter
!= pEnd
;++pIter
)
265 if( (*pIter
!= aRenameType
|| getRenameService().is()) && (*pIter
!= aAlterType
|| getAlterService().is()))
266 aOwnTypes
.push_back(*pIter
);
269 return Sequence
< Type
>(aOwnTypes
.data(), aOwnTypes
.size());
273 void SAL_CALL
ODBTable::rename( const OUString
& _rNewName
)
275 ::osl::MutexGuard
aGuard(m_aMutex
);
276 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper
.bDisposed
);
277 if ( !getRenameService().is() )
278 throw SQLException(DBA_RES(RID_STR_NO_TABLE_RENAME
),*this,SQLSTATE_GENERAL
,1000,Any() );
280 Reference
<XPropertySet
> xTable(this);
281 getRenameService()->rename(xTable
,_rNewName
);
282 ::connectivity::OTable_TYPEDEF::rename(_rNewName
);
286 void SAL_CALL
ODBTable::alterColumnByName( const OUString
& _rName
, const Reference
< XPropertySet
>& _rxDescriptor
)
288 ::osl::MutexGuard
aGuard(m_aMutex
);
289 checkDisposed(connectivity::sdbcx::OTableDescriptor_BASE::rBHelper
.bDisposed
);
290 if ( !getAlterService().is() )
291 throw SQLException(DBA_RES(RID_STR_NO_TABLE_RENAME
),*this,SQLSTATE_GENERAL
,1000,Any() );
293 if ( !m_xColumns
->hasByName(_rName
) )
294 throw SQLException(DBA_RES(RID_STR_COLUMN_NOT_VALID
),*this,SQLSTATE_GENERAL
,1000,Any() );
296 Reference
<XPropertySet
> xTable(this);
297 getAlterService()->alterColumnByName(xTable
,_rName
,_rxDescriptor
);
298 m_xColumns
->refresh();
301 sal_Int64 SAL_CALL
ODBTable::getSomething( const Sequence
< sal_Int8
>& rId
)
304 if (isUnoTunnelId
<ODBTable
>(rId
))
305 nRet
= reinterpret_cast<sal_Int64
>(this);
307 nRet
= OTable_Base::getSomething(rId
);
312 Sequence
< sal_Int8
> ODBTable::getUnoTunnelId()
314 static ::cppu::OImplementationId s_Id
;
316 return s_Id
.getImplementationId();
319 Reference
< XPropertySet
> ODBTable::createColumnDescriptor()
321 return new OTableColumnDescriptor( true );
324 sdbcx::OCollection
* ODBTable::createColumns(const ::std::vector
< OUString
>& _rNames
)
326 Reference
<XDatabaseMetaData
> xMeta
= getMetaData();
327 OColumns
* pCol
= new OColumns(*this, m_aMutex
, nullptr, isCaseSensitive(), _rNames
, this,this,
328 getAlterService().is() || (xMeta
.is() && xMeta
->supportsAlterTableWithAddColumn()),
329 getAlterService().is() || (xMeta
.is() && xMeta
->supportsAlterTableWithDropColumn()));
330 static_cast<OColumnsHelper
*>(pCol
)->setParent(this);
331 pCol
->setParent(*this);
332 m_pColumnMediator
= new OContainerMediator( pCol
, m_xColumnDefinitions
);
333 pCol
->setMediator( m_pColumnMediator
.get() );
337 sdbcx::OCollection
* ODBTable::createKeys(const ::std::vector
< OUString
>& _rNames
)
339 return new connectivity::OKeysHelper(this,m_aMutex
,_rNames
);
342 sdbcx::OCollection
* ODBTable::createIndexes(const ::std::vector
< OUString
>& _rNames
)
344 return new OIndexes(this,m_aMutex
,_rNames
,nullptr);
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */