Do not pollute sqlmaker while scanning raw sql
[dbsrgits/DBIx-Class.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
583a0c65 27sub _assemble_binds {
28 my $self = shift;
29 return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/from where oracle_connect_by having order/);
c0024355 30}
31
583a0c65 32
c0024355 33sub _emulate_limit {
2e4dd241 34 my ( $self, $syntax, $sql, $rs_attrs, $rows, $offset ) = @_;
c0024355 35
2e4dd241 36 my ($cb_sql, @cb_bind) = $self->_connect_by($rs_attrs);
c0024355 37 $sql .= $cb_sql;
583a0c65 38 $self->{oracle_connect_by_bind} = \@cb_bind;
c0024355 39
2e4dd241 40 return $self->SUPER::_emulate_limit($syntax, $sql, $rs_attrs, $rows, $offset);
c0024355 41}
42
43sub _connect_by {
2e4dd241 44 my ($self, $attrs) = @_;
45
c0024355 46 my $sql = '';
47 my @bind;
48
49 if ( ref($attrs) eq 'HASH' ) {
50 if ( $attrs->{'start_with'} ) {
51 my ($ws, @wb) = $self->_recurse_where( $attrs->{'start_with'} );
52 $sql .= $self->_sqlcase(' start with ') . $ws;
53 push @bind, @wb;
54 }
6b2fbbf0 55 if ( my $connect_by = $attrs->{'connect_by'} || $attrs->{'connect_by_nocycle'} ) {
56 my ($connect_by_sql, @connect_by_sql_bind) = $self->_recurse_where( $connect_by );
43426175 57 $sql .= sprintf(" %s %s",
ef5db928 58 ( $attrs->{'connect_by_nocycle'} ) ? $self->_sqlcase('connect by nocycle')
2ba03b16 59 : $self->_sqlcase('connect by'),
43426175 60 $connect_by_sql,
61 );
62 push @bind, @connect_by_sql_bind;
c0024355 63 }
64 if ( $attrs->{'order_siblings_by'} ) {
65 $sql .= $self->_order_siblings_by( $attrs->{'order_siblings_by'} );
66 }
67 }
68
69 return wantarray ? ($sql, @bind) : $sql;
70}
71
72sub _order_siblings_by {
2a770efe 73 my ( $self, $arg ) = @_;
74
75 my ( @sql, @bind );
76 for my $c ( $self->_order_by_chunks($arg) ) {
77 $self->_SWITCH_refkind(
78 $c,
79 {
80 SCALAR => sub { push @sql, $c },
81 ARRAYREF => sub { push @sql, shift @$c; push @bind, @$c },
82 }
83 );
84 }
c0024355 85
2a770efe 86 my $sql =
87 @sql
88 ? sprintf( '%s %s', $self->_sqlcase(' order siblings by'), join( ', ', @sql ) )
89 : '';
c0024355 90
2a770efe 91 return wantarray ? ( $sql, @bind ) : $sql;
c0024355 92}
93
9ab1e5f0 94# we need to add a '=' only when PRIOR is used against a column diretly
95# i.e. when it is invoked by a special_op callback
96sub _where_field_PRIOR {
97 my ($self, $lhs, $op, $rhs) = @_;
98 my ($sql, @bind) = $self->_recurse_where ($rhs);
99
100 $sql = sprintf ('%s = %s %s ',
101 $self->_convert($self->_quote($lhs)),
102 $self->_sqlcase ($op),
103 $sql
104 );
105
106 return ($sql, @bind);
107}
108
c0024355 1091;
110
111__END__
112
113=pod
114
115=head1 NAME
116
117DBIx::Class::SQLAHacks::Oracle - adds hierarchical query support for Oracle to SQL::Abstract
118
119=head1 DESCRIPTION
120
121See L<DBIx::Class::Storage::DBI::Oracle::Generic> for more informations about
122how to use hierarchical queries with DBIx::Class.
123
124=cut