7 use DBIC::SqlMakerTest;
9 my $schema = DBICTest->init_schema;
11 # Trick the sqlite DB to use Top limit emulation
12 # We could test all of this via $sq->$op directly,
13 # but some conditions need a $rsrc
14 $schema->storage->_sql_maker->limit_dialect ('Top');
16 my $rs = $schema->resultset ('BooksInLibrary')->search ({}, { prefetch => 'owner', rows => 1, offset => 3 });
18 sub default_test_order {
21 $rs->search ({}, {order_by => $order_by})->as_query,
23 TOP 1 me__id, source, owner, title, price, owner__id, name FROM
25 TOP 4 me.id AS me__id, me.source, me.owner, me.title, me.price, owner.id AS owner__id, owner.name
31 ) me ORDER BY me__id DESC
33 [ [ source => 'Library' ] ],
40 my $req_order = $args->{order_req}
41 ? "ORDER BY $args->{order_req}"
46 $rs->search ({}, {order_by => $args->{order_by}})->as_query,
48 me__id, source, owner, title, price, owner__id, name FROM
50 TOP 1 me__id, source, owner, title, price, owner__id, name FROM
52 TOP 4 me.id AS me__id, me.source, me.owner, me.title, me.price, owner.id AS owner__id, owner.name FROM
54 JOIN owners owner ON owner.id = me.owner
56 ORDER BY $args->{order_inner}
57 ) me ORDER BY $args->{order_outer}
60 [ [ source => 'Library' ] ],
66 order_by => \'foo DESC',
67 order_req => 'foo DESC',
68 order_inner => 'foo DESC',
69 order_outer => 'foo ASC'
72 order_by => { -asc => 'foo' },
73 order_req => 'foo ASC',
74 order_inner => 'foo ASC',
75 order_outer => 'foo DESC',
80 order_inner => 'foo ASC',
81 order_outer => 'foo DESC',
84 order_by => [ qw{ foo bar} ],
85 order_req => 'foo, bar',
86 order_inner => 'foo ASC, bar ASC',
87 order_outer => 'foo DESC, bar DESC',
90 order_by => { -desc => 'foo' },
91 order_req => 'foo DESC',
92 order_inner => 'foo DESC',
93 order_outer => 'foo ASC',
96 order_by => ['foo', { -desc => 'bar' } ],
97 order_req => 'foo, bar DESC',
98 order_inner => 'foo ASC, bar DESC',
99 order_outer => 'foo DESC, bar ASC',
102 order_by => { -asc => [qw{ foo bar }] },
103 order_req => 'foo ASC, bar ASC',
104 order_inner => 'foo ASC, bar ASC',
105 order_outer => 'foo DESC, bar DESC',
110 { -desc => [qw{bar}] },
111 { -asc => [qw{hello sensors}]},
113 order_req => 'foo ASC, bar DESC, hello ASC, sensors ASC',
114 order_inner => 'foo ASC, bar DESC, hello ASC, sensors ASC',
115 order_outer => 'foo DESC, bar ASC, hello DESC, sensors DESC',
119 my @default_tests = ( undef, '', {}, [] );
121 test_order ($_) for @tests;
122 default_test_order ($_) for @default_tests;
126 $rs->search ({}, { group_by => 'title', order_by => 'title' })->as_query,
128 me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name FROM
130 id, source, owner, title, price FROM
132 TOP 1 id, source, owner, title, price FROM
134 TOP 4 me.id, me.source, me.owner, me.title, me.price FROM
136 owners owner ON owner.id = me.owner
145 owners owner ON owner.id = me.owner WHERE
148 [ [ source => 'Library' ], [ source => 'Library' ] ],
151 my $rs_selectas_top = $schema->resultset ('BooksInLibrary')->search ({}, {
152 '+select' => ['owner.name'],
153 '+as' => ['owner_name'],
158 is_same_sql_bind( $rs_selectas_top->search({})->as_query,
160 TOP 1 me.id, me.source, me.owner, me.title, me.price,
163 JOIN owners owner ON owner.id = me.owner
167 [ [ 'source', 'Library' ] ],