1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbui.hxx"
26
27 #ifndef _DBAUI_TABLETREE_HXX_
28 #include "tabletree.hxx"
29 #endif
30 #ifndef _DBAUI_TABLETREE_HRC_
31 #include "tabletree.hrc"
32 #endif
33 #ifndef DBACCESS_IMAGEPROVIDER_HXX
34 #include "imageprovider.hxx"
35 #endif
36 #ifndef _DBAUI_MODULE_DBU_HXX_
37 #include "moduledbu.hxx"
38 #endif
39 #ifndef _DBU_CONTROL_HRC_
40 #include "dbu_control.hrc"
41 #endif
42 #ifndef _SV_MENU_HXX
43 #include <vcl/menu.hxx>
44 #endif
45 #ifndef _CONNECTIVITY_DBTOOLS_HXX_
46 #include <connectivity/dbtools.hxx>
47 #endif
48 #ifndef _COMPHELPER_TYPES_HXX_
49 #include <comphelper/types.hxx>
50 #endif
51 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
52 #include "dbustrings.hrc"
53 #endif
54 #ifndef _COM_SUN_STAR_SDB_APPLICATION_DATABASEOBJECT_HPP_
55 #include <com/sun/star/sdb/application/DatabaseObject.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_SDB_APPLICATION_DATABASEOBJECTFOLDER_HPP_
58 #include <com/sun/star/sdb/application/DatabaseObjectContainer.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_SDBC_XDRIVERACCESS_HPP_
61 #include <com/sun/star/sdbc/XDriverAccess.hpp>
62 #endif
63 #ifndef _COM_SUN_STAR_SDBCX_XDATADEFINITIONSUPPLIER_HPP_
64 #include <com/sun/star/sdbcx/XDataDefinitionSupplier.hpp>
65 #endif
66 #ifndef _COM_SUN_STAR_SDBCX_XVIEWSSUPPLIER_HPP_
67 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
68 #endif
69 #ifndef _COM_SUN_STAR_SDBCX_XTABLESSUPPLIER_HPP_
70 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
71 #endif
72 #ifndef _COM_SUN_STAR_SDB_SQLCONTEXT_HPP_
73 #include <com/sun/star/sdb/SQLContext.hpp>
74 #endif
75 #ifndef _COM_SUN_STAR_SDBC_XROW_HPP_
76 #include <com/sun/star/sdbc/XRow.hpp>
77 #endif
78 #ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
79 #include <com/sun/star/beans/XPropertySet.hpp>
80 #endif
81 #ifndef _DBAUI_COMMON_TYPES_HXX_
82 #include "commontypes.hxx"
83 #endif
84 #ifndef _DBAUI_LISTVIEWITEMS_HXX_
85 #include "listviewitems.hxx"
86 #endif
87 #ifndef TOOLS_DIAGNOSE_EX_H
88 #include <tools/diagnose_ex.h>
89 #endif
90 #ifndef _RTL_USTRBUF_HXX_
91 #include <rtl/ustrbuf.hxx>
92 #endif
93 #include <connectivity/dbmetadata.hxx>
94
95 #include <algorithm>
96 #include <functional>
97
98 //.........................................................................
99 namespace dbaui
100 {
101 //.........................................................................
102
103 using namespace ::com::sun::star::uno;
104 using namespace ::com::sun::star::sdb;
105 using namespace ::com::sun::star::lang;
106 using namespace ::com::sun::star::sdbc;
107 using namespace ::com::sun::star::sdbcx;
108 using namespace ::com::sun::star::beans;
109 using namespace ::com::sun::star::container;
110 using namespace ::com::sun::star::sdb::application;
111
112 using namespace ::dbtools;
113 using namespace ::comphelper;
114
115 namespace DatabaseObject = ::com::sun::star::sdb::application::DatabaseObject;
116 namespace DatabaseObjectContainer = ::com::sun::star::sdb::application::DatabaseObjectContainer;
117
118 //========================================================================
119 //= OTableTreeListBox
120 //========================================================================
OTableTreeListBox(Window * pParent,const Reference<XMultiServiceFactory> & _rxORB,WinBits nWinStyle,sal_Bool _bVirtualRoot)121 OTableTreeListBox::OTableTreeListBox( Window* pParent, const Reference< XMultiServiceFactory >& _rxORB, WinBits nWinStyle,sal_Bool _bVirtualRoot )
122 :OMarkableTreeListBox(pParent,_rxORB,nWinStyle)
123 ,m_pImageProvider( new ImageProvider )
124 ,m_bVirtualRoot(_bVirtualRoot)
125 ,m_bNoEmptyFolders( false )
126 {
127 implSetDefaultImages();
128 }
129 //------------------------------------------------------------------------
OTableTreeListBox(Window * pParent,const Reference<XMultiServiceFactory> & _rxORB,const ResId & rResId,sal_Bool _bVirtualRoot)130 OTableTreeListBox::OTableTreeListBox( Window* pParent, const Reference< XMultiServiceFactory >& _rxORB, const ResId& rResId ,sal_Bool _bVirtualRoot)
131 :OMarkableTreeListBox(pParent,_rxORB,rResId)
132 ,m_pImageProvider( new ImageProvider )
133 ,m_bVirtualRoot(_bVirtualRoot)
134 ,m_bNoEmptyFolders( false )
135 {
136 implSetDefaultImages();
137 }
138
139 // -----------------------------------------------------------------------------
~OTableTreeListBox()140 OTableTreeListBox::~OTableTreeListBox()
141 {
142 }
143
144 // -----------------------------------------------------------------------------
implSetDefaultImages()145 void OTableTreeListBox::implSetDefaultImages()
146 {
147 ImageProvider aImageProvider;
148 SetDefaultExpandedEntryBmp( aImageProvider.getFolderImage( DatabaseObject::TABLE, false ), BMP_COLOR_NORMAL );
149 SetDefaultExpandedEntryBmp( aImageProvider.getFolderImage( DatabaseObject::TABLE, true ), BMP_COLOR_HIGHCONTRAST );
150 SetDefaultCollapsedEntryBmp( aImageProvider.getFolderImage( DatabaseObject::TABLE, false ), BMP_COLOR_NORMAL );
151 SetDefaultCollapsedEntryBmp( aImageProvider.getFolderImage( DatabaseObject::TABLE, true ), BMP_COLOR_HIGHCONTRAST );
152 }
153
154 // -----------------------------------------------------------------------------
isFolderEntry(const SvLBoxEntry * _pEntry) const155 bool OTableTreeListBox::isFolderEntry( const SvLBoxEntry* _pEntry ) const
156 {
157 sal_Int32 nEntryType = reinterpret_cast< sal_IntPtr >( _pEntry->GetUserData() );
158 if ( ( nEntryType == DatabaseObjectContainer::TABLES )
159 || ( nEntryType == DatabaseObjectContainer::CATALOG )
160 || ( nEntryType == DatabaseObjectContainer::SCHEMA )
161 )
162 return true;
163 return false;
164 }
165
166 // -----------------------------------------------------------------------------
notifyHiContrastChanged()167 void OTableTreeListBox::notifyHiContrastChanged()
168 {
169 implSetDefaultImages();
170
171 SvLBoxEntry* pEntryLoop = First();
172 while (pEntryLoop)
173 {
174 sal_uInt16 nCount = pEntryLoop->ItemCount();
175 for (sal_uInt16 i=0;i<nCount;++i)
176 {
177 SvLBoxItem* pItem = pEntryLoop->GetItem(i);
178 if ( pItem && pItem->IsA() == SV_ITEM_ID_LBOXCONTEXTBMP)
179 {
180 SvLBoxContextBmp* pContextBitmapItem = static_cast< SvLBoxContextBmp* >( pItem );
181
182 Image aImage, aImageHC;
183 if ( isFolderEntry( pEntryLoop ) )
184 {
185 aImage = m_pImageProvider->getFolderImage( DatabaseObject::TABLE, false );
186 aImageHC = m_pImageProvider->getFolderImage( DatabaseObject::TABLE, true );
187 }
188 else
189 {
190 String sCompleteName( getQualifiedTableName( pEntryLoop ) );
191 m_pImageProvider->getImages( sCompleteName, DatabaseObject::TABLE, aImage, aImageHC );
192 }
193
194 pContextBitmapItem->SetBitmap1( aImage, BMP_COLOR_NORMAL );
195 pContextBitmapItem->SetBitmap2( aImage, BMP_COLOR_NORMAL );
196 pContextBitmapItem->SetBitmap1( aImageHC, BMP_COLOR_HIGHCONTRAST );
197 pContextBitmapItem->SetBitmap2( aImageHC, BMP_COLOR_HIGHCONTRAST );
198 // TODO: Now that we give both images to the entry item, it is not necessary anymore
199 // to do this anytime HC changes - the tree control will do this itself now.
200 // We would only need to properly initialize newly inserted entries.
201 break;
202 }
203 }
204 pEntryLoop = Next(pEntryLoop);
205 }
206 }
207
208 //------------------------------------------------------------------------
implOnNewConnection(const Reference<XConnection> & _rxConnection)209 void OTableTreeListBox::implOnNewConnection( const Reference< XConnection >& _rxConnection )
210 {
211 m_xConnection = _rxConnection;
212 m_pImageProvider.reset( new ImageProvider( m_xConnection ) );
213 }
214
215 //------------------------------------------------------------------------
UpdateTableList(const Reference<XConnection> & _rxConnection)216 void OTableTreeListBox::UpdateTableList( const Reference< XConnection >& _rxConnection )
217 {
218 Sequence< ::rtl::OUString > sTables, sViews;
219
220 String sCurrentActionError;
221 try
222 {
223 Reference< XTablesSupplier > xTableSupp( _rxConnection, UNO_QUERY_THROW );
224 sCurrentActionError = String(ModuleRes(STR_NOTABLEINFO));
225
226 Reference< XNameAccess > xTables,xViews;
227
228 Reference< XViewsSupplier > xViewSupp( _rxConnection, UNO_QUERY );
229 if ( xViewSupp.is() )
230 {
231 xViews = xViewSupp->getViews();
232 if (xViews.is())
233 sViews = xViews->getElementNames();
234 }
235
236 xTables = xTableSupp->getTables();
237 if (xTables.is())
238 sTables = xTables->getElementNames();
239 }
240 catch(RuntimeException&)
241 {
242 DBG_ERROR("OTableTreeListBox::UpdateTableList : caught an RuntimeException!");
243 }
244 catch ( const SQLException& )
245 {
246 throw;
247 }
248 catch(Exception&)
249 {
250 // a non-SQLException exception occurred ... simply throw an SQLException
251 SQLException aInfo;
252 aInfo.Message = sCurrentActionError;
253 throw aInfo;
254 }
255
256 UpdateTableList( _rxConnection, sTables, sViews );
257 }
258 // -----------------------------------------------------------------------------
259 namespace
260 {
261 struct OViewSetter
262 {
263 typedef OTableTreeListBox::TNames::value_type argument_type;
264 typedef bool result_type;
265 const Sequence< ::rtl::OUString> m_aViews;
266 ::comphelper::TStringMixEqualFunctor m_aEqualFunctor;
267
OViewSetterdbaui::__anon75b7a3130111::OViewSetter268 OViewSetter(const Sequence< ::rtl::OUString>& _rViews,sal_Bool _bCase) : m_aViews(_rViews),m_aEqualFunctor(_bCase){}
operator ()dbaui::__anon75b7a3130111::OViewSetter269 OTableTreeListBox::TNames::value_type operator() (const ::rtl::OUString& lhs)
270 {
271 OTableTreeListBox::TNames::value_type aRet;
272 aRet.first = lhs;
273 const ::rtl::OUString* pIter = m_aViews.getConstArray();
274 const ::rtl::OUString* pEnd = m_aViews.getConstArray() + m_aViews.getLength();
275 aRet.second = (::std::find_if(pIter,pEnd,::std::bind(m_aEqualFunctor, ::std::placeholders::_1,lhs)) != pEnd);
276
277 return aRet;
278 }
279 };
280
281 }
282 // -----------------------------------------------------------------------------
UpdateTableList(const Reference<XConnection> & _rxConnection,const Sequence<::rtl::OUString> & _rTables,const Sequence<::rtl::OUString> & _rViews)283 void OTableTreeListBox::UpdateTableList(
284 const Reference< XConnection >& _rxConnection,
285 const Sequence< ::rtl::OUString>& _rTables,
286 const Sequence< ::rtl::OUString>& _rViews
287 )
288 {
289 TNames aTables;
290 aTables.resize(_rTables.getLength());
291 const ::rtl::OUString* pIter = _rTables.getConstArray();
292 const ::rtl::OUString* pEnd = _rTables.getConstArray() + _rTables.getLength();
293 try
294 {
295 Reference< XDatabaseMetaData > xMeta( _rxConnection->getMetaData(), UNO_QUERY_THROW );
296 ::std::transform( pIter, pEnd,
297 aTables.begin(), OViewSetter( _rViews, xMeta->supportsMixedCaseQuotedIdentifiers() ) );
298 }
299 catch(Exception&)
300 {
301 DBG_UNHANDLED_EXCEPTION();
302 }
303 UpdateTableList( _rxConnection, aTables );
304 }
305
306 //------------------------------------------------------------------------
307 namespace
308 {
lcl_getMetaDataStrings_throw(const Reference<XResultSet> & _rxMetaDataResult,sal_Int32 _nColumnIndex)309 ::std::vector< ::rtl::OUString > lcl_getMetaDataStrings_throw( const Reference< XResultSet >& _rxMetaDataResult, sal_Int32 _nColumnIndex )
310 {
311 ::std::vector< ::rtl::OUString > aStrings;
312 Reference< XRow > xRow( _rxMetaDataResult, UNO_QUERY_THROW );
313 while ( _rxMetaDataResult->next() )
314 aStrings.push_back( xRow->getString( _nColumnIndex ) );
315 return aStrings;
316 }
317
lcl_shouldDisplayEmptySchemasAndCatalogs(const Reference<XConnection> & _rxConnection)318 bool lcl_shouldDisplayEmptySchemasAndCatalogs( const Reference< XConnection >& _rxConnection )
319 {
320 ::dbtools::DatabaseMetaData aMetaData( _rxConnection );
321 return aMetaData.displayEmptyTableFolders();
322 }
323 }
324
325 //------------------------------------------------------------------------
UpdateTableList(const Reference<XConnection> & _rxConnection,const TNames & _rTables)326 void OTableTreeListBox::UpdateTableList( const Reference< XConnection >& _rxConnection, const TNames& _rTables )
327 {
328 implOnNewConnection( _rxConnection );
329
330 // throw away all the old stuff
331 Clear();
332
333 try
334 {
335 // the root entry saying "all objects"
336 SvLBoxEntry* pAllObjects = NULL;
337 if (haveVirtualRoot())
338 {
339 String sRootEntryText;
340 TNames::const_iterator aViews = ::std::find_if(_rTables.begin(),_rTables.end(),
341 ::std::compose1(::std::bind(::std::equal_to<sal_Bool>(), ::std::placeholders::_1,sal_False),::std::select2nd<TNames::value_type>()));
342 TNames::const_iterator aTables = ::std::find_if(_rTables.begin(),_rTables.end(),
343 ::std::compose1(::std::bind(::std::equal_to<sal_Bool>(), ::std::placeholders::_1,sal_True),::std::select2nd<TNames::value_type>()));
344
345 if ( aViews == _rTables.end() )
346 sRootEntryText = String(ModuleRes(STR_ALL_TABLES));
347 else if ( aTables == _rTables.end() )
348 sRootEntryText = String(ModuleRes(STR_ALL_VIEWS));
349 else
350 sRootEntryText = String(ModuleRes(STR_ALL_TABLES_AND_VIEWS));
351 pAllObjects = InsertEntry( sRootEntryText, NULL, sal_False, LIST_APPEND, reinterpret_cast< void* >( DatabaseObjectContainer::TABLES ) );
352 }
353
354 if ( _rTables.empty() )
355 // nothing to do (besides inserting the root entry)
356 return;
357
358 // get the table/view names
359 TNames::const_iterator aIter = _rTables.begin();
360 TNames::const_iterator aEnd = _rTables.end();
361
362 Reference< XDatabaseMetaData > xMeta( _rxConnection->getMetaData(), UNO_QUERY_THROW );
363 for ( ; aIter != aEnd; ++aIter )
364 {
365 // add the entry
366 implAddEntry(
367 xMeta,
368 aIter->first,
369 sal_False
370 );
371 }
372
373 if ( !m_bNoEmptyFolders && lcl_shouldDisplayEmptySchemasAndCatalogs( _rxConnection ) )
374 {
375 sal_Bool bSupportsCatalogs = xMeta->supportsCatalogsInDataManipulation();
376 sal_Bool bSupportsSchemas = xMeta->supportsSchemasInDataManipulation();
377
378 if ( bSupportsCatalogs || bSupportsSchemas )
379 {
380 // we display empty catalogs if the DB supports catalogs, and they're noted at the beginning of a
381 // composed name. Otherwise, we display empty schematas. (also see the tree structure explained in
382 // implAddEntry)
383 bool bCatalogs = bSupportsCatalogs && xMeta->isCatalogAtStart();
384
385 ::std::vector< ::rtl::OUString > aFolderNames( lcl_getMetaDataStrings_throw(
386 bCatalogs ? xMeta->getCatalogs() : xMeta->getSchemas(), 1 ) );
387 sal_Int32 nFolderType = bCatalogs ? DatabaseObjectContainer::CATALOG : DatabaseObjectContainer::SCHEMA;
388
389 SvLBoxEntry* pRootEntry = getAllObjectsEntry();
390 for ( ::std::vector< ::rtl::OUString >::const_iterator folder = aFolderNames.begin();
391 folder != aFolderNames.end();
392 ++folder
393 )
394 {
395 SvLBoxEntry* pFolder = GetEntryPosByName( *folder, pRootEntry );
396 if ( !pFolder )
397 pFolder = InsertEntry( *folder, pRootEntry, sal_False, LIST_APPEND, reinterpret_cast< void* >( nFolderType ) );
398 }
399 }
400 }
401 }
402 catch ( const Exception& )
403 {
404 DBG_UNHANDLED_EXCEPTION();
405 }
406 }
407 //------------------------------------------------------------------------
isWildcardChecked(SvLBoxEntry * _pEntry) const408 sal_Bool OTableTreeListBox::isWildcardChecked(SvLBoxEntry* _pEntry) const
409 {
410 if (_pEntry)
411 {
412 OBoldListboxString* pTextItem = static_cast<OBoldListboxString*>(_pEntry->GetFirstItem(SV_ITEM_ID_BOLDLBSTRING));
413 if (pTextItem)
414 return pTextItem->isEmphasized();
415 }
416 return sal_False;
417 }
418
419 //------------------------------------------------------------------------
checkWildcard(SvLBoxEntry * _pEntry)420 void OTableTreeListBox::checkWildcard(SvLBoxEntry* _pEntry)
421 {
422 SetCheckButtonState(_pEntry, SV_BUTTON_CHECKED);
423 checkedButton_noBroadcast(_pEntry);
424 }
425
426 //------------------------------------------------------------------------
getAllObjectsEntry() const427 SvLBoxEntry* OTableTreeListBox::getAllObjectsEntry() const
428 {
429 return haveVirtualRoot() ? First() : NULL;
430 }
431
432 //------------------------------------------------------------------------
checkedButton_noBroadcast(SvLBoxEntry * _pEntry)433 void OTableTreeListBox::checkedButton_noBroadcast(SvLBoxEntry* _pEntry)
434 {
435 OMarkableTreeListBox::checkedButton_noBroadcast(_pEntry);
436
437 // if an entry has children, it makes a difference if the entry is checked because alls children are checked
438 // or if the user checked it explicitly.
439 // So we track explicit (un)checking
440
441 SvButtonState eState = GetCheckButtonState(_pEntry);
442 DBG_ASSERT(SV_BUTTON_TRISTATE != eState, "OTableTreeListBox::CheckButtonHdl: user action which lead to TRISTATE?");
443 implEmphasize(_pEntry, SV_BUTTON_CHECKED == eState);
444 }
445
446 //------------------------------------------------------------------------
implEmphasize(SvLBoxEntry * _pEntry,sal_Bool _bChecked,sal_Bool _bUpdateDescendants,sal_Bool _bUpdateAncestors)447 void OTableTreeListBox::implEmphasize(SvLBoxEntry* _pEntry, sal_Bool _bChecked, sal_Bool _bUpdateDescendants, sal_Bool _bUpdateAncestors)
448 {
449 DBG_ASSERT(_pEntry, "OTableTreeListBox::implEmphasize: invalid entry (NULL)!");
450
451 // special emphasizing handling for the "all objects" entry
452 // 89709 - 16.07.2001 - frank.schoenheit@sun.com
453 sal_Bool bAllObjectsEntryAffected = haveVirtualRoot() && (getAllObjectsEntry() == _pEntry);
454 if ( GetModel()->HasChilds(_pEntry) // the entry has children
455 || bAllObjectsEntryAffected // or it is the "all objects" entry
456 )
457 {
458 OBoldListboxString* pTextItem = static_cast<OBoldListboxString*>(_pEntry->GetFirstItem(SV_ITEM_ID_BOLDLBSTRING));
459 if (pTextItem)
460 pTextItem->emphasize(_bChecked);
461
462 if (bAllObjectsEntryAffected)
463 InvalidateEntry(_pEntry);
464 }
465
466 if (_bUpdateDescendants)
467 {
468 // remove the mark for all children of the checked entry
469 SvLBoxEntry* pChildLoop = FirstChild(_pEntry);
470 while (pChildLoop)
471 {
472 if (GetModel()->HasChilds(pChildLoop))
473 implEmphasize(pChildLoop, sal_False, sal_True, sal_False);
474 pChildLoop = NextSibling(pChildLoop);
475 }
476 }
477
478 if (_bUpdateAncestors)
479 {
480 // remove the mark for all ancestors of the entry
481 if (GetModel()->HasParent(_pEntry))
482 implEmphasize(GetParent(_pEntry), sal_False, sal_False, sal_True);
483 }
484 }
485
486 //------------------------------------------------------------------------
InitEntry(SvLBoxEntry * _pEntry,const XubString & _rString,const Image & _rCollapsedBitmap,const Image & _rExpandedBitmap,SvLBoxButtonKind _eButtonKind)487 void OTableTreeListBox::InitEntry(SvLBoxEntry* _pEntry, const XubString& _rString, const Image& _rCollapsedBitmap, const Image& _rExpandedBitmap, SvLBoxButtonKind _eButtonKind)
488 {
489 OMarkableTreeListBox::InitEntry(_pEntry, _rString, _rCollapsedBitmap, _rExpandedBitmap, _eButtonKind);
490
491 // replace the text item with our own one
492 SvLBoxItem* pTextItem = _pEntry->GetFirstItem(SV_ITEM_ID_LBOXSTRING);
493 DBG_ASSERT(pTextItem, "OTableTreeListBox::InitEntry: no text item!?");
494 sal_uInt16 nTextPos = _pEntry->GetPos(pTextItem);
495 DBG_ASSERT(((sal_uInt16)-1) != nTextPos, "OTableTreeListBox::InitEntry: no text item pos!");
496
497 _pEntry->ReplaceItem(new OBoldListboxString(_pEntry, 0, _rString), nTextPos);
498 }
499
500 //------------------------------------------------------------------------
implAddEntry(const Reference<XDatabaseMetaData> & _rxMeta,const::rtl::OUString & _rTableName,sal_Bool _bCheckName)501 SvLBoxEntry* OTableTreeListBox::implAddEntry(
502 const Reference< XDatabaseMetaData >& _rxMeta,
503 const ::rtl::OUString& _rTableName,
504 sal_Bool _bCheckName
505 )
506 {
507 OSL_PRECOND( _rxMeta.is(), "OTableTreeListBox::implAddEntry: invalid meta data!" );
508 if ( !_rxMeta.is() )
509 return NULL;
510
511 // split the complete name into it's components
512 ::rtl::OUString sCatalog, sSchema, sName;
513 qualifiedNameComponents( _rxMeta, _rTableName, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
514
515 SvLBoxEntry* pParentEntry = getAllObjectsEntry();
516
517 // if the DB uses catalog at the start of identifiers, then our hierarchy is
518 // catalog
519 // +- schema
520 // +- table
521 // else it is
522 // schema
523 // +- catalog
524 // +- table
525 sal_Bool bCatalogAtStart = _rxMeta->isCatalogAtStart();
526 const ::rtl::OUString& rFirstName = bCatalogAtStart ? sCatalog : sSchema;
527 const sal_Int32 nFirstFolderType = bCatalogAtStart ? DatabaseObjectContainer::CATALOG : DatabaseObjectContainer::SCHEMA;
528 const ::rtl::OUString& rSecondName = bCatalogAtStart ? sSchema : sCatalog;
529 const sal_Int32 nSecondFolderType = bCatalogAtStart ? DatabaseObjectContainer::SCHEMA : DatabaseObjectContainer::CATALOG;
530
531 if ( rFirstName.getLength() )
532 {
533 SvLBoxEntry* pFolder = GetEntryPosByName( rFirstName, pParentEntry );
534 if ( !pFolder )
535 pFolder = InsertEntry( rFirstName, pParentEntry, sal_False, LIST_APPEND, reinterpret_cast< void* >( nFirstFolderType ) );
536 pParentEntry = pFolder;
537 }
538
539 if ( rSecondName.getLength() )
540 {
541 SvLBoxEntry* pFolder = GetEntryPosByName( rSecondName, pParentEntry );
542 if ( !pFolder )
543 pFolder = InsertEntry( rSecondName, pParentEntry, sal_False, LIST_APPEND, reinterpret_cast< void* >( nSecondFolderType ) );
544 pParentEntry = pFolder;
545 }
546
547 SvLBoxEntry* pRet = NULL;
548 if ( !_bCheckName || !GetEntryPosByName( sName, pParentEntry ) )
549 {
550 pRet = InsertEntry( sName, pParentEntry, sal_False, LIST_APPEND );
551
552 Image aImage, aImageHC;
553 m_pImageProvider->getImages( _rTableName, DatabaseObject::TABLE, aImage, aImageHC );
554
555 SetExpandedEntryBmp( pRet, aImage, BMP_COLOR_NORMAL );
556 SetCollapsedEntryBmp( pRet, aImage, BMP_COLOR_NORMAL );
557 SetExpandedEntryBmp( pRet, aImageHC, BMP_COLOR_HIGHCONTRAST );
558 SetCollapsedEntryBmp( pRet, aImageHC, BMP_COLOR_HIGHCONTRAST );
559 }
560 return pRet;
561 }
562
563 //------------------------------------------------------------------------
describeObject(SvLBoxEntry * _pEntry)564 NamedDatabaseObject OTableTreeListBox::describeObject( SvLBoxEntry* _pEntry )
565 {
566 NamedDatabaseObject aObject;
567
568 sal_Int32 nEntryType = reinterpret_cast< sal_IntPtr >( _pEntry->GetUserData() );
569
570 if ( nEntryType == DatabaseObjectContainer::TABLES )
571 {
572 aObject.Type = DatabaseObjectContainer::TABLES;
573 }
574 else if ( ( nEntryType == DatabaseObjectContainer::CATALOG )
575 || ( nEntryType == DatabaseObjectContainer::SCHEMA )
576 )
577 {
578 SvLBoxEntry* pParent = GetParent( _pEntry );
579 sal_Int32 nParentEntryType = pParent ? reinterpret_cast< sal_IntPtr >( pParent->GetUserData() ) : -1;
580
581 ::rtl::OUStringBuffer buffer;
582 if ( nEntryType == DatabaseObjectContainer::CATALOG )
583 {
584 if ( nParentEntryType == DatabaseObjectContainer::SCHEMA )
585 {
586 buffer.append( GetEntryText( pParent ) );
587 buffer.append( sal_Unicode( '.' ) );
588 }
589 buffer.append( GetEntryText( _pEntry ) );
590 }
591 else if ( nEntryType == DatabaseObjectContainer::SCHEMA )
592 {
593 if ( nParentEntryType == DatabaseObjectContainer::CATALOG )
594 {
595 buffer.append( GetEntryText( pParent ) );
596 buffer.append( sal_Unicode( '.' ) );
597 }
598 buffer.append( GetEntryText( _pEntry ) );
599 }
600 }
601 else
602 {
603 aObject.Type = DatabaseObject::TABLE;
604 aObject.Name = getQualifiedTableName( _pEntry );
605 }
606
607 return aObject;
608 }
609
610 //------------------------------------------------------------------------
addedTable(const::rtl::OUString & _rName)611 SvLBoxEntry* OTableTreeListBox::addedTable( const ::rtl::OUString& _rName )
612 {
613 try
614 {
615 Reference< XDatabaseMetaData > xMeta;
616 if ( impl_getAndAssertMetaData( xMeta ) )
617 return implAddEntry( xMeta, _rName );
618 }
619 catch( const Exception& )
620 {
621 DBG_UNHANDLED_EXCEPTION();
622 }
623 return NULL;
624 }
625
626 //------------------------------------------------------------------------
impl_getAndAssertMetaData(Reference<XDatabaseMetaData> & _out_rMetaData) const627 bool OTableTreeListBox::impl_getAndAssertMetaData( Reference< XDatabaseMetaData >& _out_rMetaData ) const
628 {
629 if ( m_xConnection.is() )
630 _out_rMetaData = m_xConnection->getMetaData();
631 OSL_PRECOND( _out_rMetaData.is(), "OTableTreeListBox::impl_getAndAssertMetaData: invalid current connection!" );
632 return _out_rMetaData.is();
633 }
634
635 //------------------------------------------------------------------------
getQualifiedTableName(SvLBoxEntry * _pEntry) const636 String OTableTreeListBox::getQualifiedTableName( SvLBoxEntry* _pEntry ) const
637 {
638 OSL_PRECOND( !isFolderEntry( _pEntry ), "OTableTreeListBox::getQualifiedTableName: folder entries not allowed here!" );
639
640 try
641 {
642 Reference< XDatabaseMetaData > xMeta;
643 if ( !impl_getAndAssertMetaData( xMeta ) )
644 return String();
645
646 ::rtl::OUString sCatalog;
647 ::rtl::OUString sSchema;
648 ::rtl::OUString sTable;
649
650 SvLBoxEntry* pSchema = GetParent( _pEntry );
651 if ( pSchema )
652 {
653 SvLBoxEntry* pCatalog = GetParent( pSchema );
654 if ( pCatalog
655 || ( xMeta->supportsCatalogsInDataManipulation()
656 && !xMeta->supportsSchemasInDataManipulation()
657 ) // here we support catalog but no schema
658 )
659 {
660 if ( pCatalog == NULL )
661 {
662 pCatalog = pSchema;
663 pSchema = NULL;
664 }
665 sCatalog = GetEntryText( pCatalog );
666 }
667 if ( pSchema )
668 sSchema = GetEntryText(pSchema);
669 }
670 sTable = GetEntryText( _pEntry );
671
672 return ::dbtools::composeTableName( xMeta, sCatalog, sSchema, sTable, sal_False, ::dbtools::eInDataManipulation );
673 }
674 catch( const Exception& )
675 {
676 DBG_UNHANDLED_EXCEPTION();
677 }
678 return String();
679 }
680
681 //------------------------------------------------------------------------
getEntryByQualifiedName(const::rtl::OUString & _rName)682 SvLBoxEntry* OTableTreeListBox::getEntryByQualifiedName( const ::rtl::OUString& _rName )
683 {
684 try
685 {
686 Reference< XDatabaseMetaData > xMeta;
687 if ( !impl_getAndAssertMetaData( xMeta ) )
688 return NULL;
689
690 // split the complete name into it's components
691 ::rtl::OUString sCatalog, sSchema, sName;
692 qualifiedNameComponents(xMeta, _rName, sCatalog, sSchema, sName,::dbtools::eInDataManipulation);
693
694 SvLBoxEntry* pParent = getAllObjectsEntry();
695 SvLBoxEntry* pCat = NULL;
696 SvLBoxEntry* pSchema = NULL;
697 if ( sCatalog.getLength() )
698 {
699 pCat = GetEntryPosByName(sCatalog, pParent);
700 if ( pCat )
701 pParent = pCat;
702 }
703
704 if ( sSchema.getLength() )
705 {
706 pSchema = GetEntryPosByName(sSchema, pParent);
707 if ( pSchema )
708 pParent = pSchema;
709 }
710
711 return GetEntryPosByName(sName, pParent);
712 }
713 catch( const Exception& )
714 {
715 DBG_UNHANDLED_EXCEPTION();
716 }
717 return NULL;
718 }
719 //------------------------------------------------------------------------
removedTable(const::rtl::OUString & _rName)720 void OTableTreeListBox::removedTable( const ::rtl::OUString& _rName )
721 {
722 try
723 {
724 SvLBoxEntry* pEntry = getEntryByQualifiedName( _rName );
725 if ( pEntry )
726 GetModel()->Remove( pEntry );
727 }
728 catch( const Exception& )
729 {
730 DBG_UNHANDLED_EXCEPTION();
731 }
732 }
733
734 //.........................................................................
735 } // namespace dbaui
736 //.........................................................................
737