Fix top-level PRIOR with missing '='
[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, $order, @rest) = @_;
29
30     $self->{_db_specific_attrs} = pop @rest;
31
32     my ($sql, @bind) = $self->SUPER::select($table, $fields, $where, $order, @rest);
33     push @bind, @{$self->{_oracle_connect_by_binds}};
34
35     return wantarray ? ($sql, @bind) : $sql;
36 }
37
38 sub _emulate_limit {
39     my ( $self, $syntax, $sql, $order, $rows, $offset ) = @_;
40
41     my ($cb_sql, @cb_bind) = $self->_connect_by();
42     $sql .= $cb_sql;
43     $self->{_oracle_connect_by_binds} = \@cb_bind;
44
45     return $self->SUPER::_emulate_limit($syntax, $sql, $order, $rows, $offset);
46 }
47
48 sub _connect_by {
49     my ($self) = @_;
50     my $attrs = $self->{_db_specific_attrs};
51     my $sql = '';
52     my @bind;
53
54     if ( ref($attrs) eq 'HASH' ) {
55         if ( $attrs->{'start_with'} ) {
56             my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
57             $sql .= $self->_sqlcase(' start with ') . $ws;
58             push @bind, @wb;
59         }
60         if ( my $connect_by = $attrs->{'connect_by'} ) {
61             my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $attrs->{'connect_by'} );
62             $sql .= sprintf(" %s %s",
63                 ( $attrs->{'nocycle'} ) ? $self->_sqlcase('connect by nocycle')
64                     : $self->_sqlcase('connect by'),
65                 $connect_by_sql,
66             );
67             push @bind, @connect_by_sql_bind;
68             # $sql .= $self->_sqlcase(' connect by');
69             #             foreach my $key ( keys %$connect_by ) {
70             #                 $sql .= " $key = " . $connect_by->{$key};
71             #             }
72         }
73         if ( $attrs->{'order_siblings_by'} ) {
74             $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
75         }
76     }
77
78     return wantarray ? ($sql, @bind) : $sql;
79 }
80
81 sub _order_siblings_by {
82     my ( $self, $arg ) = @_;
83
84     my ( @sql, @bind );
85     for my $c ( $self->_order_by_chunks($arg) ) {
86         $self->_SWITCH_refkind(
87             $c,
88             {
89                 SCALAR   => sub { push @sql, $c },
90                 ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c },
91             }
92         );
93     }
94
95     my $sql =
96       @sql
97       ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
98       : '';
99
100     return wantarray ? ( $sql, @bind ) : $sql;
101 }
102
103 # we need to add a '=' only when PRIOR is used against a column diretly
104 # i.e. when it is invoked by a special_op callback
105 sub _where_field_PRIOR {
106   my ($self, $lhs, $op, $rhs) = @_;
107   my ($sql, @bind) = $self->_recurse_where ($rhs);
108
109   $sql = sprintf ('%s = %s %s ',
110     $self->_convert($self->_quote($lhs)),
111     $self->_sqlcase ($op),
112     $sql
113   );
114
115   return ($sql, @bind);
116 }
117
118 1;
119
120 __END__
121
122 =pod
123
124 =head1 NAME
125
126 DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract
127
128 =head1 DESCRIPTION
129
130 See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more informations about
131 how to use hierarchical queries with DBIx::Class.
132
133 =cut