ORDER BY, HAVING, GROUP BY and WHERE clauses on select
[dbsrgits/SQL-Abstract-2.0-ish.git] / t / 201_select.t
CommitLineData
4ee32f41 1use strict;
2use warnings;
3
924d940e 4use Test::More tests => 5;
4ee32f41 5use Test::Differences;
6
7use_ok('SQL::Abstract') or BAIL_OUT( "$@" );
8
9my $sqla = SQL::Abstract->create(1);
10
e68f980b 11my $foo_as_me = {
12 -type => 'alias',
13 ident => {-type => 'name', args => [qw/foo/]},
14 as => 'me'
15};
16my $me_foo_id = { -type => 'name', args => [qw/me foo_id/] };
17
4ee32f41 18is $sqla->dispatch(
19 { -type => 'select',
e68f980b 20 tablespec => $foo_as_me,
747f7c21 21 columns => [
22 { -type => 'name', args => [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
29is $sqla->dispatch(
30 { -type => 'select',
747f7c21 31 columns => [
32 { -type => 'name', args => [qw/me id/] },
e68f980b 33 { -type => 'alias', ident => $me_foo_id, as => 'foo' },
747f7c21 34 { -type => 'name', args => [qw/bar name/] },
64c32031 35 ],
36 tablespec => {
37 -type => 'join',
e68f980b 38 lhs => $foo_as_me,
f7dc4536 39 rhs => {-type => 'name', args => [qw/bar/] },
64c32031 40 on => {
41 -type => 'expr',
42 op => '==',
43 args => [
44 {-type => 'name', args => [qw/bar id/]},
45 {-type => 'name', args => [qw/me bar_id/]}
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
56is $sqla->dispatch(
57 { -type => 'select',
58 columns => [
59 { -type => 'name', args => [qw/me */] },
60 ],
61 tablespec => $foo_as_me,
62 where => {
63 -type => 'expr',
64 op => '==',
65 args => [
66 {-type => 'name', args => [qw/me id/]},
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
77is $sqla->dispatch(
78 { -type => 'select',
79 tablespec => $foo_as_me,
80 columns => [
81 { -type => 'name', args => [qw/me id/] },
82 { -type => 'alias', ident => $me_foo_id, as => 'foo' },
83 ],
84 order_by => [
85 { -type => 'ordering', expr => { -type => 'name', args => [qw/me name/] }, direction => 'desc' },
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";