Raised This Month: $32 Target: $400
 8% 

SQL Binding IN Clause?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
RumbleFrog
Great Tester of Whatever
Join Date: Dec 2016
Location: Fish Tank
Old 04-22-2017 , 17:38   SQL Binding IN Clause?
Reply With Quote #1

Say I have this

PHP Code:
DELETE FROM queue WHERE `idIN ( ); 

And I wish to use a prepared statement for this, how would I do this?


In PHP I use this

PHP Code:
$inQuery implode(','array_fill(0count($ids), '?'));
$stmt $this->db->prepare('DELETE FROM queue WHERE `id` IN (' $inQuery ')');
foreach (
$ids as $k => $id)
      
$stmt->bindValue(($k+1), $id); 
Not sure how would I replicate this in sourcepawn or if it's possible
RumbleFrog is offline
RumbleFrog
Great Tester of Whatever
Join Date: Dec 2016
Location: Fish Tank
Old 04-22-2017 , 17:45   Re: SQL Binding IN Clause?
Reply With Quote #2

Also to highlight that, amount of values in the IN clause is going to be random.
RumbleFrog is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 04-22-2017 , 19:37   Re: SQL Binding IN Clause?
Reply With Quote #3

You shouldn't be using prepared statements in SourcePawn, as they do not support threading.

The FormatQuery native gives you "fake" prepared statements, which is what you get with PDO in PHP by default anyway.
__________________
asherkin is offline
RumbleFrog
Great Tester of Whatever
Join Date: Dec 2016
Location: Fish Tank
Old 04-22-2017 , 20:15   Re: SQL Binding IN Clause?
Reply With Quote #4

Quote:
Originally Posted by asherkin View Post
You shouldn't be using prepared statements in SourcePawn, as they do not support threading.

The FormatQuery native gives you "fake" prepared statements, which is what you get with PDO in PHP by default anyway.
Can you give an example for a non-prepared and threaded statement for this scenario?

Last edited by RumbleFrog; 04-22-2017 at 20:16.
RumbleFrog is offline
RumbleFrog
Great Tester of Whatever
Join Date: Dec 2016
Location: Fish Tank
Old 04-22-2017 , 22:31   Re: SQL Binding IN Clause?
Reply With Quote #5

Are there anything similar to array_fill?



Edit: What's this FormatQuery native?

Last edited by RumbleFrog; 04-22-2017 at 22:41.
RumbleFrog is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 04-23-2017 , 11:40   Re: SQL Binding IN Clause?
Reply With Quote #6

SQL_FormatQuery (and Database.Format(), for that matter) are new natives in SourceMod 1.9+. It escapes unsafe string parameters, which the default Format native doesn't do.

Here's sample usage given in the PR; the old method shows the "current" way a threaded pseudo-statement is done (creating escaped strings then Formatting the query).

The native currently isn't available in the current stable branch as far as I can tell; I wouldn't use it myself unless you're willing to target SourceMod 1.9+.

Edit: Don't think you'd be able to bind a variable number of parameters with SQL_FormatQuery. You'll probably need to escape any unsafe strings, join the values, and then pass it as one string to Format.

Double edit: You can use Powerlord's implodeexplode.inc stocks to join multiple strings in an ArrayList into one.

The edits continue: Or you can use ImplodeStrings if you're willing to set up your char[][]s.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 04-24-2017 at 08:55.
nosoop 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 20:13.


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