Fix top-level PRIOR with missing '='
[dbsrgits/DBIx-Class-Historic.git] / lib / DBIx / Class / SQLAHacks / Oracle.pm
CommitLineData
c0024355 1package # Hide from PAUSE
2 DBIx::Class::SQLAHacks::Oracle;
3
9ab1e5f0 4use warnings;
5use strict;
6
c0024355 7use base qw( DBIx::Class::SQLAHacks );
8use Carp::Clan qw/^DBIx::Class|^SQL::Abstract/;
9
43426175 10#
11# TODO:
2df1857d 12# - Review by experienced DBIC/SQL:A developers :-)
2ba03b16 13# - Problem with count and connect_by look the TODO in t/73oracle.t
43426175 14#
15
9ab1e5f0 16sub 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
c0024355 27sub 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
38sub _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
48sub _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 }
2ba03b16 60 if ( my $connect_by = $attrs->{'connect_by'} ) {
43426175 61 my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $attrs->{'connect_by'} );
62 $sql .= sprintf(" %s %s",
2ba03b16 63 ( $attrs->{'nocycle'} ) ? $self->_sqlcase('connect by nocycle')
64 : $self->_sqlcase('connect by'),
43426175 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 # }
c0024355 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
81sub _order_siblings_by {
2a770efe 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 }
c0024355 94
2a770efe 95 my $sql =
96 @sql
97 ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
98 : '';
c0024355 99
2a770efe 100 return wantarray ? ( $sql, @bind ) : $sql;
c0024355 101}
102
9ab1e5f0 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
105sub _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
c0024355 1181;
119
120__END__
121
122=pod
123
124=head1 NAME
125
126DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract
127
128=head1 DESCRIPTION
129
130See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more informations about
131how to use hierarchical queries with DBIx::Class.
132
133=cut