Use connect_by OR connect_by_nocycle
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLAHacks / Oracle.pm
1 package # Hide from PAUSE
2   DBIx::Class::SQLAHacks::Oracle;
3
4 use warnings;
5 use strict;
6
7 use base qw( DBIx::Class::SQLAHacks );
8 use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
9
10
11 #  TODO:
12 #   - Review by experienced DBIC/SQL:A developers :-)
13 #   - Problem with count and connect_by look the TODO in t/73oracle.t
14
15
16 sub new {
17   my $self = shift;
18   my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
19   push @{$opts{special_ops}}, {
20     regex => qr/^prior$/i,
21     handler => '_where_field_PRIOR',
22   };
23
24   $self->SUPER::new (\%opts);
25 }
26
27 sub select {
28     my ($self, $table, $fields, $where, $rs_attrs, @rest) = @_;
29
30     my ($sql, @bind) = $self->SUPER::select($table, $fields, $where, $rs_attrs, @rest);
31     push @bind, @{$self->{_oracle_connect_by_binds}};
32
33     return wantarray ? ($sql, @bind) : $sql;
34 }
35
36 sub _emulate_limit {
37     my ( $self, $syntax, $sql, $rs_attrs, $rows, $offset ) = @_;
38
39     my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
40     $sql .= $cb_sql;
41     $self->{_oracle_connect_by_binds} = \@cb_bind;
42
43     return $self->SUPER::_emulate_limit($syntax, $sql, $rs_attrs, $rows, $offset);
44 }
45
46 sub _connect_by {
47     my ($self, $attrs) = @_;
48
49     my $sql = '';
50     my @bind;
51
52     if ( ref($attrs) eq 'HASH' ) {
53         if ( $attrs->{'start_with'} ) {
54             my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
55             $sql .= $self->_sqlcase(' start with ') . $ws;
56             push @bind, @wb;
57         }
58         if ( my $connect_by = $attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'} ) {
59             my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by );
60             $sql .= sprintf(" %s %s",
61                 ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
62                     : $self->_sqlcase('connect by'),
63                 $connect_by_sql,
64             );
65             push @bind, @connect_by_sql_bind;
66         }
67         if ( $attrs->{'order_siblings_by'} ) {
68             $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
69         }
70     }
71
72     return wantarray ? ($sql, @bind) : $sql;
73 }
74
75 sub _order_siblings_by {
76     my ( $self, $arg ) = @_;
77
78     my ( @sql, @bind );
79     for my $c ( $self->_order_by_chunks($arg) ) {
80         $self->_SWITCH_refkind(
81             $c,
82             {
83                 SCALAR   => sub { push @sql, $c },
84                 ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c },
85             }
86         );
87     }
88
89     my $sql =
90       @sql
91       ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
92       : '';
93
94     return wantarray ? ( $sql, @bind ) : $sql;
95 }
96
97 # we need to add a '=' only when PRIOR is used against a column diretly
98 # i.e. when it is invoked by a special_op callback
99 sub _where_field_PRIOR {
100   my ($self, $lhs, $op, $rhs) = @_;
101   my ($sql, @bind) = $self->_recurse_where ($rhs);
102
103   $sql = sprintf ('%s = %s %s ',
104     $self->_convert($self->_quote($lhs)),
105     $self->_sqlcase ($op),
106     $sql
107   );
108
109   return ($sql, @bind);
110 }
111
112 1;
113
114 __END__
115
116 =pod
117
118 =head1 NAME
119
120 DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract
121
122 =head1 DESCRIPTION
123
124 See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more informations about
125 how to use hierarchical queries with DBIx::Class.
126
127 =cut