Remove todo: 'Check the parameter syntax of connect_by' fixed with new SQLA release
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLAHacks / Oracle.pm
1 package # Hide from PAUSE
2   DBIx::Class::SQLAHacks::Oracle;
3
4 use base qw( DBIx::Class::SQLAHacks );
5 use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
6
7
8 #  TODO:
9 #   - Review by experienced DBIC/SQL:A developers :-)
10 #   - Problem with count and connect_by look the TODO in t/73oracle.t
11
12
13 sub select {
14     my ($self, $table, $fields, $where, $order, @rest) = @_;
15
16     $self->{_db_specific_attrs} = pop @rest;
17
18     my ($sql, @bind) = $self->SUPER::select($table, $fields, $where, $order, @rest);
19     push @bind, @{$self->{_oracle_connect_by_binds}};
20
21     return wantarray ? ($sql, @bind) : $sql;
22 }
23
24 sub _emulate_limit {
25     my ( $self, $syntax, $sql, $order, $rows, $offset ) = @_;
26
27     my ($cb_sql, @cb_bind) = $self->_connect_by();
28     $sql .= $cb_sql;
29     $self->{_oracle_connect_by_binds} = \@cb_bind;
30
31     return $self->SUPER::_emulate_limit($syntax, $sql, $order, $rows, $offset);
32 }
33
34 sub _connect_by {
35     my ($self) = @_;
36     my $attrs = $self->{_db_specific_attrs};
37     my $sql = '';
38     my @bind;
39
40     if ( ref($attrs) eq 'HASH' ) {
41         if ( $attrs->{'start_with'} ) {
42             my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
43             $sql .= $self->_sqlcase(' start with ') . $ws;
44             push @bind, @wb;
45         }
46         if ( my $connect_by = $attrs->{'connect_by'} ) {
47             my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $attrs->{'connect_by'} );
48             $sql .= sprintf(" %s %s",
49                 ( $attrs->{'nocycle'} ) ? $self->_sqlcase('connect by nocycle')
50                     : $self->_sqlcase('connect by'),
51                 $connect_by_sql,
52             );
53             push @bind, @connect_by_sql_bind;
54             # $sql .= $self->_sqlcase(' connect by');
55             #             foreach my $key ( keys %$connect_by ) {
56             #                 $sql .= " $key = " . $connect_by->{$key};
57             #             }
58         }
59         if ( $attrs->{'order_siblings_by'} ) {
60             $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
61         }
62     }
63
64     return wantarray ? ($sql, @bind) : $sql;
65 }
66
67 sub _order_siblings_by {
68     my ( $self, $arg ) = @_;
69
70     my ( @sql, @bind );
71     for my $c ( $self->_order_by_chunks($arg) ) {
72         $self->_SWITCH_refkind(
73             $c,
74             {
75                 SCALAR   => sub { push @sql, $c },
76                 ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c },
77             }
78         );
79     }
80
81     my $sql =
82       @sql
83       ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
84       : '';
85
86     return wantarray ? ( $sql, @bind ) : $sql;
87 }
88
89 1;
90
91 __END__
92
93 =pod
94
95 =head1 NAME
96
97 DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract
98
99 =head1 DESCRIPTION
100
101 See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more informations about
102 how to use hierarchical queries with DBIx::Class.
103
104 =cut