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