A little cleanup of ->id callsites (incomplete)
[dbsrgits/DBIx-Class.git] / t / lib / DBICTest / Schema / CD.pm
1 package # hide from PAUSE
2     DBICTest::Schema::CD;
3
4 use warnings;
5 use strict;
6
7 use base 'DBICTest::BaseResult';
8 use DBICTest::Util 'check_customcond_args';
9
10 # this tests table name as scalar ref
11 # DO NOT REMOVE THE \
12 __PACKAGE__->table(\'cd');
13
14 __PACKAGE__->add_columns(
15   'cdid' => {
16     data_type => 'integer',
17     is_auto_increment => 1,
18   },
19   'artist' => {
20     data_type => 'integer',
21   },
22   'title' => {
23     data_type => 'varchar',
24     size      => 100,
25   },
26   'year' => {
27     data_type => 'varchar',
28     size      => 100,
29   },
30   'genreid' => {
31     data_type => 'integer',
32     is_nullable => 1,
33     accessor => undef,
34   },
35   'single_track' => {
36     data_type => 'integer',
37     is_nullable => 1,
38     is_foreign_key => 1,
39   }
40 );
41 __PACKAGE__->set_primary_key('cdid');
42 __PACKAGE__->add_unique_constraint([ qw/artist title/ ]);
43
44 __PACKAGE__->belongs_to( artist => 'DBICTest::Schema::Artist', undef, {
45     is_deferrable => 1,
46     proxy => { artist_name => 'name' },
47 });
48 __PACKAGE__->belongs_to( very_long_artist_relationship => 'DBICTest::Schema::Artist', 'artist', {
49     is_deferrable => 1,
50 });
51
52 # in case this is a single-cd it promotes a track from another cd
53 __PACKAGE__->belongs_to( single_track => 'DBICTest::Schema::Track',
54   { 'foreign.trackid' => 'self.single_track' },
55   { join_type => 'left'},
56 );
57
58 # add a non-left single relationship for the complex prefetch tests
59 __PACKAGE__->belongs_to( existing_single_track => 'DBICTest::Schema::Track',
60   { 'foreign.trackid' => 'self.single_track' },
61 );
62
63 __PACKAGE__->has_many( tracks => 'DBICTest::Schema::Track' );
64 __PACKAGE__->has_many(
65     tags => 'DBICTest::Schema::Tag', undef,
66     { order_by => 'tag' },
67 );
68 __PACKAGE__->has_many(
69     cd_to_producer => 'DBICTest::Schema::CD_to_Producer' => 'cd'
70 );
71
72 __PACKAGE__->has_many( twokeys => 'DBICTest::Schema::TwoKeys', 'cd' );
73
74
75 # the undef condition in this rel is *deliberate*
76 # tests oddball legacy syntax
77 __PACKAGE__->might_have(
78     liner_notes => 'DBICTest::Schema::LinerNotes', undef,
79     { proxy => [ qw/notes/ ] },
80 );
81 __PACKAGE__->might_have(artwork => 'DBICTest::Schema::Artwork', 'cd_id');
82 __PACKAGE__->has_one(mandatory_artwork => 'DBICTest::Schema::Artwork', 'cd_id');
83
84 __PACKAGE__->many_to_many( producers => cd_to_producer => 'producer' );
85 __PACKAGE__->many_to_many(
86     producers_sorted => cd_to_producer => 'producer',
87     { order_by => 'producer.name' },
88 );
89
90 __PACKAGE__->belongs_to('genre', 'DBICTest::Schema::Genre',
91     'genreid',
92     {
93         join_type => 'left',
94         on_delete => 'SET NULL',
95         on_update => 'CASCADE',
96     },
97 );
98
99 #This second relationship was added to test the short-circuiting of pointless
100 #queries provided by undef_on_null_fk. the relevant test in 66relationship.t
101 __PACKAGE__->belongs_to('genre_inefficient', 'DBICTest::Schema::Genre',
102     { 'foreign.genreid' => 'self.genreid' },
103     {
104         join_type => 'left',
105         on_delete => 'SET NULL',
106         on_update => 'CASCADE',
107         undef_on_null_fk => 0,
108     },
109 );
110
111
112 # This is insane. Don't ever do anything like that
113 # This is for testing purposes only!
114
115 # mst: mo: DBIC is an "object relational mapper"
116 # mst: mo: not an "object relational hider-because-mo-doesn't-understand-databases
117 # ribasushi: mo: try it with a subselect nevertheless, I'd love to be proven wrong
118 # ribasushi: mo: does sqlite actually take this?
119 # ribasushi: an order in a correlated subquery is insane - how long does it take you on real data?
120
121 __PACKAGE__->might_have(
122     'last_track',
123     'DBICTest::Schema::Track',
124     sub {
125         # This is for test purposes only. A regular user does not
126         # need to sanity check the passed-in arguments, this is what
127         # the tests are for :)
128         my $args = &check_customcond_args;
129
130         return (
131             {
132                 "$args->{foreign_alias}.trackid" => { '=' =>
133                     $args->{self_resultsource}->schema->resultset('Track')->search(
134                        { 'correlated_tracks.cd' => { -ident => "$args->{self_alias}.cdid" } },
135                        {
136                           order_by => { -desc => 'position' },
137                           rows     => 1,
138                           alias    => 'correlated_tracks',
139                           columns  => ['trackid']
140                        },
141                     )->as_query
142                 }
143             }
144         );
145     },
146 );
147
148 1;