fixup SQL::Abstract::ExtraClauses to subclass SQLA, remove ::Clauses
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / ExtraClauses.pm
index 0be3021..73e7073 100644 (file)
@@ -4,13 +4,12 @@ use strict;
 use warnings;
 use if $] < '5.010', 'MRO::Compat';
 use mro 'c3';
-use base qw(SQL::Abstract::Clauses);
+use base qw(SQL::Abstract);
 
 BEGIN { *puke = \&SQL::Abstract::puke }
 
-sub register_defaults {
-  my $self = shift;
-  $self->next::method(@_);
+sub new {
+  my $self = shift->next::method(@_);
   my @clauses = $self->clauses_of('select');
   my @before_setop;
   CLAUSE: foreach my $idx (0..$#clauses) {
@@ -153,7 +152,7 @@ sub register_defaults {
     return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
   });
   $self->clause_expander('select.with_recursive', $with_expander);
-  $self->clause_renderer('select.with' => sub {
+  $self->clause_renderer('select.with' => my $with_renderer = sub {
     my ($self, undef, $with) = @_;
     my $q_part = $self->join_query_parts(', ',
       map {
@@ -170,6 +169,20 @@ sub register_defaults {
       $q_part,
     );
   });
+  foreach my $stmt (qw(insert update delete)) {
+    $self->clauses_of($stmt => 'with', $self->clauses_of($stmt));
+    $self->clause_expander("${stmt}.$_", $with_expander)
+      for qw(with with_recursive);
+    $self->clause_renderer("${stmt}.with", $with_renderer);
+  }
+  $self->expander(cast => sub {
+    return { -func => [ cast => $_[2] ] } if ref($_[2]) eq 'HASH';
+    my ($cast, $to) = @{$_[2]};
+    +{ -func => [ cast => { -as => [
+      $self->expand_expr($cast),
+      $self->expand_expr($to, -ident),
+    ] } ] };
+  });
 
   return $self;
 }