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