Numeric values in DBIC

xsawyerx on 2009-03-19T08:37:12

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;


possible solution

mintywalker on 2009-03-19T19:11:56

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" })

However, this worked better

xsawyerx on 2009-04-16T14:12:18

... than the other suggested solution.

find()/find_or_create() did not accept 0+$num as a number. Thanks.

It's not DBIx-Class

phaylon on 2009-03-19T23:32:09

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",
});

Re:It's not DBIx-Class

xsawyerx on 2009-04-16T12:50:19

I just had to import things into a database and tried this method to resolve a find_or_create({}).

It worked perfectly, thank you!