--- /dev/null
+package
+ DBIx::Class::SQLMaker::Pg;
+
+use strict;
+use warnings;
+use base qw( DBIx::Class::SQLMaker );
+
+sub new {
+ my $self = shift;
+ my %opts = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
+
+ $self->next::method(\%opts);
+}
+
+sub _assemble_binds {
+ my $self = shift;
+ return map { @{ (delete $self->{"${_}_bind"}) || [] } }
+ (qw/select from where with_recursive group having order limit/);
+}
+
+sub _parse_rs_attrs {
+ my $self = shift;
+ my ($rs_attrs) = @_;
+
+ my ($cb_sql, @cb_bind) = $self->_with_recursive($rs_attrs);
+ push @{$self->{with_recursive_bind}}, @cb_bind;
+
+ my $sql = $self->next::method(@_);
+
+ return "$cb_sql $sql";
+}
+
+# with_recursive =>{
+# -columns => [ ... ],
+# -nrt => $blargh->search....
+# -rt => $blargh->search...
+# -union_all 1|0
+sub _with_recursive {
+ my ($self, $attrs) = @_;
+
+ my $sql = '';
+ my @bind;
+
+ if ( ref($attrs) eq 'HASH' ) {
+ if ( $attrs->{'with_recursive'} ) {
+ my $with_recursive = $attrs->{'with_recursive'};
+ my @fields = @{$with_recursive->{'-columns'}};
+ my $nrt = $with_recursive->{'-nrt'};
+ my $rt = $with_recursive->{'-rt'};
+ my $union = $with_recursive->{'-union_all'};
+# my ($wr, @wb) = $self->_recurse_where( $attrs->{'with_recursive'} );
+ my ($with_nrt_sql, @with_nrt_bind) = $nrt->as_query;
+ my ($with_rt_sql, @with_rt_bind) = $rt->as_query;
+ push @bind, @with_nrt_bind;
+ push @bind, @with_rt_bind;
+ $sql .= $self->_sqlcase(' with recursive ') . ' temp_wr_query ' . '(' .
+ join(', ', @fields) . ') ' . $self->_sqlcase('as') . ' ( ';
+ $sql .= $with_nrt_sql;
+ $sql .= $self->_sqlcase(' union all ');
+ $sql .= $with_rt_sql;
+ $sql .= ' ) ';
+
+ return ($sql, @bind);
+ }
+ }
+
+ return wantarray ? ($sql, @bind) : $sql;
+
+}
+1;
--- /dev/null
+use strict;
+use warnings;
+
+use Test::Exception;
+use Test::More;
+use DBIx::Class::Optional::Dependencies ();
+use lib qw(t/lib);
+use DBIC::SqlMakerTest;
+use DBIx::Class::SQLMaker::LimitDialects;
+my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
+my $TOTAL = DBIx::Class::SQLMaker::LimitDialects->__total_bindtype,
+$ENV{NLS_SORT} = "BINARY";
+$ENV{NLS_COMP} = "BINARY";
+$ENV{NLS_LANG} = "AMERICAN";
+use DBICTest::Schema::Artist;
+BEGIN {
+ DBICTest::Schema::Artist->add_column('parentid' => { data_type => 'integer', is_nullable => 0 });
+
+ DBICTest::Schema::Artist->has_many(
+ children => 'DBICTest::Schema::Artist',
+ { 'foreign.parentid' => 'self.artistid' }
+ );
+
+ DBICTest::Schema::Artist->belongs_to(
+ parent => 'DBICTest::Schema::Artist',
+ { 'foreign.artistid' => 'self.parentid' }
+ );
+}
+use DBICTest::Schema;
+my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
+my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
+
+note "Pg Version: " . $schema->storage->_server_info->{dbms_version};
+
+my $dbh = $schema->storage->dbh;
+
+$schema->txn_do( sub {
+$schema->deploy;
+### test hierarchical queries
+#{
+ $schema->resultset('Artist')->create ({
+ name => 'root',
+ rank => 1,
+ cds => [],
+ children => [
+ {
+ name => 'child1',
+ rank => 2,
+ children => [
+ {
+ name => 'grandchild',
+ rank => 3,
+ cds => [
+ {
+ title => "grandchilds's cd" ,
+ year => '2008',
+ tracks => [
+ {
+ position => 1,
+ title => 'Track 1 grandchild',
+ }
+ ],
+ }
+ ],
+ children => [
+ {
+ name => 'greatgrandchild',
+ rank => 3,
+ }
+ ],
+ }
+ ],
+ },
+ {
+ name => 'child2',
+ rank => 3,
+ },
+ ],
+ });
+
+ $schema->resultset('Artist')->create({
+ name => 'cycle-root',
+ children => [
+ {
+ name => 'cycle-child1',
+ children => [ { name => 'cycle-grandchild' } ],
+ },
+ {
+ name => 'cycle-child2'
+ },
+ ],
+ });
+
+ $schema->resultset('Artist')->find({ name => 'cycle-root' })
+ ->update({ parentid => { -ident => 'artistid' } });
+#}
+});
+
+done_testing;