blib/lib/DBIx/QuickORM/SchemaBuilder/Table/Temp.pm

filter: Statementstmt 64.7% 33 / 51 Branchbran 43.7% 7 / 16 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 66.6% 10 / 15 Podpod 75.0% 6 / 8 totaltotal 61.0% 61 / 100 CC 11 SCAR 9.3
CC
11
Cov
59%
add_column
4.1
add_column
4.1
add_foreign_key
2.8
CRAP 2.5
« blib/lib/DBIx/QuickORM/Connection/Offline.pm ↑ Summary blib/lib/DBIx/QuickORM/Exception/DB.pm »
1 package DBIx::QuickORM::SchemaBuilder::Table::Temp;
2 260 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 260 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 260 our $VERSION = '0.000033';
6  
7 260 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 260 ▶use parent 'DBIx::QuickORM::SchemaBuilder::Table';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =pod
12  
13 =encoding UTF-8
14  
15 =head1 NAME
16  
17 DBIx::QuickORM::SchemaBuilder::Table::Temp - Describe a temporary table, then build it.
18  
19 =head1 DESCRIPTION
20  
21 What C<< $orm->temp_table >> returns or passes to its block. It describes the
22 table as L<DBIx::QuickORM::SchemaBuilder::Table> does, and C<build> creates it
23 as a temporary table on the ORM's current connection.
24  
25 =head1 SYNOPSIS
26  
27 my $builder = $orm->temp_table(name => 'staged');
28 $builder->add_column('id', {
29 db_type => 'integer',
30 primary_key => 1,
31 });
32 my $temp = $builder->build;
33  
34 =head1 ATTRIBUTES
35  
36 As L<DBIx::QuickORM::SchemaBuilder::Table>, with C<orm> required.
37  
38 =cut
39  
40 52 ▶sub init {
Subroutine init: called CC=2 SCAR=9.2
Pod undocumented
41 0 ▶ my $self = shift;
Subroutine init: not called CC=2 SCAR=9.2
Pod undocumented
42 0 ▶ croak "A temporary table builder needs the 'orm'" unless $self->orm;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->orm 1 51 52
unless $self->orm 0 0 0
43 0 $self->SUPER::init();
44 }
45  
46 =head1 PUBLIC METHODS
47  
48 =over 4
49  
50 =item $builder->add_column($name, \%options)
51  
52 =item $builder->add_foreign_key($columns, \%options)
53  
54 As inherited, but a temporary table's foreign keys are links in the map
55 only.
56  
57 my $scores = $orm->temp_table(sub {
58 my $t = shift;
59 $t->add_column('user_id', {
60 db_type => 'integer',
61 references => ['users', 'id'], # for joins and follow_fk
62 });
63 $t->add_column('score', {db_type => 'integer'});
64 });
65  
66 # users.id must be in the ORM's map, but the database does not check the
67 # key: no user 999 is needed
68 $scores->handle->insert({
69 user_id => 999,
70 score => 1,
71 });
72  
73 No database key acts on them, so C<on_delete> and C<on_update> croak.
74  
75 An enum column is C<text> with a C<CHECK> on PostgreSQL and DuckDB, since a
76 type created for it would outlive the table.
77  
78 =back
79  
80 =cut
81  
82 83 ▶sub add_column {
Subroutine add_column: called CC=3 SCAR=14.2
Pod documented
83 0 ▶ my $self = shift;
Subroutine add_column: not called CC=3 SCAR=14.2
Pod documented
84 0 my ($name, $how) = @_;
85 0 ▶ $self->_no_actions("column '$name': 'references'", $self->_check_references($name, $how->{references})) if ref($how) eq 'HASH' && exists $how->{references};
Branch: 2 branches
BranchTrueFalseTotal
if ref $how eq "HASH" and exists $how->{"references"} 6 77 83
if ref $how eq "HASH" and exists $how->{"references"} 0 0 0
Condition50%3/6
ref $how eq "HASH" and exists $how->{"references"}
!ll&&!rl&&r
2756
ref $how eq "HASH" and exists $how->{"references"}
!ll&&!rl&&r
000
MC/DC100%2/2
ref $how eq "HASH" and exists $how->{"references"}
ref $how eq "HASH" exists $how->{"references"}
MC/DC0%0/2
ref $how eq "HASH" and exists $how->{"references"}
ref $how eq "HASH" exists $how->{"references"}
Truth table100%3/3
A and B
A= ref $how eq "HASH"B= exists $how->{"references"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $how eq "HASH"B= exists $how->{"references"}
ABresult
0X0
100
111
86 0 return $self->SUPER::add_column(@_);
87 }
88  
89 4 ▶sub add_foreign_key {
Subroutine add_foreign_key: called CC=2 SCAR=10.3
Pod documented
90 0 ▶ my $self = shift;
Subroutine add_foreign_key: not called CC=2 SCAR=10.3
Pod documented
91 0 my (undef, $opts) = @_;
92 0 ▶ $self->_no_actions('add_foreign_key()', $opts) if ref($opts) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
if ref $opts eq "HASH" 4 0 4
if ref $opts eq "HASH" 0 0 0
93 0 return $self->SUPER::add_foreign_key(@_);
94 }
95  
96 10 ▶sub _no_actions {
Subroutine _no_actions: called CC=2 SCAR=9.2
97 0 ▶ my $self = shift;
Subroutine _no_actions: not called CC=2 SCAR=9.2
98 0 my ($what, $opts) = @_;
99 0 my @actions = grep { exists $opts->{$_} } qw/on_delete on_update/;
100 0 ▶ croak "$what: a temporary table's foreign keys are links in the map only, so they take no '" . join("' or '" => @actions) . "'" if @actions;
Branch: 2 branches
BranchTrueFalseTotal
if @actions 5 5 10
if @actions 0 0 0
101 }
102  
103 =pod
104  
105 =over 4
106  
107 =item $temp = $builder->build
108  
109 Creates the table and returns its L<DBIx::QuickORM::Source::Table::Temp>.
110 Croaks when called twice, when there are no columns, or when the primary key
111 is given both ways or names a column not added.
112  
113 =back
114  
115 =cut
116  
117 44 ▶sub build {
Subroutine build: called CC=1 SCAR=0.6
Pod documented
118 0 ▶ my $self = shift;
Subroutine build: not called CC=1 SCAR=0.6
Pod documented
119 0 ▶ return $self->_finish(sub { $self->orm->_build_temp_table($self) });
Subroutine __ANON__: called CC=1 SCAR=0.4
120 }
121  
122 260 1;
123  
124 __END__
« blib/lib/DBIx/QuickORM/Connection/Offline.pm ↑ Summary blib/lib/DBIx/QuickORM/Exception/DB.pm »