blib/lib/DBIx/QuickORM/SchemaBuilder/View.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 8 / 8 Conditioncond 78.9% 15 / 19 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 7 / 7 Podpod 66.6% 2 / 3 totaltotal 90.0% 81 / 90 CC 13 SCAR 19.7
CC
13
Cov
94%
add_sql
11.1
init
3.0
BEGIN
1.0
CRAP 7.1
« blib/lib/DBIx/QuickORM/WhereBuilder/Simple.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect.pm »
1 package DBIx::QuickORM::SchemaBuilder::View;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 our $VERSION = '0.000033';
6  
7 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 <name
11 +entries
12 4 };
13  
14 =pod
15  
16 =encoding UTF-8
17  
18 =head1 NAME
19  
20 DBIx::QuickORM::SchemaBuilder::View - A view's description for schema building.
21  
22 =head1 DESCRIPTION
23  
24 Holds what a table class's C<view_sql> calls give (see
25 L<DBIx::QuickORM::DSL/":schema">): the view's query per dialect, the tables
26 and views it reads, and the column types of what it selects that cannot be
27 traced to a table.
28  
29 =head1 SYNOPSIS
30  
31 A view class's C<view_sql> passes its arguments to C<add_sql>:
32  
33 package My::App::ExampleUsers;
34 use DBIx::QuickORM::DSL qw/:view :schema/;
35  
36 view { name 'example_users'; elective }; # may be missing: none on SQLite
37  
38 schema {
39 view_sql(
40 postgresql => q{SELECT id, name FROM users WHERE email ILIKE '%@example.com'},
41 any_mysql => q{SELECT id, name FROM users WHERE email LIKE '%@example.com'},
42 any => undef,
43 depends => ['users'],
44 );
45 };
46  
47 An undef query builds no view on that dialect, so the view class needs
48 C<elective>, or C<new> croaks there for the missing view.
49  
50 =head1 ATTRIBUTES
51  
52 =over 4
53  
54 =item $name = $view->name
55  
56 The view's name. Required.
57  
58 =back
59  
60 =cut
61  
62 19 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
Pod undocumented
63 19 my $self = shift;
64 19 my $name = $self->{+NAME};
65 19 ▶ croak "A view needs a 'name'" unless defined($name) && length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 18 19
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
1018
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
66 18 $self->{+ENTRIES} = [];
67 }
68  
69 =head1 PUBLIC METHODS
70  
71 =over 4
72  
73 =item $view->add_sql(dialect => $query, ..., depends => \@names, columns => \%types)
74  
75 Adds the view's query for some dialects. Here a query comes from a handle,
76 through C<view_sql> in a view class:
77  
78 package My::App::Shouted;
79 use DBIx::QuickORM::DSL qw/:view :schema/;
80  
81 view { name 'shouted' };
82  
83 schema {
84 view_sql(
85 any => sub {
86 my $orm = shift; # offline: builds SQL, has no database connection
87 return $orm->handle('users')
88 ->where({name => {'!=' => 'admin'}})
89 ->fields({
90 id => 'id',
91 shout => \'upper(name)',
92 });
93 },
94 depends => ['users'],
95 columns => {shout => 'text'}, # a computed column's type
96 );
97 };
98  
99 On PostgreSQL that builds:
100  
101 =for highlighter language=sql
102  
103 CREATE VIEW "shouted" AS SELECT "id" AS "id", upper(name) AS "shout" FROM "users" WHERE ( "name" != 'admin' );
104  
105 =for highlighter language=perl
106  
107 Queries are given per dialect, keyed as L<DBIx::QuickORM::Dialect>'s C<dialect_keys>
108 (C<any_mysql> for MySQL and MariaDB, C<any> for every database), the exact
109 dialect first, then C<any_mysql>, then C<any>. A query is a string or a
110 reference to one, undef for no view on that dialect, or a code reference: it
111 gets an offline ORM of the ORM's class (one built for SQL only, with no
112 database connection) and returns a handle, whose C<SELECT>, binds written
113 in, is the query.
114  
115 That ORM's map holds the tables built from C<schema> blocks and the views
116 built from handles before this one. Tables and views given as SQL are not in
117 it, even when C<depends> names them, so a handle cannot read them.
118  
119 C<depends> names the tables and views these queries read, created first.
120 C<columns> gives the column type of each column a handle selects that is not
121 one table field (a computed column, an alias of an expression), as
122 C<< {name => $db_type} >>. Both apply to this call's dialects only, so
123 several calls can give each dialect its own. A dialect key in two calls
124 croaks. Returns the builder.
125  
126 =item \%spec = $view->spec
127  
128 The description as plain data: C<< {entries => [{sql => \%by_dialect,
129 depends => \@names, columns => \%types}, ...]} >>, one per C<add_sql> call.
130  
131 =back
132  
133 =cut
134  
135 23 ▶sub add_sql {
Subroutine add_sql: called CC=11 SCAR=24.0
Pod documented
136 23 my $self = shift;
137 23 my $name = $self->{+NAME};
138 23 require DBIx::QuickORM::Dialect;
139 23 my ($sql, $opts) = DBIx::QuickORM::Dialect->dialect_pairs("view_sql for '$name'", [@_], options => ['depends', 'columns'], code => 1);
140 17 ▶ croak "The view '$name' takes 'depends' as an arrayref of names" if exists($opts->{depends}) && ref($opts->{depends}) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"depends"} and ref $opts->{"depends"} ne "ARRAY" 1 16 17
Condition100%3/3
exists $opts->{"depends"} and ref $opts->{"depends"} ne "ARRAY"
!ll&&!rl&&r
971
MC/DC100%2/2
exists $opts->{"depends"} and ref $opts->{"depends"} ne "ARRAY"
exists $opts->{"depends"} ref $opts->{"depends"} ne "ARRAY"
Truth table100%3/3
A and B
A= exists $opts->{"depends"}B= ref $opts->{"depends"} ne "ARRAY"
ABresult
0X0
100
111
141 16 ▶ croak "The view '$name' takes 'columns' as a hashref of name => column type" if exists($opts->{columns}) && (ref($opts->{columns}) ne 'HASH' || grep { !defined($_) || ref($_) } values %{$opts->{columns}});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"columns"} and ref $opts->{"columns"} ne "HASH" || grep({not defined $_ or ref $_;} values %{$$opts{"columns"};}) 1 15 16
Condition66%6/9
exists $opts->{"columns"} and ref $opts->{"columns"} ne "HASH" || grep({not defined $_ or ref $_;} values %{$$opts{"columns"};})
!ll&&!rl&&r
1231
ref $opts->{"columns"} ne "HASH" or grep {not defined $_ or ref $_;} values %{$$opts{"columns"};}
l!l&&r!l&&!r
103
!defined($_) || ref $_
l!l&&r!l&&!r
003
MC/DC66%2/3
exists $opts->{"columns"} and ref $opts->{"columns"} ne "HASH" || grep({not defined $_ or ref $_;} values %{$$opts{"columns"};})
exists $opts->{"columns"} ref $opts->{"columns"} ne "HASH" grep {not defined $_ or ref $_;} values %{$$opts{"columns"};}
MC/DC0%0/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table75%3/4
A and B or C
A= exists $opts->{"columns"}B= ref $opts->{"columns"} ne "HASH"C= grep {not defined $_ or ref $_;} values %{$$opts{"columns"};}
ABCresult
0XX0
1000
11X1
1011
Truth table33%1/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
142 15 for my $key (sort keys %$sql) {
143 19 ▶ croak "The view '$name' gives '$key' in two view_sql calls" if grep { exists $_->{sql}->{$key} } @{$self->{+ENTRIES}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {exists $_->{"sql"}{$key};} @{$$self{"entries"};} 2 17 19
144 }
145 13 ▶ push @{$self->{+ENTRIES}} => {sql => $sql, depends => [@{$opts->{depends} // []}], columns => {%{$opts->{columns} // {}}}};
Condition100%4/4
$opts->{"depends"} // []
l!l
76
$opts->{"columns"} // {}
l!l
310
MC/DC100%1/1
$opts->{"depends"} // []
$opts->{"depends"}
MC/DC100%1/1
$opts->{"columns"} // {}
$opts->{"columns"}
Truth table100%2/2
A
A= $opts->{"depends"}
Aresult
11
00
Truth table100%2/2
A
A= $opts->{"columns"}
Aresult
11
00
146 13 return $self;
147 }
148  
149 6 ▶sub spec {
Subroutine spec: called CC=1 SCAR=0.0
Pod documented
150 6 my $self = shift;
151 6 return {entries => [map { +{sql => {%{$_->{sql}}}, depends => [@{$_->{depends}}], columns => {%{$_->{columns}}}} } @{$self->{+ENTRIES}}]};
152 }
153  
154 4 1;
155  
156 __END__
« blib/lib/DBIx/QuickORM/WhereBuilder/Simple.pm ↑ Summary blib/lib/DBIx/QuickORM/Dialect.pm »