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 "querydlg.hxx"
21 #include "dbu_qry.hrc"
22 #include <tools/debug.hxx>
23 #include <tools/diagnose_ex.h>
24 #include "QTableConnectionData.hxx"
25 #include "querycontroller.hxx"
26 #include "QueryTableView.hxx"
27 #include "QueryDesignView.hxx"
28 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
29 #include "RelationControl.hxx"
30 #include <vcl/msgbox.hxx>
31 #include <vcl/settings.hxx>
33 #define ID_INNER_JOIN 1
34 #define ID_LEFT_JOIN 2
35 #define ID_RIGHT_JOIN 3
36 #define ID_FULL_JOIN 4
37 #define ID_CROSS_JOIN 5
39 using namespace dbaui
;
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star::container
;
42 using namespace ::com::sun::star::sdbc
;
44 DlgQryJoin::DlgQryJoin( OQueryTableView
* pParent
,
45 const TTableConnectionData::value_type
& _pData
,
46 OJoinTableView::OTableWindowMap
* _pTableMap
,
47 const Reference
< XConnection
>& _xConnection
,
48 bool _bAllowTableSelect
)
49 : ModalDialog( pParent
, "JoinDialog", "dbaccess/ui/joindialog.ui" )
50 , m_pTableControl( NULL
)
51 , m_pTableMap(_pTableMap
)
52 , m_pTableView(pParent
)
53 , eJoinType(static_cast<OQueryTableConnectionData
*>(_pData
.get())->GetJoinType())
54 , m_pOrigConnData(_pData
)
55 , m_xConnection(_xConnection
)
57 get(m_pML_HelpText
, "helptext");
58 Size
aSize(LogicToPixel(Size(179, 49), MAP_APPFONT
));
59 //alternatively loop through the STR_QUERY_* strings with their STR_JOIN_TYPE_HINT
60 //suffix to find the longest entry at runtime
61 m_pML_HelpText
->set_height_request(aSize
.Height());
62 m_pML_HelpText
->set_width_request(aSize
.Width());
63 get(m_pLB_JoinType
, "type");
64 get(m_pCBNatural
, "natural");
67 m_pML_HelpText
->SetControlBackground( GetSettings().GetStyleSettings().GetFaceColor() );
68 // Connection kopieren
69 m_pConnData
.reset(_pData
->NewInstance());
70 m_pConnData
->CopyFrom(*_pData
);
72 m_pTableControl
= new OTableListBoxControl(this, m_pTableMap
, this);
74 m_pCBNatural
->Check(static_cast<OQueryTableConnectionData
*>(m_pConnData
.get())->isNatural());
76 if( _bAllowTableSelect
)
78 m_pTableControl
->Init( m_pConnData
);
79 m_pTableControl
->fillListBoxes();
83 m_pTableControl
->fillAndDisable(m_pConnData
);
84 m_pTableControl
->Init( m_pConnData
);
87 m_pTableControl
->lateUIInit();
89 bool bSupportFullJoin
= false;
90 Reference
<XDatabaseMetaData
> xMeta
;
93 xMeta
= m_xConnection
->getMetaData();
95 bSupportFullJoin
= xMeta
->supportsFullOuterJoins();
100 bool bSupportOuterJoin
= false;
104 bSupportOuterJoin
= xMeta
->supportsOuterJoins();
110 setJoinType(eJoinType
);
112 m_pPB_OK
->SetClickHdl( LINK(this, DlgQryJoin
, OKClickHdl
) );
114 m_pLB_JoinType
->SetSelectHdl(LINK(this,DlgQryJoin
,LBChangeHdl
));
115 m_pCBNatural
->SetToggleHdl(LINK(this,DlgQryJoin
,NaturalToggleHdl
));
117 if ( static_cast<OQueryTableView
*>(pParent
)->getDesignView()->getController().isReadOnly() )
119 m_pLB_JoinType
->Disable();
120 m_pCBNatural
->Disable();
121 m_pTableControl
->Disable();
125 const sal_uInt16 nCount
= m_pLB_JoinType
->GetEntryCount();
126 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
128 const sal_IntPtr nJoinTyp
= reinterpret_cast<sal_IntPtr
>(m_pLB_JoinType
->GetEntryData(i
));
129 if ( !bSupportFullJoin
&& nJoinTyp
== ID_FULL_JOIN
)
130 m_pLB_JoinType
->RemoveEntry(i
);
131 else if ( !bSupportOuterJoin
&& (nJoinTyp
== ID_LEFT_JOIN
|| nJoinTyp
== ID_RIGHT_JOIN
) )
132 m_pLB_JoinType
->RemoveEntry(i
);
135 m_pTableControl
->NotifyCellChange();
136 m_pTableControl
->enableRelation(!static_cast<OQueryTableConnectionData
*>(m_pConnData
.get())->isNatural() && eJoinType
!= CROSS_JOIN
);
140 DlgQryJoin::~DlgQryJoin()
145 void DlgQryJoin::dispose()
147 delete m_pTableControl
;
148 m_pML_HelpText
.clear();
150 m_pLB_JoinType
.clear();
151 m_pCBNatural
.clear();
152 m_pTableView
.clear();
153 ModalDialog::dispose();
156 IMPL_LINK( DlgQryJoin
, LBChangeHdl
, ListBox
*, /*pListBox*/ )
158 if (m_pLB_JoinType
->GetSelectEntryPos() == m_pLB_JoinType
->GetSavedValue() )
161 m_pLB_JoinType
->SaveValue();
162 m_pML_HelpText
->SetText(OUString());
164 m_pTableControl
->enableRelation(true);
166 OUString sFirstWinName
= m_pConnData
->getReferencingTable()->GetWinName();
167 OUString sSecondWinName
= m_pConnData
->getReferencedTable()->GetWinName();
168 const EJoinType eOldJoinType
= eJoinType
;
169 sal_uInt16 nResId
= 0;
170 const sal_uInt16 nPos
= m_pLB_JoinType
->GetSelectEntryPos();
171 const sal_IntPtr nJoinType
= reinterpret_cast<sal_IntPtr
>(m_pLB_JoinType
->GetEntryData(nPos
));
172 bool bAddHint
= true;
177 nResId
= STR_QUERY_INNER_JOIN
;
179 eJoinType
= INNER_JOIN
;
182 nResId
= STR_QUERY_LEFTRIGHT_JOIN
;
183 eJoinType
= LEFT_JOIN
;
187 nResId
= STR_QUERY_LEFTRIGHT_JOIN
;
188 eJoinType
= RIGHT_JOIN
;
189 OUString sTemp
= sFirstWinName
;
190 sFirstWinName
= sSecondWinName
;
191 sSecondWinName
= sTemp
;
195 nResId
= STR_QUERY_FULL_JOIN
;
196 eJoinType
= FULL_JOIN
;
200 nResId
= STR_QUERY_CROSS_JOIN
;
201 eJoinType
= CROSS_JOIN
;
203 m_pConnData
->ResetConnLines();
204 m_pTableControl
->lateInit();
205 m_pCBNatural
->Check(false);
206 m_pTableControl
->enableRelation(false);
208 m_pConnData
->AppendConnLine(sEmpty
,sEmpty
);
209 m_pPB_OK
->Enable(true);
214 m_pCBNatural
->Enable(eJoinType
!= CROSS_JOIN
);
216 if ( eJoinType
!= eOldJoinType
&& eOldJoinType
== CROSS_JOIN
)
218 m_pConnData
->ResetConnLines();
220 if ( eJoinType
!= CROSS_JOIN
)
222 m_pTableControl
->NotifyCellChange();
223 NaturalToggleHdl(m_pCBNatural
);
226 m_pTableControl
->Invalidate();
228 OUString sHelpText
= ModuleRes( nResId
);
231 sHelpText
= sHelpText
.replaceFirst( "%1", sFirstWinName
);
232 sHelpText
= sHelpText
.replaceFirst( "%2", sSecondWinName
);
237 sHelpText
+= ModuleRes( STR_JOIN_TYPE_HINT
);
240 m_pML_HelpText
->SetText( sHelpText
);
244 IMPL_LINK( DlgQryJoin
, OKClickHdl
, Button
*, /*pButton*/ )
247 m_pConnData
->Update();
248 m_pOrigConnData
->CopyFrom( *m_pConnData
);
254 IMPL_LINK( DlgQryJoin
, NaturalToggleHdl
, CheckBox
*, /*pButton*/ )
256 bool bChecked
= m_pCBNatural
->IsChecked();
257 static_cast<OQueryTableConnectionData
*>(m_pConnData
.get())->setNatural(bChecked
);
258 m_pTableControl
->enableRelation(!bChecked
);
261 m_pConnData
->ResetConnLines();
264 Reference
<XNameAccess
> xReferencedTableColumns(m_pConnData
->getReferencedTable()->getColumns());
265 Sequence
< OUString
> aSeq
= m_pConnData
->getReferencingTable()->getColumns()->getElementNames();
266 const OUString
* pIter
= aSeq
.getConstArray();
267 const OUString
* pEnd
= pIter
+ aSeq
.getLength();
268 for(;pIter
!= pEnd
;++pIter
)
270 if ( xReferencedTableColumns
->hasByName(*pIter
) )
271 m_pConnData
->AppendConnLine(*pIter
,*pIter
);
274 catch( const Exception
& )
276 DBG_UNHANDLED_EXCEPTION();
278 m_pTableControl
->NotifyCellChange();
279 m_pTableControl
->Invalidate();
285 TTableConnectionData::value_type
DlgQryJoin::getConnectionData() const
290 void DlgQryJoin::setValid(bool _bValid
)
292 m_pPB_OK
->Enable(_bValid
|| eJoinType
== CROSS_JOIN
);
295 void DlgQryJoin::notifyConnectionChange( )
297 setJoinType( static_cast<OQueryTableConnectionData
*>(m_pConnData
.get())->GetJoinType() );
298 m_pCBNatural
->Check(static_cast<OQueryTableConnectionData
*>(m_pConnData
.get())->isNatural());
299 NaturalToggleHdl(m_pCBNatural
);
302 void DlgQryJoin::setJoinType(EJoinType _eNewJoinType
)
304 eJoinType
= _eNewJoinType
;
305 m_pCBNatural
->Enable(eJoinType
!= CROSS_JOIN
);
307 sal_IntPtr nJoinType
= 0;
312 nJoinType
= ID_INNER_JOIN
;
315 nJoinType
= ID_LEFT_JOIN
;
318 nJoinType
= ID_RIGHT_JOIN
;
321 nJoinType
= ID_FULL_JOIN
;
324 nJoinType
= ID_CROSS_JOIN
;
328 const sal_uInt16 nCount
= m_pLB_JoinType
->GetEntryCount();
329 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
331 if ( nJoinType
== reinterpret_cast<sal_IntPtr
>(m_pLB_JoinType
->GetEntryData(i
)) )
333 m_pLB_JoinType
->SelectEntryPos(i
);
338 LBChangeHdl(m_pLB_JoinType
);
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */