Stop mangling sql on the way out of the limit dialects
[dbsrgits/DBIx-Class.git] / t / sqlmaker / limit_dialects / rownum.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5
6 use lib qw(t/lib);
7 use DBICTest;
8 use DBIC::SqlMakerTest;
9
10 my $s = DBICTest->init_schema (no_deploy => 1, );
11 $s->storage->sql_maker->limit_dialect ('RowNum');
12
13 my $rs = $s->resultset ('CD');
14
15 is_same_sql_bind (
16   $rs->search ({}, { rows => 1, offset => 3,columns => [
17       { id => 'foo.id' },
18       { 'bar.id' => 'bar.id' },
19       { bleh => \ 'TO_CHAR (foo.womble, "blah")' },
20     ]})->as_query,
21   '(
22     SELECT id, bar__id, bleh
23       FROM (
24         SELECT id, bar__id, bleh, ROWNUM rownum__index
25           FROM (
26             SELECT foo.id AS id, bar.id AS bar__id, TO_CHAR(foo.womble, "blah") AS bleh
27               FROM cd me
28           ) me
29         WHERE ROWNUM <= 4
30       ) me
31     WHERE rownum__index >= 4
32   )',
33   [],
34   'Rownum subsel aliasing works correctly'
35 );
36
37 is_same_sql_bind (
38   $rs->search ({}, { rows => 2, offset => 3,columns => [
39       { id => 'foo.id' },
40       { 'ends_with_me.id' => 'ends_with_me.id' },
41     ]})->as_query,
42   '(SELECT id, ends_with_me__id
43       FROM (
44         SELECT id, ends_with_me__id, ROWNUM rownum__index
45           FROM (
46             SELECT foo.id AS id, ends_with_me.id AS ends_with_me__id
47               FROM cd me
48           ) me
49         WHERE ROWNUM <= 5
50       ) me
51     WHERE rownum__index >= 4
52   )',
53   [],
54   'Rownum subsel aliasing works correctly'
55 );
56
57 {
58   $rs = $s->resultset('Artist')->search({}, {
59     columns => 'name',
60     offset => 1,
61     order_by => 'name',
62   });
63   local $rs->result_source->{name} = "weird \n newline/multi \t \t space containing \n table";
64
65   like (
66     ${$rs->as_query}->[0],
67     qr| weird \s \n \s newline/multi \s \t \s \t \s space \s containing \s \n \s table|x,
68     'Newlines/spaces preserved in final sql',
69   );
70 }
71
72
73 done_testing;