AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Threaded query (https://forums.alliedmods.net/showthread.php?t=140578)

sparkey 10-14-2010 07:13

Threaded query
 
Hey,

I have about 4000 rows of code where i have been using the old fashion sqlx way. I have now been experiencing alot of lag on the servers due to our population and i need to migrate into threaded queries.

I have been playing around and read here on the forum but cant for some reason make it work. I have a global variable outside the function which i tries to set in the threaded sql function but i cant get it to work.

No errors when compiling it just does not store the data in the specified variable.

PHP Code:

    formatex(mysqlIp127"SELECT id,port FROM %s WHERE ip = '%s' LIMIT 1"MYSQL_SERVER_TABLEip);
    
SQL_ThreadQuery(g_SqlTuple"QueryStartup"mysqlIpip21);

 
public 
QueryStartup(FailState,Handle:query,Error[],Errcode,ip[],DataSize)
{
    if (
FailState == TQUERY_CONNECT_FAILED)
    {
        
log_amx("[%s]%s Error connecting to SQL: %s (%s)"log_time(), SERVER_PREFIXErrorErrcode);
        return;
    }
    if (
FailState == TQUERY_QUERY_FAILED)
    {
        
log_amx("[%s]%s Error executing Query: %s (%s)"log_time(), SERVER_PREFIXErrorErrcode);
        return;
    }
    if(!
SQL_Execute(query))
    {
        
log_amx("[%s]%s Error executing Query: %s (%s)"log_time(), SERVER_PREFIXErrorErrcode);
        return;
    }
    if (
SQL_NumResults(query))
    {
        
log_amx("[%s]%s Error Could not find a server with the address %s."log_time(), SERVER_PREFIXip);
    }
    
serverid SQL_ReadResult(query0);
    
ghost_port SQL_ReadResult(query1);
    
log_amx("[%s]%s OK! Fetched the address %s."log_time(), SERVER_PREFIXip);


Any help is appriciated.

xPaw 10-14-2010 09:04

Re: Threaded query
 
'OK! Fetched the address'
is this message being printed? Any debugs?

sparkey 10-14-2010 09:22

Re: Threaded query
 
Thanks for your answer.

It only reports errors for later functions that dont get the serverid and ghost_port.

It does not print OK and it does not print Error so it seems that it does not even run the QueryStartup function :(

Sylwester 10-14-2010 10:06

Re: Threaded query
 
Using SQL_Execute with threaded queries is wrong.

QueryStartup function is not a startup for query.
You start query when you call SQL_ThtreadQuery.
When QueryStartup function is called, it means that you have already received reply from sql database.

Check this complete example (it's using sqlite, but it's really easy to convert it to mysql):
http://forums.alliedmods.net/showthr...66#post1204466

sparkey 10-14-2010 10:27

Re: Threaded query
 
fantastic, works like a charm.

Let me just ask so i got this straight.

If i for example executes the threadquery it runs on its own thread the the code itself continues to run?

If i for example does this and tries to print the variable that i sets in the QueryStartup function directly after the SQL_ThreadQuery it wont print? So i have to do all the comparing in the SQL_ThreadQuery function?

PHP Code:

    formatex(mysqlIp127"SELECT id,port FROM %s WHERE ip = '%s' LIMIT 1"MYSQL_SERVER_TABLEip);
    
SQL_ThreadQuery(g_SqlTuple"QueryStartup"mysqlIpip21);
    
server_print("ghost_port: %d.",ghost_port); 

PHP Code:

public QueryStartup(FailState,Handle:query,Error[],Errcode,ip[],DataSize)
{
    if (
FailState == TQUERY_CONNECT_FAILED)
    {
        
server_print("[%s]%s Error connecting to SQL: %s (%s)"log_time(), SERVER_PREFIXErrorErrcode);
        return;
    }
    if (
FailState == TQUERY_QUERY_FAILED)
    {
        
server_print("[%s]%s Error executing Query: %s (%s)"log_time(), SERVER_PREFIXErrorErrcode);
        return;
    }
    if (!
SQL_MoreResults(query))
        return;
 
    
serverid SQL_ReadResult(query0);
    
ghost_port SQL_ReadResult(query1);
    
server_print("[%s]%s OK! Fetched the address %s."log_time(), SERVER_PREFIXip);
 
    
//// HERE I HAVE TO DO THE COMPARING FOR EXAMPLE IF ghost_port != BLA BLA THEN KICK? ////
 


Hope you can see what i mean :)

Sylwester 10-14-2010 12:48

Re: Threaded query
 
You are right. The query runs in its own thread, and it won't print variable directly after SQL_ThreadQuery. You need to do the comparison and print it in QueryStartup function.

Also you don't need to check twice for errors:
PHP Code:


public QueryStartup(FailState,Handle:query,Error[],Errcode,ip[],DataSize)
{
    if (
FailState == TQUERY_CONNECT_FAILED)
    {
        
server_print("[%s]%s Error connecting to SQL: %s (%s)"log_time(), SERVER_PREFIXErrorErrcode);
        return;
    }
    if (
FailState == TQUERY_QUERY_FAILED)
    {
        
server_print("[%s]%s Error executing Query: %s (%s)"log_time(), SERVER_PREFIXErrorErrcode);
        return;
    }
    
//CODE ...


-->
PHP Code:

public QueryStartup(FailState,Handle:query,Error[],Errcode,ip[],DataSize)
    if(
FailState){
        
log_amx("SQL Error: %s (%d)"ErrorErrcode);
        return;
    }
    
//CODE ...



sparkey 10-14-2010 12:55

Re: Threaded query
 
Alright i see :)

Thanks alot m8! Got alot of rewrite to do but eventually it will be great i hope. Thanks bigtime for your time and your help!


All times are GMT -4. The time now is 10:22.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.