Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Class::DBI and Ima::DBI and ints and objects

by rkg (Hermit)
on Aug 20, 2003 at 08:27 UTC ( #285130=perlquestion: print w/ replies, xml ) Need Help??
rkg has asked for the wisdom of the Perl Monks concerning the following question:

Hi folks --

I'm using Class::DBI, and I need an aggregate function, so as per the docs I'm using set_sql, as Class::DBI inherits from Ima::DBI. This works fine

Rk::M->set_sql(maxid => q(select max(m) from m)); my $sth = Rk::M->sql_maxid; $sth->execute; my ($max) = $sth->fetchrow_array;
However, the Class::DBI docs indicate this shorter form might work too:
Rk::M->set_sql(maxid => q(select max(m) from m)); my $max = Rk::M->maxid;
It doesn't, complaining it can't locate object method 'maxid' via package Rk::M.

Am I reading the docs wrong here? I get the sense Class::DBI always wants to return a Rk::M (or whatever) object, not a simple int, but perhaps that's a second problem here -- for now, I am wondering why Class::DBI isn't even executing the method.

Thanks for your advice.

rkg

Comment on Class::DBI and Ima::DBI and ints and objects
Select or Download Code
Re: Class::DBI and Ima::DBI and ints and objects
by broquaint (Abbot) on Aug 20, 2003 at 09:32 UTC
    There must a better way that this but ...
    Rk::M->set_sql(maxid => q(select max(m) from m)); my $max = +(Rk::M->search_maxid)[0]->{'max(m)'};
    But that breaks encapsulation rather blatantly. Since Class::DBI is really about the mapping of tables to classes (roughly) it's probably a better idea just to drop into raw DBI e.g
    my $max = $dbh->selectrow_array('select max(m) from m');

    HTH

    _________
    broquaint

Re: Class::DBI and Ima::DBI and ints and objects
by PodMaster (Abbot) on Aug 20, 2003 at 09:50 UTC
    I'm guessing you really wanted to say Rk::M->search_maxid();
    `perldoc Class::DBI':
            Music::CD->set_sql(new_music => qq{
                    SELECT __ESSENTIAL__
                      FROM __TABLE__
                     WHERE year > ?
            };
    
    This approach automatically sets up the method Music::CD->search_new_music(), which will execute this search and return the relevant objects or Iterator. (If you have placeholders in your query, you must pass the relevant arguments when calling your search method.)

    update: Try print "$_\n" for keys %Rk::M::; for kicks.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Class::DBI and Ima::DBI and ints and objects
by perrin (Chancellor) on Aug 20, 2003 at 14:58 UTC
    The shorter form (which is called "search_maxid") only works if you want objects back. Your first approach is the best one.
Re: Class::DBI and Ima::DBI and ints and objects
by trs80 (Priest) on Aug 20, 2003 at 21:13 UTC
    set_sql creates a *closure* named 'sql_' + key. There is no corresponding closure with a name without the 'sql_' on it. There is a set_sql in the Class::DBI module that checks the connection to make sure the correct one gets passed to the Ima::DBI set_sql method. set_sql in Ima::DBI passes the statement to _mk_sql_closure to make the closure. So sql_maxid is NOT a method, but rather a closure. There is some GLOB magic going on that makes it appear like a method. See the subs sql_set and _mk_sql_closure inside of the Ima::DBI module.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2014-12-19 18:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (91 votes), past polls