Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Distiguishing arguments: number-strings vs real integer

by syphilis (Bishop)
on Aug 10, 2018 at 13:18 UTC ( #1220193=note: print w/replies, xml ) Need Help??


in reply to Re^2: Distiguishing arguments: number-strings vs real integer
in thread Distiguishing arguments: number-strings vs real integer

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
  • Comment on Re^3: Distiguishing arguments: number-strings vs real integer

Replies are listed 'Best First'.
Re^4: Distiguishing arguments: number-strings vs real integer
by LanX (Archbishop) on Aug 10, 2018 at 13:54 UTC
    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

      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.

      Ok ... so you firstly need to know which option the user has chosen.
      Having determined that, I would have thought you could just pass on that user-supplied argument as "$x" (if string was chosen) or "$x"+0 (if integer was chosen) without any need to analyse just what that argument was.

      But I've never worked with databases so you should probably simply ignore me (and perhaps even be envious ;-)

      Cheers,
      Rob
        In the case of (auto generated) placeholders I will pass a reference to the original value into DBI->bind_param() , so this will be fine.

        It's the case of literals causing me headaches.

        The raw concept in short:

        • the DB-table is handled as a Perl package with it's proper namespace,
        • the columns are constants in this package returning objects blessed into a class SQL::Column
        • operators are overloaded for SQL::Column
        • operations return objects of class SQL::Operation with nested attributes like {operator => "<", left =>  bless (['col'], '::Column'), right=> bless (["42"], '::Literal')
        • higher syntax expressions like WHERE are just functions imported into that table package, dealing with the nested objects and returning another container object
        • In the end I get an abstract syntax tree of nested objects which I can walk and translate to the target SQL dialect
        So something like this
        query { package t_table; WHERE col < "42" }

        needs to distinguish if the literal was string "42" or 42

        Of course I could think about overloading lt too, to have a more Perlish way to get the type (and crosscheck against the table's definition), but the more information I have to catch errors, the better.

        I hope my approach is clearer now! :)

        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://1220193]
help
Chatterbox?
and the web crawler heard nothing...

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
















    Results (36 votes). Check out past polls.

    Notices?