Raised This Month: $ Target: $400
 0% 

Threaded query


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sparkey
Senior Member
Join Date: May 2010
Old 10-14-2010 , 07:13   Threaded query
Reply With Quote #1

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.
sparkey is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 10-14-2010 , 09:04   Re: Threaded query
Reply With Quote #2

'OK! Fetched the address'
is this message being printed? Any debugs?
__________________
xPaw is offline
sparkey
Senior Member
Join Date: May 2010
Old 10-14-2010 , 09:22   Re: Threaded query
Reply With Quote #3

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
sparkey is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 10-14-2010 , 10:06   Re: Threaded query
Reply With Quote #4

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
__________________
Impossible is Nothing
Sylwester is offline
sparkey
Senior Member
Join Date: May 2010
Old 10-14-2010 , 10:27   Re: Threaded query
Reply With Quote #5

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
sparkey is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 10-14-2010 , 12:48   Re: Threaded query
Reply With Quote #6

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 ...

__________________
Impossible is Nothing
Sylwester is offline
sparkey
Senior Member
Join Date: May 2010
Old 10-14-2010 , 12:55   Re: Threaded query
Reply With Quote #7

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!
sparkey is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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