Kwiki::DB::ClassDBI - A Class::DBI wrapper for Kwiki


Kwiki-DB documentation Contained in the Kwiki-DB distribution.

Index


Code Index:

NAME

Top

  Kwiki::DB::ClassDBI - A Class::DBI wrapper for Kwiki

SYNOPSIS

Top

  package Kwiki::MyPlugin;
  use Kwiki::Plugin -Base;

  # setup Music::Artist and Music::CD as in Class::DBI pod.
  sub init {
      super;
      $self->hub->config->add_field("db_class" => 'Kwiki::DB::DBI');
      field db => -init => "\$self->hub->load_class('db')";

      $self->db->entity( artist => 'Music::Artist' );
      $self->db->entity(     cd => 'Music::CD'     );
      $self->connection("dbi:SQLite:dbfile.sqlt");
  }

  sub my_action {
      $self->cdb->artist->create(...)
  }

DESCRIPTION

Top

This module privdes a bridge between Class::DBI and Kwiki programming environment. After adding Kwiki::DB::ClassDBI into your plugins file, there will be a convienent $self->hub->cdbi reference to an instantiated object which acts as the door to all your Class::DBI based classes.

Instead of using class name to access data, this module requires you give several "entity" names in the init phrase. Each entity has a short name, and a corresponding Class::DBI based class name. Writing

    $self->hub->cdbi->entity( artist => 'Music::Artist' );

would create a object held in $self->db->artist, and delegates all methods to Music::Artist. So these two lines are doing the same work:

    $self->hub->cdbi->artist->create({ artistid => 1, name => 'U2' });
    Music::Artist->create({ artistid => 1, name => 'U2' });

They return the same type of value, because $self->db->artist only delegates the create method to Music::Artist.

People could even directly use the $hub->db to access database in their kwiki template like this:

    The band is [% hub.db.artist.retrieve(1).name %].

Also, you may want to read the test t/02.classdbi-sqlite.t and t/lib/Kwiki/DB/Music* as a live example for how to use this bridge.

SEE ALSO

Top

Class::DBI, Kwiki::DB::DBI

COPYRIGHT

Top


Kwiki-DB documentation Contained in the Kwiki-DB distribution.

package Kwiki::DB::ClassDBI;
use Kwiki::DB -Base;

our $VERSION = '0.03';

const class_id    => 'cdbi';
const class_title => 'Kwiki ClassDBI';

field '_base';
field entities => {};

sub base {
    return $self->_base unless @_;
    my $class = shift;
    eval "require $class";
    die $@ if $@;
    $self->_base($class);
}

sub entity {
    my ($entity,$class) = @_;
    my $object = Kwiki::DB::ClassDBI->new(base => $class);
    $object->init;
    $self->entities->{$entity} = $object;
}

sub AUTOLOAD {
    my ($p,$func) = $Kwiki::DB::ClassDBI::AUTOLOAD =~ m/(.*)::(.*?)$/;
    for(keys %{$self->entities}) {
        if ($_ eq $func) {
            return $self->entities->{$_};
        }
    }
    if(my $base = $self->base) {
        $base->$func(@_);
    }
}

__END__