First attempt at SQLA2Support
Matt S Trout [Thu, 25 Apr 2019 14:06:36 +0000 (14:06 +0000)]
lib/DBIx/Class/SQLMaker/SQLA2Support.pm [new file with mode: 0644]
lib/SQL/Abstract/Clauses.pm
lib/with.pm

diff --git a/lib/DBIx/Class/SQLMaker/SQLA2Support.pm b/lib/DBIx/Class/SQLMaker/SQLA2Support.pm
new file mode 100644 (file)
index 0000000..c35bda9
--- /dev/null
@@ -0,0 +1,26 @@
+package DBIx::Class::SQLMaker::SQLA2Support;
+
+use strict;
+use warnings;
+use if $] < '5.010', 'MRO::Compat';
+use mro 'c3';
+use base qw(
+  DBIx::Class::SQLMaker
+  SQL::Abstract::ExtraClauses
+);
+
+sub select {
+  my $self = shift;
+  my ($sql, @bind) = $self->next::method(@_);
+  my (undef, undef, undef, $attrs) = @_;
+  if (my $with = delete $attrs->{with}) {
+    my ($wsql, @wbind) = @{ $self->render_statement({
+      -select => { with => $with }
+    }) };
+    unshift @bind, @wbind;
+    $sql = "${wsql} ${sql}";
+  }
+  return wantarray ? ($sql, @bind) : $sql;
+}
+
+1;
index 78ec300..74bb20e 100644 (file)
@@ -63,6 +63,21 @@ sub register_defaults {
   $self->{expand}{exists} = sub {
     $_[0]->_expand_op(undef, [ exists => $_[2] ]);
   };
+
+  # check for overriden methods
+  if ($self->can('_table') ne SQL::Abstract->can('_table')) {
+    $self->{expand_clause}{'select.from'} = sub {
+      return +{ -literal => [ $_[0]->_table($_[2]) ] };
+    };
+  }
+  if ($self->can('_order_by') ne SQL::Abstract->can('_order_by')) {
+    $self->{expand_clause}{'select.order_by'} = sub {
+      my ($osql, @obind) = $_[0]->_order_by($_[2]);
+      $osql =~ s/^order by //i;
+      return undef unless length($osql);
+      return +{ -literal => [ $osql, @obind ] };
+    };
+  }
   return $self;
 }
 
@@ -212,7 +227,6 @@ sub render_statement {
 
 sub select {
   my ($self, @args) = @_;
-
   my $stmt = do {
     if (ref(my $sel = $args[0]) eq 'HASH') {
       $sel
index 28055b4..96c2a66 100644 (file)
@@ -18,8 +18,9 @@ sub components {
   Class::C3::Componentised->ensure_class_loaded($_) for @comp_classes;
   Class::C3::Componentised->inject_base(
     $new_class,
-    @comp_classes, $class,
+    @comp_classes, $class
   );
+  mro::set_mro($new_class, 'c3');
   return $new_class unless ref($inv);
   return bless($inv, $new_class);
 }