Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / querydesign / QTableWindow.cxx
blob836850cf2fc634b9d34ae73f42322059994900f4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 "QTableWindow.hxx"
21 #include "QueryTableView.hxx"
22 #include "dbustrings.hrc"
23 #include <osl/diagnose.h>
24 #include "dbaccess_helpid.hrc"
25 #include "QueryDesignView.hxx"
26 #include "browserids.hxx"
27 #include "querycontroller.hxx"
28 #include <vcl/image.hxx>
29 #include "TableWindowListBox.hxx"
30 #include "dbu_qry.hrc"
31 #include "Query.hrc"
32 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
33 #include <com/sun/star/container/XNameAccess.hpp>
34 #include <com/sun/star/container/XIndexAccess.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/sdbcx/KeyType.hpp>
37 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
38 #include "TableFieldInfo.hxx"
39 #include <comphelper/extract.hxx>
40 #include <comphelper/string.hxx>
41 #include <comphelper/uno3.hxx>
42 #include "UITools.hxx"
43 #include "svtools/treelistentry.hxx"
45 using namespace ::com::sun::star::sdbc;
46 using namespace ::com::sun::star::sdbcx;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::container;
49 using namespace ::com::sun::star::beans;
50 using namespace dbaui;
51 // class OQueryTableWindow
52 OQueryTableWindow::OQueryTableWindow( vcl::Window* pParent, const TTableWindowData::value_type& pTabWinData, sal_Unicode* pszInitialAlias)
53 :OTableWindow( pParent, pTabWinData )
54 ,m_nAliasNum(0)
56 if (pszInitialAlias != NULL)
57 m_strInitialAlias = OUString(pszInitialAlias);
58 else
59 m_strInitialAlias = GetAliasName();
61 // if table name matches alias, do not pass to InitialAlias,
62 // as the appending of a possible token could not succeed...
63 if (m_strInitialAlias == pTabWinData->GetTableName())
64 m_strInitialAlias.clear();
66 SetHelpId(HID_CTL_QRYDGNTAB);
69 bool OQueryTableWindow::Init()
71 bool bSuccess = OTableWindow::Init();
72 if (!bSuccess)
73 return bSuccess;
75 OQueryTableView* pContainer = static_cast<OQueryTableView*>(getTableView());
77 // first determine Alias
78 OUString sAliasName;
80 TTableWindowData::value_type pWinData = GetData();
82 if (!m_strInitialAlias.isEmpty() )
83 // Alias was explicitly given
84 sAliasName = m_strInitialAlias;
85 else if ( GetTable().is() )
86 GetTable()->getPropertyValue( PROPERTY_NAME ) >>= sAliasName;
87 else
88 return false;
90 // Alias with successive number
91 if (pContainer->CountTableAlias(sAliasName, m_nAliasNum))
93 sAliasName += OUString('_');
94 sAliasName += OUString::number(m_nAliasNum);
97 sAliasName = comphelper::string::remove(sAliasName, '"');
98 SetAliasName(sAliasName);
99 // SetAliasName passes it as WinName, hence it uses the base class
100 // reset the title
101 m_xTitle->SetText( pWinData->GetWinName() );
102 m_xTitle->Show();
104 getTableView()->getDesignView()->getController().InvalidateFeature(ID_BROWSER_QUERY_EXECUTE);
105 return bSuccess;
108 void* OQueryTableWindow::createUserData(const Reference< XPropertySet>& _xColumn,bool _bPrimaryKey)
110 OTableFieldInfo* pInfo = new OTableFieldInfo();
111 pInfo->SetKey(_bPrimaryKey ? TAB_PRIMARY_FIELD : TAB_NORMAL_FIELD);
112 if ( _xColumn.is() )
113 pInfo->SetDataType(::comphelper::getINT32(_xColumn->getPropertyValue(PROPERTY_TYPE)));
114 return pInfo;
117 void OQueryTableWindow::deleteUserData(void*& _pUserData)
119 delete static_cast<OTableFieldInfo*>(_pUserData);
120 _pUserData = NULL;
123 void OQueryTableWindow::OnEntryDoubleClicked(SvTreeListEntry* pEntry)
125 OSL_ENSURE(pEntry != NULL, "OQueryTableWindow::OnEntryDoubleClicked : pEntry must not be NULL !");
126 // you could also scan that and then return, but like this it could possibly hint to faults at the caller
128 if (getTableView()->getDesignView()->getController().isReadOnly())
129 return;
131 OTableFieldInfo* pInf = static_cast<OTableFieldInfo*>(pEntry->GetUserData());
132 OSL_ENSURE(pInf != NULL, "OQueryTableWindow::OnEntryDoubleClicked : field doesn't have FieldInfo !");
134 // build up DragInfo
135 OTableFieldDescRef aInfo = new OTableFieldDesc(GetTableName(), m_xListBox->GetEntryText(pEntry));
136 aInfo->SetTabWindow(this);
137 aInfo->SetAlias(GetAliasName());
138 aInfo->SetFieldIndex(m_xListBox->GetModel()->GetAbsPos(pEntry));
139 aInfo->SetDataType(pInf->GetDataType());
141 // and insert corresponding field
142 static_cast<OQueryTableView*>(getTableView())->InsertField(aInfo);
145 bool OQueryTableWindow::ExistsField(const OUString& strFieldName, OTableFieldDescRef& rInfo)
147 OSL_ENSURE(m_xListBox != nullptr, "OQueryTableWindow::ExistsField : doesn't have ::com::sun::star::form::ListBox !");
148 OSL_ENSURE(rInfo.is(),"OQueryTableWindow::ExistsField: invalid argument for OTableFieldDescRef!");
149 Reference< XConnection> xConnection = getTableView()->getDesignView()->getController().getConnection();
150 bool bExists = false;
151 if(xConnection.is())
153 SvTreeListEntry* pEntry = m_xListBox->First();
156 Reference<XDatabaseMetaData> xMeta = xConnection->getMetaData();
157 ::comphelper::UStringMixEqual bCase(xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers());
159 while (pEntry)
161 if (bCase(strFieldName,OUString(m_xListBox->GetEntryText(pEntry))))
163 OTableFieldInfo* pInf = static_cast<OTableFieldInfo*>(pEntry->GetUserData());
164 assert(pInf && "OQueryTableWindow::ExistsField : field doesn't have FieldInfo !");
166 rInfo->SetTabWindow(this);
167 rInfo->SetField(strFieldName);
168 rInfo->SetTable(GetTableName());
169 rInfo->SetAlias(GetAliasName());
170 rInfo->SetFieldIndex(m_xListBox->GetModel()->GetAbsPos(pEntry));
171 rInfo->SetDataType(pInf->GetDataType());
172 bExists = true;
173 break;
175 pEntry = m_xListBox->Next(pEntry);
178 catch(SQLException&)
183 return bExists;
186 bool OQueryTableWindow::ExistsAVisitedConn() const
188 return static_cast<const OQueryTableView*>(getTableView())->ExistsAVisitedConn(this);
191 void OQueryTableWindow::KeyInput( const KeyEvent& rEvt )
193 OTableWindow::KeyInput( rEvt );
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */