*names_of = sub (@cols) { map $_->{name}, @cols };
- *function_body = sub ($name, $args, $body_parts) {
+ *functionbody = sub ($name, $args, $body_parts) {
my $arglist = join(
', ',
map "_${\$_->{name}} ${\uc($_->{data_type})}",
# NOTE: this assumes a single PK col called id with a sequence somewhere
# but nothing else -should- so fixing this should make everything work
my $insert_func =
- function_body
+ functionbody
$self->name.'_insert',
\@body_cols,
[
# UPDATE function
my $update_func =
- function_body
+ functionbody
$self->name.'_update',
[ @pk_cols, @body_cols ],
[ map $sqla->update(
# DELETE function
my $delete_func =
- function_body
+ functionbody
$self->name.'_delete',
[ @pk_cols ],
[ map $sqla->delete($_->name, $pk_where), @sources ];
=head1 SYNOPSIS
{
- package MyApp::Schema::Result::Coffee;
+ package Cafe::Result::Coffee;
- __PACKAGE__->table_class(
- 'DBIx::Class::ResultSource::MultipleTableInheritance'
- );
+ use strict;
+ use warnings;
+ use parent 'DBIx::Class::Core';
+ use aliased 'DBIx::Class::ResultSource::MultipleTableInheritance'
+ => 'MTI';
+
+ __PACKAGE__->table_class(MTI);
__PACKAGE__->table('coffee');
__PACKAGE__->add_columns(
- "id",
- {
- data_type => "integer",
- default_value => "nextval('coffee_seq'::regclass)",
- is_auto_increment => 1,
- is_foreign_key => 1,
- is_nullable => 0,
- size => 4,
- },
- "flavor",
- {
- data_type => "text",
- default_value => "good",
- },
+ "id", { data_type => "integer" },
+ "flavor", {
+ data_type => "text",
+ default_value => "good" },
);
__PACKAGE__->set_primary_key("id");
}
{
- package MyApp::Schema::Result::Sumatra;
+ package Cafe::Result::Sumatra;
- use parent 'Coffee';
+ use parent 'Cafe::Result::Coffee';
__PACKAGE__->table('sumatra');
- __PACKAGE__->add_columns(
- "aroma",
- {
- data_type => "text",
- default_value => undef,
- is_nullable => 0,
- },
+ __PACKAGE__->add_columns( "aroma",
+ { data_type => "text" }
);
1;
...
- my $schema = MyApp::Schema->connect($dsn);
-
- my $cup = $schema->resultset('Sumatra')->new;
+ my $schema = Cafe->connect($dsn,$user,$pass);
- print STDERR DwarnS $cup->columns;
+ my $cup = $schema->resultset('Sumatra');
- $VAR1 = 'id';
- $VAR2 = 'flavor';
- $VAR3 = 'aroma';
+ print STDERR Dwarn $cup->result_source->columns;
+ "id"
+ "flavor"
+ "aroma"
+ ..
Inherit from this package and you can make a resultset class from a view, but
that's more than a little bit misleading: the result is B<transparently
+++ /dev/null
-sub argify (@cols) {
- map $_->new(%$_, name => '_'.$_->name), @cols;
-}
-
-sub body_cols ($source) {
- grep $_->name ne 'id', $source->all_cols;
-}
-
-my @pk_col = ($table->col('id'));
-
-my @sources = grep defined, $table, $super_view;
-
-my @body_cols = map body_cols($_), @sources;
-
-CREATE VIEW $view_name =>
- SELECT {
- (map $_->qualify, @pk_col),
- @body_cols,
- } FROM {
- $super_view ? ($table->join($super_view)->using(@pk_col)) : $table
- };
-
-my ($now, $next) = grep defined, $super_view, $table;
-
-CREATE FUNCTION "${view_name}_insert" =>
- (argify @body_cols)
- => RETURNS VOID => AS {
- INSERT INTO { $now } (body_cols $now)
- => VALUES (argify body_cols $now);
- if ($next) {
- INSERT INTO { $next } ($next->all_cols)
- => VALUES {
- $root_table->col('id')->sequence->currval,
- argify body_cols $next
- };
- }
- };
-
-my $pk_eq = AND( map (expr { $_ == argify $_ }), @pk_col);
-
-CREATE FUNCTION "${view_name}_update" =>
- (argify @pk_col, @body_cols)
- => RETURNS VOID => AS {
- foreach my $s (@sources) {
- UPDATE { $s } SET { map ($_ => argify $_), body_cols $s }
- WHERE { $pk_eq };
- }
- };
-
-CREATE FUNCTION "${view_name}_delete" =>
- (argify @pk_col)
- => RETURNS VOID => AS {
- foreach my $s (@sources) {
- DELETE FROM { $s } WHERE { $pk_eq };
- }
- };
-
-