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 <RTableConnectionData.hxx>
21 #include <com/sun/star/sdbc/KeyRule.hpp>
22 #include <com/sun/star/sdbcx/KeyType.hpp>
23 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
24 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
25 #include <com/sun/star/sdbcx/XAppend.hpp>
26 #include <com/sun/star/sdbcx/XDrop.hpp>
27 #include <com/sun/star/container/XIndexAccess.hpp>
28 #include <strings.hrc>
29 #include <strings.hxx>
30 #include <core_resource.hxx>
31 #include <connectivity/dbexception.hxx>
32 #include <connectivity/dbtools.hxx>
33 #include <osl/diagnose.h>
35 using namespace dbaui
;
36 using namespace ::com::sun::star::sdbc
;
37 using namespace ::com::sun::star::sdbcx
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::beans
;
40 using namespace ::com::sun::star::container
;
41 using namespace ::com::sun::star::lang
;
43 ORelationTableConnectionData::ORelationTableConnectionData()
44 :m_nUpdateRules(KeyRule::NO_ACTION
)
45 ,m_nDeleteRules(KeyRule::NO_ACTION
)
46 ,m_nCardinality(Cardinality::Undefined
)
50 ORelationTableConnectionData::ORelationTableConnectionData( const TTableWindowData::value_type
& _pReferencingTable
,
51 const TTableWindowData::value_type
& _pReferencedTable
,
52 const OUString
& rConnName
)
53 :OTableConnectionData( _pReferencingTable
, _pReferencedTable
)
54 ,m_nUpdateRules(KeyRule::NO_ACTION
)
55 ,m_nDeleteRules(KeyRule::NO_ACTION
)
56 ,m_nCardinality(Cardinality::Undefined
)
58 m_aConnName
= rConnName
;
60 if ( !m_aConnName
.isEmpty() )
64 ORelationTableConnectionData::ORelationTableConnectionData( const ORelationTableConnectionData
& rConnData
)
65 :OTableConnectionData( rConnData
)
70 ORelationTableConnectionData::~ORelationTableConnectionData()
74 void ORelationTableConnectionData::DropRelation()
76 ::osl::MutexGuard
aGuard( m_aMutex
);
78 Reference
< XIndexAccess
> xKeys
= getReferencingTable()->getKeys();
79 if( m_aConnName
.isEmpty() || !xKeys
.is() )
82 const sal_Int32 nCount
= xKeys
->getCount();
83 for(sal_Int32 i
= 0;i
< nCount
;++i
)
85 Reference
< XPropertySet
> xKey(xKeys
->getByIndex(i
),UNO_QUERY
);
86 OSL_ENSURE(xKey
.is(),"Key is not valid!");
90 xKey
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
91 if(sName
== m_aConnName
)
93 Reference
< XDrop
> xDrop(xKeys
,UNO_QUERY
);
94 OSL_ENSURE(xDrop
.is(),"can't drop key because we haven't a drop interface!");
96 xDrop
->dropByIndex(i
);
103 void ORelationTableConnectionData::ChangeOrientation()
105 // exchange Source- and DestFieldName of the lines
106 OUString sTempString
;
107 for (auto const& elem
: m_vConnLineData
)
109 sTempString
= elem
->GetSourceFieldName();
110 elem
->SetSourceFieldName( elem
->GetDestFieldName() );
111 elem
->SetDestFieldName( sTempString
);
115 std::swap( m_pReferencingTable
, m_pReferencedTable
);
118 void ORelationTableConnectionData::SetCardinality()
120 ::osl::MutexGuard
aGuard( m_aMutex
);
121 m_nCardinality
= Cardinality::Undefined
;
123 if( IsSourcePrimKey() )
125 if( IsDestPrimKey() )
126 m_nCardinality
= Cardinality::OneOne
;
128 m_nCardinality
= Cardinality::OneMany
;
131 if( IsDestPrimKey() )
133 if( !IsSourcePrimKey() )
134 m_nCardinality
= Cardinality::ManyOne
;
139 bool ORelationTableConnectionData::checkPrimaryKey(const Reference
< XPropertySet
>& i_xTable
,EConnectionSide _eEConnectionSide
) const
141 // check if Table has the primary key column depending on _eEConnectionSide
142 sal_uInt16 nPrimKeysCount
= 0,
143 nValidLinesCount
= 0;
144 const Reference
< XNameAccess
> xKeyColumns
= dbtools::getPrimaryKeyColumns_throw(i_xTable
);
145 if ( xKeyColumns
.is() )
147 Sequence
< OUString
> aKeyColumns
= xKeyColumns
->getElementNames();
149 for (auto& keyColumn
: aKeyColumns
)
151 for (auto const& elem
: m_vConnLineData
)
154 if (elem
->GetFieldName(_eEConnectionSide
) == keyColumn
)
161 if ( nPrimKeysCount
!= aKeyColumns
.getLength() )
164 return nPrimKeysCount
&& nPrimKeysCount
== nValidLinesCount
;
167 void ORelationTableConnectionData::IsConnectionPossible()
169 ::osl::MutexGuard
aGuard( m_aMutex
);
171 // if the SourceFields are a PrimKey, it's only the orientation which is wrong
172 if ( IsSourcePrimKey() && !IsDestPrimKey() )
176 void ORelationTableConnectionData::CopyFrom(const OTableConnectionData
& rSource
)
178 // retract to the (non-virtual) operator= like in the base class
179 *this = *static_cast<const ORelationTableConnectionData
*>(&rSource
);
182 ORelationTableConnectionData
& ORelationTableConnectionData::operator=( const ORelationTableConnectionData
& rConnData
)
184 if (&rConnData
== this)
187 OTableConnectionData::operator=( rConnData
);
188 m_nUpdateRules
= rConnData
.GetUpdateRules();
189 m_nDeleteRules
= rConnData
.GetDeleteRules();
190 m_nCardinality
= rConnData
.GetCardinality();
197 bool operator==(const ORelationTableConnectionData
& lhs
, const ORelationTableConnectionData
& rhs
)
199 bool bEqual
= (lhs
.m_nUpdateRules
== rhs
.m_nUpdateRules
)
200 && (lhs
.m_nDeleteRules
== rhs
.m_nDeleteRules
)
201 && (lhs
.m_nCardinality
== rhs
.m_nCardinality
)
202 && (lhs
.getReferencingTable() == rhs
.getReferencingTable())
203 && (lhs
.getReferencedTable() == rhs
.getReferencedTable())
204 && (lhs
.m_aConnName
== rhs
.m_aConnName
)
205 && (lhs
.m_vConnLineData
.size() == rhs
.m_vConnLineData
.size());
210 for (auto const& elem
: lhs
.m_vConnLineData
)
212 if ( *(rhs
.m_vConnLineData
[i
]) != *elem
)
225 bool ORelationTableConnectionData::Update()
227 ::osl::MutexGuard
aGuard( m_aMutex
);
228 // delete old relation
231 IsConnectionPossible();
234 // reassign the keys because the orientation could be changed
235 Reference
<XPropertySet
> xTableProp(getReferencingTable()->getTable());
236 Reference
< XIndexAccess
> xKeys ( getReferencingTable()->getKeys());
240 // create new relation
241 Reference
<XDataDescriptorFactory
> xKeyFactory(xKeys
,UNO_QUERY
);
242 OSL_ENSURE(xKeyFactory
.is(),"No XDataDescriptorFactory Interface!");
243 Reference
<XAppend
> xAppend(xKeyFactory
,UNO_QUERY
);
244 OSL_ENSURE(xAppend
.is(),"No XAppend Interface!");
246 Reference
<XPropertySet
> xKey
= xKeyFactory
->createDataDescriptor();
247 OSL_ENSURE(xKey
.is(),"Key is null!");
248 if ( xKey
.is() && xTableProp
.is() )
250 // build a foreign key name
251 OUString sSourceName
;
252 xTableProp
->getPropertyValue(PROPERTY_NAME
) >>= sSourceName
;
253 OUString sKeyName
= sSourceName
+ getReferencedTable()->GetTableName();
255 xKey
->setPropertyValue(PROPERTY_NAME
,Any(sKeyName
));
256 xKey
->setPropertyValue(PROPERTY_TYPE
,Any(KeyType::FOREIGN
));
257 // get the full name of the tables to ensure uniqueness across catalogs and schema
258 xKey
->setPropertyValue(PROPERTY_REFERENCEDTABLE
,Any(getReferencedTable()->GetComposedName()));
259 xKey
->setPropertyValue(PROPERTY_UPDATERULE
, Any(GetUpdateRules()));
260 xKey
->setPropertyValue(PROPERTY_DELETERULE
, Any(GetDeleteRules()));
263 Reference
<XColumnsSupplier
> xColSup(xKey
,UNO_QUERY
);
266 Reference
<XNameAccess
> xColumns
= xColSup
->getColumns();
267 Reference
<XDataDescriptorFactory
> xColumnFactory(xColumns
,UNO_QUERY
);
268 Reference
<XAppend
> xColumnAppend(xColumns
,UNO_QUERY
);
269 if ( xColumnFactory
.is() )
271 for (auto const& elem
: m_vConnLineData
)
273 if(!(elem
->GetSourceFieldName().isEmpty() || elem
->GetDestFieldName().isEmpty()))
275 Reference
<XPropertySet
> xColumn
= xColumnFactory
->createDataDescriptor();
278 xColumn
->setPropertyValue(PROPERTY_NAME
,Any(elem
->GetSourceFieldName()));
279 xColumn
->setPropertyValue(PROPERTY_RELATEDCOLUMN
,Any(elem
->GetDestFieldName()));
280 xColumnAppend
->appendByDescriptor(xColumn
);
285 if ( xColumns
->hasElements() )
286 xAppend
->appendByDescriptor(xKey
);
288 // to get the key we have to reget it because after append it is no longer valid
291 // get the name of foreign key; search for columns
294 bool bDropRelation
= false;
295 for(sal_Int32 i
=0;i
<xKeys
->getCount();++i
)
297 xKeys
->getByIndex(i
) >>= xKey
;
298 OSL_ENSURE(xKey
.is(),"Key is not valid!");
301 OUString sReferencedTable
;
302 xKey
->getPropertyValue(PROPERTY_REFERENCEDTABLE
) >>= sReferencedTable
;
303 if ( sReferencedTable
== getReferencedTable()->GetComposedName() )
305 xColSup
.set(xKey
,UNO_QUERY_THROW
);
308 Reference
<XNameAccess
> xColumns
= xColSup
->getColumns();
309 Sequence
< OUString
> aNames
= xColumns
->getElementNames();
310 const OUString
* pIter
= aNames
.begin();
311 const OUString
* pEnd
= aNames
.end();
313 OUString sName
,sRelatedColumn
;
314 for ( ; pIter
!= pEnd
; ++pIter
)
316 Reference
<XPropertySet
> xColumn(xColumns
->getByName(*pIter
),UNO_QUERY_THROW
);
317 xColumn
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
318 xColumn
->getPropertyValue(PROPERTY_RELATEDCOLUMN
) >>= sRelatedColumn
;
320 if (std::none_of(m_vConnLineData
.begin(), m_vConnLineData
.end(),
321 [&sName
, &sRelatedColumn
](auto& elem
) {
322 return elem
->GetSourceFieldName() == sName
323 && elem
->GetDestFieldName() == sRelatedColumn
;
329 xKey
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
331 bDropRelation
= !aNames
.hasElements(); // the key contains no column, so it isn't valid and we have to drop it
332 //here we already know our column structure so we don't have to recreate the table connection data
347 OUString
sError(DBA_RES(STR_QUERY_REL_COULD_NOT_CREATE
));
348 ::dbtools::throwGenericSQLException(sError
,nullptr);
351 // The fields the relation marks may not be the same as our LineDatas mark after the relation has been updated
354 OConnectionLineDataVec().swap(m_vConnLineData
);
355 Reference
<XNameAccess
> xColumns
= xColSup
->getColumns();
356 Sequence
< OUString
> aNames
= xColumns
->getElementNames();
358 m_vConnLineData
.reserve( aNames
.getLength() );
359 Reference
<XPropertySet
> xColumn
;
360 OUString sName
,sRelatedColumn
;
362 for (auto& colName
: aNames
)
364 xColumns
->getByName(colName
) >>= xColumn
;
367 OConnectionLineDataRef pNewData
= new OConnectionLineData();
369 xColumn
->getPropertyValue(PROPERTY_NAME
) >>= sName
;
370 xColumn
->getPropertyValue(PROPERTY_RELATEDCOLUMN
) >>= sRelatedColumn
;
372 pNewData
->SetSourceFieldName(sName
);
373 pNewData
->SetDestFieldName(sRelatedColumn
);
374 m_vConnLineData
.push_back(pNewData
);
378 // NOTE : the caller is responsible for updating any other objects referencing the old LineDatas (for instance a ConnLine)
380 // determine cardinality
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */