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