7 use DBIC::SqlMakerTest;
9 my $schema = DBICTest->init_schema;
10 $schema->storage_type('::DBI::MSSQL');
12 # Trick the sqlite DB to use Top limit emulation
13 # We could test all of this via $sq->$op directly,
14 # but some conditions need a $rsrc
15 delete $schema->storage->_sql_maker->{_cached_syntax};
16 $schema->storage->_sql_maker->limit_dialect ('RowNumberOver');
18 my $rs = $schema->resultset ('BooksInLibrary')->search ({}, { prefetch => 'owner', rows => 1, offset => 3 });
20 sub default_test_order {
23 $rs->search ({}, {order_by => $order_by})->as_query,
24 "(SELECT me__id, me__source, me__owner, me__title, me__price, owner__id, owner__name FROM
26 SELECT Q1.*, ROW_NUMBER() OVER( ) AS ROW_NUM FROM (
27 SELECT me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name
28 FROM books me JOIN owners owner ON owner.id = me.owner WHERE ( source = ? )
31 WHERE ROW_NUM BETWEEN 4 and 4
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,
47 "(SELECT me__id, me__source, me__owner, me__title, me__price, owner__id, owner__name FROM
49 SELECT Q1.*, ROW_NUMBER() OVER( ) AS ROW_NUM FROM (
50 SELECT me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name
51 FROM books me JOIN owners owner ON owner.id = me.owner WHERE ( source = ? )
55 WHERE ROW_NUM BETWEEN 4 and 4
57 [ [ source => 'Library' ] ],
63 order_by => \'foo DESC',
64 order_req => 'foo DESC',
65 order_inner => 'foo DESC',
66 order_outer => 'foo ASC'
69 order_by => { -asc => 'foo' },
70 order_req => 'foo ASC',
71 order_inner => 'foo ASC',
72 order_outer => 'foo DESC',
77 order_inner => 'foo ASC',
78 order_outer => 'foo DESC',
81 order_by => [ qw{ foo bar} ],
82 order_req => 'foo, bar',
83 order_inner => 'foo ASC,bar ASC',
84 order_outer => 'foo DESC, bar DESC',
87 order_by => { -desc => 'foo' },
88 order_req => 'foo DESC',
89 order_inner => 'foo DESC',
90 order_outer => 'foo ASC',
93 order_by => ['foo', { -desc => 'bar' } ],
94 order_req => 'foo, bar DESC',
95 order_inner => 'foo ASC, bar DESC',
96 order_outer => 'foo DESC, bar ASC',
99 order_by => { -asc => [qw{ foo bar }] },
100 order_req => 'foo ASC, bar ASC',
101 order_inner => 'foo ASC, bar ASC',
102 order_outer => 'foo DESC, bar DESC',
107 { -desc => [qw{bar}] },
108 { -asc => [qw{hello sensors}]},
110 order_req => 'foo ASC, bar DESC, hello ASC, sensors ASC',
111 order_inner => 'foo ASC, bar DESC, hello ASC, sensors ASC',
112 order_outer => 'foo DESC, bar ASC, hello DESC, sensors DESC',
116 my @default_tests = ( undef, '', {}, [] );
118 plan (tests => scalar @tests + scalar @default_tests + 1);
120 test_order ($_) for @tests;
121 default_test_order ($_) for @default_tests;
125 $rs->search ({}, { group_by => 'title', order_by => 'title' })->as_query,
127 me.id, me.source, me.owner, me.title, me.price, owner.id, owner.name FROM
129 id, source, owner, title, price FROM
131 TOP 1 id, source, owner, title, price FROM
133 TOP 4 me.id, me.source, me.owner, me.title, me.price FROM
135 owners owner ON owner.id = me.owner
144 owners owner ON owner.id = me.owner WHERE
147 [ [ source => 'Library' ], [ source => 'Library' ] ],