1. is this a bug in $dbh->quote()

This cannot be answered without referring to the documentation. There it says:

As a special case, the standard numeric types are optimized to return $value without calling type_info.

The answer is therefore: no, this is expected documented behaviour, not a bug. You could however argue that it is a wart - having gone to the trouble of providing a quoting mechanism, it seems a bit unhelpful to exempt these types as a special case.

2. is there exists more correct way to insert numbers into SQL

All I can suggest is to create your own quoting method. Something like:

sub quote_number { my($dbh, $value) = @_; if (defined $value) { # force to number return $value + 0; } else { return $dbh->quote($value); } }

Note that as written, that will give a warning (assuming they're turned on) on anything that doesn't look enough like a number; you may prefer to treat that more severely (die rather than warn) or less severely (don't warn at all); similarly the above code will permit decimals and negative numbers: if you want to be more restrictive than that you'll need to add some code to test either mathematically or with regexps, eg:

sub quote_integer { my($dbh, $value) = @_; if (defined $value) { # pattern approach die "Not an integer" unless $value =~ /^[-+]?\d+\z/; # maths approach die "Not an integer" unless $value == int($value); return $value + 0; } else { return $dbh->quote($value); } }

Hugo


In reply to Re: $dbh->quote(..., SQL_INTEGER) by hv
in thread $dbh->quote(..., SQL_INTEGER) by powerman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.