Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

xsawyerx (8978)

xsawyerx
  (email not shown publicly)

Journal of xsawyerx (8978)

Thursday March 19, 2009
03:37 AM

Numeric values in DBIC

[ #38665 ]

I'm guessing it's one of those "you just don't know how to do it right" type of things, but I couldn't get DBIx::Class to treat my number as numeric in a search() or find(). DBIC_TRACE shows that it quotes it every time. I guess it makes sense since I send it in a hash, and there's no way to know what I really want with it.

In the end ("end" being my term for "searched for an hour, read documentation, tried some stuff, and asked around), I opted for adding a specific WHERE statement in my search. I don't think it's very elegant and perhaps there is a better way to do this. I'd be more than appreciative if anyone could let me know.

    my $os = $mysql_schema->resultset('Os')->find( {
        os      => $map->os,
        distro  => $map->os_distro,
    }, {
        where => \( 'version = ' . $map->os_version ),
    } ) || $EMPTY;

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • I know this sort of thing works (on Postgres)

            find({ date => \'>= Current_Timestamp' })

    so I'm thinking you want something like:

        my $version = $map->os_version;
        # equality
        find({ version => \"= $version" })
        # greater than or equal to
        find({ version => \">= $version" })

  • It's actually DBI (or the DBD, I'm not quite sure) which is taking a look at the scalar itself to figure out if it's numerical or not. What you could try is this:

    my $int = 23;
    my $str = "17";

    # force to PV:
    my $int_as_str = "$int";

    # force to IV:
    my $str_as_int = 0+$int;

    # or in your example:
    my $item = $rs->find({
        something => 0+$str,
        another   => "$int",
    });

    --
    Ordinary morality is for ordinary people. -- Aleister Crowley
    • I just had to import things into a database and tried this method to resolve a find_or_create({}).

      It worked perfectly, thank you!