blib/lib/DBIx/QuickORM/Source/Query.pm

filter: Statementstmt 90.0% 36 / 40 Branchbran 50.0% 4 / 8 Conditioncond 43.7% 7 / 16 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 92.8% 13 / 14 Podpod 77.7% 7 / 9 totaltotal 73.6% 70 / 95 CC 11 SCAR 17.3
CC
11
Cov
73%
init
11.4
init
11.4
BEGIN
1.0
CRAP 5.6
« blib/lib/DBIx/QuickORM/Dialect/DuckDB.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Table.pm »
1 package DBIx::QuickORM::Source::Query;
2 305 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 305 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 305 our $VERSION = '0.000032';
6  
7 305 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 305 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 <sql
11 <binds
12 305 };
13  
14 305 ▶use Role::Tiny::With qw/with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 305 with 'DBIx::QuickORM::Role::Source';
16  
17 =pod
18  
19 =encoding UTF-8
20  
21 =head1 NAME
22  
23 DBIx::QuickORM::Source::Query - A query as a source.
24  
25 =head1 DESCRIPTION
26  
27 An SQL query and its binds, as L<DBIx::QuickORM::Role::Source> describes. The
28 SQL is used as written and its binds are sent without types. Its rows belong
29 to no table: they bypass the lookup table, and the ORM's default row class
30 wraps them unless the handle sets another.
31  
32 A handle that adds nothing to the query runs it as it is. One that adds a
33 C<where>, an order, a limit or offset, or fields selects from it as a
34 subquery. Some databases need a subquery's column names to be distinct, and an
35 C<ORDER BY> inside one may be lost.
36  
37 =head1 SYNOPSIS
38  
39 my $h = $orm->handle(\['SELECT id, name FROM users WHERE age > ?', 40]);
40  
41 =head1 ATTRIBUTES
42  
43 =over 4
44  
45 =item $sql = $source->sql
46  
47 Required, and not blank.
48  
49 =item \@binds = $source->binds
50  
51 Empty unless given. Must be an arrayref.
52  
53 =back
54  
55 =cut
56  
57 21 ▶sub init {
Subroutine init: called CC=6 SCAR=24.3
Pod undocumented
58 0 ▶ my $self = shift;
Subroutine init: not called CC=6 SCAR=24.3
Pod undocumented
59  
60 0 my $sql = $self->{+SQL};
61 0 ▶ croak "A query source needs its SQL as a string" unless defined($sql) && !ref($sql) && $sql =~ m/\S/;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $sql and not ref $sql and $sql =~ /\S/ 2 19 21
unless defined $sql and not ref $sql and $sql =~ /\S/ 0 0 0
Condition41%5/12
defined $sql and not ref $sql and $sql =~ /\S/
!ll&&!rl&&r
1119
defined $sql and not ref $sql
!ll&&!rl&&r
0120
defined $sql and not ref $sql and $sql =~ /\S/
!ll&&!rl&&r
000
defined $sql and not ref $sql
!ll&&!rl&&r
000
MC/DC66%2/3
defined $sql and not ref $sql and $sql =~ /\S/
defined $sql not ref $sql $sql =~ /\S/
MC/DC0%0/3
defined $sql and not ref $sql and $sql =~ /\S/
defined $sql not ref $sql $sql =~ /\S/
Truth table75%3/4
A and not(B) and C
A= defined $sqlB= not ref $sqlC= $sql =~ /\S/
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and not(B) and C
A= defined $sqlB= not ref $sqlC= $sql =~ /\S/
ABCresult
0XX0
10X0
1100
1111
62 0 ▶ croak "'binds' must be an arrayref" unless ref($self->{+BINDS} //= []) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref($self->{"binds"} //= []) eq "ARRAY" 1 18 19
unless ref($self->{"binds"} //= []) eq "ARRAY" 0 0 0
Condition50%2/4
$self->{"binds"} //= []
l!l
712
$self->{"binds"} //= []
l!l
00
MC/DC100%1/1
$self->{"binds"} //= []
$self->{"binds"}
MC/DC0%0/1
$self->{"binds"} //= []
$self->{"binds"}
Truth table100%2/2
A
A= $self->{"binds"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"binds"}
Aresult
11
00
63 }
64  
65 =head1 PUBLIC METHODS
66  
67 =over 4
68  
69 =item undef = $source->table
70  
71 =item undef = $source->pk
72  
73 Undef: a query source has no table, so no primary key.
74  
75 =item $fields = $source->select_fields($given)
76  
77 What the handle gives, or undef (every column).
78  
79 =item {} = $source->field_records
80  
81 Empty: the query's columns have no field records.
82  
83 =back
84  
85 =cut
86  
87 22 ▶sub table { undef }
Subroutine table: called CC=1 SCAR=0.0
Pod documented
88 1 ▶sub pk { undef }
Subroutine pk: called CC=1 SCAR=0.0
Pod documented
89 9 ▶sub select_fields { $_[1] }
Subroutine select_fields: called CC=1 SCAR=0.0
Pod documented
90 22 ▶sub field_records { +{} }
Subroutine field_records: called CC=1 SCAR=0.0
Pod documented
91  
92 =pod
93  
94 =over 4
95  
96 =item ($sql, \@binds) = $source->from_sql($dialect)
97  
98 The query in parentheses with the alias C<qorm_query>.
99  
100 =back
101  
102 =cut
103  
104 5 ▶sub from_sql {
Subroutine from_sql: called CC=1 SCAR=0.0
Pod documented
105 5 my $self = shift;
106 5 my ($sql, $binds) = $self->query_sql;
107 5 return ("($sql) qorm_query", $binds);
108 }
109  
110 =pod
111  
112 =over 4
113  
114 =item ($sql, \@binds) = $source->query_sql
115  
116 The query itself.
117  
118 =back
119  
120 =cut
121  
122 15 ▶sub query_sql {
Subroutine query_sql: called CC=1 SCAR=0.0
Pod documented
123 15 my $self = shift;
124 15 return ($self->{+SQL}, [map { [undef, $_] } @{$self->{+BINDS}}]);
125 }
126  
127 =pod
128  
129 =over 4
130  
131 =item (\@rows, \&build) = $source->results($orm, \@raw, $ingest, %opts)
132  
133 The rows as fetched (nothing to ingest), built with the handle's C<row_class>
134 when given, otherwise the ORM's.
135  
136 =back
137  
138 =cut
139  
140 8 ▶sub results {
Subroutine results: called CC=1 SCAR=0.0
Pod documented
141 8 my $self = shift;
142 8 my ($orm, $raw, $ingest, %opts) = @_;
143 8 return ($raw, $orm->_row_builder(undef, %opts));
144 }
145  
146 305 1;
147  
148 __END__
« blib/lib/DBIx/QuickORM/Dialect/DuckDB.pm ↑ Summary blib/lib/DBIx/QuickORM/Source/Table.pm »