File Coverage

blib/lib/DBIx/Class/SQLA2.pm
Criterion Covered Total %
statement 48 50 96.0
branch 9 14 64.2
condition 3 5 60.0
subroutine 12 12 100.0
pod 3 4 75.0
total 75 85 88.2


line stmt bran cond sub pod time code
1             package DBIx::Class::SQLA2;
2 4     4   94438 use strict;
  4         14  
  4         123  
3 4     4   43 use warnings;
  4         32  
  4         126  
4 4     4   38 use feature 'postderef';
  4         9  
  4         801  
5 4     4   29 no warnings 'experimental::postderef';
  4         9  
  4         142  
6 4     4   23 use mro 'c3';
  4         8  
  4         31  
7              
8 4         3600 use base qw(
9             DBIx::Class::SQLMaker::ClassicExtensions
10             SQL::Abstract
11             SQL::Abstract::Classic
12 4     4   169 );
  4         7  
13              
14 4     4   85041 use Role::Tiny;
  4         13140  
  4         46  
15              
16             sub select {
17 30     30 1 263541 my ($self, $table, $fields, $where, $rs_attrs, $limit, $offset) = @_;
18              
19             my $expand_hashrefref = sub {
20 30     30   54 my $list = shift;
21 30 50       134 my @fields = ref $list eq 'ARRAY' ? @$list : $list;
22             return [
23             map {
24 30         71 ref $_ eq 'REF' && ref $$_ eq 'HASH'
25 62 100 66     275 ? do {
26 5         45 my %f = $$_->%*;
27 5         20 my $as = delete $f{-as};
28             \[
29 5 50       91 $as
30             ? $self->render_expr({ -op => [ 'as', \%f, { -ident => $as } ] })
31             : $self->render_expr(\%f)
32             ];
33             }
34             : $_
35             } @fields
36             ];
37 30         186 };
38 30         86 $fields = $expand_hashrefref->($fields);
39 30 50       1817 if (my $gb = $rs_attrs->{group_by}) {
40 0         0 $rs_attrs = { %$rs_attrs, group_by => $expand_hashrefref->($gb) };
41             }
42 30         144 $self->next::method($table, $fields, $where, $rs_attrs, $limit, $offset);
43             }
44              
45              
46             sub insert {
47             # TODO - this works, ish. The issue is that if you have rels involved, you may actually
48             # hit `insert` before the intended insert. Not sure what to do but put that on the
49             # user...
50 20     20 1 31927 my ($self, $source, $cols, $attrs) = @_;
51 20   50     168 $attrs ||= {};
52 20 100       86 if (my $extra_attrs = $self->{_sqla2_insert_attrs}) {
53 6         30 $attrs = { %$attrs, %$extra_attrs };
54             }
55 20         101 $self->next::method($source, $cols, $attrs);
56             }
57              
58             sub expand_clause {
59 2     2 0 7 my ($self, $clause, $value) = @_;
60 2         5 my ($probably_key, $expanded) = $self->${ \$self->clause_expander($clause) }(undef, $value);
  2         54  
61 2 50       1071 if ($expanded) {
62 2         11 return ($probably_key => $expanded);
63             } else {
64 0         0 return (undef => $probably_key);
65             }
66             }
67              
68              
69             sub new {
70 9     9 1 810594 my $new = shift->next::method(@_);
71 9 50       2838 unless (grep {m/^with$/} $new->clauses_of('select')) {
  36         238  
72 9         84 $new->plugin("+$_") for qw/ExtraClauses WindowFunctions Upsert BangOverrides CaseExpr/;
73             }
74 9         555 return $new
75             }
76              
77             our $VERSION = '0.01';
78              
79             1;
80              
81             =encoding utf8
82              
83             =head1 NAME
84              
85             DBIx::Class::SQLA2 - SQL::Abstract v2 support in DBIx::Class
86              
87             =head1 SYNOPSIS
88              
89             $schema->connect_call_rebase_sqlmaker('DBIx::Class::SQLA2');
90              
91             =head1 DESCRIPTION
92              
93             This is a work in progress for simplifying using SQLA2 with DBIC. This is for using w/ the
94             most recent version of DBIC.
95              
96             For a simple way of using this, take a look at L.
97              
98             B
99              
100             This role itself will add handling of hashref-refs to select lists + group by clauses,
101             which will render the inner hashref as if it had been passed through to SQLA2 rather than
102             doing the recursive function rendering that DBIC does.
103              
104             =head2 Included Plugins
105              
106             This will add the following SQLA2 plugins:
107              
108             =over 2
109              
110             =item L
111              
112             Adds support for CTEs, and other fun new SQL syntax
113              
114             =item L
115              
116             Adds support for window functions and advanced aggregates.
117              
118             =item L
119              
120             Adds support for Upserts (ON CONFLICT clause)
121              
122             =item L
123              
124             Adds some hacky stuff so you can bypass/supplement DBIC's handling of certain clauses
125              
126             =back
127              
128             =head1 AUTHOR
129              
130             Copyright (c) 2022 Veesh Goldman
131              
132             =head1 LICENSE
133              
134             This module is free software; you may copy this under the same
135             terms as perl itself (either the GNU General Public License or
136             the Artistic License)
137              
138             =cut