AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Change normal Query into Threaded Query (https://forums.alliedmods.net/showthread.php?t=288334)

SkinnyBruv 09-27-2016 05:10

Change normal Query into Threaded Query
 
Howdy guys,

How would i go about in changing this query: to a Threaded Query!

Code:

public Action:Cmd_Create(client, args)
{
        new String:query[200];
        new Handle:querySend = INVALID_HANDLE;
        new String:gangTableName[64];
        new String:arg1[80];
        new gangCount;
       
        Format(query, sizeof(query), "SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'fngls' AND TABLE_NAME = 'gfng'");//Gets GangCount (or largest gang id)
        querySend = SQL_Query(dbConn, query);
       
        if(SQL_FetchRow(querySend))
        {
                gangCount = (SQL_FetchInt(querySend, 0) + 1);//One more than the current amount of gangs.
        }
       
        Format(query, sizeof(query), "INSERT INTO gfng (gangid, name, admincount, membercount) VALUES ('%d', '%s', '%d', '1')", gangCount, arg1, 1);
        querySend = SQL_Query(dbConn, query);
       
        Format(query, sizeof(query), "UPDATE gfng SET level='%d' WHERE gangid='0'", gangCount);
        querySend = SQL_Query(dbConn, query);
       
        CS_SetClientClanTag(client, arg1);
       
        return Plugin_Handled;
}


xines 09-27-2016 10:18

Re: Change normal Query into Threaded Query
 
Something like this.

PHP Code:

querySend SQL_Query(dbConnquery); 

to:

PHP Code:

SQL_TQuery(dbConnSQL_MyCallbackquery); 



The callback:
PHP Code:

public void SQL_MyCallback(Handle ownerHandle hndl, const char[] errorany data)
{
    if (
hndl == null || strlen(error) > 0)
    {
        
LogError("[Mysql] Query failed! %s"error);
    }
    else
    {
        
//Do stuff
    
}



Neuro Toxin 09-27-2016 19:29

Re: Change normal Query into Threaded Query
 
Looking at your mysql statement. This would be useful to learn.

SkinnyBruv 09-28-2016 01:09

Re: Change normal Query into Threaded Query
 
zzz, don't know how to put multiple MYSQL statements into one threaded query :/

Neuro Toxin 09-28-2016 01:16

Re: Change normal Query into Threaded Query
 
You cant. You have to chain the queries through multiple callbacks.

Potato Uno 09-28-2016 03:08

Re: Change normal Query into Threaded Query
 
You can also use transactions to insert multiple queries at once.

SkinnyBruv 09-30-2016 21:44

Re: Change normal Query into Threaded Query
 
Cheers guys for all the help!

SkinnyBruv 10-01-2016 04:22

Threaded Query not keeping Information
 
Howdy guys,

A command is called: Cmd_CreateGang
Code:

public Action:Cmd_CreateGang(client, args)
{
        // Return values of client, GangID, GangName, GangRank
        GetClientIndexes(client);
       
        if(gVIP[client] <= 0)
        {
                PrintToChat(client, "[SM] Please contact an admin about becoming VIP to create a gang. '%s': ");
                return Plugin_Handled;
        }
       
        if(GID[client] > 0)
        {
                PrintToChat(client, "[SM] Please leave you're current gang, before trying to create a new one.");
                return Plugin_Handled;
        }
       
        if(args != 1)
        {
                PrintToChat(client, "[SM] Usage: !create [Gang-Name]");
                return Plugin_Handled;
        }
       
       
        // Setup for the MYSQL call
        new String:query[255];
        new String:arg1[80];
       
        // Check to see what the PlayerTable ConVar is for MYSQL
        GetConVarTable(TABLE_PLAYER); //FNGLS
        GetConVarTable(TABLE_GANG); //GangTable
       
        GetCmdArg(1, arg1, sizeof(arg1));
       
        Format(query, sizeof(query), "SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s", tb_connValue, tableGang);//Gets GangCount (or largest gang id)
        SQL_TQuery(dbConn, SQL_CreateGang, query, client);
       
        GetCmdArg(1, arg1, sizeof(arg1));
        Format(query, sizeof(query), "SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s;", tableGang, gangCount, arg1);
        SQL_TQuery(dbConn, SQL_dbDoNothing, query);
       
        new String:SID[30];
        GetClientAuthId(client, AuthId_Steam3, SID, sizeof(SID));
       
        Format(query, sizeof(query), sQuery_UpdatePlayer, tablePlayer, gangCount, arg1, SID);
        SQL_TQuery(dbConn, SQL_dbDoNothing, query);
       
        SQL_GetError(dbConn, dbError, sizeof(dbError));
        PrintToServer("Attempted to retrieve an error: %s", dbError);
       
        gRank[client] = 1;
        GID[client] = gangCount;
       
        /*for(new i=0;i<=MaxClients;i++)
        {
                if(gGID[i] <= 0)
                {//Free Spot
                        gGID[i] = gangCount;
                        gLevel[i] = pLevel[client];
                        break;
                }
        }*/
       
        CS_SetClientClanTag(client, arg1);
        PrintToChat(client, "[SM] You have successfully created the gang: '%s'.", arg1);
       
        return Plugin_Handled;
}

At the start Indexes Like GID, gName, gRank are given (Just things for the Command)

The Highlighted bit of the Code is Whats next... The "Client" name/data/thing is not going into the next part of the code:
Code:

public void SQL_CreateGang(Handle owner, Handle hndl, char[] error, int client)
{
        if(SQL_FetchRow(hndl))
        {
                gangCount = (SQL_FetchInt(hndl, 0) + 1);//One more than the current amount of gangs.
        }
       
        else
        {
                PrintToServer("[DrugMoney] SQL-ERROR[004]: Could not find default gang row.");
        }
}

Soo therefore it doesn't Get the Current amount of Gangs, to use for the next part of the plugin?

Anyone know an easy fix whilst keeping the Threaded Queries?

Phil25 10-01-2016 04:44

Re: Threaded Query not keeping Information
 
The problem is that you're executing 3 queries on a single frame. In reality, only the last one goes through.

To fix this, you'll need to use Transactions.

Here's the basic syntax:
PHP Code:

Transaction hTxn SQL_CreateTransaction();
char sQuery[255];

Format(sQuerysizeof(sQuery), "first query");
SQL_AddQuery(hTxnsQuery);

Format(sQuerysizeof(sQuery), "second query");
SQL_AddQuery(hTxnsQuery);

Format(sQuerysizeof(sQuery), "third query");
SQL_AddQuery(hTxnsQuery);

SQL_ExecuteTransaction(hDatabasehTxnview_as<SQLTxnSuccess>(TXN_Success), view_as<SQLTxnFailure>(TXN_Failure)); 

Where TXN_Success and TXN_Failure are callback functions that go like this:
PHP Code:

public void TXN_Success    (Handle hDBTXNany aDataint iQueryCount, const char[] sTxnErrorint iFailany[] aQueryData){} 


SkinnyBruv 10-01-2016 04:56

Re: Threaded Query not keeping Information
 
Quote:

Originally Posted by Phil25 (Post 2458256)
The problem is that you're executing 3 queries on a single frame. In reality, only the last one goes through.

To fix this, you'll need to use Transactions.

Here's the basic syntax:
PHP Code:

Transaction hTxn SQL_CreateTransaction();
char sQuery[255];

Format(sQuerysizeof(sQuery), "first query");
SQL_AddQuery(hTxnsQuery);

Format(sQuerysizeof(sQuery), "second query");
SQL_AddQuery(hTxnsQuery);

Format(sQuerysizeof(sQuery), "third query");
SQL_AddQuery(hTxnsQuery);

SQL_ExecuteTransaction(hDatabasehTxnview_as<SQLTxnSuccess>(TXN_Success), view_as<SQLTxnFailure>(TXN_Failure)); 

Where TXN_Success and TXN_Failure are callback functions that go like this:
PHP Code:

public void TXN_Success    (Handle hDBTXNany aDataint iQueryCount, const char[] sTxnErrorint iFailany[] aQueryData){} 


Hmmkay Doctor Phil :D

Ill get to work... Do i need to state what TXN_Success and TXN_Failure?
An example would be great... And then if i do this how do i run SQL_CreateGang :p


All times are GMT -4. The time now is 21:42.

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