Kwiki::DB::ClassDBI - A Class::DBI wrapper for Kwiki
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(...) }
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.
Kwiki::DB::ClassDBI
plugins
Class::DBI
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:
Music::Artist
$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.
create
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.
t/02.classdbi-sqlite.t
t/lib/Kwiki/DB/Music*
Class::DBI, Kwiki::DB::DBI
Copyright 2005 by Kang-min Liu <gugod@gugod.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <http://www.perl.com/perl/misc/Artistic.html>
To install Kwiki::DB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Kwiki::DB
CPAN shell
perl -MCPAN -e shell install Kwiki::DB
For more information on module installation, please visit the detailed CPAN module installation guide.