LibreOffice Module dbaccess (master) 1
querydlg.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "querydlg.hxx"
21#include <JoinController.hxx>
22#include <JoinDesignView.hxx>
23#include <strings.hrc>
26#include <core_resource.hxx>
27#include <QueryTableView.hxx>
28#include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
29#include <com/sun/star/sdbc/SQLException.hpp>
30#include <RelationControl.hxx>
31
32#define ID_INNER_JOIN 1
33#define ID_LEFT_JOIN 2
34#define ID_RIGHT_JOIN 3
35#define ID_FULL_JOIN 4
36#define ID_CROSS_JOIN 5
37
38using namespace dbaui;
39using namespace ::com::sun::star::uno;
40using namespace ::com::sun::star::container;
41using namespace ::com::sun::star::sdbc;
42
43DlgQryJoin::DlgQryJoin(const OQueryTableView* pParent,
44 const TTableConnectionData::value_type& _pData,
45 const OJoinTableView::OTableWindowMap* _pTableMap,
46 const Reference< XConnection >& _xConnection,
47 bool _bAllowTableSelect)
48 : GenericDialogController(pParent->GetFrameWeld(), "dbaccess/ui/joindialog.ui", "JoinDialog")
49 , eJoinType(static_cast<OQueryTableConnectionData*>(_pData.get())->GetJoinType())
50 , m_pOrigConnData(_pData)
51 , m_xConnection(_xConnection)
52 , m_xML_HelpText(m_xBuilder->weld_label("helptext"))
53 , m_xPB_OK(m_xBuilder->weld_button("ok"))
54 , m_xLB_JoinType(m_xBuilder->weld_combo_box("type"))
55 , m_xCBNatural(m_xBuilder->weld_check_button("natural"))
56{
57 Size aSize(m_xML_HelpText->get_approximate_digit_width() * 44,
58 m_xML_HelpText->get_text_height() * 6);
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_xML_HelpText->set_size_request(aSize.Width(), aSize.Height());
62
63 // Copy connection
64 m_pConnData = _pData->NewInstance();
65 m_pConnData->CopyFrom(*_pData);
66
67 m_xTableControl.reset(new OTableListBoxControl(m_xBuilder.get(), _pTableMap, this));
68
69 m_xCBNatural->set_active(static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural());
70
71 if( _bAllowTableSelect )
72 {
74 m_xTableControl->fillListBoxes();
75 }
76 else
77 {
78 m_xTableControl->fillAndDisable(m_pConnData);
80 }
81
82 m_xTableControl->lateUIInit();
83
84 bool bSupportFullJoin = false;
86 try
87 {
88 xMeta = m_xConnection->getMetaData();
89 if ( xMeta.is() )
90 bSupportFullJoin = xMeta->supportsFullOuterJoins();
91 }
92 catch(SQLException&)
93 {
94 }
95 bool bSupportOuterJoin = false;
96 try
97 {
98 if ( xMeta.is() )
99 bSupportOuterJoin= xMeta->supportsOuterJoins();
100 }
101 catch(SQLException&)
102 {
103 }
104
106
107 m_xPB_OK->connect_clicked(LINK(this, DlgQryJoin, OKClickHdl));
108
109 m_xLB_JoinType->connect_changed(LINK(this,DlgQryJoin,LBChangeHdl));
110 m_xCBNatural->connect_toggled(LINK(this,DlgQryJoin,NaturalToggleHdl));
111
112 if ( pParent->getDesignView()->getController().isReadOnly() )
113 {
114 m_xLB_JoinType->set_sensitive(false);
115 m_xCBNatural->set_sensitive(false);
116 m_xTableControl->Disable();
117 }
118 else
119 {
120 for (sal_Int32 i = 0; i < m_xLB_JoinType->get_count();)
121 {
122 const sal_Int32 nJoinTyp = m_xLB_JoinType->get_id(i).toInt32();
123 if ( !bSupportFullJoin && nJoinTyp == ID_FULL_JOIN )
124 m_xLB_JoinType->remove(i);
125 else if ( !bSupportOuterJoin && (nJoinTyp == ID_LEFT_JOIN || nJoinTyp == ID_RIGHT_JOIN) )
126 m_xLB_JoinType->remove(i);
127 else
128 ++i;
129 }
130
131 m_xTableControl->NotifyCellChange();
132 m_xTableControl->enableRelation(!static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural() && eJoinType != CROSS_JOIN );
133 }
134}
135
137{
138}
139
141{
142 if (!m_xLB_JoinType->get_value_changed_from_saved())
143 return;
144
145 m_xLB_JoinType->save_value();
146 m_xML_HelpText->set_label(OUString());
147
148 m_xTableControl->enableRelation(true);
149
150 OUString sFirstWinName = m_pConnData->getReferencingTable()->GetWinName();
151 OUString sSecondWinName = m_pConnData->getReferencedTable()->GetWinName();
152 const EJoinType eOldJoinType = eJoinType;
153 TranslateId pResId;
154 const sal_Int32 nPos = m_xLB_JoinType->get_active();
155 const sal_Int32 nJoinType = m_xLB_JoinType->get_id(nPos).toInt32();
156 bool bAddHint = true;
157 switch ( nJoinType )
158 {
159 default:
160 case ID_INNER_JOIN:
161 pResId = STR_QUERY_INNER_JOIN;
162 bAddHint = false;
163 eJoinType = INNER_JOIN;
164 break;
165 case ID_LEFT_JOIN:
166 pResId = STR_QUERY_LEFTRIGHT_JOIN;
167 eJoinType = LEFT_JOIN;
168 break;
169 case ID_RIGHT_JOIN:
170 pResId = STR_QUERY_LEFTRIGHT_JOIN;
171 eJoinType = RIGHT_JOIN;
172 std::swap( sFirstWinName, sSecondWinName );
173 break;
174 case ID_FULL_JOIN:
175 pResId = STR_QUERY_FULL_JOIN;
176 eJoinType = FULL_JOIN;
177 break;
178 case ID_CROSS_JOIN:
179 {
180 pResId = STR_QUERY_CROSS_JOIN;
181 eJoinType = CROSS_JOIN;
182
183 m_pConnData->ResetConnLines();
184 m_xTableControl->lateInit();
185 m_xCBNatural->set_active(false);
186 m_xTableControl->enableRelation(false);
187 m_pConnData->AppendConnLine("","");
188 m_xPB_OK->set_sensitive(true);
189 }
190 break;
191 }
192
193 m_xCBNatural->set_sensitive(eJoinType != CROSS_JOIN);
194
195 if ( eJoinType != eOldJoinType && eOldJoinType == CROSS_JOIN )
196 {
197 m_pConnData->ResetConnLines();
198 }
199 if ( eJoinType != CROSS_JOIN )
200 {
201 m_xTableControl->NotifyCellChange();
202 NaturalToggleHdl(*m_xCBNatural);
203 }
204
205 m_xTableControl->Invalidate();
206
207 OUString sHelpText = DBA_RES(pResId);
208 if( nPos )
209 {
210 sHelpText = sHelpText.replaceFirst( "%1", sFirstWinName );
211 sHelpText = sHelpText.replaceFirst( "%2", sSecondWinName );
212 }
213 if ( bAddHint )
214 {
215 sHelpText += "\n" + DBA_RES( STR_JOIN_TYPE_HINT );
216 }
217
218 m_xML_HelpText->set_label( sHelpText );
219}
220
222{
223 m_pConnData->Update();
224 m_pOrigConnData->CopyFrom( *m_pConnData );
225
226 m_xDialog->response(RET_OK);
227}
228
230{
231 bool bChecked = m_xCBNatural->get_active();
232 static_cast<OQueryTableConnectionData*>(m_pConnData.get())->setNatural(bChecked);
233 m_xTableControl->enableRelation(!bChecked);
234 if ( !bChecked )
235 return;
236
237 m_pConnData->ResetConnLines();
238 try
239 {
240 Reference<XNameAccess> xReferencedTableColumns(m_pConnData->getReferencedTable()->getColumns());
241 Sequence< OUString> aSeq = m_pConnData->getReferencingTable()->getColumns()->getElementNames();
242 const OUString* pIter = aSeq.getConstArray();
243 const OUString* pEnd = pIter + aSeq.getLength();
244 for(;pIter != pEnd;++pIter)
245 {
246 if ( xReferencedTableColumns->hasByName(*pIter) )
247 m_pConnData->AppendConnLine(*pIter,*pIter);
248 }
249 }
250 catch( const Exception& )
251 {
252 DBG_UNHANDLED_EXCEPTION("dbaccess");
253 }
254 m_xTableControl->NotifyCellChange();
255 m_xTableControl->Invalidate();
256}
257
258void DlgQryJoin::setValid(bool _bValid)
259{
260 m_xPB_OK->set_sensitive(_bValid || eJoinType == CROSS_JOIN );
261}
262
264{
266 m_xCBNatural->set_active(static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural());
267 NaturalToggleHdl(*m_xCBNatural);
268}
269
271{
272 eJoinType = _eNewJoinType;
273 m_xCBNatural->set_sensitive(eJoinType != CROSS_JOIN);
274
275 sal_Int32 nJoinType = 0;
276 switch ( eJoinType )
277 {
278 default:
279 case INNER_JOIN:
280 nJoinType = ID_INNER_JOIN;
281 break;
282 case LEFT_JOIN:
283 nJoinType = ID_LEFT_JOIN;
284 break;
285 case RIGHT_JOIN:
286 nJoinType = ID_RIGHT_JOIN;
287 break;
288 case FULL_JOIN:
289 nJoinType = ID_FULL_JOIN;
290 break;
291 case CROSS_JOIN:
292 nJoinType = ID_CROSS_JOIN;
293 break;
294 }
295
296 const sal_Int32 nCount = m_xLB_JoinType->get_count();
297 for (sal_Int32 i = 0; i < nCount; ++i)
298 {
299 if (nJoinType == m_xLB_JoinType->get_id(i).toInt32())
300 {
301 m_xLB_JoinType->set_active(i);
302 break;
303 }
304 }
305
306 LBChangeHdl(*m_xLB_JoinType);
307}
308
309/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XExecutableDialog > m_xDialog
constexpr tools::Long Height() const
constexpr tools::Long Width() const
std::unique_ptr< weld::Label > m_xML_HelpText
Definition: querydlg.hxx:41
void setJoinType(EJoinType _eNewJoinType)
setJoinType enables and set the new join type
Definition: querydlg.cxx:270
virtual ~DlgQryJoin() override
Definition: querydlg.cxx:136
std::unique_ptr< OTableListBoxControl > m_xTableControl
Definition: querydlg.hxx:45
virtual void notifyConnectionChange() override
notifyConnectionChange is callback which is called when the table selection has changed and a new con...
Definition: querydlg.cxx:263
std::unique_ptr< weld::ComboBox > m_xLB_JoinType
Definition: querydlg.hxx:43
css::uno::Reference< css::sdbc::XConnection > m_xConnection
Definition: querydlg.hxx:39
TTableConnectionData::value_type m_pConnData
Definition: querydlg.hxx:37
std::unique_ptr< weld::CheckButton > m_xCBNatural
Definition: querydlg.hxx:44
virtual void setValid(bool _bValid) override
setValid set the valid inside, can be used for OK buttons
Definition: querydlg.cxx:258
std::unique_ptr< weld::Button > m_xPB_OK
Definition: querydlg.hxx:42
EJoinType eJoinType
Definition: querydlg.hxx:36
OJoinController & getController() const
std::map< OUString, VclPtr< OTableWindow > > OTableWindowMap
OJoinDesignView * getDesignView() const
void ResetConnLines()
Deletes list of ConnLines.
std::unique_ptr< weld::Builder > m_xBuilder
#define DBA_RES(id)
int nCount
#define DBG_UNHANDLED_EXCEPTION(...)
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
sal_uInt16 nPos
Sequence< sal_Int8 > aSeq
@ Exception
IMPL_LINK_NOARG(OApplicationController, OnClipboardChanged, TransferableDataHelper *, void)
@ RIGHT_JOIN
Definition: QEnumTypes.hxx:57
@ FULL_JOIN
Definition: QEnumTypes.hxx:55
@ LEFT_JOIN
Definition: QEnumTypes.hxx:56
@ INNER_JOIN
Definition: QEnumTypes.hxx:59
@ CROSS_JOIN
Definition: QEnumTypes.hxx:58
int i
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
Reference< XConnection > m_xConnection
Definition: objectnames.cxx:79
#define ID_LEFT_JOIN
Definition: querydlg.cxx:33
#define ID_CROSS_JOIN
Definition: querydlg.cxx:36
#define ID_FULL_JOIN
Definition: querydlg.cxx:35
#define ID_RIGHT_JOIN
Definition: querydlg.cxx:34
#define ID_INNER_JOIN
Definition: querydlg.cxx:32
RET_OK