Merge branch 'current/for_cpan_index' into current/dq
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest / Schema / Artist.pm
1 package # hide from PAUSE
2     DBICTest::Schema::Artist;
3
4 use warnings;
5 use strict;
6
7 use base qw/DBICTest::BaseResult/;
8 use Carp qw/confess/;
9 use Data::Query::ExprDeclare;
10
11 __PACKAGE__->table('artist');
12 __PACKAGE__->source_info({
13     "source_info_key_A" => "source_info_value_A",
14     "source_info_key_B" => "source_info_value_B",
15     "source_info_key_C" => "source_info_value_C",
16 });
17 __PACKAGE__->add_columns(
18   'artistid' => {
19     data_type => 'integer',
20     is_auto_increment => 1,
21   },
22   'name' => {
23     data_type => 'varchar',
24     size      => 100,
25     is_nullable => 1,
26   },
27   rank => {
28     data_type => 'integer',
29     default_value => 13,
30   },
31   charfield => {
32     data_type => 'char',
33     size => 10,
34     is_nullable => 1,
35   },
36 );
37 __PACKAGE__->set_primary_key('artistid');
38 __PACKAGE__->add_unique_constraint(['name']);
39 __PACKAGE__->add_unique_constraint(artist => ['artistid']); # do not remove, part of a test
40 __PACKAGE__->add_unique_constraint(u_nullable => [qw/charfield rank/]);
41
42
43 __PACKAGE__->mk_classdata('field_name_for', {
44     artistid    => 'primary key',
45     name        => 'artist name',
46 });
47
48 # the undef condition in this rel is *deliberate*
49 # tests oddball legacy syntax
50 __PACKAGE__->has_many(
51     cds => 'DBICTest::Schema::CD',
52     expr { $_->foreign->artist == $_->self->artistid },
53     { order_by => { -asc => 'year'} },
54 );
55
56
57 __PACKAGE__->has_many(
58   cds_80s => 'DBICTest::Schema::CD',
59   sub {
60     my $args = shift;
61
62     # This is for test purposes only. A regular user does not
63     # need to sanity check the passed-in arguments, this is what
64     # the tests are for :)
65     my @missing_args = grep { ! defined $args->{$_} }
66       qw/self_alias foreign_alias self_resultsource foreign_relname/;
67     confess "Required arguments not supplied to custom rel coderef: @missing_args\n"
68       if @missing_args;
69
70     return (
71       { "$args->{foreign_alias}.artist" => { '=' => { -ident => "$args->{self_alias}.artistid"} },
72         "$args->{foreign_alias}.year"   => { '>' => 1979, '<' => 1990 },
73       },
74       $args->{self_rowobj} && {
75         "$args->{foreign_alias}.artist" => $args->{self_rowobj}->artistid,
76         "$args->{foreign_alias}.year"   => { '>' => 1979, '<' => 1990 },
77       }
78     );
79   },
80 );
81
82
83 __PACKAGE__->has_many(
84   cds_84 => 'DBICTest::Schema::CD',
85   sub {
86     my $args = shift;
87
88     # This is for test purposes only. A regular user does not
89     # need to sanity check the passed-in arguments, this is what
90     # the tests are for :)
91     my @missing_args = grep { ! defined $args->{$_} }
92       qw/self_alias foreign_alias self_resultsource foreign_relname/;
93     confess "Required arguments not supplied to custom rel coderef: @missing_args\n"
94       if @missing_args;
95
96     return (
97       { "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" },
98         "$args->{foreign_alias}.year"   => 1984,
99       },
100       $args->{self_rowobj} && {
101         "$args->{foreign_alias}.artist" => $args->{self_rowobj}->artistid,
102         "$args->{foreign_alias}.year"   => 1984,
103       }
104     );
105   }
106 );
107
108
109 __PACKAGE__->has_many(
110   cds_90s => 'DBICTest::Schema::CD',
111   sub {
112     my $args = shift;
113
114     # This is for test purposes only. A regular user does not
115     # need to sanity check the passed-in arguments, this is what
116     # the tests are for :)
117     my @missing_args = grep { ! defined $args->{$_} }
118       qw/self_alias foreign_alias self_resultsource foreign_relname/;
119     confess "Required arguments not supplied to custom rel coderef: @missing_args\n"
120       if @missing_args;
121
122     return (
123       { "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" },
124         "$args->{foreign_alias}.year"   => { '>' => 1989, '<' => 2000 },
125       }
126     );
127   }
128 );
129
130
131 __PACKAGE__->has_many(
132     cds_unordered => 'DBICTest::Schema::CD'
133 );
134 __PACKAGE__->has_many(
135     cds_very_very_very_long_relationship_name => 'DBICTest::Schema::CD'
136 );
137
138 __PACKAGE__->has_many( twokeys => 'DBICTest::Schema::TwoKeys' );
139 __PACKAGE__->has_many( onekeys => 'DBICTest::Schema::OneKey' );
140
141 __PACKAGE__->has_many(
142   artist_undirected_maps => 'DBICTest::Schema::ArtistUndirectedMap',
143   [ {'foreign.id1' => 'self.artistid'}, {'foreign.id2' => 'self.artistid'} ],
144   { cascade_copy => 0 } # this would *so* not make sense
145 );
146
147 __PACKAGE__->has_many(
148     artwork_to_artist => 'DBICTest::Schema::Artwork_to_Artist' => 'artist_id'
149 );
150 __PACKAGE__->many_to_many('artworks', 'artwork_to_artist', 'artwork');
151
152 __PACKAGE__->has_many(
153     cds_without_genre => 'DBICTest::Schema::CD',
154     sub {
155         my $args = shift;
156         return (
157           {
158             "$args->{foreign_alias}.artist" => { -ident => "$args->{self_alias}.artistid" },
159             "$args->{foreign_alias}.genreid" => undef,
160           }, $args->{self_rowobj} && {
161             "$args->{foreign_alias}.artist" => $args->{self_rowobj}->artistid,
162             "$args->{foreign_alias}.genreid" => undef,
163           }
164         ),
165     },
166 );
167
168 sub sqlt_deploy_hook {
169   my ($self, $sqlt_table) = @_;
170
171   if ($sqlt_table->schema->translator->producer_type =~ /SQLite$/ ) {
172     $sqlt_table->add_index( name => 'artist_name_hookidx', fields => ['name'] )
173       or die $sqlt_table->error;
174   }
175 }
176
177 sub store_column {
178   my ($self, $name, $value) = @_;
179   $value = 'X '.$value if ($name eq 'name' && $value && $value =~ /(X )?store_column test/);
180   $self->next::method($name, $value);
181 }
182
183
184 1;