mysqlmanagerview.cpp 27.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
// MySqlManagerView.cpp : implementation of the CMySqlManagerView class
//

#include "stdafx.h"
#include "MySqlManager.h"
#include "MySqlManagerDoc.h"
#include "MySqlManagerView.h"
#include "mainfrm.h"
#include "ToolSql.h"
#include "RegisterServer.h"

class XStatus
{
public:
   XStatus ( LPCSTR fmt, ... )
   {
      char buf [2048];
      va_list args;
      va_start(args, fmt);
      int ret = vsprintf(buf, fmt, args);
      MainFrame->StatusMsg ( "%s", buf );
      va_end(args);
   }
   ~XStatus()
   {
      MainFrame->StatusMsg ( " ");
   }
private:
   XStatus();
};

#ifdef _DEBUG
   #define new DEBUG_NEW
   #undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(CMySqlManagerView, CTreeView)

BEGIN_MESSAGE_MAP(CMySqlManagerView, CTreeView)
//{{AFX_MSG_MAP(CMySqlManagerView)
ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
ON_COMMAND(IDM_SQL_TOOL_QUERY, OnSqlToolQuery)
ON_COMMAND(IDM_REFRESH, OnRefresh)
ON_COMMAND(IDM_TOOLS_SERVER_PROPERTIES,OnServerProperties)
ON_COMMAND(IDM_TOOLS_REGISTER_SERVER, OnRegisterServer)
ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

CMySqlManagerView::CMySqlManagerView()
: m_pTree(0)
, m_pImages(0)
, m_pbmBmp(0)
, m_pTool(0)
{
}

/////////////////////////////////////////////////////////////////////////////

CMySqlManagerView::~CMySqlManagerView()
{
   if ( m_pbmBmp ) delete m_pbmBmp;
   if ( m_pImages ) delete m_pImages;
   if ( m_pTool )
   {
      m_pTool->DestroyWindow();
      delete m_pTool;
   }
}

/////////////////////////////////////////////////////////////////////////////

BOOL CMySqlManagerView::PreCreateWindow(CREATESTRUCT& cs)
{
   return CTreeView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnDraw(CDC* pDC)
{
   CMySqlManagerDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
}

/////////////////////////////////////////////////////////////////////////////

BOOL CMySqlManagerView::OnPreparePrinting(CPrintInfo* pInfo)
{
   return DoPreparePrinting(pInfo);
}

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}

/////////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
void CMySqlManagerView::AssertValid() const
{
   CTreeView::AssertValid();
}

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::Dump(CDumpContext& dc) const
{
   CTreeView::Dump(dc);
}

/////////////////////////////////////////////////////////////////////////////

CMySqlManagerDoc* CMySqlManagerView::GetDocument() // non-debug version is inline
{
   ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMySqlManagerDoc)));
   return (CMySqlManagerDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnInitialUpdate()
{

   CTreeView::OnInitialUpdate();

   m_pTree = & GetTreeCtrl();
   m_pImages = new CImageList;
   m_pImages->Create( 16, 16, FALSE, 0, 10 );
   m_pbmBmp = new CBitmap;
   m_pbmBmp->LoadBitmap( IDB_BITMAP1 );
   m_pImages->Add( m_pbmBmp, (COLORREF)0 );
   m_pTree->SetImageList( m_pImages, TVSIL_NORMAL );

   HTREEITEM h = AddResource ( TVI_ROOT, new CResourceServer ( "MySQL", "localhost", "root", "" ) );
//   AddResource ( h, new CResourceProcesslist () );
   h = AddResource ( TVI_ROOT, new CResourceServer ( "Test", "localhost", "test", "" ) );
//   AddResource ( h, new CResourceProcesslist () );

   m_pTree->ModifyStyle(0, TVS_HASLINES|TVS_HASBUTTONS);

}

/////////////////////////////////////////////////////////////////////////////

HTREEITEM CMySqlManagerView::AddResource ( HTREEITEM hParent, CResource* pRes, HTREEITEM hLastItem )
{

   TV_INSERTSTRUCT ItemStruct;
   memset( &ItemStruct, 0, sizeof(ItemStruct) );
   ItemStruct.hParent = hParent;
   ItemStruct.hInsertAfter = hLastItem;
   ItemStruct.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_SELECTEDIMAGE | TVIF_IMAGE;
   ItemStruct.item.hItem = 0;
   ItemStruct.item.state = 0;
   ItemStruct.item.stateMask = 0;
   ItemStruct.item.pszText = (LPSTR) pRes->GetDisplayName();
   ItemStruct.item.cchTextMax =  (int) strlen( ItemStruct.item.pszText );
   ItemStruct.item.iImage = 2;
   ItemStruct.item.iSelectedImage = 3;
   ItemStruct.item.cChildren = 0;
   ItemStruct.item.lParam = (long) pRes;
   hLastItem = m_pTree->InsertItem( &ItemStruct );
   return hLastItem;
}

//int InsertNetResources( LPNETRESOURCE lpNetResource, CTreeCtrl *pTreeCtrl, HTREEITEM hParent, int *pnCount )
//{
//
//   DWORD Erc;
//   NETRESOURCE   *pNetRes;
//   HANDLE hEnum;
//
//   if( !pTreeCtrl ) return -1;
//   if( pnCount ) *pnCount = 0;
//   Erc = WNetOpenEnum(
//                  RESOURCE_GLOBALNET,//DWORD dwScope,   // scope of enumeration
//                  RESOURCETYPE_ANY,//DWORD dwType,   // resource types to list
//                  0,//DWORD dwUsage,   // resource usage to list
//                  lpNetResource,//LPNETRESOURCE lpNetResource,   // pointer to resource structure
//                  &hEnum//LPHANDLE lphEnum    // pointer to enumeration handle buffer
//                  );
//   if( Erc )
//   {
//      ShowError( Erc );
//      return Erc;
//   }
//
//
//   DWORD dwBufferSize = 1024;
//   pNetRes = (NETRESOURCE *)malloc( dwBufferSize );
//
//   while( TRUE )
//   {
//      DWORD dwCount = 0xFFFFFFFF;
//      Erc = WNetEnumResource(
//                        hEnum,//HANDLE hEnum,   // handle to enumeration
//                        &dwCount,//LPDWORD lpcCount,   // pointer to entries to list
//                        pNetRes,//LPVOID lpBuffer,   // pointer to buffer for results
//                        &dwBufferSize//LPDWORD lpBufferSize    // pointer to buffer size variable
//                        );
//      if( Erc == ERROR_NO_MORE_ITEMS ) return 0;
//      if( Erc )
//      {
//         free( pNetRes );
//         pNetRes = (NETRESOURCE *)malloc( dwBufferSize );
//         Erc = WNetEnumResource(
//                           hEnum,//HANDLE hEnum,   // handle to enumeration
//                           &dwCount,//LPDWORD lpcCount,   // pointer to entries to list
//                           pNetRes,//LPVOID lpBuffer,   // pointer to buffer for results
//                           &dwBufferSize//LPDWORD lpBufferSize    // pointer to buffer size variable
//                           );
//      }
//      if( Erc ){ ShowError( Erc ); return Erc; }
//
//      TV_INSERTSTRUCT ItemStruct;
//      HTREEITEM hLastItem = TVI_FIRST;
//      DWORD i;
//
//      if( pnCount ) *pnCount += dwCount;
//      for( i=0; i<dwCount; i++ )
//      {
//         memset( &ItemStruct, 0, sizeof(ItemStruct) );
//         ItemStruct.hParent = hParent;
//         ItemStruct.hInsertAfter = hLastItem;
//         ItemStruct.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_SELECTEDIMAGE | TVIF_IMAGE;
//         ItemStruct.item.hItem = 0;
//         ItemStruct.item.state = 0;
//         ItemStruct.item.stateMask = 0;
//         ItemStruct.item.pszText = pNetRes[i].lpRemoteName;
//         ItemStruct.item.cchTextMax = strlen( ItemStruct.item.pszText );
//         ItemStruct.item.iImage = 2;
//         ItemStruct.item.iSelectedImage = 3;
//         ItemStruct.item.cChildren = 0;
//         ItemStruct.item.lParam = (long) (new CNetResource( &pNetRes[i] ));
//
//         hLastItem = pTreeCtrl->InsertItem( &ItemStruct );
//      }
//   }//end while()
//
//   WNetCloseEnum( hEnum );
//   free( pNetRes );
//   return Erc;
//}

/////////////////////////////////////////////////////////////////////////////

static void print_top(MYSQL_RES *result)
{
   uint length;
   MYSQL_FIELD *field;
   mysql_field_seek(result,0);
   while ((field = mysql_fetch_field(result)))
   {
      if ((length= (uint) strlen(field->name)) > field->max_length)
         field->max_length=length;
      else
         length=field->max_length;
   }
}

/////////////////////////////////////////////////////////////////////////////

static void print_header(MYSQL_RES *result,CStringArray& rg)
{
   MYSQL_FIELD *field;
   print_top(result);
   mysql_field_seek(result,0);
   while ((field = mysql_fetch_field(result)))
   {
//    printf(" %-*s|",field->max_length+1,field->name);
      rg.Add(field->name);
   }
   print_top(result);
}


/////////////////////////////////////////////////////////////////////////////

static void print_row(MYSQL_RES *result,MYSQL_ROW row,CStringArray& rg)
{
   uint i,length;
   MYSQL_FIELD *field;
   mysql_field_seek(result,0);
   for (i=0 ; i < mysql_num_fields(result); i++)
   {
      field = mysql_fetch_field(result);
      length=field->max_length;
      rg.Add(row[i] ? (char*) row[i] : "");
//    printf(" %-*s|",length+1,row[i] ? (char*) row[i] : "");
   }
}

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::ProcessResultSet ( HTREEITEM hItem, LPVOID r, CResource* pResource )
{

   MYSQL_RES* result = (MYSQL_RES *) r;
   MYSQL_ROW row;

   switch (pResource->GetType())
   {
      case CResource::eProcesslist:
         {
            CResourceProcesslist* p = (CResourceProcesslist*) pResource;
            CResourceProcesslistItem* pi = new CResourceProcesslistItem ();
            CString strText;
            print_header(result,p->m_rgFields);
            for (int i = 0; i<p->m_rgFields.GetSize(); i++ )
            {
               strText += p->m_rgFields[i];
               strText += " ";
            }
            pi->m_strName = strText;
            AddResource ( hItem, pi );
            for (int index=0;(row=mysql_fetch_row(result));index++)
            {
               pi = new CResourceProcesslistItem ();
               print_row(result,row,pi->m_rgFields);
               strText.Empty();
               for (int i = 0; i<pi->m_rgFields.GetSize(); i++ )
               {
                  strText += pi->m_rgFields[i];
                  strText += " ";
               }
               pi->m_strName = strText;
               AddResource ( hItem, pi );
            }
            print_top(result);
            break;
         }
      case CResource::eServer:
         {
            CResourceServer* p = (CResourceServer*) pResource;
            CResourceDatabase* pi = new CResourceDatabase ();
            CString strText;
            /* print_header(result,p->m_rgFields); */
            for (int i = 0; i<p->m_rgFields.GetSize(); i++ )
            {
               strText += p->m_rgFields[i];
               strText += " ";
            }
            pi->m_strName = strText;
            /* AddResource ( hItem, pi ); */
            for (int index=0;(row=mysql_fetch_row(result));index++)
            {
               pi = new CResourceDatabase ();
               print_row(result,row,pi->m_rgFields);
               strText.Empty();
               for (int i = 0; i<pi->m_rgFields.GetSize(); i++ )
               {
                  strText += pi->m_rgFields[i];
                  strText += " ";
               }
               pi->m_strName = strText;
               AddResource ( hItem, pi );
            }
            print_top(result);
            break;
         }
      case CResource::eDatabase:
         {
            CResourceDatabase* p = (CResourceDatabase*) pResource;
            CResourceTable* pi = new CResourceTable ();
            CString strText;
            /* print_header(result,p->m_rgFields); */
            for (int i = 0; i<p->m_rgFields.GetSize(); i++ )
            {
               strText += p->m_rgFields[i];
               strText += " ";
            }
            pi->m_strName = strText;
            /* AddResource ( hItem, pi ); */
            for (int index=0;(row=mysql_fetch_row(result));index++)
            {
               pi = new CResourceTable ();
               print_row(result,row,pi->m_rgFields);
               strText.Empty();
               for (int i = 0; i<pi->m_rgFields.GetSize(); i++ )
               {
                  strText += pi->m_rgFields[i];
                  strText += " ";
               }
               pi->m_strName = strText;
               AddResource ( hItem, pi );
            }
            print_top(result);
            break;
         }
      case CResource::eTable:
         {
            CResourceTable* p = (CResourceTable*) pResource;
            CResourceField* pi = new CResourceField ();
            CString strText;
            /* print_header(result,p->m_rgFields); */
            for (int i = 0; i<p->m_rgFields.GetSize(); i++ )
            {
               strText += p->m_rgFields[i];
               strText += " ";
            }
            pi->m_strName = strText;
            /* AddResource ( hItem, pi ); */
            for (int index=0;(row=mysql_fetch_row(result));index++)
            {
               pi = new CResourceField ();
               print_row(result,row,pi->m_rgFields);
               strText.Empty();
               for (int i = 0; i<pi->m_rgFields.GetSize(); i++ )
               {
                  strText += pi->m_rgFields[i];
                  strText += " ";
               }
               pi->m_strName = strText;
               AddResource ( hItem, pi );
            }
            print_top(result);
            break;
         }
   }


}

/////////////////////////////////////////////////////////////////////////////

CResource* CMySqlManagerView::GetSelectedResource(HTREEITEM* phItemRet)
{
   CResource* pResource = NULL;
   HTREEITEM  hItem = m_pTree->GetSelectedItem();
   if ( hItem )
   {
      TV_ITEM item;
      memset( &item, 0, sizeof(TV_ITEM) );
      item.hItem = hItem;
      item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
      m_pTree->GetItem( &item );
      if ( item.lParam )
      {
         pResource = (CResource*) item.lParam;
      }
   }
   if (phItemRet)
   {
      *phItemRet = hItem;
   }
   return pResource;
}

/////////////////////////////////////////////////////////////////////////////

CResourceServer* CMySqlManagerView::GetServerResource(HTREEITEM hItem)
{

   TV_ITEM item;

   memset( &item, 0, sizeof(TV_ITEM) );
   item.hItem = hItem;
   item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
   m_pTree->GetItem( &item );
   if ( !item.lParam ) return NULL;

   CResource* pResource = (CResource*) item.lParam;

   switch (pResource->GetType())
   {
      case CResource::eServer:
         {
            return (CResourceServer*) pResource;
         }
      case CResource::eDatabase:
         {
            HTREEITEM hParent = m_pTree->GetParentItem(hItem);
            memset( &item, 0, sizeof(TV_ITEM) );
            item.hItem = hParent;
            item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
            m_pTree->GetItem( &item );
            if ( !item.lParam ) return NULL;
            return (CResourceServer*) item.lParam;
         }
      case CResource::eTable:
         {
            HTREEITEM hParent = m_pTree->GetParentItem(m_pTree->GetParentItem(hItem));
            memset( &item, 0, sizeof(TV_ITEM) );
            item.hItem = hParent;
            item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
            m_pTree->GetItem( &item );
            if ( !item.lParam ) return NULL;
            return (CResourceServer*) item.lParam;
         }
   }

   return NULL;

}
/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
   HTREEITEM hItem;
   hItem = m_pTree->GetSelectedItem();
   *pResult = 0;
   if ( !hItem ) return;

   TV_ITEM item;
   memset( &item, 0, sizeof(TV_ITEM) );
   item.hItem = hItem;
   item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
   m_pTree->GetItem( &item );

   if ( ! item.lParam ) return;

   if ( item.cChildren ) return; //if has got children expand only

   CWaitCursor x;

   CResource* pResource = (CResource*) item.lParam;

   MYSQL mysql;
   MYSQL_RES *result;

   switch (pResource->GetType())
   {
      case CResource::eProcesslist:
         {
            XStatus x ( "Connecting to server %s on host %s..."
                        , (LPCTSTR) pResource->GetDisplayName()
                        , (LPCTSTR) pResource->GetHostName()
                      );
            mysql_init(&mysql);
            if (!mysql_real_connect(&mysql,pResource->GetHostName(), pResource->GetUserName(),pResource->GetPassword(),0,pResource->GetPortNumber(), NullS,0))
            {
					PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
               return;
            }
554
            mysql.reconnect= 1;
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
            if (!(result=mysql_list_processes(&mysql)))
            {
               return;
            }
            ProcessResultSet ( hItem, result, pResource );
            mysql_free_result(result);
            mysql_close(&mysql);
            break;
         }
      case CResource::eServer:
         {
            MainFrame->StatusMsg ( "Connecting to server %s on host %s..."
                                   , (LPCTSTR) pResource->GetDisplayName()
                                   , (LPCTSTR) pResource->GetHostName()
                                 );
            mysql_init(&mysql);
            if (!mysql_real_connect(&mysql,pResource->GetHostName(), pResource->GetUserName(),pResource->GetPassword(),0,pResource->GetPortNumber(), NullS,0))
            {
					PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
               MainFrame->StatusMsg ( "Error: Connecting to server %s... (%s)"
                                      , (LPCTSTR) pResource->GetDisplayName()
                                      , mysql_error(&mysql)
                                    );
               return;
            }
580
            mysql.reconnect= 1;
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
            if (!(result=mysql_list_dbs(&mysql,0)))
            {
            }
            ProcessResultSet ( hItem, result, pResource );
            mysql_free_result(result);
            mysql_close(&mysql);
            MainFrame->StatusMsg ( " " );
            break;
         }
      case CResource::eDatabase:
         {
            CResourceServer* pServer = GetServerResource(hItem);
            if (!pServer) return;
            MainFrame->StatusMsg ( "Connecting to server %s on host %s..."
                                   , (LPCTSTR) pServer->GetDisplayName()
                                   , (LPCTSTR) pServer->GetHostName()
                                 );
            mysql_init(&mysql);
            if (!mysql_real_connect(&mysql,pServer->GetHostName(), pServer->GetUserName(),pServer->GetPassword(),0,pServer->GetPortNumber(), NullS,0))
            {
					PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
               MainFrame->StatusMsg ( "Error: Connecting to server %s... (%s)"
                                      , (LPCTSTR) pServer->GetDisplayName()
                                      , mysql_error(&mysql)
                                    );
               return;
            }
608
            mysql.reconnect= 1;
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
            CResourceDatabase* pRes = (CResourceDatabase*) pResource;
            CString strDB = pResource->GetDisplayName();
            strDB.TrimRight();
            if (mysql_select_db(&mysql,strDB))
            {
               MainFrame->StatusMsg ( "Error: Selecting database %s... (%s)"
                                      , (LPCTSTR) strDB
                                      , mysql_error(&mysql)
                                    );
               return;
            }
            if (!(result=mysql_list_tables(&mysql,0)))
            {
            }
            ProcessResultSet ( hItem, result, pRes );
            mysql_free_result(result);
            mysql_close(&mysql);
            MainFrame->StatusMsg ( " " );
            break;
         }
      case CResource::eTable:
         {
            CResourceServer* pServer = GetServerResource(hItem);
            if (!pServer) return;
            MainFrame->StatusMsg ( "Connecting to server %s on host %s..."
                                   , (LPCTSTR) pServer->GetDisplayName()
                                   , (LPCTSTR) pServer->GetHostName()
                                 );
            mysql_init(&mysql);
            if (!mysql_real_connect(&mysql,pServer->GetHostName(), pServer->GetUserName(),pServer->GetPassword(),0,pServer->GetPortNumber(), NullS,0))
            {
					PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
               MainFrame->StatusMsg ( "Error: Connecting to server %s... (%s)"
                                      , (LPCTSTR) pServer->GetDisplayName()
                                      , mysql_error(&mysql)
                                    );
               return;
            }
647
            mysql.reconnect= 1;
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
            HTREEITEM hParent = m_pTree->GetParentItem(hItem);
            memset( &item, 0, sizeof(TV_ITEM) );
            item.hItem = hParent;
            item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
            m_pTree->GetItem( &item );
            if ( item.lParam )
            {
               CResourceDatabase* pResDatabase = (CResourceDatabase*) item.lParam;
               CResourceTable* pRes = (CResourceTable*) pResource;
               CString strDB = pResDatabase->GetDisplayName();
               CString strTable = pResource->GetDisplayName();
               strDB.TrimRight();
               strTable.TrimRight();
               if (mysql_select_db(&mysql,strDB))
               {
                  return;
               }
               CString str; str.Format("show fields from %s",(LPCTSTR)strTable);
               if ( mysql_query(&mysql,str)==0 )
               {
                  MYSQL_RES *result;
                  if ((result=mysql_store_result(&mysql)))
                  {
                     ProcessResultSet ( hItem, result, pRes );
                     mysql_free_result(result);
                  }
               }
            }
            mysql_close(&mysql);
            break;
         }
   }

//  InsertNetResources( (LPNETRESOURCE)pTvItem->lParam,
//                 &m_TreeCtrl,
//                 hItem,
//                 &pTvItem->cChildren );
//  pTvItem->mask = TVIF_CHILDREN;
//  m_TreeCtrl.SetItem( pTvItem );

}

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnRefresh()
{
   HTREEITEM hItem = NULL;
   CResource* pResource = GetSelectedResource(&hItem);
   if (pResource&&hItem)
   {
      switch (pResource->GetType())
      {
         case CResource::eTable:
            {

               TV_ITEM item;
               MYSQL mysql;
//             MYSQL_RES *result;

               HTREEITEM hParent = m_pTree->GetParentItem(hItem);

               HTREEITEM hChild = m_pTree->GetChildItem(hItem);
               while (hChild)
               {
                  HTREEITEM h = m_pTree->GetNextSiblingItem(hChild);
                  BOOL b = m_pTree->DeleteItem(hChild);
                  hChild = h;
               }
               mysql_init(&mysql);
               if (!mysql_real_connect(&mysql,pResource->GetHostName(), pResource->GetUserName(),pResource->GetPassword(),0,pResource->GetPortNumber(), NullS,0))
               {
                  return;
               }
721
               mysql.reconnect= 1;
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
               memset( &item, 0, sizeof(TV_ITEM) );
               item.hItem = hParent;
               item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
               m_pTree->GetItem( &item );
               if ( item.lParam )
               {
                  CResourceDatabase* pResDatabase = (CResourceDatabase*) item.lParam;
                  CResourceTable* pRes = (CResourceTable*) pResource;
                  CString strDB = pResDatabase->GetDisplayName();
                  CString strTable = pResource->GetDisplayName();
                  strDB.TrimRight();
                  strTable.TrimRight();
                  if (mysql_select_db(&mysql,strDB))
                  {
                     return;
                  }
                  CString str; str.Format("show fields from %s",(LPCTSTR)strTable);
                  if ( mysql_query(&mysql,str)==0 )
                  {
                     MYSQL_RES *result;
                     if ((result=mysql_store_result(&mysql)))
                     {
                        ProcessResultSet ( hItem, result, pRes );
                        mysql_free_result(result);
                     }
                  }
               }
               mysql_close(&mysql);
               break;
            }
      }
   }
}

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnRegisterServer()
{
   CRegisterServer dlg;
   if (dlg.DoModal()!=IDOK) return;
   AddResource (
               TVI_ROOT,
               new CResourceServer ( dlg.m_strServer, dlg.m_strHost, dlg.m_strUser, dlg.m_strPassword, dlg.m_strPort )
               );
}

/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnServerProperties()
{
   HTREEITEM hItem;
   CResource* pRes = GetSelectedResource(&hItem);
   if (!pRes) return;
   if (pRes->GetType()!=CResource::eServer) return;
   CResourceServer* pResource = (CResourceServer*)pRes;
   CRegisterServer dlg;
   dlg.m_strHost       = pResource->GetHostName();
   dlg.m_strUser       = pResource->GetUserName();
   dlg.m_strPassword   = pResource->GetPassword();
   dlg.m_strPort       = pResource->GetPortName();
   if (dlg.DoModal()!=IDOK) return;
   pResource->m_strHost     = dlg.m_strHost    ;
   pResource->m_strUser     = dlg.m_strUser    ;
   pResource->m_strPassword = dlg.m_strPassword;
   pResource->m_strPort     = dlg.m_strPort    ;
   TV_ITEM item;
   memset( &item, 0, sizeof(TV_ITEM) );
   item.hItem = hItem;
   item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
   m_pTree->GetItem( &item );
}


/////////////////////////////////////////////////////////////////////////////

void CMySqlManagerView::OnSqlToolQuery()
{

   HTREEITEM hItem;

   CResource* pResource = GetSelectedResource(&hItem);

   if (!pResource) return;

   CResourceServer* pServer = GetServerResource(hItem);
   if (!pServer) return; /* Avoid bug when selecting field */

   m_pTool = new CToolSql ( AfxGetMainWnd(), pServer, pResource );

   if ( ! m_pTool->Create(IDD_TOOL_SQL,this) )
   {
      delete m_pTool;
      m_pTool = 0;
		PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
   }
   else
   {
      m_pTool->ShowWindow(SW_SHOW);
   }

}


/////////////////////////////////////////////////////////////////////////////

BOOL CMySqlManagerView::PreTranslateMessage(MSG* pMsg)
{
   if (m_pTool && m_pTool->PreTranslateMessage(pMsg))
      return TRUE;
   return CTreeView::PreTranslateMessage(pMsg);
}

void CMySqlManagerView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
{

   POINT pt;

   GetCursorPos ( & pt );

   CMenu menu;

   menu.CreatePopupMenu ();

   menu.AppendMenu ( MF_ENABLED , IDM_SQL_TOOL_QUERY,          "SQL Query" );
   menu.AppendMenu ( MF_ENABLED , IDM_REFRESH,                 "Refresh active item(s)" );
   menu.AppendMenu ( MF_ENABLED , IDM_TOOLS_REGISTER_SERVER,   "Register server" );
   menu.AppendMenu ( MF_ENABLED , IDM_TOOLS_SERVER_PROPERTIES, "Properties" );

   menu.TrackPopupMenu ( TPM_LEFTALIGN | TPM_RIGHTBUTTON , pt.x, pt.y, CWnd::GetParent(), NULL );

   *pResult = 0;

}