From: Peter Rabbitson Date: Mon, 25 Aug 2008 10:19:10 +0000 (-0400) Subject: Merge 'trunk' into 'view_support' X-Git-Tag: v0.08240~88^2~4^2~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ef2233538f2581237df7ba110a6f755ab3c34b4f;hp=555b3385868f4954e1faf0a01d4d691a07905250;p=dbsrgits%2FDBIx-Class.git Merge 'trunk' into 'view_support' r21901@martha (orig r4767): ribasushi | 2008-08-25 05:19:10 -0400 Force on_delete/on_update arguments to upper case for consistency with the rest of SQLT --- diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index a026339..1734d52 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -71,6 +71,18 @@ and don't actually accomplish anything on their own: "_engine" => 'InnoDB', }); +=cut + +sub is_virtual { 0 } + +=pod + +=head2 is_virtual + +Returns true if the resultsource is a virtual result source. This stub +method returns false by default, see L +for more information. + =head2 add_columns $table->add_columns(qw/col1 col2 col3/); diff --git a/lib/DBIx/Class/ResultSource/View.pm b/lib/DBIx/Class/ResultSource/View.pm new file mode 100644 index 0000000..7f6bec7 --- /dev/null +++ b/lib/DBIx/Class/ResultSource/View.pm @@ -0,0 +1,58 @@ +package DBIx::Class::ResultSource::View; + +use strict; +use warnings; + +use DBIx::Class::ResultSet; + +use base qw/DBIx::Class/; +__PACKAGE__->load_components(qw/ResultSource/); +__PACKAGE__->mk_group_accessors( + 'simple' => qw(is_virtual view_definition) +); + +=head1 NAME + +DBIx::Class::ResultSource::Table - Table object + +=head1 SYNOPSIS + +=head1 DESCRIPTION + +Table object that inherits from L + +=head1 METHODS + +=head2 is_virtual + +Attribute to declare a view as virtual. + +=head2 from + +Returns the FROM entry for the table (i.e. the view name) +or the definition if this is a virtual view. + +=cut + +sub from { + my $self = shift; + return \"(${\$self->view_definition})" if $self->is_virtual; + return $self->name; +} + +1; + +=head1 AUTHORS + +Matt S. Trout + +With Contributions from: + +Guillermo Roditi Egroditi@cpan.orgE + +=head1 LICENSE + +You may distribute this code under the same terms as Perl itself. + +=cut + diff --git a/lib/SQL/Translator/Parser/DBIx/Class.pm b/lib/SQL/Translator/Parser/DBIx/Class.pm index d4499aa..3e915bc 100644 --- a/lib/SQL/Translator/Parser/DBIx/Class.pm +++ b/lib/SQL/Translator/Parser/DBIx/Class.pm @@ -65,8 +65,18 @@ sub parse { @monikers = grep { $sources->{$_} } @monikers; } + my(@table_monikers, @view_monikers); + for my $moniker (@monikers){ + my $source = $dbicschema->source($moniker); + next if $source->is_virtual; + if ( $source->isa('DBIx::Class::ResultSource::Table') ) { + push(@table_monikers, $moniker); + } elsif( $source->isa('DBIx::Class::ResultSource::View') ){ + push(@view_monikers, $moniker); + } + } - foreach my $moniker (sort @monikers) + foreach my $moniker (sort @table_monikers) { my $source = $dbicschema->source($moniker); @@ -219,6 +229,26 @@ sub parse { } } + foreach my $moniker (sort @view_monikers) + { + my $source = $dbicschema->source($moniker); + # Skip custom query sources + next if ref($source->name); + + # Its possible to have multiple DBIC source using same table + next if $seen_tables{$source->name}++; + + my $view = $schema->add_view( + name => $source->name, + fields => [ $source->columns ], + ($source->view_definition ? $source->view_definition : ()) + ); + if ($source->result_class->can('sqlt_deploy_hook')) { + $source->result_class->sqlt_deploy_hook($view); + } + } + + if ($dbicschema->can('sqlt_deploy_hook')) { $dbicschema->sqlt_deploy_hook($schema); } diff --git a/t/99dbic_sqlt_parser.t b/t/99dbic_sqlt_parser.t index 34547db..42eeb92 100644 --- a/t/99dbic_sqlt_parser.t +++ b/t/99dbic_sqlt_parser.t @@ -9,7 +9,7 @@ BEGIN { eval "use DBD::mysql; use SQL::Translator 0.09;"; plan $@ ? ( skip_all => 'needs SQL::Translator 0.09 for testing' ) - : ( tests => 99 ); + : ( tests => 102 ); } my $schema = DBICTest->init_schema(); diff --git a/t/lib/DBICTest/Schema.pm b/t/lib/DBICTest/Schema.pm index 22eddff..857c18c 100644 --- a/t/lib/DBICTest/Schema.pm +++ b/t/lib/DBICTest/Schema.pm @@ -16,6 +16,7 @@ __PACKAGE__->load_classes(qw/ #dummy Track Tag + Year2000CDs /, { 'DBICTest::Schema' => [qw/ LinerNotes diff --git a/t/lib/DBICTest/Schema/Year2000CDs.pm b/t/lib/DBICTest/Schema/Year2000CDs.pm new file mode 100644 index 0000000..2a2c85b --- /dev/null +++ b/t/lib/DBICTest/Schema/Year2000CDs.pm @@ -0,0 +1,30 @@ +package # hide from PAUSE + DBICTest::Schema::Year2000CDs; + +use base 'DBIx::Class::Core'; +use DBIx::Class::ResultSource::View; + +__PACKAGE__->table_class('DBIx::Class::ResultSource::View'); + +__PACKAGE__->table('cd'); +__PACKAGE__->result_source_instance->view_definition( + "SELECT cdid, artist, title FROM cd WHERE year ='2000'" +); +__PACKAGE__->add_columns( + 'cdid' => { + data_type => 'integer', + is_auto_increment => 1, + }, + 'artist' => { + data_type => 'integer', + }, + 'title' => { + data_type => 'varchar', + size => 100, + }, + +); +__PACKAGE__->set_primary_key('cdid'); +__PACKAGE__->add_unique_constraint([ qw/artist title/ ]); + +1;