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