initial commit, attempting to get test deploy working
Devin Austin [Thu, 12 Apr 2012 21:53:28 +0000 (14:53 -0700)]
lib/DBIx/Class/SQLAHacks/Pg.pm [new file with mode: 0644]
lib/DBIx/Class/SQLMaker/Pg.pm [new file with mode: 0644]
t/pg_with_recursive.t [new file with mode: 0644]

diff --git a/lib/DBIx/Class/SQLAHacks/Pg.pm b/lib/DBIx/Class/SQLAHacks/Pg.pm
new file mode 100644 (file)
index 0000000..06c3e44
--- /dev/null
@@ -0,0 +1,6 @@
+package # Hide from PAUSE
+  DBIx::Class::SQLAHacks::Pg;
+
+use base qw( DBIx::Class::SQLMaker::Pg );
+
+1;
diff --git a/lib/DBIx/Class/SQLMaker/Pg.pm b/lib/DBIx/Class/SQLMaker/Pg.pm
new file mode 100644 (file)
index 0000000..2431c6a
--- /dev/null
@@ -0,0 +1,70 @@
+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;
diff --git a/t/pg_with_recursive.t b/t/pg_with_recursive.t
new file mode 100644 (file)
index 0000000..189a3a2
--- /dev/null
@@ -0,0 +1,99 @@
+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;