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