Commit | Line | Data |
4ee32f41 |
1 | use strict; |
2 | use warnings; |
3 | |
924d940e |
4 | use Test::More tests => 5; |
4ee32f41 |
5 | use Test::Differences; |
6 | |
7 | use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); |
8 | |
9 | my $sqla = SQL::Abstract->create(1); |
10 | |
e68f980b |
11 | my $foo_as_me = { |
12 | -type => 'alias', |
627dcb62 |
13 | ident => {-type => 'identifier', elements => [qw/foo/]}, |
e68f980b |
14 | as => 'me' |
15 | }; |
627dcb62 |
16 | my $me_foo_id = { -type => 'identifier', elements => [qw/me foo_id/] }; |
e68f980b |
17 | |
4ee32f41 |
18 | is $sqla->dispatch( |
19 | { -type => 'select', |
e68f980b |
20 | tablespec => $foo_as_me, |
747f7c21 |
21 | columns => [ |
627dcb62 |
22 | { -type => 'identifier', elements => [qw/me id/] }, |
e68f980b |
23 | { -type => 'alias', ident => $me_foo_id, as => 'foo' }, |
4ee32f41 |
24 | ] |
25 | } |
26 | ), "SELECT me.id, me.foo_id AS foo FROM foo AS me", |
27 | "simple select clause"; |
28 | |
29 | is $sqla->dispatch( |
30 | { -type => 'select', |
747f7c21 |
31 | columns => [ |
627dcb62 |
32 | { -type => 'identifier', elements => [qw/me id/] }, |
e68f980b |
33 | { -type => 'alias', ident => $me_foo_id, as => 'foo' }, |
627dcb62 |
34 | { -type => 'identifier', elements => [qw/bar name/] }, |
64c32031 |
35 | ], |
36 | tablespec => { |
37 | -type => 'join', |
e68f980b |
38 | lhs => $foo_as_me, |
627dcb62 |
39 | rhs => {-type => 'identifier', elements => [qw/bar/] }, |
64c32031 |
40 | on => { |
41 | -type => 'expr', |
42 | op => '==', |
43 | args => [ |
627dcb62 |
44 | {-type => 'identifier', elements => [qw/bar id/]}, |
45 | {-type => 'identifier', elements => [qw/me bar_id/]} |
64c32031 |
46 | ], |
47 | } |
48 | }, |
747f7c21 |
49 | } |
50 | |
51 | |
4ee32f41 |
52 | ), "SELECT me.id, me.foo_id AS foo, bar.name FROM foo AS me JOIN bar ON (bar.id = me.bar_id)", |
53 | "select with join clause"; |
54 | |
e68f980b |
55 | |
56 | is $sqla->dispatch( |
57 | { -type => 'select', |
58 | columns => [ |
627dcb62 |
59 | { -type => 'identifier', elements => [qw/me */] }, |
e68f980b |
60 | ], |
61 | tablespec => $foo_as_me, |
62 | where => { |
63 | -type => 'expr', |
64 | op => '==', |
65 | args => [ |
627dcb62 |
66 | {-type => 'identifier', elements => [qw/me id/]}, |
e68f980b |
67 | {-type => 'value', value => 1 }, |
68 | ] |
69 | } |
70 | } |
71 | |
72 | |
73 | ), "SELECT me.* FROM foo AS me WHERE me.id = ?", |
74 | "select with where"; |
924d940e |
75 | |
76 | |
77 | is $sqla->dispatch( |
78 | { -type => 'select', |
79 | tablespec => $foo_as_me, |
80 | columns => [ |
627dcb62 |
81 | { -type => 'identifier', elements => [qw/me id/] }, |
924d940e |
82 | { -type => 'alias', ident => $me_foo_id, as => 'foo' }, |
83 | ], |
84 | order_by => [ |
627dcb62 |
85 | { -type => 'ordering', expr => { -type => 'identifier', elements => [qw/me name/] }, direction => 'desc' }, |
924d940e |
86 | $me_foo_id, |
87 | ] |
88 | } |
89 | ), "SELECT me.id, me.foo_id AS foo FROM foo AS me ORDER BY me.name DESC, me.foo_id", |
90 | "select clause with order by"; |