| 1 | package DBIx::QuickORM::Source::Table::Temp; | ||||||||||||||||||||||||||||||||||||||
| 2 | 258 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 3 | 258 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | 258 | our $VERSION = '0.000033'; | |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 258 | ▶ | use Carp qw/croak/; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 258 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||
| 10 | 258 | ▶ | use parent 'DBIx::QuickORM::Source::Table'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | 258 | ▶ | use Object::HashBase qw{ | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 12 | <orm | ||||||||||||||||||||||||||||||||||||||
| 13 | <kind | ||||||||||||||||||||||||||||||||||||||
| 14 | +connection | ||||||||||||||||||||||||||||||||||||||
| 15 | +from_handle | ||||||||||||||||||||||||||||||||||||||
| 16 | +record | ||||||||||||||||||||||||||||||||||||||
| 17 | 258 | }; | |||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | =pod | ||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | DBIx::QuickORM::Source::Table::Temp - A temporary table or view the ORM created, as a source. | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | What C<temp_table>, C<temp_view>, and C<temp> return: a | ||||||||||||||||||||||||||||||||||||||
| 30 | L<DBIx::QuickORM::Source::Table> over one temporary table or view, so a | ||||||||||||||||||||||||||||||||||||||
| 31 | handle reads from it directly (C<< $orm->handle($temp) >>). The connection | ||||||||||||||||||||||||||||||||||||||
| 32 | owns the table or view itself, so letting this object go drops nothing. | ||||||||||||||||||||||||||||||||||||||
| 33 | C<< $orm->temp($name) >> gives a new one, and C<< $orm->temp_tables >> lists | ||||||||||||||||||||||||||||||||||||||
| 34 | the names. | ||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||
| 36 | A temporary table or view belongs to the connection that created it. It ends | ||||||||||||||||||||||||||||||||||||||
| 37 | when dropped, when the transaction that created it rolls back, or when that | ||||||||||||||||||||||||||||||||||||||
| 38 | connection ends (a fork or reconnect replaces it). After that, C<handle>, | ||||||||||||||||||||||||||||||||||||||
| 39 | C<drop>, and every use as a source croak saying why, and C<is_live> is false. | ||||||||||||||||||||||||||||||||||||||
| 40 | The object knows the exact table it was made for, so it never acts on a later | ||||||||||||||||||||||||||||||||||||||
| 41 | one that reuses the name. | ||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | my $t = $orm->temp_table(sub { | ||||||||||||||||||||||||||||||||||||||
| 46 | my $t = shift; | ||||||||||||||||||||||||||||||||||||||
| 47 | $t->add_column('id', { | ||||||||||||||||||||||||||||||||||||||
| 48 | db_type => 'integer', | ||||||||||||||||||||||||||||||||||||||
| 49 | primary_key => 1, | ||||||||||||||||||||||||||||||||||||||
| 50 | }); | ||||||||||||||||||||||||||||||||||||||
| 51 | }); | ||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||
| 53 | $t->handle->insert({id => 1}); | ||||||||||||||||||||||||||||||||||||||
| 54 | my @rows = $orm->handle($t)->all; | ||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | $t->drop; | ||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | As L<DBIx::QuickORM::Source::Table>, plus: | ||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||
| 62 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||
| 64 | =item $orm = $temp->orm | ||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||
| 66 | The ORM. Required. | ||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||
| 68 | =item $kind = $temp->kind | ||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||
| 70 | C<table> or C<view>. Required. | ||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||
| 72 | =back | ||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | =cut | ||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | 58 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=6 SCAR=25.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
init: not called CC=6 SCAR=25.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | croak "'orm' is required" unless $self->{+ORM}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | croak "'kind' must be 'table' or 'view'" unless ($self->{+KIND} // '') =~ m/^(?:table|view)\z/; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition25%1/4
$self->{"kind"} // ""
$self->{"kind"} // ""
MC/DC0%0/1
$self->{"kind"} // ""
$self->{"kind"}
MC/DC0%0/1
$self->{"kind"} // ""
$self->{"kind"}
Truth table50%1/2
A
A= $self->{"kind"}
Truth table0%0/2
A
A= $self->{"kind"}
| |||||||||||||||||||||||||||||||||||||||
| 80 | 0 | $self->SUPER::init(); | |||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my $record = $self->{+DB_MAP}->table($self->{+TABLE}); | |||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | croak "The table '$self->{+TABLE}' is not a temporary table or view" unless $record->{temp}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $self->{+RECORD} = $record; | |||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $self->{+CONNECTION} //= $self->{+ORM}->connection; | ||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$self->{"connection"} //= $self->{"orm"}->connection
$self->{"connection"} //= $self->{"orm"}->connection
MC/DC0%0/2
$self->{"connection"} //= $self->{"orm"}->connection
$self->{"connection"}
$self->{"orm"}->connection
MC/DC0%0/2
$self->{"connection"} //= $self->{"orm"}->connection
$self->{"connection"}
$self->{"orm"}->connection
Truth table66%2/3
A //= B
A= $self->{"connection"}B= $self->{"orm"}->connection
Truth table0%0/3
A //= B
A= $self->{"connection"}B= $self->{"orm"}->connection
| |||||||||||||||||||||||||||||||||||||||
| 87 | 0 | weaken($self->{+CONNECTION}); | |||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||
| 94 | =item $name = $temp->name | ||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||
| 96 | Its name in the map, the same as C<table>. | ||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||
| 98 | =item $h = $temp->handle | ||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||
| 100 | A L<DBIx::QuickORM::Handle> over it, the same as C<< $orm->handle($temp) >>. | ||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||
| 102 | =item $temp->drop | ||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||
| 104 | Drops it. | ||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | my $txn = $orm->txn; | ||||||||||||||||||||||||||||||||||||||
| 107 | $t->drop; | ||||||||||||||||||||||||||||||||||||||
| 108 | $txn->rollback; | ||||||||||||||||||||||||||||||||||||||
| 109 | my $back = $t->is_live; # true: the rollback undid the drop | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | Dropped inside a transaction, a rollback of that transaction brings it back, | ||||||||||||||||||||||||||||||||||||||
| 112 | unless the same transaction created it (see | ||||||||||||||||||||||||||||||||||||||
| 113 | L<DBIx::QuickORM::Manual::TempTables>). | ||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | =item $bool = $temp->is_live | ||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||
| 117 | True while it exists for the ORM's current connection. | ||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | my $name = $t->name; | ||||||||||||||||||||||||||||||||||||||
| 120 | $t->drop; | ||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||
| 122 | my $new = $orm->temp_table( | ||||||||||||||||||||||||||||||||||||||
| 123 | sub { $_[0]->add_column('id', {db_type => 'integer'}) }, | ||||||||||||||||||||||||||||||||||||||
| 124 | name => $name, | ||||||||||||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||
| 127 | my $old_live = $t->is_live; # false: $t was dropped | ||||||||||||||||||||||||||||||||||||||
| 128 | my $new_live = $new->is_live; # true | ||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||
| 130 | False once it is gone, even when another temporary table or view now has its | ||||||||||||||||||||||||||||||||||||||
| 131 | name. | ||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||
| 133 | =back | ||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||
| 137 | 36 | ▶ | sub name { $_[0]->{+TABLE} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||
| 139 | 44 | ▶ | sub handle { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
handle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 140 | 44 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 141 | 44 | $self->_check; | |||||||||||||||||||||||||||||||||||||
| 142 | 40 | return $self->{+ORM}->handle($self); | |||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | 54 | ▶ | sub drop { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
drop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 146 | 54 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 147 | 54 | $self->_check->drop_temp($self->{+TABLE}); | |||||||||||||||||||||||||||||||||||||
| 148 | 51 | return; | |||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||
| 151 | 38 | ▶ | sub is_live { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
is_live: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 152 | 38 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 153 | 38 | ▶ | my $conn = $self->{+CONNECTION} or return 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 154 | 38 | ▶ | return 0 unless $self->{+ORM}->connection == $conn; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 155 | 38 | my $now = $self->{+DB_MAP}->table($self->{+TABLE}); | |||||||||||||||||||||||||||||||||||||
| 156 | 38 | ▶ | return $now && $now == $self->{+RECORD} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$now and $now == $self->{"record"}
MC/DC100%2/2
$now and $now == $self->{"record"}
$now
$now == $self->{"record"}
Truth table100%3/3
A and B
A= $nowB= $now == $self->{"record"}
| |||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||
| 159 | =pod | ||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||
| 161 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||
| 163 | =item \@fields = $temp->pk | ||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||
| 165 | =item $fields = $temp->select_fields($given) | ||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||
| 167 | =item ($sql, \@binds) = $temp->from_sql($dialect) | ||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||
| 169 | As L<DBIx::QuickORM::Source::Table>, after checking that it still exists, so a | ||||||||||||||||||||||||||||||||||||||
| 170 | handle made before it ended never reads another table. | ||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||
| 172 | =back | ||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||
| 176 | 19 | ▶ | sub pk { my $self = shift; $self->_check; $self->SUPER::pk(@_) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
pk: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 177 | 23 | ▶ | sub select_fields { my $self = shift; $self->_check; $self->SUPER::select_fields(@_) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
select_fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 178 | 21 | ▶ | sub from_sql { my $self = shift; $self->_check; $self->SUPER::from_sql(@_) } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
from_sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||
| 180 | =head1 PRIVATE METHODS | ||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||
| 182 | =over 4 | ||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||
| 184 | =item $conn = $temp->_check | ||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||
| 186 | The Connection that created it. Croaks when the ORM has another connection | ||||||||||||||||||||||||||||||||||||||
| 187 | now, when the table or view ended, or when its name now belongs to another | ||||||||||||||||||||||||||||||||||||||
| 188 | one. One that a handle's C<temp_table> or C<temp_view> made says how to get a | ||||||||||||||||||||||||||||||||||||||
| 189 | new one there. | ||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||
| 191 | =back | ||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||
| 195 | 161 | ▶ | sub _check { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
_check: called CC=8 SCAR=21.1
| |||||||||||||||||||||||||||||||||||||||
| 196 | 161 | my $self = shift; | |||||||||||||||||||||||||||||||||||||
| 197 | 161 | my ($name, $kind) = @{$self}{+TABLE, +KIND}; | |||||||||||||||||||||||||||||||||||||
| 198 | 161 | my $conn = $self->{+CONNECTION}; | |||||||||||||||||||||||||||||||||||||
| 199 | 161 | ▶ | croak "The temporary $kind '$name' belonged to a connection the ORM has since replaced (after a fork or reconnect), which took it along" unless $conn && $self->{+ORM}->connection == $conn; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$conn and $self->{"orm"}->connection == $conn
MC/DC0%0/2
$conn and $self->{"orm"}->connection == $conn
$conn
$self->{"orm"}->connection == $conn
Truth table33%1/3
A and B
A= $connB= $self->{"orm"}->connection == $conn
| |||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||
| 201 | 161 | my $now = $self->{+DB_MAP}->table($name); | |||||||||||||||||||||||||||||||||||||
| 202 | 161 | ▶ | return $conn if $now && $now == $self->{+RECORD}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$now and $now == $self->{"record"}
MC/DC100%2/2
$now and $now == $self->{"record"}
$now
$now == $self->{"record"}
Truth table100%3/3
A and B
A= $nowB= $now == $self->{"record"}
| |||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||
| 204 | 9 | ▶ | my $reason = $now ? "it was replaced by another temporary table or view of the same name" : $conn->temp_gone($name) // 'dropped'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$conn->temp_gone($name) // "dropped"
MC/DC0%0/1
$conn->temp_gone($name) // "dropped"
$conn->temp_gone($name)
Truth table50%1/2
A
A= $conn->temp_gone($name)
| |||||||||||||||||||||||||||||||||||||||
| 205 | 9 | ▶ | my $again = $self->{+FROM_HANDLE} ? ". For a new one, call temp_$kind(rebuild => 1) on its handle, or temp_$kind() there with arguments" : ''; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 206 | 9 | croak "The temporary $kind '$name' no longer exists: $reason$again"; | |||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||
| 209 | 258 | 1; | |||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||||||||||||||||||||