Raised This Month: $51 Target: $400
 12% 

Change normal Query into Threaded Query


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SkinnyBruv
AlliedModders Donor
Join Date: Feb 2013
Location: Straya
Old 09-27-2016 , 05:10   Change normal Query into Threaded Query
Reply With Quote #1

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;
}
__________________
www.ancientgaming.net is my story.
Everything happens for a reason.
Nothing is random...
#Do you even AGN...

Last edited by asherkin; 10-01-2016 at 05:58. Reason: Deleteing irrelevant Information
SkinnyBruv is offline
Send a message via Skype™ to SkinnyBruv
xines
Veteran Member
Join Date: Aug 2013
Location: Denmark
Old 09-27-2016 , 10:18   Re: Change normal Query into Threaded Query
Reply With Quote #2

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
    
}

__________________
xines is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 09-27-2016 , 19:29   Re: Change normal Query into Threaded Query
Reply With Quote #3

Looking at your mysql statement. This would be useful to learn.
__________________
Neuro Toxin is offline
SkinnyBruv
AlliedModders Donor
Join Date: Feb 2013
Location: Straya
Old 09-28-2016 , 01:09   Re: Change normal Query into Threaded Query
Reply With Quote #4

zzz, don't know how to put multiple MYSQL statements into one threaded query :/
__________________
www.ancientgaming.net is my story.
Everything happens for a reason.
Nothing is random...
#Do you even AGN...
SkinnyBruv is offline
Send a message via Skype™ to SkinnyBruv
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 09-28-2016 , 01:16   Re: Change normal Query into Threaded Query
Reply With Quote #5

You cant. You have to chain the queries through multiple callbacks.
__________________
Neuro Toxin is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 09-28-2016 , 03:08   Re: Change normal Query into Threaded Query
Reply With Quote #6

You can also use transactions to insert multiple queries at once.
Potato Uno is offline
SkinnyBruv
AlliedModders Donor
Join Date: Feb 2013
Location: Straya
Old 09-30-2016 , 21:44   Re: Change normal Query into Threaded Query
Reply With Quote #7

Cheers guys for all the help!
__________________
www.ancientgaming.net is my story.
Everything happens for a reason.
Nothing is random...
#Do you even AGN...
SkinnyBruv is offline
Send a message via Skype™ to SkinnyBruv
SkinnyBruv
AlliedModders Donor
Join Date: Feb 2013
Location: Straya
Old 10-01-2016 , 04:22   Threaded Query not keeping Information
Reply With Quote #8

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?
__________________
www.ancientgaming.net is my story.
Everything happens for a reason.
Nothing is random...
#Do you even AGN...

Last edited by SkinnyBruv; 10-01-2016 at 22:29.
SkinnyBruv is offline
Send a message via Skype™ to SkinnyBruv
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 10-01-2016 , 04:44   Re: Threaded Query not keeping Information
Reply With Quote #9

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){} 
Phil25 is offline
SkinnyBruv
AlliedModders Donor
Join Date: Feb 2013
Location: Straya
Old 10-01-2016 , 04:56   Re: Threaded Query not keeping Information
Reply With Quote #10

Quote:
Originally Posted by Phil25 View Post
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

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
__________________
www.ancientgaming.net is my story.
Everything happens for a reason.
Nothing is random...
#Do you even AGN...
SkinnyBruv is offline
Send a message via Skype™ to SkinnyBruv
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 19:25.


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