Chage {-type => 'name', args => [] } to {-type => 'identifier', elements => [] }
[dbsrgits/SQL-Abstract-2.0-ish.git] / t / 201_select.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 5;
5 use Test::Differences;
6
7 use_ok('SQL::Abstract') or BAIL_OUT( "$@" );
8
9 my $sqla = SQL::Abstract->create(1);
10
11 my $foo_as_me = {
12   -type => 'alias', 
13   ident => {-type => 'identifier', elements => [qw/foo/]}, 
14   as => 'me'
15 };
16 my $me_foo_id = { -type => 'identifier', elements => [qw/me foo_id/] };
17
18 is $sqla->dispatch(
19   { -type => 'select',
20     tablespec => $foo_as_me,
21     columns => [
22       { -type => 'identifier', elements => [qw/me id/] },
23       { -type => 'alias', ident => $me_foo_id, as => 'foo' },
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',
31     columns => [
32       { -type => 'identifier', elements => [qw/me id/] },
33       { -type => 'alias', ident => $me_foo_id, as => 'foo' },
34       { -type => 'identifier', elements => [qw/bar name/] },
35     ],
36     tablespec => {
37       -type => 'join',
38       lhs => $foo_as_me,
39       rhs => {-type => 'identifier', elements => [qw/bar/] },
40       on => {
41         -type => 'expr',
42         op => '==',
43         args => [
44           {-type => 'identifier', elements => [qw/bar id/]}, 
45           {-type => 'identifier', elements => [qw/me bar_id/]}
46         ],
47       }
48     },
49   }
50
51
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
55
56 is $sqla->dispatch(
57   { -type => 'select',
58     columns => [
59       { -type => 'identifier', elements => [qw/me */] },
60     ],
61     tablespec => $foo_as_me,
62     where => {
63       -type => 'expr',
64       op => '==',
65       args => [
66         {-type => 'identifier', elements => [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";
75
76
77 is $sqla->dispatch(
78   { -type => 'select',
79     tablespec => $foo_as_me,
80     columns => [
81       { -type => 'identifier', elements => [qw/me id/] },
82       { -type => 'alias', ident => $me_foo_id, as => 'foo' },
83     ],
84     order_by => [
85       { -type => 'ordering', expr => { -type => 'identifier', elements => [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";