RE: Class::DBI::Loader and accessors

[prev] [thread] [next] [Date index for 2005/03/17]

From: Zhuang Li
Subject: RE: Class::DBI::Loader and accessors
Date: 18:48 on 17 Mar 2005
I strongly suggest to include my patch for retrieve_all method,
http://groups.kasei.com/mail/arc/cdbi-talk/2005-02/msg00281.html,
in the future release so that one doesn't have to remember to use
Class::DBI::Plugin::RetrieveAll.

Before then, Kingsley, you can easily create your Class::DBI wrapper:

Package My::DBI;
use base 'Class::DBI';

__PACKAGE__->set_sql(RetrieveAll =3D> <<'');
SELECT __ESSENTIAL__
FROM __TABLE__
%s

sub retrieve_all {
  my ($class, $arg) =3D @_;

  if (ref $arg eq "HASH" && $arg->{order_by}) {
    return $class->sth_to_objects($class->sql_RetrieveAll(" ORDER BY
       $arg->{order_by}"));
  } else {
    return $class->sth_to_objects('RetrieveAll');
  }
}

This will also make 'retrieve_all' to have the same 'order_by' syntax as
'search' method.

Cheers,

John (Zhuang) Li

> -----Original Message-----
> From: Kingsley Kerce [mailto:kingsley@xxxxxxxxxxxxxxxx.xxx]
> Sent: Wednesday, March 16, 2005 9:51 AM
> To: cdbi-talk@xxxxxx.xxxxx.xxx
> Subject: Re: Class::DBI::Loader and accessors
>=20
> Kingsley Kerce writes:
>  > Perrin Harkins writes:
>  >  > Isn't this what the "additional_base_classes" parameter in
Loader is
>  >  > for?
>  >
>  > Well, yeah, if you're running CDBIL >=3D 0.06, unlike me. :)
>  > I'll upgrade.  Thanks for the reality check, Perrin.
>=20
> Using Class::DBI::Loader 0.17 and the additional*_classes params, I've
> managed to eliminate looping/eval'ing over all my CDBI table classes
> in order to include my base class My::DBI and the plugin class
> Class::DBI::Plugin::RetrieveAll.
>=20
> But I can't find a way to move some add_relationship_type() and
> columns() invocations -- which must be done for every table class --
> into my base class.  Appended below is my app-initialization code,
> before and after using CDBIL 0.17.
>=20
> Looking at the second chunk of code below, is there a way to eliminate
> looping over the table classes, and move the add_relationship_type()
> and columns() calls into my base class, such that those calls will
> occur during the CDBIL phase?
>=20
> Thanks,
> Kings
>=20
>=20
>
#-----------------------------------------------------------------------
-------
> #-- OLD METHOD, USING Class::DBI::Loader 0.03
---------------------------------
>
#-----------------------------------------------------------------------
-------
>=20
>   my $loader =3D Class::DBI::Loader->new(
>     dsn =3D> $data_source,
>     user =3D> $db_user,
>     password =3D> $db_pass,
>     namespace =3D> "My",
>     options =3D> {
>       # ...
>     }
>   ));
>=20
>   # We do not use triggers in this application.  In particular, we
want
>   # PostgreSQL to cascade deletions rather than having Class::DBI do
it.
>   eval "package Class::DBI::Relationship::HasMany::NoCascadeDelete;
>=20
>         use base 'Class::DBI::Relationship::HasMany';
>=20
>         sub triggers { return () }
>        ";
>   confess $@ if $@;
>=20
>   foreach my $class ($loader->classes) {
>     eval "package $class;
>=20
>           use base 'My::DBI';
>=20
>           use Class::DBI::Plugin::RetrieveAll;
>           __PACKAGE__->add_relationship_type(has_many =3D>
> 'Class::DBI::Relationship::HasMany::NoCascadeDelete');
>           if ('$class' =3D~ /Type\$/) {
>             # May as well pull in all columns of db tables named
%_type
>             __PACKAGE__->columns(Essential =3D> __PACKAGE__->columns);
>           }
>     ";
>     confess $@ if $@;
>   }
>=20
>
#-----------------------------------------------------------------------
-------
> #-- IMPROVED METHOD, USING Class::DBI::Loader 0.17
----------------------------
>
#-----------------------------------------------------------------------
-------
>=20
>   my $loader(Class::DBI::Loader->new(
>     dsn =3D> $data_source,
>     user =3D> $db_user,
>     password =3D> $db_pass,
>     namespace =3D> "My",
>     additional_classes =3D> qw/Class::DBI::Plugin::RetrieveAll/,
>     additional_base_classes =3D> qw/My::DBI/,
>     options =3D> {
>       # ...
>     }
>   ));
>=20
>   # We do not use triggers in this application.  In particular, we
want
>   # PostgreSQL to cascade deletions rather than having Class::DBI do
it.
>   eval "package Class::DBI::Relationship::HasMany::NoCascadeDelete;
>=20
>         use base 'Class::DBI::Relationship::HasMany';
>=20
>         sub triggers { return () }
>        ";
>   confess $@ if $@;
>=20
>   foreach my $class ($loader->classes) {
>     $class->add_relationship_type(has_many =3D>
'Class::DBI::Relationship::HasMany::NoCascadeDelete');
>     if ($class =3D~ /Type$/) {
>       # May as well pull in all columns of db tables named %_type
>       $class->columns(Essential =3D> $class->columns);
>     }
>   }
>=20
>
#-----------------------------------------------------------------------
-------
>=20


(message missing)

Class::DBI::Loader and accessors
Sean Davis 21:10 on 15 Mar 2005

Re: Class::DBI::Loader and accessors
Kingsley Kerce 21:29 on 15 Mar 2005

Re: Class::DBI::Loader and accessors
Perrin Harkins 21:51 on 15 Mar 2005

Re: Class::DBI::Loader and accessors
Kingsley Kerce 22:28 on 15 Mar 2005

Re: Class::DBI::Loader and accessors
Kingsley Kerce 17:51 on 16 Mar 2005

Re: Class::DBI::Loader and accessors
Perrin Harkins 19:22 on 17 Mar 2005

RE: Class::DBI::Loader and accessors
Zhuang Li 18:48 on 17 Mar 2005

RE: Class::DBI::Loader and accessors
Kingsley Kerce 19:07 on 17 Mar 2005

RE: Class::DBI::Loader and accessors
Zhuang Li 20:02 on 17 Mar 2005

RE: Class::DBI::Loader and accessors
Kingsley Kerce 21:02 on 17 Mar 2005

Re: Class::DBI::Loader and accessors
Tony Bowden 23:16 on 17 Mar 2005

RE: Class::DBI::Loader and accessors
Zhuang Li 21:43 on 17 Mar 2005

Can't call id() as a class method
Tom Gazzini 22:44 on 17 Mar 2005

Re: Can't call id() as a class method
Perrin Harkins 22:52 on 17 Mar 2005

Re: Can't call id() as a class method
Tony Bowden 23:15 on 17 Mar 2005

RE: Class::DBI::Loader and accessors
Zhuang Li 21:54 on 23 Mar 2005

Re: Class::DBI::Loader and accessors
Tim Bunce 22:26 on 23 Mar 2005

Re: Class::DBI::Loader and accessors
Tony Bowden 23:00 on 23 Mar 2005

Re: Class::DBI::Loader and accessors
Tony Bowden 23:34 on 23 Mar 2005

RE: Class::DBI::Loader and accessors
Zhuang Li 23:07 on 23 Mar 2005

RE: Class::DBI::Loader and accessors
Zhuang Li 23:50 on 23 Mar 2005

RE: Class::DBI::Loader and accessors
Peter Speltz 00:04 on 24 Mar 2005

Generated at 15:09 on 27 Mar 2005 by mariachi v0.52