AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Multiple ordered SQL queries (https://forums.alliedmods.net/showthread.php?t=165140)

AnIHiL 08-19-2011 16:01

Multiple ordered SQL queries
 
I checked all topic related to MySQL queries but I didn't find out how to solve my problem.

How can I make 3-4 SQL threaded queries but they should run in exact order:
1. Query 1 -> give results
2. Query 2 -> give results
...

Query 2 should be executed after I get all results from query 1.

I can make something like this:

Code:

SQL_TQuery(hdatabase, Q1Res, query1, userid);

public Q1Res(Handle:owner, Handle:hndl, const String:error[], any:data)
{
    // get results
    ...
    SQL_TQuery(hdatabase, Q2Res, query2, userid);
}

public Q2Res(Handle:owner, Handle:hndl, const String:error[], any:data)
{
    // get results
    ...
    SQL_TQuery(hdatabase, Q3Res, query3, userid);
}

public Q3Res(Handle:owner, Handle:hndl, const String:error[], any:data)
{
    // get results
    ...
    SQL_TQuery(hdatabase, Q4Res, query4, userid);
}

public Q4Res(Handle:owner, Handle:hndl, const String:error[], any:data)
{
    // get results
    ...
}

But as you can see this code will be very complicated if I insert more queries there.

Is there any other solution how to solve it?

Powerlord 08-19-2011 17:05

Re: Multiple ordered SQL queries
 
Quote:

Originally Posted by AnIHiL (Post 1536510)
Is there any other solution how to solve it?

Use SQL_Connect and SQL_Query instead of their threaded equivalents.

Essentially, the threaded versions are there so you don't have to wait for a response from the database before you continue execution.

FaTony 08-19-2011 17:11

Re: Multiple ordered SQL queries
 
IIRC MySQL allows you to chain queries separating them by ;

AnIHiL 08-19-2011 17:42

Re: Multiple ordered SQL queries
 
Quote:

Originally Posted by Powerlord (Post 1536555)
Use SQL_Connect and SQL_Query instead of their threaded equivalents.

Essentially, the threaded versions are there so you don't have to wait for a response from the database before you continue execution.

I need to use threads because I have a lot of queries to database and when If I don't use threads I have lags on server.

Quote:

Originally Posted by FaTony (Post 1536560)
IIRC MySQL allows you to chain queries separating them by ;

I need to query #2 after I get all results from query #1 because those results set some variables which are required for query #2 so separating those queries with ';' won't work for me.

psychonic 08-19-2011 18:43

Re: Multiple ordered SQL queries
 
Quote:

Originally Posted by FaTony (Post 1536560)
IIRC MySQL allows you to chain queries separating them by ;

The implementation needs to be compiled with support for that specifically enabled. SM's mysql extension does not have it enabled afaik.

DarkEnergy 08-19-2011 19:46

Re: Multiple ordered SQL queries
 
SM MYSQL EXTENSION does NOT do concurrent requests. that is

function(){
threadedquery1
threadedquery2
threadedquery3
threadedquery4
threadedquery5 to a different database
}

Query 1 will always finish before query 2...etc. Even if they are different database connections.
The extension basically has 1 thread, looks at the pending queries, takes the top one, waits for it to return and calls the results callback function on the next game frame.
Rinse and repeat.

The queue has priority, so just keep all your sequential queries the same priority.

MYSQL queries are also limited to a max of 10 / second (arbitrary? limit set by dvander)

AnIHiL 08-20-2011 03:44

Re: Multiple ordered SQL queries
 
So it will alway s be like:

function(){
threadedquery1
callback query1
threadedquery2
callback query2
threadedquery3
callback query3
threadedquery4
callback query4
threadedquery5 to a different database
callback query5
}

For me its important that "threadedquery2" should run after whole code in "callback query1" executes.

Limit is 10/sec not 20/sec ?

berni 08-20-2011 10:23

Re: Multiple ordered SQL queries
 
Quote:

Originally Posted by AnIHiL (Post 1536811)

For me its important that "threadedquery2" should run after whole code in "callback query1" executes.

Why don't you just use the callback functions ?

DarkEnergy 08-21-2011 16:26

Re: Multiple ordered SQL queries
 
Quote:

Originally Posted by AnIHiL (Post 1536811)
So it will alway s be like:

function(){
threadedquery1
callback query1
threadedquery2
callback query2
threadedquery3
callback query3
threadedquery4
callback query4
threadedquery5 to a different database
callback query5
}

For me its important that "threadedquery2" should run after whole code in "callback query1" executes.

Limit is 10/sec not 20/sec ?

function(){
threadedquery1
threadedquery2
threadedquery3
threadedquery4
threadedquery5 to a different database
}

function completes immediately because all these queries are queued into another thread. The whole purpose of threaded queries is that they do not BLOCK the function. threadquery1 callback will happen before threadquery2 can actually start. the callback is called whenever the results are returned.
queries can share callbacks

if your queries rely on the results of the previous query, just SQL_TQuery within the results callback.


All times are GMT -4. The time now is 19:20.

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