bump product version to 4.1.6.2
[LibreOffice.git] / dbaccess / source / ui / querydesign / QTableWindow.cxx
blob3e890c8550cf6b46f2bb55b8535d57da3f434674
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 //========================================================================
52 // class OQueryTableWindow
53 //========================================================================
54 DBG_NAME(OQueryTableWindow)
55 //------------------------------------------------------------------------------
56 OQueryTableWindow::OQueryTableWindow( Window* pParent, const TTableWindowData::value_type& pTabWinData, sal_Unicode* pszInitialAlias)
57 :OTableWindow( pParent, pTabWinData )
58 ,m_nAliasNum(0)
60 DBG_CTOR(OQueryTableWindow,NULL);
61 if (pszInitialAlias != NULL)
62 m_strInitialAlias = OUString(pszInitialAlias);
63 else
64 m_strInitialAlias = GetAliasName();
66 // if table name matches alias, do not pass to InitialAlias,
67 // as the appending of a possible token could not succeed...
68 if (m_strInitialAlias == pTabWinData->GetTableName())
69 m_strInitialAlias = OUString();
71 SetHelpId(HID_CTL_QRYDGNTAB);
74 //------------------------------------------------------------------------------
75 OQueryTableWindow::~OQueryTableWindow()
77 DBG_DTOR(OQueryTableWindow,NULL);
80 //------------------------------------------------------------------------------
81 sal_Bool OQueryTableWindow::Init()
83 sal_Bool bSuccess = OTableWindow::Init();
84 if(!bSuccess)
85 return bSuccess;
87 OQueryTableView* pContainer = static_cast<OQueryTableView*>(getTableView());
89 // first determine Alias
90 OUString sAliasName;
92 TTableWindowData::value_type pWinData = GetData();
94 if (!m_strInitialAlias.isEmpty() )
95 // Alias was explicitly given
96 sAliasName = m_strInitialAlias;
97 else if ( GetTable().is() )
98 GetTable()->getPropertyValue( PROPERTY_NAME ) >>= sAliasName;
99 else
100 return sal_False;
102 // Alias with successive number
103 if (pContainer->CountTableAlias(sAliasName, m_nAliasNum))
105 sAliasName += OUString('_');
106 sAliasName += OUString::valueOf(m_nAliasNum);
110 sAliasName = comphelper::string::remove(sAliasName, '"');
111 SetAliasName(sAliasName);
112 // SetAliasName passes it as WinName, hence it uses the base class
113 // reset the title
114 m_aTitle.SetText( pWinData->GetWinName() );
115 m_aTitle.Show();
117 if (!bSuccess)
118 { // it should just open a dummy window...
119 OSL_ENSURE(!GetAliasName().isEmpty(), "OQueryTableWindow::Init : kein Alias- UND kein Tabellenname geht nicht !");
120 // .. but that needs at least an Alias
122 // create ::com::sun::star::form::ListBox
123 if (!m_pListBox)
124 m_pListBox = CreateListBox();
126 // set titel
127 m_aTitle.SetText(GetAliasName());
128 m_aTitle.Show();
130 clearListBox();
131 // don't need to refill them as I don't have a table
132 m_pListBox->Show();
135 getTableView()->getDesignView()->getController().InvalidateFeature(ID_BROWSER_QUERY_EXECUTE);
136 return bSuccess;
138 // -----------------------------------------------------------------------------
139 void* OQueryTableWindow::createUserData(const Reference< XPropertySet>& _xColumn,bool _bPrimaryKey)
141 OTableFieldInfo* pInfo = new OTableFieldInfo();
142 pInfo->SetKey(_bPrimaryKey ? TAB_PRIMARY_FIELD : TAB_NORMAL_FIELD);
143 if ( _xColumn.is() )
144 pInfo->SetDataType(::comphelper::getINT32(_xColumn->getPropertyValue(PROPERTY_TYPE)));
145 return pInfo;
147 // -----------------------------------------------------------------------------
148 void OQueryTableWindow::deleteUserData(void*& _pUserData)
150 delete static_cast<OTableFieldInfo*>(_pUserData);
151 _pUserData = NULL;
153 //------------------------------------------------------------------------------
154 void OQueryTableWindow::OnEntryDoubleClicked(SvTreeListEntry* pEntry)
156 OSL_ENSURE(pEntry != NULL, "OQueryTableWindow::OnEntryDoubleClicked : pEntry must not be NULL !");
157 // you could also scan that and then return, but like this it could possibly hint to faults at the caller
159 if (getTableView()->getDesignView()->getController().isReadOnly())
160 return;
162 OTableFieldInfo* pInf = static_cast<OTableFieldInfo*>(pEntry->GetUserData());
163 OSL_ENSURE(pInf != NULL, "OQueryTableWindow::OnEntryDoubleClicked : field doesn't have FieldInfo !");
165 // build up DragInfo
166 OTableFieldDescRef aInfo = new OTableFieldDesc(GetTableName(),m_pListBox->GetEntryText(pEntry));
167 aInfo->SetTabWindow(this);
168 aInfo->SetAlias(GetAliasName());
169 aInfo->SetFieldIndex(m_pListBox->GetModel()->GetAbsPos(pEntry));
170 aInfo->SetDataType(pInf->GetDataType());
172 // and insert corresponding field
173 static_cast<OQueryTableView*>(getTableView())->InsertField(aInfo);
176 //------------------------------------------------------------------------------
177 sal_Bool OQueryTableWindow::ExistsField(const OUString& strFieldName, OTableFieldDescRef& rInfo)
179 OSL_ENSURE(m_pListBox != NULL, "OQueryTableWindow::ExistsField : doesn't have ::com::sun::star::form::ListBox !");
180 OSL_ENSURE(rInfo.is(),"OQueryTableWindow::ExistsField: invalid argument for OTableFieldDescRef!");
181 Reference< XConnection> xConnection = getTableView()->getDesignView()->getController().getConnection();
182 sal_Bool bExists = sal_False;
183 if(xConnection.is())
185 SvTreeListEntry* pEntry = m_pListBox->First();
188 Reference<XDatabaseMetaData> xMeta = xConnection->getMetaData();
189 ::comphelper::UStringMixEqual bCase(xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers());
191 while (pEntry)
193 if (bCase(strFieldName,OUString(m_pListBox->GetEntryText(pEntry))))
195 OTableFieldInfo* pInf = static_cast<OTableFieldInfo*>(pEntry->GetUserData());
196 OSL_ENSURE(pInf != NULL, "OQueryTableWindow::ExistsField : field doesn't have FieldInfo !");
198 rInfo->SetTabWindow(this);
199 rInfo->SetField(strFieldName);
200 rInfo->SetTable(GetTableName());
201 rInfo->SetAlias(GetAliasName());
202 rInfo->SetFieldIndex(m_pListBox->GetModel()->GetAbsPos(pEntry));
203 rInfo->SetDataType(pInf->GetDataType());
204 bExists = sal_True;
205 break;
207 pEntry = m_pListBox->Next(pEntry);
210 catch(SQLException&)
215 return bExists;
218 //------------------------------------------------------------------------------
219 sal_Bool OQueryTableWindow::ExistsAVisitedConn() const
221 return static_cast<const OQueryTableView*>(getTableView())->ExistsAVisitedConn(this);
224 //------------------------------------------------------------------------------
225 void OQueryTableWindow::KeyInput( const KeyEvent& rEvt )
227 OTableWindow::KeyInput( rEvt );
229 // -----------------------------------------------------------------------------
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */