File: distinct.t

package info (click to toggle)
libdbix-class-perl 0.082843-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,320 kB
  • sloc: perl: 27,215; sql: 322; sh: 29; makefile: 16
file content (114 lines) | stat: -rw-r--r-- 2,595 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use strict;
use warnings;

use Test::More;

use lib qw(t/lib);
use DBICTest ':DiffSQL';

my $schema = DBICTest->init_schema();

# make sure order + distinct do not double-inject group criteria
my $rs = $schema->resultset ('CD')->search ({}, {
  distinct => 1,
  columns => 'title',
});

# title + cdid == unique constraint
my $unique_rs = $rs->search ({}, {
  '+columns' => 'cdid',
});

is_same_sql_bind (
  $rs->search({}, { order_by => 'title' })->as_query,
  '(
    SELECT me.title
      FROM cd me
    GROUP BY me.title
    ORDER BY title
  )',
  [],
  'Correct GROUP BY on selection+order_by on same column',
);

is_same_sql_bind (
  $rs->search({}, { order_by => 'year' })->as_query,
  '(
    SELECT me.title
      FROM cd me
    GROUP BY me.title
    ORDER BY MIN(year)
  )',
  [],
  'Correct GROUP BY on non-unique selection and order by a different column',
);

is_same_sql_bind (
  $unique_rs->search({}, { order_by => 'year' })->as_query,
  '(
    SELECT me.title, me.cdid
      FROM cd me
    GROUP BY me.title, me.cdid, me.year
    ORDER BY year
  )',
  [],
  'Correct GROUP BY on unique selection and order by a different column',
);

is_same_sql_bind (
  $rs->search({}, { order_by => 'artist.name', join => 'artist'  })->as_query,
  '(
    SELECT me.title
      FROM cd me
      JOIN artist artist
        ON artist.artistid = me.artist
    GROUP BY me.title
    ORDER BY MIN(artist.name)
  )',
  [],
  'Correct GROUP BY on non-unique selection and external single order_by',
);

is_same_sql_bind (
  $unique_rs->search({}, { order_by => 'artist.name', join => 'artist'  })->as_query,
  '(
    SELECT me.title, me.cdid
      FROM cd me
      JOIN artist artist
        ON artist.artistid = me.artist
    GROUP BY me.title, me.cdid, artist.name
    ORDER BY artist.name
  )',
  [],
  'Correct GROUP BY on unique selection and external single order_by',
);

is_same_sql_bind (
  $rs->search({}, { order_by => 'tracks.title', join => 'tracks'  })->as_query,
  '(
    SELECT me.title
      FROM cd me
      LEFT JOIN track tracks
        ON tracks.cd = me.cdid
    GROUP BY me.title
    ORDER BY MIN(tracks.title)
  )',
  [],
  'Correct GROUP BY on non-unique selection and external multi order_by',
);

is_same_sql_bind (
  $unique_rs->search({}, { order_by => 'tracks.title', join => 'tracks'  })->as_query,
  '(
    SELECT me.title, me.cdid
      FROM cd me
      LEFT JOIN track tracks
        ON tracks.cd = me.cdid
    GROUP BY me.title, me.cdid
    ORDER BY MIN(tracks.title)
  )',
  [],
  'Correct GROUP BY on unique selection and external multi order_by',
);

done_testing;