wyBool 
ConnectionCommunity::OnNewSameConnection(HWND hwndactive, MDIWindow *pcquerywnd, ConnectionInfo &conninfo)
{
    MYSQL       *mysqlnew, *mysqlold, *mysqltemp;
    Tunnel	    *oldtunnel, *newtunnel;	
    wyString    msg;

	if(hwndactive)
	{
		oldtunnel   = pcquerywnd->m_tunnel;
        newtunnel   = CreateTunnel(wyFalse);
        mysqlnew	= newtunnel->mysql_init((MYSQL*)0);
        mysqlold	= pcquerywnd->m_mysql;

		if(!mysqlnew)
		{
			yog_message(pGlobals->m_pcmainwin->m_hwndmain, _(L"Could not initialize a new window."), pGlobals->m_appname.GetAsWideChar(), MB_ICONERROR | MB_OK | MB_HELP);
			delete newtunnel;
			return wyFalse;
		}
		
		SetCursor(LoadCursor(NULL, IDC_WAIT	));
		ShowCursor(1);

        InitConInfo(pcquerywnd->m_conninfo, conninfo);

		//Get SQL_MODE info //following function remarked for issue #1654
		//GetAdvancedTabDetailsFromFile(&conninfo, (wyChar *)pcquerywnd->m_currentconn.GetString(), NULL);
	
		//post 8.01
		/*InvalidateRect(pcquerywnd->GetActiveTabEditor()->m_peditorbase->m_hwnd, NULL, FALSE);
		UpdateWindow(pcquerywnd->GetActiveTabEditor()->m_peditorbase->m_hwnd);*/
		
        if(conninfo.m_initcommand.GetLength())
            ExecuteInitCommands(mysqlnew, newtunnel, conninfo.m_initcommand);

		SetMySQLOptions(&pcquerywnd->m_conninfo, newtunnel, &mysqlnew);
		
        mysqltemp = newtunnel->mysql_real_connect(mysqlnew, mysqlold->host, mysqlold->user, 
                    mysqlold->passwd, NULL, mysqlold->port, NULL, 
                    mysqlold->client_flag | CLIENT_MULTI_RESULTS, NULL);
		
		if(!mysqltemp)
        {
			ShowMySQLError(pGlobals->m_pcmainwin->m_hwndmain, oldtunnel, &mysqlnew, NULL, wyTrue);
			newtunnel->mysql_close(mysqlnew);
			delete	newtunnel;
			return wyFalse;
		}

		/* change the conninfo things */
		conninfo.m_tunnel		= newtunnel;
		conninfo.m_mysql		= mysqlnew;
		conninfo.m_hprocess	    = INVALID_HANDLE_VALUE;
	}
    return wyTrue;
}
/*Importing process
Also if select a db in OB and use 'Restore from SQL dump' warning pops up
*/
void 
ImportBatch::ImportDump()
{
	MDIWindow *wnd;
	HTREEITEM	hitem = NULL;
	wyString	msg, dbname;
	wyInt32		ret = 0;

	if((SendMessage(m_hwndedit, WM_GETTEXTLENGTH, 0, 0)) > 0)
    {
		VERIFY(wnd = GetActiveWin());

		//Check whether 'Restore from SQL dump' used after selecting a db in OB
		if(m_importing == wyFalse && wnd && wnd->m_pcqueryobject && wnd->m_pcqueryobject->GetSelectionImage() == NDATABASE)
		{
			hitem = wnd->m_pcqueryobject->GetDatabaseNode();
			wnd->m_pcqueryobject->GetDatabaseName(hitem);

			dbname.SetAs(wnd->m_pcqueryobject->m_seldatabase);
		    
			//warning shows when restore from sql dump does database(to avoid confusion of USE db in dump)
			if(dbname.GetLength())
			{
				msg.Sprintf(_("The current database context is `%s`. SQL statements in the file will be executed here, unless one or more USE statement(s) specifying another database context is in the file. Do you want to continue?"),
								dbname.GetString()); 

				ret = MessageBox(wnd->m_hwnd, msg.GetAsWideChar(),pGlobals->m_appname.GetAsWideChar(), MB_ICONQUESTION | MB_YESNO);
				if(ret != IDYES)
					return;
			}
		}
		
		if(m_importing) 
        {
			/* behave as if the stop has been pushed */
//			DEBUG_LOG("stopping query");
			m_stopimport = wyTrue;
		} 
        else 
        {
//			DEBUG_LOG("executing import");
			VERIFY(wyFalse == m_importing);
			SetCursor(LoadCursor(NULL, IDC_WAIT));
			ExecuteBatch();
			SetCursor(LoadCursor(NULL, IDC_ARROW));
		}
	}
	else
		yog_message(m_hwnd, _(L"Please enter a filename"), pGlobals->m_appname.GetAsWideChar(), MB_OK | MB_ICONINFORMATION);

	return;
}
Esempio n. 3
0
wyBool
BlobMgmt::SaveToFile()
{
	OPENFILENAME	open;
	wyWChar         filename[MAX_PATH + 1] = {0};
	HANDLE			hfile = NULL;
	DWORD			dwbytesread;

	if(!m_piub->m_data)
    {
		yog_message(m_hwnddlg, _(L"Cannot write NULL value!"), pGlobals->m_appname.GetAsWideChar(), MB_OK | MB_ICONINFORMATION);
		return wyFalse;
	}

	memset(&open, 0, sizeof(open));

	open.lStructSize       = OPENFILENAME_SIZE_VERSION_400;
	open.hwndOwner         = m_hwnddlg;
	open.hInstance         = pGlobals->m_pcmainwin->GetHinstance();
	open.lpstrFilter       = L"All Files(*.*)\0*.*\0";
	open.lpstrCustomFilter =(LPWSTR)NULL;
	open.nFilterIndex      = 1L;
	open.lpstrFile         = filename;
	open.nMaxFile          = MAX_PATH;
	open.lpstrTitle        = L"Save As";
	open.Flags             = OFN_OVERWRITEPROMPT;
	
	if(! GetSaveFileName(&open))
	   return wyFalse;

	// else successfull.
	hfile = CreateFile((LPCWSTR)filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,
						 NULL);	

	if(hfile == INVALID_HANDLE_VALUE)
	{
		DisplayErrorText(GetLastError(), _("Could not create file !"));
		return wyFalse;
	}

	VERIFY(WriteFile(hfile, m_piub->m_data, m_piub->m_datasize, &dwbytesread, NULL));
	CloseHandle(hfile);
	return wyTrue;
}
// Function to Create Favorites  folder // 
wyBool
CFavoriteMenu::CreateFavoriteFolder()
{
	wyWChar	path[MAX_PATH + 1] = {0};
    wyString fullpath;
		
	// get application data path //
	if(pGlobals->m_configdirpath.GetLength() || SUCCEEDED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, path)))
	{
		if(pGlobals->m_configdirpath.GetLength())
			fullpath.SetAs(pGlobals->m_configdirpath);

		else
		{
			fullpath.SetAs(path);
			fullpath.Add("\\SQLyog");
		}

		// Create SQLyog folder 
        if( ::CreateDirectory(fullpath.GetAsWideChar(), NULL) == 0)
        {
            if(GetLastError() != ERROR_ALREADY_EXISTS)
                return OnError(_("Error while creating favorites folder"));
        }
        fullpath.Add("\\Favorites");
		// Create Favorites folder
        if( ::CreateDirectory(fullpath.GetAsWideChar(), NULL) == 0)
        {
            if(GetLastError() != ERROR_ALREADY_EXISTS)
                return OnError(_("Error while creating favorites folder"));
        }
        return wyTrue;
	} 
    else
    {
		yog_message(NULL, _(L"Error while creating favorites folder"), pGlobals->m_appname.GetAsWideChar(), MB_OK);
        return wyFalse;
	}
}
/* Function just tests the connection and checks whether it is correct or not */
wyBool 
ConnectionCommunity::TestConnection(HWND hwnd, wyBool showmsg /*=wyTrue*/)
{
	ConnectionInfo	dbname; 
	MYSQL			*mysql;
	Tunnel			*tunnel;
    wyString	    query("select version()"), version;
	wyInt32         ret;
	MYSQL_RES	    *res;
	MYSQL_ROW	    row;
	wyString		myrowstr;

	InitConnectionDetails(&dbname);

    tunnel = CreateTunnel(wyFalse);
	dbname.m_tunnel = tunnel;

	// Call ConnectToMySQL function to initialize a new mySQL structure and connect to
	// mySQL server with the connection details. The function will return a valid mysql
	// pointer if connection successful and if not then it will return NULL.
	// If null then show the standard mySQL errors given by the mySQL API.
	//if database name is not correct, then throw error
	mysql = pGlobals->m_pcmainwin->m_connection->ConnectToMySQL(hwnd, &dbname);
	if(mysql == NULL)
    {
        if(tunnel)
            delete tunnel;
		return wyFalse;
	} 
	else if((dbname.m_db.GetLength() != 0) && (IsDatabaseValid(dbname.m_db, mysql, tunnel) == wyFalse))
	{
		query.Clear();
		goto cleanup;
	}

	/* now we send a test query "select version()", if everything is ok then we show a message
	   with the version number */
	ret = HandleMySQLRealQuery(tunnel, mysql, query.GetString(), query.GetLength(), false);

	if(ret)
		goto cleanup;

	res = tunnel->mysql_store_result(mysql);
	if(!res && mysql->affected_rows == -1)
		goto cleanup;

	row = tunnel->mysql_fetch_row(res);
	
	myrowstr.SetAs(row[0], IsMySQL41(dbname.m_tunnel, &mysql));
	version.Sprintf(_("Connection successful!\nMySQL version: %s"), myrowstr.GetString());

	if(showmsg)
		yog_message(hwnd, version.GetAsWideChar(), _(L"Connection Info"), MB_OK | MB_ICONINFORMATION);

	tunnel->mysql_free_result(res);
	tunnel->mysql_close(mysql);

	delete	tunnel;

	return wyTrue;

cleanup:
	if(query.GetLength())
		ShowMySQLError(hwnd, tunnel, &mysql, query.GetString());
	else
		ShowMySQLError(hwnd, tunnel, &mysql, NULL, wyTrue);
    delete tunnel;
	return wyFalse;
}
// Function deletes the conn detail section from the ini file.
// Basically it sets the name to zero.
wyInt32 
ConnectionCommunity::DeleteConnDetail(HWND hdlg)
{
	wyInt32     count, cursel, totcount, confirm;
	HWND		hwndcombo;
    wyWChar      directory[MAX_PATH + 1]={0}, *lpfileport = 0, connname[SIZE_512] = {0};
    wyString    conn, autodirectory, dirstr;
	wyUInt32    ret;
	
	confirm = yog_message(hdlg, _(L"Do you really want to delete the connection detail?"), pGlobals->m_appname.GetAsWideChar(), 
                            MB_YESNO | MB_HELP | MB_ICONQUESTION | MB_DEFBUTTON2);

	if(confirm != IDYES)
		return FALSE;

	// Get the complete path.
	ret = SearchFilePath(L"sqlyog", L".ini", MAX_PATH, directory, &lpfileport);
	if(ret == 0)
		return 0;

	VERIFY(hwndcombo = GetDlgItem(hdlg, IDC_DESC));

	totcount  = SendMessage(hwndcombo, CB_GETCOUNT, 0, 0);

	if(totcount == 0)
		return 0;

	VERIFY ((cursel = SendMessage(hwndcombo, CB_GETCURSEL, 0, 0))!= CB_ERR);
	count = SendMessage(hwndcombo, CB_GETITEMDATA, cursel, 0);
	conn.Sprintf("Connection %u", count);
	
	dirstr.SetAs(directory);
	//ret = WritePrivateProfileStringA(conn.GetString(), NULL, NULL, dirstr.GetString());
	ret = wyIni::IniDeleteSection(conn.GetString(), dirstr.GetString());
	
	SendMessage(hwndcombo, WM_GETTEXT, SIZE_512 - 1,(LPARAM)connname);

	SendMessage(hwndcombo, CB_DELETESTRING, cursel, 0);

	if(totcount == 1)
    {
		EnableWindow(GetDlgItem(hdlg, IDC_SAVE), FALSE);
		ChangeConnStates(hdlg, wyFalse);
		TabCtrl_SetCurSel(GetDlgItem(hdlg, IDC_CONNTAB), 0);
		OnConnDialogTabSelChange(hdlg, GetDlgItem(hdlg, IDC_CONNTAB));
		m_conndetaildirty = wyFalse;

		SetFocus(hdlg);

		return 1;
	} 
    else if(cursel ==(totcount - 1)) 
		SendMessage(hwndcombo, CB_SETCURSEL, cursel-1, 0);
	else if(cursel == 0)
		SendMessage(hwndcombo, CB_SETCURSEL, 0, 0);
	else
		SendMessage(hwndcombo, CB_SETCURSEL, cursel-1, 0);

	GetInitialDetails(hdlg);

	//populate color array for connection combo
	PopulateColorArray(hwndcombo, &dirstr);

    TabCtrl_SetCurSel(GetDlgItem(hdlg, IDC_CONNTAB), 0);
	m_conndetaildirty = wyFalse;
    //for showing the server tab and hiding all other tabs
    ShowServerOptions(hdlg);

	return 1;
}
Esempio n. 7
0
//this function is called whenever the table link is clicked from the DB level
void HandleClickTableURL(const wchar_t *url)
{
    MDIWindow   *wnd = NULL;
    HTREEITEM   hitem = NULL;
    wyWChar     buff[MAX_PATH] = {0};
    TVITEM      tvitem;

    wnd = GetActiveWin();

    if(!wnd || !wnd->m_pcqueryobject)
    {
		return;
    }

    //switch the current selection image
    switch(wnd->m_pcqueryobject->GetSelectionImage())
    {
        case NDATABASE:
            //get the selection
            hitem = TreeView_GetSelection(wnd->m_pcqueryobject->m_hwnd);

            if(hitem)
            {
                //expand and get the first child
                TreeView_Expand(wnd->m_pcqueryobject->m_hwnd, hitem, TVE_EXPAND);
                hitem = TreeView_GetChild(wnd->m_pcqueryobject->m_hwnd, hitem);
            }
            break;

        case NTABLES:
            //get the selection
            hitem = TreeView_GetSelection(wnd->m_pcqueryobject->m_hwnd);
    }

    if(!hitem)
    {
        return;
    }

    //expand the Tables folder
    TreeView_Expand(wnd->m_pcqueryobject->m_hwnd, hitem, TVE_EXPAND);
    
    //traverse through the tables
    for(hitem = TreeView_GetChild(wnd->m_pcqueryobject->m_hwnd, hitem); hitem; 
        hitem = TreeView_GetNextSibling(wnd->m_pcqueryobject->m_hwnd, hitem))
    {
        //get the item
        tvitem.hItem = hitem;
        tvitem.mask = TVIF_TEXT;
        buff[0] = 0;
        tvitem.pszText = buff;
        tvitem.cchTextMax = MAX_PATH - 1;
        TreeView_GetItem(wnd->m_pcqueryobject->m_hwnd, &tvitem);

        //compare the item with the url
        if(!wcscmp(url, tvitem.pszText))
        {
            //set the selection; this will generate  TVN_SELCHANGING and TVN_SELCHANGED messages
            TreeView_SelectItem(wnd->m_pcqueryobject->m_hwnd, hitem);
            break;
        }
    }

    if(!hitem)
    {
        MDIWindow *wnd = GetActiveWin();
        yog_message(wnd->m_hwnd, PROMPT_MESSAGE, pGlobals->m_appname.GetAsWideChar(), MB_OK | MB_ICONINFORMATION);
    }

    return;
}