passthrough role for DBIC sqlmaker
Matt S Trout [Sun, 13 Oct 2019 04:24:43 +0000 (04:24 +0000)]
lib/DBIx/Class/SQLMaker/Role/SQLA2Passthrough.pm [new file with mode: 0644]

diff --git a/lib/DBIx/Class/SQLMaker/Role/SQLA2Passthrough.pm b/lib/DBIx/Class/SQLMaker/Role/SQLA2Passthrough.pm
new file mode 100644 (file)
index 0000000..39e3a56
--- /dev/null
@@ -0,0 +1,32 @@
+package DBIx::Class::SQLMaker::Role::SQLA2Passthrough;
+
+use Role::Tiny;
+
+around select => sub {
+  my ($orig, $self, $table, $fields, $where, $rs_attrs, $limit, $offset) = @_;
+
+  $fields = \[ $self->render_expr({ -list => [
+    grep defined,
+    map +(ref($_) eq 'HASH'
+          ? do {
+              my %f = %$_;
+              my $as = delete $f{-as};
+              my ($f, $rhs) = %f;
+              my $func = +{ "-${f}" => $rhs };
+              ($as
+                ? +{ -op => [ 'as', $func, { -ident => [ $as ] } ] }
+                : $func)
+            }
+          : $_), ref($fields) eq 'ARRAY' ? @$fields : $fields
+  ] }, -ident) ];
+
+  if (my $gb = $rs_attrs->{group_by}) {
+    $rs_attrs = {
+      %$rs_attrs,
+      group_by => \[ $self->render_expr({ -list => $gb }, -ident) ]
+    };
+  }
+  $self->$orig($table, $fields, $where, $rs_attrs, $limit, $offset);
+});
+
+1;