Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Distiguishing arguments: number-strings vs real integer

by choroba (Archbishop)
on Aug 09, 2018 at 23:39 UTC ( #1220167=note: print w/replies, xml ) Need Help??


in reply to Distiguishing arguments: number-strings vs real integer

The real question is Why do you need it? Smells of an XY problem.

E.g. JSON modules try to detect the fact and keep the nature of numbers and strings, and in my talk at Glasgow, I'll show why it's the wrong approach and what a more Perlish way could be. See also my blog.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Distiguishing arguments: number-strings vs real integer
by LanX (Archbishop) on Aug 09, 2018 at 23:59 UTC
    I'm hacking a DBI abstraction, and I'm not sure about the side-effects of binding 42 vs "42" to a placeholder.

    So I want to play safe and keep full control.

    see also should-i-quote-numbers-in-sql

    I hope that DBI handles that automatically according to the type of the column, but again I'm not sure ...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      I'm hacking a DBI abstraction, and I'm not sure about the side-effects of binding 42 vs "42" to a placeholder.

      Isn't irrelevant what Perl thinks of a variable's type when DB already knows/was told what the type of that column should be at create time? If DB column was declared as INT then don't quote (and possibly warn user of a type mismatch if any), else quote.

      Having said that I read in Programming the Perl DBI:

      It's equally simple to specify multiple bind values within one statement, since bind_ param( ) takes the index, starting from 1, of the parameter to bind the given value to. For example:
      
          $sth = $dbh->prepare( "
                      SELECT name, location
                      FROM megaliths
                      WHERE name = ?
                      AND mapref = ?
                      AND type LIKE ?
                  " );
          $sth->bind_param( 1, "Avebury" );
          $sth->bind_param( 2, $mapreference );
          $sth->bind_param( 3, "%Stone Circle%" );
      
      You may have noticed that we haven't called the quote( ) method on the values. Bind values are passed to the database separately from the SQL statement,[50] so there's no need to "wrap up" the value in SQL quoting rules.
      
          [50] This is not strictly true, since some drivers emulate placeholders by doing a textual replacement of the placeholders with bind values before passing the SQL to the database. Such drivers use Perl's internal information to guess whether each value needs quoting or not. Refer to the driver documentation for more information. [emphasis mine]
      
        > Isn't irrelevant what Perl thinks of a variable's type when DB already knows/was told what the type of that column should be at create time?

        some testing on MariaDB

        CREATE TABLE `t_test_type` ( `f_num` INT(11) NULL DEFAULT NULL, `f_str` CHAR(50) NULL DEFAULT NULL ) ENGINE=MyISAM ; select * from t_test_type; 1;1 2;2 3;3 select * from t_test_type where f_str < "2.0"; 1;1 2;2 select * from t_test_type where f_str < 2.0; 1;1

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      I'm hacking a DBI abstraction, and I'm not sure about the side-effects of binding 42 vs "42" to a placeholder

      Therefore, you need to determine whether the placeholder needs to be 42 or "42".

      So I want to play safe and keep full control

      Therefore, either:
      1) having determined that the placeholder needs to be an integer, you provide $x + 0;
      or
      2) having determined that the placeholder needs to be a string, you provide "$x".

      But I don't see that you would need to determine whether $x is an integer or a string.
      Of course, things get a little more complex if there's a need to check the validity of the integer value, but that doesn't appear to be an issue (going by your description of the problem).

      Cheers,
      Rob
        I find SQL very confusing and archaic that's why I want a safer abstraction layer.

        Saying this it becomes even more confusing because of different dialects and differently implemented DB drivers.

        > Therefore, you need to determine whether the placeholder needs to be 42 or "42".

        This is up to the user of the layer, that's why I need to know if he passed a literal string or number. And to be able to warn him if necessary.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: Distiguishing arguments: number-strings vs real integer
by LanX (Archbishop) on Aug 10, 2018 at 01:11 UTC
    > E.g. JSON modules try to detect the fact and keep the nature of numbers and strings,

    which solves my issue, thanks! :)

    $str=encode_json( [$_[0]] ); does the trick, I only need to check if /^\[".*"\]$/ is matching.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1220167]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-04-06 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (36 votes). Check out past polls.

    Notices?