home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / mysqlmanager / mysqlmanagerview.cpp < prev    next >
C/C++ Source or Header  |  2000-08-23  |  28KB  |  850 lines

  1. // MySqlManagerView.cpp : implementation of the CMySqlManagerView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MySqlManager.h"
  6. #include "MySqlManagerDoc.h"
  7. #include "MySqlManagerView.h"
  8. #include "mainfrm.h"
  9. #include "ToolSql.h"
  10. #include "RegisterServer.h"
  11.  
  12. class XStatus
  13. {
  14. public:
  15.    XStatus ( LPCSTR fmt, ... )
  16.    {
  17.       char buf [2048];
  18.       va_list args;
  19.       va_start(args, fmt);
  20.       int ret = vsprintf(buf, fmt, args);
  21.       MainFrame->StatusMsg ( "%s", buf );
  22.       va_end(args);
  23.    }
  24.    ~XStatus()
  25.    {
  26.       MainFrame->StatusMsg ( " ");
  27.    }
  28. private:
  29.    XStatus();
  30. };
  31.  
  32. #ifdef _DEBUG
  33.    #define new DEBUG_NEW
  34.    #undef THIS_FILE
  35. static char THIS_FILE[] = __FILE__;
  36. #endif
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39.  
  40. IMPLEMENT_DYNCREATE(CMySqlManagerView, CTreeView)
  41.  
  42. BEGIN_MESSAGE_MAP(CMySqlManagerView, CTreeView)
  43. //{{AFX_MSG_MAP(CMySqlManagerView)
  44. ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
  45. ON_COMMAND(IDM_SQL_TOOL_QUERY, OnSqlToolQuery)
  46. ON_COMMAND(IDM_REFRESH, OnRefresh)
  47. ON_COMMAND(IDM_TOOLS_SERVER_PROPERTIES,OnServerProperties)
  48. ON_COMMAND(IDM_TOOLS_REGISTER_SERVER, OnRegisterServer)
  49. ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)
  50. //}}AFX_MSG_MAP
  51. // Standard printing commands
  52. ON_COMMAND(ID_FILE_PRINT, CTreeView::OnFilePrint)
  53. ON_COMMAND(ID_FILE_PRINT_DIRECT, CTreeView::OnFilePrint)
  54. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CTreeView::OnFilePrintPreview)
  55. END_MESSAGE_MAP()
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58.  
  59. CMySqlManagerView::CMySqlManagerView()
  60. : m_pTree(0)
  61. , m_pImages(0)
  62. , m_pbmBmp(0)
  63. , m_pTool(0)
  64. {
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68.  
  69. CMySqlManagerView::~CMySqlManagerView()
  70. {
  71.    if ( m_pbmBmp ) delete m_pbmBmp;
  72.    if ( m_pImages ) delete m_pImages;
  73.    if ( m_pTool )
  74.    {
  75.       m_pTool->DestroyWindow();
  76.       delete m_pTool;
  77.    }
  78. }
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81.  
  82. BOOL CMySqlManagerView::PreCreateWindow(CREATESTRUCT& cs)
  83. {
  84.    return CTreeView::PreCreateWindow(cs);
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88.  
  89. void CMySqlManagerView::OnDraw(CDC* pDC)
  90. {
  91.    CMySqlManagerDoc* pDoc = GetDocument();
  92.    ASSERT_VALID(pDoc);
  93. }
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96.  
  97. BOOL CMySqlManagerView::OnPreparePrinting(CPrintInfo* pInfo)
  98. {
  99.    return DoPreparePrinting(pInfo);
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103.  
  104. void CMySqlManagerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  105. {
  106. }
  107.  
  108. /////////////////////////////////////////////////////////////////////////////
  109.  
  110. void CMySqlManagerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  111. {
  112. }
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115.  
  116. #ifdef _DEBUG
  117. void CMySqlManagerView::AssertValid() const
  118. {
  119.    CTreeView::AssertValid();
  120. }
  121.  
  122. /////////////////////////////////////////////////////////////////////////////
  123.  
  124. void CMySqlManagerView::Dump(CDumpContext& dc) const
  125. {
  126.    CTreeView::Dump(dc);
  127. }
  128.  
  129. /////////////////////////////////////////////////////////////////////////////
  130.  
  131. CMySqlManagerDoc* CMySqlManagerView::GetDocument() // non-debug version is inline
  132. {
  133.    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMySqlManagerDoc)));
  134.    return (CMySqlManagerDoc*)m_pDocument;
  135. }
  136. #endif //_DEBUG
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139.  
  140. void CMySqlManagerView::OnInitialUpdate()
  141. {
  142.  
  143.    CTreeView::OnInitialUpdate();
  144.  
  145.    m_pTree = & GetTreeCtrl();
  146.    m_pImages = new CImageList;
  147.    m_pImages->Create( 16, 16, FALSE, 0, 10 );
  148.    m_pbmBmp = new CBitmap;
  149.    m_pbmBmp->LoadBitmap( IDB_BITMAP1 );
  150.    m_pImages->Add( m_pbmBmp, (COLORREF)0 );
  151.    m_pTree->SetImageList( m_pImages, TVSIL_NORMAL );
  152.  
  153.    HTREEITEM h = AddResource ( TVI_ROOT, new CResourceServer ( "MySQL", "localhost", "root", "" ) );
  154. //   AddResource ( h, new CResourceProcesslist () );
  155.    h = AddResource ( TVI_ROOT, new CResourceServer ( "Test", "localhost", "test", "" ) );
  156. //   AddResource ( h, new CResourceProcesslist () );
  157.  
  158.    m_pTree->ModifyStyle(0, TVS_HASLINES|TVS_HASBUTTONS);
  159.  
  160. }
  161.  
  162. /////////////////////////////////////////////////////////////////////////////
  163.  
  164. HTREEITEM CMySqlManagerView::AddResource ( HTREEITEM hParent, CResource* pRes, HTREEITEM hLastItem )
  165. {
  166.  
  167.    TV_INSERTSTRUCT ItemStruct;
  168.    memset( &ItemStruct, 0, sizeof(ItemStruct) );
  169.    ItemStruct.hParent = hParent;
  170.    ItemStruct.hInsertAfter = hLastItem;
  171.    ItemStruct.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_SELECTEDIMAGE | TVIF_IMAGE;
  172.    ItemStruct.item.hItem = 0;
  173.    ItemStruct.item.state = 0;
  174.    ItemStruct.item.stateMask = 0;
  175.    ItemStruct.item.pszText = (LPSTR) pRes->GetDisplayName();
  176.    ItemStruct.item.cchTextMax =  (int) strlen( ItemStruct.item.pszText );
  177.    ItemStruct.item.iImage = 2;
  178.    ItemStruct.item.iSelectedImage = 3;
  179.    ItemStruct.item.cChildren = 0;
  180.    ItemStruct.item.lParam = (long) pRes;
  181.    hLastItem = m_pTree->InsertItem( &ItemStruct );
  182.    return hLastItem;
  183. }
  184.  
  185. //int InsertNetResources( LPNETRESOURCE lpNetResource, CTreeCtrl *pTreeCtrl, HTREEITEM hParent, int *pnCount )
  186. //{
  187. //
  188. //   DWORD Erc;
  189. //   NETRESOURCE   *pNetRes;
  190. //   HANDLE hEnum;
  191. //
  192. //   if( !pTreeCtrl ) return -1;
  193. //   if( pnCount ) *pnCount = 0;
  194. //   Erc = WNetOpenEnum(
  195. //                  RESOURCE_GLOBALNET,//DWORD dwScope,   // scope of enumeration
  196. //                  RESOURCETYPE_ANY,//DWORD dwType,   // resource types to list
  197. //                  0,//DWORD dwUsage,   // resource usage to list
  198. //                  lpNetResource,//LPNETRESOURCE lpNetResource,   // pointer to resource structure
  199. //                  &hEnum//LPHANDLE lphEnum    // pointer to enumeration handle buffer
  200. //                  );
  201. //   if( Erc )
  202. //   {
  203. //      ShowError( Erc );
  204. //      return Erc;
  205. //   }
  206. //
  207. //
  208. //   DWORD dwBufferSize = 1024;
  209. //   pNetRes = (NETRESOURCE *)malloc( dwBufferSize );
  210. //
  211. //   while( TRUE )
  212. //   {
  213. //      DWORD dwCount = 0xFFFFFFFF;
  214. //      Erc = WNetEnumResource(
  215. //                        hEnum,//HANDLE hEnum,   // handle to enumeration
  216. //                        &dwCount,//LPDWORD lpcCount,   // pointer to entries to list
  217. //                        pNetRes,//LPVOID lpBuffer,   // pointer to buffer for results
  218. //                        &dwBufferSize//LPDWORD lpBufferSize    // pointer to buffer size variable
  219. //                        );
  220. //      if( Erc == ERROR_NO_MORE_ITEMS ) return 0;
  221. //      if( Erc )
  222. //      {
  223. //         free( pNetRes );
  224. //         pNetRes = (NETRESOURCE *)malloc( dwBufferSize );
  225. //         Erc = WNetEnumResource(
  226. //                           hEnum,//HANDLE hEnum,   // handle to enumeration
  227. //                           &dwCount,//LPDWORD lpcCount,   // pointer to entries to list
  228. //                           pNetRes,//LPVOID lpBuffer,   // pointer to buffer for results
  229. //                           &dwBufferSize//LPDWORD lpBufferSize    // pointer to buffer size variable
  230. //                           );
  231. //      }
  232. //      if( Erc ){ ShowError( Erc ); return Erc; }
  233. //
  234. //      TV_INSERTSTRUCT ItemStruct;
  235. //      HTREEITEM hLastItem = TVI_FIRST;
  236. //      DWORD i;
  237. //
  238. //      if( pnCount ) *pnCount += dwCount;
  239. //      for( i=0; i<dwCount; i++ )
  240. //      {
  241. //         memset( &ItemStruct, 0, sizeof(ItemStruct) );
  242. //         ItemStruct.hParent = hParent;
  243. //         ItemStruct.hInsertAfter = hLastItem;
  244. //         ItemStruct.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_SELECTEDIMAGE | TVIF_IMAGE;
  245. //         ItemStruct.item.hItem = 0;
  246. //         ItemStruct.item.state = 0;
  247. //         ItemStruct.item.stateMask = 0;
  248. //         ItemStruct.item.pszText = pNetRes[i].lpRemoteName;
  249. //         ItemStruct.item.cchTextMax = strlen( ItemStruct.item.pszText );
  250. //         ItemStruct.item.iImage = 2;
  251. //         ItemStruct.item.iSelectedImage = 3;
  252. //         ItemStruct.item.cChildren = 0;
  253. //         ItemStruct.item.lParam = (long) (new CNetResource( &pNetRes[i] ));
  254. //
  255. //         hLastItem = pTreeCtrl->InsertItem( &ItemStruct );
  256. //      }
  257. //   }//end while()
  258. //
  259. //   WNetCloseEnum( hEnum );
  260. //   free( pNetRes );
  261. //   return Erc;
  262. //}
  263.  
  264. /////////////////////////////////////////////////////////////////////////////
  265.  
  266. static void print_top(MYSQL_RES *result)
  267. {
  268.    uint length;
  269.    MYSQL_FIELD *field;
  270.    mysql_field_seek(result,0);
  271.    while ((field = mysql_fetch_field(result)))
  272.    {
  273.       if ((length= (uint) strlen(field->name)) > field->max_length)
  274.          field->max_length=length;
  275.       else
  276.          length=field->max_length;
  277.    }
  278. }
  279.  
  280. /////////////////////////////////////////////////////////////////////////////
  281.  
  282. static void print_header(MYSQL_RES *result,CStringArray& rg)
  283. {
  284.    MYSQL_FIELD *field;
  285.    print_top(result);
  286.    mysql_field_seek(result,0);
  287.    while ((field = mysql_fetch_field(result)))
  288.    {
  289. //    printf(" %-*s|",field->max_length+1,field->name);
  290.       rg.Add(field->name);
  291.    }
  292.    print_top(result);
  293. }
  294.  
  295.  
  296. /////////////////////////////////////////////////////////////////////////////
  297.  
  298. static void print_row(MYSQL_RES *result,MYSQL_ROW row,CStringArray& rg)
  299. {
  300.    uint i,length;
  301.    MYSQL_FIELD *field;
  302.    mysql_field_seek(result,0);
  303.    for (i=0 ; i < mysql_num_fields(result); i++)
  304.    {
  305.       field = mysql_fetch_field(result);
  306.       length=field->max_length;
  307.       rg.Add(row[i] ? (char*) row[i] : "");
  308. //    printf(" %-*s|",length+1,row[i] ? (char*) row[i] : "");
  309.    }
  310. }
  311.  
  312. /////////////////////////////////////////////////////////////////////////////
  313.  
  314. void CMySqlManagerView::ProcessResultSet ( HTREEITEM hItem, LPVOID r, CResource* pResource )
  315. {
  316.  
  317.    MYSQL_RES* result = (MYSQL_RES *) r;
  318.    MYSQL_ROW row;
  319.  
  320.    switch (pResource->GetType())
  321.    {
  322.       case CResource::eProcesslist:
  323.          {
  324.             CResourceProcesslist* p = (CResourceProcesslist*) pResource;
  325.             CResourceProcesslistItem* pi = new CResourceProcesslistItem ();
  326.             CString strText;
  327.             print_header(result,p->m_rgFields);
  328.             for (int i = 0; i<p->m_rgFields.GetSize(); i++ )
  329.             {
  330.                strText += p->m_rgFields[i];
  331.                strText += " ";
  332.             }
  333.             pi->m_strName = strText;
  334.             AddResource ( hItem, pi );
  335.             for (int index=0;(row=mysql_fetch_row(result));index++)
  336.             {
  337.                pi = new CResourceProcesslistItem ();
  338.                print_row(result,row,pi->m_rgFields);
  339.                strText.Empty();
  340.                for (int i = 0; i<pi->m_rgFields.GetSize(); i++ )
  341.                {
  342.                   strText += pi->m_rgFields[i];
  343.                   strText += " ";
  344.                }
  345.                pi->m_strName = strText;
  346.                AddResource ( hItem, pi );
  347.             }
  348.             print_top(result);
  349.             break;
  350.          }
  351.       case CResource::eServer:
  352.          {
  353.             CResourceServer* p = (CResourceServer*) pResource;
  354.             CResourceDatabase* pi = new CResourceDatabase ();
  355.             CString strText;
  356.             /* print_header(result,p->m_rgFields); */
  357.             for (int i = 0; i<p->m_rgFields.GetSize(); i++ )
  358.             {
  359.                strText += p->m_rgFields[i];
  360.                strText += " ";
  361.             }
  362.             pi->m_strName = strText;
  363.             /* AddResource ( hItem, pi ); */
  364.             for (int index=0;(row=mysql_fetch_row(result));index++)
  365.             {
  366.                pi = new CResourceDatabase ();
  367.                print_row(result,row,pi->m_rgFields);
  368.                strText.Empty();
  369.                for (int i = 0; i<pi->m_rgFields.GetSize(); i++ )
  370.                {
  371.                   strText += pi->m_rgFields[i];
  372.                   strText += " ";
  373.                }
  374.                pi->m_strName = strText;
  375.                AddResource ( hItem, pi );
  376.             }
  377.             print_top(result);
  378.             break;
  379.          }
  380.       case CResource::eDatabase:
  381.          {
  382.             CResourceDatabase* p = (CResourceDatabase*) pResource;
  383.             CResourceTable* pi = new CResourceTable ();
  384.             CString strText;
  385.             /* print_header(result,p->m_rgFields); */
  386.             for (int i = 0; i<p->m_rgFields.GetSize(); i++ )
  387.             {
  388.                strText += p->m_rgFields[i];
  389.                strText += " ";
  390.             }
  391.             pi->m_strName = strText;
  392.             /* AddResource ( hItem, pi ); */
  393.             for (int index=0;(row=mysql_fetch_row(result));index++)
  394.             {
  395.                pi = new CResourceTable ();
  396.                print_row(result,row,pi->m_rgFields);
  397.                strText.Empty();
  398.                for (int i = 0; i<pi->m_rgFields.GetSize(); i++ )
  399.                {
  400.                   strText += pi->m_rgFields[i];
  401.                   strText += " ";
  402.                }
  403.                pi->m_strName = strText;
  404.                AddResource ( hItem, pi );
  405.             }
  406.             print_top(result);
  407.             break;
  408.          }
  409.       case CResource::eTable:
  410.          {
  411.             CResourceTable* p = (CResourceTable*) pResource;
  412.             CResourceField* pi = new CResourceField ();
  413.             CString strText;
  414.             /* print_header(result,p->m_rgFields); */
  415.             for (int i = 0; i<p->m_rgFields.GetSize(); i++ )
  416.             {
  417.                strText += p->m_rgFields[i];
  418.                strText += " ";
  419.             }
  420.             pi->m_strName = strText;
  421.             /* AddResource ( hItem, pi ); */
  422.             for (int index=0;(row=mysql_fetch_row(result));index++)
  423.             {
  424.                pi = new CResourceField ();
  425.                print_row(result,row,pi->m_rgFields);
  426.                strText.Empty();
  427.                for (int i = 0; i<pi->m_rgFields.GetSize(); i++ )
  428.                {
  429.                   strText += pi->m_rgFields[i];
  430.                   strText += " ";
  431.                }
  432.                pi->m_strName = strText;
  433.                AddResource ( hItem, pi );
  434.             }
  435.             print_top(result);
  436.             break;
  437.          }
  438.    }
  439.  
  440.  
  441. }
  442.  
  443. /////////////////////////////////////////////////////////////////////////////
  444.  
  445. CResource* CMySqlManagerView::GetSelectedResource(HTREEITEM* phItemRet)
  446. {
  447.    CResource* pResource = NULL;
  448.    HTREEITEM  hItem = m_pTree->GetSelectedItem();
  449.    if ( hItem )
  450.    {
  451.       TV_ITEM item;
  452.       memset( &item, 0, sizeof(TV_ITEM) );
  453.       item.hItem = hItem;
  454.       item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
  455.       m_pTree->GetItem( &item );
  456.       if ( item.lParam )
  457.       {
  458.          pResource = (CResource*) item.lParam;
  459.       }
  460.    }
  461.    if (phItemRet)
  462.    {
  463.       *phItemRet = hItem;
  464.    }
  465.    return pResource;
  466. }
  467.  
  468. /////////////////////////////////////////////////////////////////////////////
  469.  
  470. CResourceServer* CMySqlManagerView::GetServerResource(HTREEITEM hItem)
  471. {
  472.  
  473.    TV_ITEM item;
  474.  
  475.    memset( &item, 0, sizeof(TV_ITEM) );
  476.    item.hItem = hItem;
  477.    item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
  478.    m_pTree->GetItem( &item );
  479.    if ( !item.lParam ) return NULL;
  480.  
  481.    CResource* pResource = (CResource*) item.lParam;
  482.  
  483.    switch (pResource->GetType())
  484.    {
  485.       case CResource::eServer:
  486.          {
  487.             return (CResourceServer*) pResource;
  488.          }
  489.       case CResource::eDatabase:
  490.          {
  491.             HTREEITEM hParent = m_pTree->GetParentItem(hItem);
  492.             memset( &item, 0, sizeof(TV_ITEM) );
  493.             item.hItem = hParent;
  494.             item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
  495.             m_pTree->GetItem( &item );
  496.             if ( !item.lParam ) return NULL;
  497.             return (CResourceServer*) item.lParam;
  498.          }
  499.       case CResource::eTable:
  500.          {
  501.             HTREEITEM hParent = m_pTree->GetParentItem(m_pTree->GetParentItem(hItem));
  502.             memset( &item, 0, sizeof(TV_ITEM) );
  503.             item.hItem = hParent;
  504.             item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
  505.             m_pTree->GetItem( &item );
  506.             if ( !item.lParam ) return NULL;
  507.             return (CResourceServer*) item.lParam;
  508.          }
  509.    }
  510.  
  511.    return NULL;
  512.  
  513. }
  514. /////////////////////////////////////////////////////////////////////////////
  515.  
  516. void CMySqlManagerView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
  517. {
  518.    HTREEITEM hItem;
  519.    hItem = m_pTree->GetSelectedItem();
  520.    *pResult = 0;
  521.    if ( !hItem ) return;
  522.  
  523.    TV_ITEM item;
  524.    memset( &item, 0, sizeof(TV_ITEM) );
  525.    item.hItem = hItem;
  526.    item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
  527.    m_pTree->GetItem( &item );
  528.  
  529.    if ( ! item.lParam ) return;
  530.  
  531.    if ( item.cChildren ) return; //if has got children expand only
  532.  
  533.    CWaitCursor x;
  534.  
  535.    CResource* pResource = (CResource*) item.lParam;
  536.  
  537.    MYSQL mysql;
  538.    MYSQL_RES *result;
  539.  
  540.    switch (pResource->GetType())
  541.    {
  542.       case CResource::eProcesslist:
  543.          {
  544.             XStatus x ( "Connecting to server %s on host %s..."
  545.                         , (LPCTSTR) pResource->GetDisplayName()
  546.                         , (LPCTSTR) pResource->GetHostName()
  547.                       );
  548.             mysql_init(&mysql);
  549.             if (!mysql_real_connect(&mysql,pResource->GetHostName(), pResource->GetUserName(),pResource->GetPassword(),0,pResource->GetPortNumber(), NullS,0))
  550.             {
  551.                     PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
  552.                return;
  553.             }
  554.             if (!(result=mysql_list_processes(&mysql)))
  555.             {
  556.                return;
  557.             }
  558.             ProcessResultSet ( hItem, result, pResource );
  559.             mysql_free_result(result);
  560.             mysql_close(&mysql);
  561.             break;
  562.          }
  563.       case CResource::eServer:
  564.          {
  565.             MainFrame->StatusMsg ( "Connecting to server %s on host %s..."
  566.                                    , (LPCTSTR) pResource->GetDisplayName()
  567.                                    , (LPCTSTR) pResource->GetHostName()
  568.                                  );
  569.             mysql_init(&mysql);
  570.             if (!mysql_real_connect(&mysql,pResource->GetHostName(), pResource->GetUserName(),pResource->GetPassword(),0,pResource->GetPortNumber(), NullS,0))
  571.             {
  572.                     PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
  573.                MainFrame->StatusMsg ( "Error: Connecting to server %s... (%s)"
  574.                                       , (LPCTSTR) pResource->GetDisplayName()
  575.                                       , mysql_error(&mysql)
  576.                                     );
  577.                return;
  578.             }
  579.             if (!(result=mysql_list_dbs(&mysql,0)))
  580.             {
  581.             }
  582.             ProcessResultSet ( hItem, result, pResource );
  583.             mysql_free_result(result);
  584.             mysql_close(&mysql);
  585.             MainFrame->StatusMsg ( " " );
  586.             break;
  587.          }
  588.       case CResource::eDatabase:
  589.          {
  590.             CResourceServer* pServer = GetServerResource(hItem);
  591.             if (!pServer) return;
  592.             MainFrame->StatusMsg ( "Connecting to server %s on host %s..."
  593.                                    , (LPCTSTR) pServer->GetDisplayName()
  594.                                    , (LPCTSTR) pServer->GetHostName()
  595.                                  );
  596.             mysql_init(&mysql);
  597.             if (!mysql_real_connect(&mysql,pServer->GetHostName(), pServer->GetUserName(),pServer->GetPassword(),0,pServer->GetPortNumber(), NullS,0))
  598.             {
  599.                     PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
  600.                MainFrame->StatusMsg ( "Error: Connecting to server %s... (%s)"
  601.                                       , (LPCTSTR) pServer->GetDisplayName()
  602.                                       , mysql_error(&mysql)
  603.                                     );
  604.                return;
  605.             }
  606.             CResourceDatabase* pRes = (CResourceDatabase*) pResource;
  607.             CString strDB = pResource->GetDisplayName();
  608.             strDB.TrimRight();
  609.             if (mysql_select_db(&mysql,strDB))
  610.             {
  611.                MainFrame->StatusMsg ( "Error: Selecting database %s... (%s)"
  612.                                       , (LPCTSTR) strDB
  613.                                       , mysql_error(&mysql)
  614.                                     );
  615.                return;
  616.             }
  617.             if (!(result=mysql_list_tables(&mysql,0)))
  618.             {
  619.             }
  620.             ProcessResultSet ( hItem, result, pRes );
  621.             mysql_free_result(result);
  622.             mysql_close(&mysql);
  623.             MainFrame->StatusMsg ( " " );
  624.             break;
  625.          }
  626.       case CResource::eTable:
  627.          {
  628.             CResourceServer* pServer = GetServerResource(hItem);
  629.             if (!pServer) return;
  630.             MainFrame->StatusMsg ( "Connecting to server %s on host %s..."
  631.                                    , (LPCTSTR) pServer->GetDisplayName()
  632.                                    , (LPCTSTR) pServer->GetHostName()
  633.                                  );
  634.             mysql_init(&mysql);
  635.             if (!mysql_real_connect(&mysql,pServer->GetHostName(), pServer->GetUserName(),pServer->GetPassword(),0,pServer->GetPortNumber(), NullS,0))
  636.             {
  637.                     PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
  638.                MainFrame->StatusMsg ( "Error: Connecting to server %s... (%s)"
  639.                                       , (LPCTSTR) pServer->GetDisplayName()
  640.                                       , mysql_error(&mysql)
  641.                                     );
  642.                return;
  643.             }
  644.             HTREEITEM hParent = m_pTree->GetParentItem(hItem);
  645.             memset( &item, 0, sizeof(TV_ITEM) );
  646.             item.hItem = hParent;
  647.             item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
  648.             m_pTree->GetItem( &item );
  649.             if ( item.lParam )
  650.             {
  651.                CResourceDatabase* pResDatabase = (CResourceDatabase*) item.lParam;
  652.                CResourceTable* pRes = (CResourceTable*) pResource;
  653.                CString strDB = pResDatabase->GetDisplayName();
  654.                CString strTable = pResource->GetDisplayName();
  655.                strDB.TrimRight();
  656.                strTable.TrimRight();
  657.                if (mysql_select_db(&mysql,strDB))
  658.                {
  659.                   return;
  660.                }
  661.                CString str; str.Format("show fields from %s",(LPCTSTR)strTable);
  662.                if ( mysql_query(&mysql,str)==0 )
  663.                {
  664.                   MYSQL_RES *result;
  665.                   if ((result=mysql_store_result(&mysql)))
  666.                   {
  667.                      ProcessResultSet ( hItem, result, pRes );
  668.                      mysql_free_result(result);
  669.                   }
  670.                }
  671.             }
  672.             mysql_close(&mysql);
  673.             break;
  674.          }
  675.    }
  676.  
  677. //  InsertNetResources( (LPNETRESOURCE)pTvItem->lParam,
  678. //                 &m_TreeCtrl,
  679. //                 hItem,
  680. //                 &pTvItem->cChildren );
  681. //  pTvItem->mask = TVIF_CHILDREN;
  682. //  m_TreeCtrl.SetItem( pTvItem );
  683.  
  684. }
  685.  
  686. /////////////////////////////////////////////////////////////////////////////
  687.  
  688. void CMySqlManagerView::OnRefresh()
  689. {
  690.    HTREEITEM hItem = NULL;
  691.    CResource* pResource = GetSelectedResource(&hItem);
  692.    if (pResource&&hItem)
  693.    {
  694.       switch (pResource->GetType())
  695.       {
  696.          case CResource::eTable:
  697.             {
  698.  
  699.                TV_ITEM item;
  700.                MYSQL mysql;
  701. //             MYSQL_RES *result;
  702.  
  703.                HTREEITEM hParent = m_pTree->GetParentItem(hItem);
  704.  
  705.                HTREEITEM hChild = m_pTree->GetChildItem(hItem);
  706.                while (hChild)
  707.                {
  708.                   HTREEITEM h = m_pTree->GetNextSiblingItem(hChild);
  709.                   BOOL b = m_pTree->DeleteItem(hChild);
  710.                   hChild = h;
  711.                }
  712.                mysql_init(&mysql);
  713.                if (!mysql_real_connect(&mysql,pResource->GetHostName(), pResource->GetUserName(),pResource->GetPassword(),0,pResource->GetPortNumber(), NullS,0))
  714.                {
  715.                   return;
  716.                }
  717.                memset( &item, 0, sizeof(TV_ITEM) );
  718.                item.hItem = hParent;
  719.                item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
  720.                m_pTree->GetItem( &item );
  721.                if ( item.lParam )
  722.                {
  723.                   CResourceDatabase* pResDatabase = (CResourceDatabase*) item.lParam;
  724.                   CResourceTable* pRes = (CResourceTable*) pResource;
  725.                   CString strDB = pResDatabase->GetDisplayName();
  726.                   CString strTable = pResource->GetDisplayName();
  727.                   strDB.TrimRight();
  728.                   strTable.TrimRight();
  729.                   if (mysql_select_db(&mysql,strDB))
  730.                   {
  731.                      return;
  732.                   }
  733.                   CString str; str.Format("show fields from %s",(LPCTSTR)strTable);
  734.                   if ( mysql_query(&mysql,str)==0 )
  735.                   {
  736.                      MYSQL_RES *result;
  737.                      if ((result=mysql_store_result(&mysql)))
  738.                      {
  739.                         ProcessResultSet ( hItem, result, pRes );
  740.                         mysql_free_result(result);
  741.                      }
  742.                   }
  743.                }
  744.                mysql_close(&mysql);
  745.                break;
  746.             }
  747.       }
  748.    }
  749. }
  750.  
  751. /////////////////////////////////////////////////////////////////////////////
  752.  
  753. void CMySqlManagerView::OnRegisterServer()
  754. {
  755.    CRegisterServer dlg;
  756.    if (dlg.DoModal()!=IDOK) return;
  757.    AddResource (
  758.                TVI_ROOT,
  759.                new CResourceServer ( dlg.m_strServer, dlg.m_strHost, dlg.m_strUser, dlg.m_strPassword, dlg.m_strPort )
  760.                );
  761. }
  762.  
  763. /////////////////////////////////////////////////////////////////////////////
  764.  
  765. void CMySqlManagerView::OnServerProperties()
  766. {
  767.    HTREEITEM hItem;
  768.    CResource* pRes = GetSelectedResource(&hItem);
  769.    if (!pRes) return;
  770.    if (pRes->GetType()!=CResource::eServer) return;
  771.    CResourceServer* pResource = (CResourceServer*)pRes;
  772.    CRegisterServer dlg;
  773.    dlg.m_strHost       = pResource->GetHostName();
  774.    dlg.m_strUser       = pResource->GetUserName();
  775.    dlg.m_strPassword   = pResource->GetPassword();
  776.    dlg.m_strPort       = pResource->GetPortName();
  777.    if (dlg.DoModal()!=IDOK) return;
  778.    pResource->m_strHost     = dlg.m_strHost    ;
  779.    pResource->m_strUser     = dlg.m_strUser    ;
  780.    pResource->m_strPassword = dlg.m_strPassword;
  781.    pResource->m_strPort     = dlg.m_strPort    ;
  782.    TV_ITEM item;
  783.    memset( &item, 0, sizeof(TV_ITEM) );
  784.    item.hItem = hItem;
  785.    item.mask = TVIF_TEXT | TVIF_HANDLE | TVIF_CHILDREN | TVIF_PARAM ;
  786.    m_pTree->GetItem( &item );
  787. }
  788.  
  789.  
  790. /////////////////////////////////////////////////////////////////////////////
  791.  
  792. void CMySqlManagerView::OnSqlToolQuery()
  793. {
  794.  
  795.    HTREEITEM hItem;
  796.  
  797.    CResource* pResource = GetSelectedResource(&hItem);
  798.  
  799.    if (!pResource) return;
  800.  
  801.    CResourceServer* pServer = GetServerResource(hItem);
  802.    if (!pServer) return; /* Avoid bug when selecting field */
  803.  
  804.    m_pTool = new CToolSql ( AfxGetMainWnd(), pServer, pResource );
  805.  
  806.    if ( ! m_pTool->Create(IDD_TOOL_SQL,this) )
  807.    {
  808.       delete m_pTool;
  809.       m_pTool = 0;
  810.         PostMessage(WM_COMMAND,IDM_TOOLS_SERVER_PROPERTIES);
  811.    }
  812.    else
  813.    {
  814.       m_pTool->ShowWindow(SW_SHOW);
  815.    }
  816.  
  817. }
  818.  
  819.  
  820. /////////////////////////////////////////////////////////////////////////////
  821.  
  822. BOOL CMySqlManagerView::PreTranslateMessage(MSG* pMsg)
  823. {
  824.    if (m_pTool && m_pTool->PreTranslateMessage(pMsg))
  825.       return TRUE;
  826.    return CTreeView::PreTranslateMessage(pMsg);
  827. }
  828.  
  829. void CMySqlManagerView::OnRclick(NMHDR* pNMHDR, LRESULT* pResult)
  830. {
  831.  
  832.    POINT pt;
  833.  
  834.    GetCursorPos ( & pt );
  835.  
  836.    CMenu menu;
  837.  
  838.    menu.CreatePopupMenu ();
  839.  
  840.    menu.AppendMenu ( MF_ENABLED , IDM_SQL_TOOL_QUERY,          "SQL Query" );
  841.    menu.AppendMenu ( MF_ENABLED , IDM_REFRESH,                 "Refresh active item(s)" );
  842.    menu.AppendMenu ( MF_ENABLED , IDM_TOOLS_REGISTER_SERVER,   "Register server" );
  843.    menu.AppendMenu ( MF_ENABLED , IDM_TOOLS_SERVER_PROPERTIES, "Properties" );
  844.  
  845.    menu.TrackPopupMenu ( TPM_LEFTALIGN | TPM_RIGHTBUTTON , pt.x, pt.y, CWnd::GetParent(), NULL );
  846.  
  847.    *pResult = 0;
  848.  
  849. }
  850.