From: Amiri Barksdale at Home Date: Wed, 2 Jun 2010 21:57:45 +0000 (-0700) Subject: Extend Cafe tests. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f88641341b1074d549842f94c7e3122c902de1df;hp=3c9baea8904d3c68732aef37cb9d4962674fa420;p=dbsrgits%2FDBIx-Class-ResultSource-MultipleTableInheritance.git Extend Cafe tests. --- diff --git a/.gitignore b/.gitignore index b701be6..6193840 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ Makefile pm_to_blib *.sw? *.tmp +src diff --git a/lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm b/lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm index d492490..6e948f7 100644 --- a/lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm +++ b/lib/DBIx/Class/ResultSource/MultipleTableInheritance.pm @@ -276,7 +276,7 @@ BEGIN { *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})}", @@ -368,7 +368,7 @@ method view_definition () { # 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, [ @@ -396,7 +396,7 @@ method view_definition () { # UPDATE function my $update_func = - function_body + functionbody $self->name.'_update', [ @pk_cols, @body_cols ], [ map $sqla->update( @@ -409,7 +409,7 @@ method view_definition () { # DELETE function my $delete_func = - function_body + functionbody $self->name.'_delete', [ @pk_cols ], [ map $sqla->delete($_->name, $pk_where), @sources ]; @@ -438,27 +438,21 @@ This only works with PostgreSQL for the moment. =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"); @@ -467,19 +461,14 @@ This only works with PostgreSQL for the moment. } { - 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; @@ -487,16 +476,16 @@ This only works with PostgreSQL for the moment. ... - 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 Bcreate(1); - -warn $sqla->dispatch( - { -type => 'list', args => [ - mk_name('foo') - ] }); diff --git a/t/sql/MTITest-0.1-PostgreSQL.sql b/t/sql/MTITest-0.1-PostgreSQL.sql index cd2386e..28ae17a 100644 --- a/t/sql/MTITest-0.1-PostgreSQL.sql +++ b/t/sql/MTITest-0.1-PostgreSQL.sql @@ -1,6 +1,6 @@ -- -- Created by SQL::Translator::Producer::PostgreSQL --- Created on Tue Jun 1 18:40:12 2010 +-- Created on Tue Jun 1 20:00:58 2010 -- -- -- Table: just_a_table diff --git a/vdef b/vdef deleted file mode 100644 index b097bcc..0000000 --- a/vdef +++ /dev/null @@ -1,58 +0,0 @@ -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 }; - } - }; - -