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

Simple Question Regarding SQL Callbacks


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Chief149
Member
Join Date: Sep 2010
Old 09-28-2016 , 19:53   Simple Question Regarding SQL Callbacks
Reply With Quote #1

So we of course have queries being called in the following manner:

PHP Code:
public void doMySQL() {
    
Handle g_hDB Some Valid MySQL Handle//PART1
    
char sQuery[256] = "SELECT * FROM mytable WHERE myconditions=true;";
    
SQL_TQuery(g_hDBSQL_HandleQuerysQuery);
}

public 
void SQL_HandleQuery(Handle dbHandle resultschar[] sErrorany data) {
    if(
db != INVALID_HANDLE)
        
delete db//PART2

So at //PART1 I have g_hDB which is a handle to a valid database. If I use this handle, the query is going to work, and eventually a response (or error) will come along to the callback.
The callback has "Handle db" as a parameter. This handle is a handle to the same database as g_hDB.

My question is, is db a clone of g_hDB. If I do "delete db" at the line commented with //PART2, then would I also be closing g_hDB? Or is db just a reference to the same handle as g_hDB?

Lastly, is it necessary to delete db? Assuming db is a clone of g_hDB. I only ask this because I have gone over a year without my plugin, which performs MANY queries, being closed due to a memory leak.
Chief149 is offline
Chief149
Member
Join Date: Sep 2010
Old 09-28-2016 , 20:18   Re: Simple Question Regarding SQL Callbacks
Reply With Quote #2

A second question that is an afterthought.
I made a furniture plugin that re-uses entities. When an entity is needed, my plugin checks to see if there is an unused entity in the list. If not then it makes a new one, and if so, it repurposes an available entity.

I'm currently using SetEntityModel to change the models of the entities that I reuse as needed. But now I am noticing some odd things. Mainly players getting pushed around by invisible entities.

My plugin teleports entities that are no longer being used off map.

So I am wondering, does SetEntityModel also change the collision mesh? Is there anything I need to know when it comes to re-use of entities (and changing their models)?
Chief149 is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 09-28-2016 , 21:57   Re: Simple Question Regarding SQL Callbacks
Reply With Quote #3

Quote:
Originally Posted by Chief149 View Post
This handle is a handle to the same database as g_hDB.
I believe it's the same reference so if you close it it'll also be closing the other. Correct me if I'm wrong
headline is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 09-28-2016 , 21:59   Re: Simple Question Regarding SQL Callbacks
Reply With Quote #4

db should be equal to g_hDB (you can check by doing PrintToServer("%d, %d", db, g_hDB) and check that the two numbers are identical).

For callbacks, any handles provided will be automatically destroyed once the callback ends, so you don't ever need to do any handle deletion (and in fact, you risk introducing bugs if you do so). Always check the API docs on this however, some may require you to delete it, but most will automatically close the handle for you.

As for your 2nd question, just setting the model only changes 1 property of the entity, but the others will remain the same as from the old entity. If possible, you are better off remaking the entity from scratch (if you can).
Potato Uno is offline
Chief149
Member
Join Date: Sep 2010
Old 09-28-2016 , 22:33   Re: Simple Question Regarding SQL Callbacks
Reply With Quote #5

So deleting that db handle will not delete g_hDB? Because while they reference the same databases, they are clones?

Would something like this cause problems?
PHP Code:
Handle g_hDB Some Valid DB Handle;
SQL_TQuery(my query params... );
delete g_hDB
Aka deleting the database handle immediately after sending the query.

Last edited by Chief149; 09-28-2016 at 23:05.
Chief149 is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 09-29-2016 , 01:12   Re: Simple Question Regarding SQL Callbacks
Reply With Quote #6

Reference and clones are both opposite things.

A reference points to an object.
A clone is a copy of the object and has no relationship to the original.

Because the threaded query may not execute instantly (it's threaded, after all), deleting the database handle may cause problems. If you want to delete the database handle, do it in the query callback.

Last edited by Potato Uno; 09-29-2016 at 01:14.
Potato Uno is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 09-29-2016 , 07:20   Re: Simple Question Regarding SQL Callbacks
Reply With Quote #7

The callback handle from query operations is a unique handle that will not equality compare to your g_hDB handle (== operator), instead you have to do
PHP Code:
g_hDB.IsSameConnection(db); 
in the callback to check if it's the same DB handle. Oh and you can't delete the db handle given to the query.
__________________
WildCard65 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 03:04.


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