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 "QTableWindow.hxx"
21 #include <QueryTableView.hxx>
22 #include <JoinController.hxx>
23 #include <JoinDesignView.hxx>
24 #include <osl/diagnose.h>
25 #include <helpids.h>
26 #include <browserids.hxx>
27 #include <TableWindowListBox.hxx>
28 #include <strings.hxx>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/sdbc/SQLException.hpp>
31 #include "TableFieldInfo.hxx"
32 #include <comphelper/stl_types.hxx>
33 #include <comphelper/types.hxx>
34 
35 using namespace ::com::sun::star::sdbc;
36 using namespace ::com::sun::star::uno;
37 using namespace ::com::sun::star::container;
38 using namespace ::com::sun::star::beans;
39 using namespace dbaui;
OQueryTableWindow(vcl::Window * pParent,const TTableWindowData::value_type & pTabWinData)40 OQueryTableWindow::OQueryTableWindow( vcl::Window* pParent, const TTableWindowData::value_type& pTabWinData)
41     :OTableWindow( pParent, pTabWinData )
42     ,m_nAliasNum(0)
43 {
44     m_strInitialAlias = GetAliasName();
45 
46     // if table name matches alias, do not pass to InitialAlias,
47     // as the appending of a possible token could not succeed...
48     if (m_strInitialAlias == pTabWinData->GetTableName())
49         m_strInitialAlias.clear();
50 
51     SetHelpId(HID_CTL_QRYDGNTAB);
52 }
53 
Init()54 bool OQueryTableWindow::Init()
55 {
56     bool bSuccess = OTableWindow::Init();
57     if (!bSuccess)
58         return bSuccess;
59 
60     OQueryTableView* pContainer = static_cast<OQueryTableView*>(getTableView());
61 
62     // first determine Alias
63     OUString sAliasName;
64 
65     TTableWindowData::value_type pWinData = GetData();
66 
67     if (!m_strInitialAlias.isEmpty() )
68         // Alias was explicitly given
69         sAliasName = m_strInitialAlias;
70     else if ( GetTable().is() )
71         GetTable()->getPropertyValue( PROPERTY_NAME ) >>= sAliasName;
72     else
73         return false;
74 
75     // Alias with successive number
76     if (pContainer->CountTableAlias(sAliasName, m_nAliasNum))
77     {
78         sAliasName += "_" + OUString::number(m_nAliasNum);
79     }
80 
81     sAliasName = sAliasName.replaceAll("\"", "");
82     SetAliasName(sAliasName);
83         // SetAliasName passes it as WinName, hence it uses the base class
84     // reset the title
85     m_xTitle->SetText( pWinData->GetWinName() );
86     m_xTitle->Show();
87 
88     getTableView()->getDesignView()->getController().InvalidateFeature(ID_BROWSER_QUERY_EXECUTE);
89     return bSuccess;
90 }
91 
createUserData(const Reference<XPropertySet> & _xColumn,bool _bPrimaryKey)92 void* OQueryTableWindow::createUserData(const Reference< XPropertySet>& _xColumn,bool _bPrimaryKey)
93 {
94     OTableFieldInfo* pInfo = new OTableFieldInfo();
95     pInfo->SetKey(_bPrimaryKey ? TAB_PRIMARY_FIELD : TAB_NORMAL_FIELD);
96     if ( _xColumn.is() )
97         pInfo->SetDataType(::comphelper::getINT32(_xColumn->getPropertyValue(PROPERTY_TYPE)));
98     return pInfo;
99 }
100 
deleteUserData(void * & _pUserData)101 void OQueryTableWindow::deleteUserData(void*& _pUserData)
102 {
103     delete static_cast<OTableFieldInfo*>(_pUserData);
104     _pUserData = nullptr;
105 }
106 
OnEntryDoubleClicked(weld::TreeIter & rEntry)107 void OQueryTableWindow::OnEntryDoubleClicked(weld::TreeIter& rEntry)
108 {
109     if (getTableView()->getDesignView()->getController().isReadOnly())
110         return;
111 
112     weld::TreeView& rTreeView = m_xListBox->get_widget();
113     OTableFieldInfo* pInf = reinterpret_cast<OTableFieldInfo*>(rTreeView.get_id(rEntry).toUInt64());
114     OSL_ENSURE(pInf != nullptr, "OQueryTableWindow::OnEntryDoubleClicked : field doesn't have FieldInfo !");
115 
116     // build up DragInfo
117     OTableFieldDescRef aInfo = new OTableFieldDesc(GetTableName(), rTreeView.get_text(rEntry));
118     aInfo->SetTabWindow(this);
119     aInfo->SetAlias(GetAliasName());
120     aInfo->SetFieldIndex(rTreeView.get_iter_index_in_parent(rEntry));
121     aInfo->SetDataType(pInf->GetDataType());
122 
123     // and insert corresponding field
124     static_cast<OQueryTableView*>(getTableView())->InsertField(aInfo);
125 }
126 
ExistsField(const OUString & strFieldName,OTableFieldDescRef const & rInfo)127 bool OQueryTableWindow::ExistsField(const OUString& strFieldName, OTableFieldDescRef const & rInfo)
128 {
129     OSL_ENSURE(m_xListBox != nullptr, "OQueryTableWindow::ExistsField : doesn't have css::form::ListBox !");
130     OSL_ENSURE(rInfo.is(),"OQueryTableWindow::ExistsField: invalid argument for OTableFieldDescRef!");
131     Reference< XConnection> xConnection = getTableView()->getDesignView()->getController().getConnection();
132     bool bExists = false;
133     if(xConnection.is())
134     {
135         weld::TreeView& rTreeView = m_xListBox->get_widget();
136         std::unique_ptr<weld::TreeIter> xEntry(rTreeView.make_iterator());
137         bool bEntry = rTreeView.get_iter_first(*xEntry);
138         try
139         {
140             Reference<XDatabaseMetaData> xMeta = xConnection->getMetaData();
141             ::comphelper::UStringMixEqual bCase(xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers());
142 
143             while (bEntry)
144             {
145                 if (bCase(strFieldName, rTreeView.get_text(*xEntry)))
146                 {
147                     OTableFieldInfo* pInf = reinterpret_cast<OTableFieldInfo*>(rTreeView.get_id(*xEntry).toUInt64());
148                     assert(pInf && "OQueryTableWindow::ExistsField : field doesn't have FieldInfo !");
149 
150                     rInfo->SetTabWindow(this);
151                     rInfo->SetField(strFieldName);
152                     rInfo->SetTable(GetTableName());
153                     rInfo->SetAlias(GetAliasName());
154                     rInfo->SetFieldIndex(rTreeView.get_iter_index_in_parent(*xEntry));
155                     rInfo->SetDataType(pInf->GetDataType());
156                     bExists = true;
157                     break;
158                 }
159                 bEntry = rTreeView.iter_next(*xEntry);
160             }
161         }
162         catch(SQLException&)
163         {
164         }
165     }
166 
167     return bExists;
168 }
169 
ExistsAVisitedConn() const170 bool OQueryTableWindow::ExistsAVisitedConn() const
171 {
172     return static_cast<const OQueryTableView*>(getTableView())->ExistsAVisitedConn(this);
173 }
174 
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
176