mysqlmanagerview.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:27k
源码类别:

MySQL数据库

开发平台:

Visual C++

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