1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBAUI_QUERY_TABLEWINDOWDATA_HXX
31 #include "QTableWindow.hxx"
32 #endif
33 #ifndef DBAUI_QUERYTABLEVIEW_HXX
34 #include "QueryTableView.hxx"
35 #endif
36 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
37 #include "dbustrings.hrc"
38 #endif
39 #ifndef _TOOLS_DEBUG_HXX
40 #include <tools/debug.hxx>
41 #endif
42 #include "dbaccess_helpid.hrc"
43 #ifndef DBAUI_QUERYDESIGNVIEW_HXX
44 #include "QueryDesignView.hxx"
45 #endif
46 #ifndef DBACCESS_UI_BROWSER_ID_HXX
47 #include "browserids.hxx"
48 #endif
49 #ifndef DBAUI_QUERYCONTROLLER_HXX
50 #include "querycontroller.hxx"
51 #endif
52 #ifndef _SV_IMAGE_HXX
53 #include <vcl/image.hxx>
54 #endif
55 #ifndef DBAUI_TABLEWINDOWLISTBOX_HXX
56 #include "TableWindowListBox.hxx"
57 #endif
58 #ifndef _DBU_QRY_HRC_
59 #include "dbu_qry.hrc"
60 #endif
61 #ifndef DBAUI_QUERY_HRC
62 #include "Query.hrc"
63 #endif
64 #ifndef _COM_SUN_STAR_SDBCX_XKEYSSUPPLIER_HPP_
65 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
66 #endif
67 #ifndef _COM_SUN_STAR_CONTAINER_XNAMEACCESS_HPP_
68 #include <com/sun/star/container/XNameAccess.hpp>
69 #endif
70 #ifndef _COM_SUN_STAR_CONTAINER_XINDEXACCESS_HPP_
71 #include <com/sun/star/container/XIndexAccess.hpp>
72 #endif
73 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
74 #include <com/sun/star/beans/XPropertySet.hpp>
75 #endif
76 #ifndef _COM_SUN_STAR_SDBCX_KEYTYPE_HPP_
77 #include <com/sun/star/sdbcx/KeyType.hpp>
78 #endif
79 #ifndef _COM_SUN_STAR_SDBCX_XCOLUMNSSUPPLIER_HPP_
80 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
81 #endif
82 #ifndef DBAUI_TABLEFIELDINFO_HXX
83 #include "TableFieldInfo.hxx"
84 #endif
85 #ifndef _COMPHELPER_UNO3_HXX_
86 #include <comphelper/uno3.hxx>
87 #endif
88 #ifndef _COMPHELPER_EXTRACT_HXX_
89 #include <comphelper/extract.hxx>
90 #endif
91 #ifndef DBAUI_TOOLS_HXX
92 #include "UITools.hxx"
93 #endif
94 
95 
96 using namespace ::com::sun::star::sdbc;
97 using namespace ::com::sun::star::sdbcx;
98 using namespace ::com::sun::star::uno;
99 using namespace ::com::sun::star::container;
100 using namespace ::com::sun::star::beans;
101 using namespace dbaui;
102 //========================================================================
103 // class OQueryTableWindow
104 //========================================================================
105 DBG_NAME(OQueryTableWindow)
106 //------------------------------------------------------------------------------
107 OQueryTableWindow::OQueryTableWindow( Window* pParent, const TTableWindowData::value_type& pTabWinData, sal_Unicode* pszInitialAlias)
108 	:OTableWindow( pParent, pTabWinData )
109 	,m_nAliasNum(0)
110 {
111 	DBG_CTOR(OQueryTableWindow,NULL);
112 	if (pszInitialAlias != NULL)
113 		m_strInitialAlias = ::rtl::OUString(pszInitialAlias);
114 	else
115 		m_strInitialAlias = GetAliasName();
116 
117 	// wenn der Tabellen- gleich dem Aliasnamen ist, dann darf ich das nicht an InitialAlias weiterreichen, denn das Anhaengen
118 	// eines eventuelle Tokens nicht klappen ...
119 	if (m_strInitialAlias == pTabWinData->GetTableName())
120 		m_strInitialAlias = ::rtl::OUString();
121 
122 	SetHelpId(HID_CTL_QRYDGNTAB);
123 }
124 
125 //------------------------------------------------------------------------------
126 OQueryTableWindow::~OQueryTableWindow()
127 {
128 	DBG_DTOR(OQueryTableWindow,NULL);
129 }
130 
131 //------------------------------------------------------------------------------
132 sal_Bool OQueryTableWindow::Init()
133 {
134 	sal_Bool bSuccess = OTableWindow::Init();
135 	if(!bSuccess)
136 		return bSuccess;
137 
138 	OQueryTableView* pContainer = static_cast<OQueryTableView*>(getTableView());
139 
140 	// zuerst Alias bestimmen
141 	::rtl::OUString sAliasName;
142 
143 	TTableWindowData::value_type pWinData = GetData();
144 
145 	if (m_strInitialAlias.getLength() )
146 		// Der Alias wurde explizit mit angegeben
147 		sAliasName = m_strInitialAlias;
148 	else if ( GetTable().is() )
149 		GetTable()->getPropertyValue( PROPERTY_NAME ) >>= sAliasName;
150     else
151         return sal_False;
152 
153 	// Alias mit fortlaufender Nummer versehen
154 	if (pContainer->CountTableAlias(sAliasName, m_nAliasNum))
155 	{
156 		sAliasName += ::rtl::OUString('_');
157 		sAliasName += ::rtl::OUString::valueOf(m_nAliasNum);
158 	}
159 
160 
161 	sAliasName = String(sAliasName).EraseAllChars('"');
162 	SetAliasName(sAliasName);
163 		// SetAliasName reicht das als WinName weiter, dadurch benutzt es die Basisklasse
164 	// reset the titel
165 	m_aTitle.SetText( pWinData->GetWinName() );
166 	m_aTitle.Show();
167 
168 	//	sal_Bool bSuccess(sal_True);
169 	if (!bSuccess)
170 	{	// es soll nur ein Dummy-Window aufgemacht werden ...
171 		DBG_ASSERT(GetAliasName().getLength(), "OQueryTableWindow::Init : kein Alias- UND kein Tabellenname geht nicht !");
172 			// .. aber das braucht wenigstens einen Alias
173 
174 		// ::com::sun::star::form::ListBox anlegen
175 		if (!m_pListBox)
176 			m_pListBox = CreateListBox();
177 
178 		// Titel setzen
179 		m_aTitle.SetText(GetAliasName());
180 		m_aTitle.Show();
181 
182 		clearListBox();
183 			// neu zu fuellen brauche ich die nicht, da ich ja keine Tabelle habe
184 		m_pListBox->Show();
185 	}
186 
187 	getTableView()->getDesignView()->getController().InvalidateFeature(ID_BROWSER_QUERY_EXECUTE);
188 	return bSuccess;
189 }
190 // -----------------------------------------------------------------------------
191 void* OQueryTableWindow::createUserData(const Reference< XPropertySet>& _xColumn,bool _bPrimaryKey)
192 {
193 	OTableFieldInfo* pInfo = new OTableFieldInfo();
194 	pInfo->SetKey(_bPrimaryKey ? TAB_PRIMARY_FIELD : TAB_NORMAL_FIELD);
195 	if ( _xColumn.is() )
196 		pInfo->SetDataType(::comphelper::getINT32(_xColumn->getPropertyValue(PROPERTY_TYPE)));
197 	return pInfo;
198 }
199 // -----------------------------------------------------------------------------
200 void OQueryTableWindow::deleteUserData(void*& _pUserData)
201 {
202 	delete static_cast<OTableFieldInfo*>(_pUserData);
203 	_pUserData = NULL;
204 }
205 //------------------------------------------------------------------------------
206 void OQueryTableWindow::OnEntryDoubleClicked(SvLBoxEntry* pEntry)
207 {
208 	DBG_ASSERT(pEntry != NULL, "OQueryTableWindow::OnEntryDoubleClicked : pEntry darf nicht NULL sein !");
209 		// man koennte das auch abfragen und dann ein return hinsetzen, aber so weist es vielleicht auf Fehler bei Aufrufer hin
210 
211 	if (getTableView()->getDesignView()->getController().isReadOnly())
212 		return;
213 
214 	OTableFieldInfo* pInf = static_cast<OTableFieldInfo*>(pEntry->GetUserData());
215 	DBG_ASSERT(pInf != NULL, "OQueryTableWindow::OnEntryDoubleClicked : Feld hat keine FieldInfo !");
216 
217 	// eine DragInfo aufbauen
218 	OTableFieldDescRef aInfo = new OTableFieldDesc(GetTableName(),m_pListBox->GetEntryText(pEntry));
219 	aInfo->SetTabWindow(this);
220 	aInfo->SetAlias(GetAliasName());
221 	aInfo->SetFieldIndex(m_pListBox->GetModel()->GetAbsPos(pEntry));
222 	aInfo->SetDataType(pInf->GetDataType());
223 
224 	// und das entsprechende Feld einfuegen
225 	static_cast<OQueryTableView*>(getTableView())->InsertField(aInfo);
226 }
227 
228 //------------------------------------------------------------------------------
229 sal_Bool OQueryTableWindow::ExistsField(const ::rtl::OUString& strFieldName, OTableFieldDescRef& rInfo)
230 {
231 	DBG_ASSERT(m_pListBox != NULL, "OQueryTableWindow::ExistsField : habe keine ::com::sun::star::form::ListBox !");
232 	OSL_ENSURE(rInfo.isValid(),"OQueryTableWindow::ExistsField: invlid argument for OTableFieldDescRef!");
233 	Reference< XConnection> xConnection = getTableView()->getDesignView()->getController().getConnection();
234 	sal_Bool bExists = sal_False;
235 	if(xConnection.is())
236 	{
237 		SvLBoxEntry* pEntry = m_pListBox->First();
238 		try
239 		{
240 			Reference<XDatabaseMetaData> xMeta = xConnection->getMetaData();
241 			::comphelper::UStringMixEqual bCase(xMeta.is() && xMeta->supportsMixedCaseQuotedIdentifiers());
242 
243 			while (pEntry)
244 			{
245 				if (bCase(strFieldName,::rtl::OUString(m_pListBox->GetEntryText(pEntry))))
246 				{
247 					OTableFieldInfo* pInf = static_cast<OTableFieldInfo*>(pEntry->GetUserData());
248 					DBG_ASSERT(pInf != NULL, "OQueryTableWindow::ExistsField : Feld hat keine FieldInfo !");
249 
250 					rInfo->SetTabWindow(this);
251 					rInfo->SetField(strFieldName);
252 					rInfo->SetTable(GetTableName());
253 					rInfo->SetAlias(GetAliasName());
254 					rInfo->SetFieldIndex(m_pListBox->GetModel()->GetAbsPos(pEntry));
255 					rInfo->SetDataType(pInf->GetDataType());
256 					bExists = sal_True;
257 					break;
258 				}
259 				pEntry = m_pListBox->Next(pEntry);
260 			}
261 		}
262 		catch(SQLException&)
263 		{
264 		}
265 	}
266 
267 	return bExists;
268 }
269 
270 //------------------------------------------------------------------------------
271 sal_Bool OQueryTableWindow::ExistsAVisitedConn() const
272 {
273 	return static_cast<const OQueryTableView*>(getTableView())->ExistsAVisitedConn(this);
274 }
275 
276 //------------------------------------------------------------------------------
277 void OQueryTableWindow::KeyInput( const KeyEvent& rEvt )
278 {
279 	OTableWindow::KeyInput( rEvt );
280 }
281 // -----------------------------------------------------------------------------
282 
283 
284 
285