Results 1 to 10 of 25

Thread: Asynchronous mysql queries

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Assadministrator IzNoGoD's Avatar
    Join Date
    Aug 2012
    Posts
    1,718
    Thanks
    17
    Thanked 1,068 Times in 674 Posts
    Ok, it's been added and pushed and updated and....

    here is a helper function for you:
    Code:
    init_async_mysql()
    {
    	host = getcvar("mysql_host");
    	user = getcvar("mysql_user");
    	port = getcvarint("mysql_port");
    	pass = getcvar("mysql_password");
    	db = getcvar("mysql_database");
    	mysql_async_initializer(host, user, pass, db, port, 4);
    	level.mysql_async = [];
    	while(true)
    	{
    		list = mysql_async_getdone_list();
    		for(i = 0; i < list.size; i++)
    		{
    			//iprintln("Query done");
    			result = mysql_async_getresult_and_free(list[i]);
    			if(!isdefined(result))
    				continue;
    			if(result == 0)
    			{
    				result = undefined;
    			}
    			f = level.mysql_async["" + list[i]];
    			if(isdefined(f))
    			{
    				if(isdefined(f.function))
    				{
    					thread [[f.function]](result, f.args);
    				}
    				else if(isdefined(result))
    				{
    					mysql_free_result(result);
    				}
    				f = undefined;
    			}
    			level.mysql_async["" + list[i]] = undefined;
    		}
    		wait .05;
    	}
    }
    
    add_async_query_nosave(q, function, args)
    {
    	if(getcvarint("show_mysql") == 1)
    		printf("mysql_query async nosave:" + q + "\n");
    	id = mysql_async_create_query_nosave(q);
    	f = spawnstruct();
    	f.function = function;
    	f.args = args;
    	level.mysql_async["" + id] = f;
    }
    
    add_async_query(q, function, args)
    {
    	if(getcvarint("show_mysql") == 1)
    		printf("mysql_query async:" + q + "\n");
    	id = mysql_async_create_query(q);
    	f = spawnstruct();
    	f.function = function;
    	f.args = args;
    	level.mysql_async["" + id] = f;
    }
    Usage is like this then:

    Code:
    some_function()
    {
    	args = [];
    	args[0] = self;
    	add_async_query("SELECT * FROM table ORDER BY foo LIMIT 1000", ::bar, args);
    }
    
    bar(result, args)
    {
    	player = args[0];
    	if(!isdefined(player))
    	{
    		if(isdefined(result))
    			mysql_free_result(result);
    		return;
    	}
    	if(isdefined(result))
    	{
    		rowcount = mysql_num_rows(result);
    		for(i = 0; i < rowcount; i++)
    			player iprintln(mysql_fetch_row(result)[0]);
    		mysql_free_result(result);
    	}
    }
    Good luck with it
    "Does not work" is an error report for a bug between keyboard and chair.

    All hail Artie Effem

  2. The Following 2 Users Say Thank You to IzNoGoD For This Useful Post:

    iBuddie (29th January 2016),RobsoN (9th April 2014)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •