committing for review
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLMaker / PostgreSQL.pm
CommitLineData
5f168723 1package
316a3159 2 DBIx::Class::SQLMaker::PostgreSQL;
5f168723 3
4use strict;
5use warnings;
6use base qw( DBIx::Class::SQLMaker );
7
8sub new {
9 my $self = shift;
10 my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
e46bcbe9 11 push @{$opts{special_ops}}, {
12 regex => qr/^with_recursive$/i,
13 handler => '_with_recursive',
14 };
5f168723 15
16 $self->next::method(\%opts);
17}
18
19sub _assemble_binds {
20 my $self = shift;
21 return map { @{ (delete $self->{"${_}_bind"}) || [] } }
22 (qw/select from where with_recursive group having order limit/);
23}
24
25sub _parse_rs_attrs {
26 my $self = shift;
e46bcbe9 27 warn "INSIDE RS ATTRS";
5f168723 28 my ($rs_attrs) = @_;
29
30 my ($cb_sql, @cb_bind) = $self->_with_recursive($rs_attrs);
31 push @{$self->{with_recursive_bind}}, @cb_bind;
32
33 my $sql = $self->next::method(@_);
34
35 return "$cb_sql $sql";
36}
37
5f168723 38sub _with_recursive {
39 my ($self, $attrs) = @_;
40
e46bcbe9 41 warn "INSIDE WITH RECURSIVE";
5f168723 42 my $sql = '';
43 my @bind;
44
45 if ( ref($attrs) eq 'HASH' ) {
46 if ( $attrs->{'with_recursive'} ) {
47 my $with_recursive = $attrs->{'with_recursive'};
48 my @fields = @{$with_recursive->{'-columns'}};
e46bcbe9 49 my $nrt = $with_recursive->{'-initial'};
50 my $rt = $with_recursive->{'-recursive'};
5f168723 51 my $union = $with_recursive->{'-union_all'};
52# my ($wr, @wb) = $self->_recurse_where( $attrs->{'with_recursive'} );
53 my ($with_nrt_sql, @with_nrt_bind) = $nrt->as_query;
54 my ($with_rt_sql, @with_rt_bind) = $rt->as_query;
55 push @bind, @with_nrt_bind;
56 push @bind, @with_rt_bind;
57 $sql .= $self->_sqlcase(' with recursive ') . ' temp_wr_query ' . '(' .
58 join(', ', @fields) . ') ' . $self->_sqlcase('as') . ' ( ';
59 $sql .= $with_nrt_sql;
e46bcbe9 60 $sql .= $self->_sqlcase(' union all ') if $union;
5f168723 61 $sql .= $with_rt_sql;
62 $sql .= ' ) ';
e46bcbe9 63 warn "SQL $sql";
5f168723 64 return ($sql, @bind);
65 }
66 }
67
68 return wantarray ? ($sql, @bind) : $sql;
69
70}
711;