| 1 | package DBIx::QuickORM::Debug; | ||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 5 | our $VERSION = '0.000033'; | |||||||||
| 6 | |||||||||||
| 7 | 5 | ▶ | use Hash::Util::FieldHash qw/fieldhash/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 5 | ▶ | use Object::HashBase qw{ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | caller | ||||||||||
| 11 | +notes | ||||||||||
| 12 | +clears | ||||||||||
| 13 | 5 | }; | |||||||||
| 14 | |||||||||||
| 15 | =pod | ||||||||||
| 16 | |||||||||||
| 17 | =encoding UTF-8 | ||||||||||
| 18 | |||||||||||
| 19 | =head1 NAME | ||||||||||
| 20 | |||||||||||
| 21 | DBIx::QuickORM::Debug - What the ORM records with debug => 1. | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | The ORM builds one of these when given C<< debug => 1 >>, and passes it to its | ||||||||||
| 26 | Connection, the Connection's lookup table, and its row state. Without | ||||||||||
| 27 | C<debug> none is built, this module is never loaded, and C<< $orm->debug >> | ||||||||||
| 28 | is undef. | ||||||||||
| 29 | |||||||||||
| 30 | It records who made a row hash stop being canonical (see | ||||||||||
| 31 | L<DBIx::QuickORM::LookupTable::Identity/"Who made a hash stop being canon">): | ||||||||||
| 32 | |||||||||||
| 33 | =over 4 | ||||||||||
| 34 | |||||||||||
| 35 | =item * | ||||||||||
| 36 | |||||||||||
| 37 | The C<caller> slot: the public call in progress, as a frame of package, file, | ||||||||||
| 38 | line, and the method called. | ||||||||||
| 39 | |||||||||||
| 40 | =item * | ||||||||||
| 41 | |||||||||||
| 42 | Notes on row hashes, keyed by the hash itself (a fieldhash), never stored in | ||||||||||
| 43 | it: why a hash stopped being canon, and which call marked it deleted. | ||||||||||
| 44 | |||||||||||
| 45 | =item * | ||||||||||
| 46 | |||||||||||
| 47 | The call that last cleared each table, or everything. A reconnect counts as | ||||||||||
| 48 | clearing everything. | ||||||||||
| 49 | |||||||||||
| 50 | =back | ||||||||||
| 51 | |||||||||||
| 52 | It refers to nothing else, so anything may hold it strongly. A reconnect | ||||||||||
| 53 | passes the same object to the new Connection, so its notes outlive the old | ||||||||||
| 54 | lookup table. | ||||||||||
| 55 | |||||||||||
| 56 | =head2 The caller slot | ||||||||||
| 57 | |||||||||||
| 58 | Public APIs that may write or replace canonical row data set the slot to | ||||||||||
| 59 | their caller, unless it is already set, so the outermost such call is the one | ||||||||||
| 60 | recorded. It clears when they return: | ||||||||||
| 61 | |||||||||||
| 62 | my $debug = $orm->debug; | ||||||||||
| 63 | local $debug->{caller} = [(caller(0))[0 .. 3]] if $debug && !$debug->{caller}; | ||||||||||
| 64 | |||||||||||
| 65 | No call stack is walked. Code that does its work further down passes the | ||||||||||
| 66 | public call's frame down and sets the slot there. | ||||||||||
| 67 | |||||||||||
| 68 | =head1 SYNOPSIS | ||||||||||
| 69 | |||||||||||
| 70 | my $orm = DBIx::QuickORM->new(credentials => \%credentials, debug => 1); | ||||||||||
| 71 | my $debug = $orm->debug; | ||||||||||
| 72 | |||||||||||
| 73 | my $frame = $debug->caller; # [$package, $file, $line, $method] or undef | ||||||||||
| 74 | |||||||||||
| 75 | =head1 ATTRIBUTES | ||||||||||
| 76 | |||||||||||
| 77 | =over 4 | ||||||||||
| 78 | |||||||||||
| 79 | =item $frame = $debug->caller | ||||||||||
| 80 | |||||||||||
| 81 | The public call in progress, or undef. Set with C<local> as shown above. | ||||||||||
| 82 | |||||||||||
| 83 | =back | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | 12 | ▶ | sub init { | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 88 | 12 | my $self = shift; | |||||||||
| 89 | 12 | fieldhash my %notes; | |||||||||
| 90 | 12 | $self->{+NOTES} = \%notes; | |||||||||
| 91 | 12 | $self->{+CLEARS} = {}; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | =head1 PUBLIC METHODS | ||||||||||
| 95 | |||||||||||
| 96 | =over 4 | ||||||||||
| 97 | |||||||||||
| 98 | =item $debug->note_decanon($cause, \%row_data) | ||||||||||
| 99 | |||||||||||
| 100 | Notes that the hash stopped being canonical, and why (C<replaced> or | ||||||||||
| 101 | C<cache_cleared>), with the C<caller> slot's frame. Does nothing for undef. | ||||||||||
| 102 | |||||||||||
| 103 | =item $note = $debug->decanon(\%row_data) | ||||||||||
| 104 | |||||||||||
| 105 | The note: C<[$cause, $frame_or_undef]>, or undef. | ||||||||||
| 106 | |||||||||||
| 107 | my $debug = DBIx::QuickORM::Debug->new; | ||||||||||
| 108 | my $data = { | ||||||||||
| 109 | id => 1, | ||||||||||
| 110 | name => 'Ann', | ||||||||||
| 111 | }; | ||||||||||
| 112 | { | ||||||||||
| 113 | local $debug->{caller} = ['My::App', 'lib/My/App.pm', 12, 'DBIx::QuickORM::find']; | ||||||||||
| 114 | $debug->note_decanon(replaced => $data); | ||||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | my $note = $debug->decanon($data); | ||||||||||
| 118 | # ['replaced', ['My::App', 'lib/My/App.pm', 12, 'DBIx::QuickORM::find']] | ||||||||||
| 119 | |||||||||||
| 120 | =back | ||||||||||
| 121 | |||||||||||
| 122 | =cut | ||||||||||
| 123 | |||||||||||
| 124 | 35 | ▶ | sub decanon { my $n = $_[0]->{+NOTES}->{$_[1]} or return undef; $n->{decanon} } | ||||||||
|
Subroutine
decanon: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 125 | |||||||||||
| 126 | 30 | ▶ | sub note_decanon { | ||||||||
|
Subroutine
note_decanon: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 127 | 30 | my $self = shift; | |||||||||
| 128 | 30 | my ($cause, $data) = @_; | |||||||||
| 129 | 30 | ▶ | $self->{+NOTES}->{$data}->{decanon} = [$cause, $self->{+CALLER}] if $data; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 30 | return; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | =over 4 | ||||||||||
| 134 | |||||||||||
| 135 | =item $debug->note_deleted(\%row_data) | ||||||||||
| 136 | |||||||||||
| 137 | Notes the C<caller> slot's frame as the call that marked the hash deleted, | ||||||||||
| 138 | replacing any older note (undef when the slot is empty). | ||||||||||
| 139 | |||||||||||
| 140 | =item $frame = $debug->deleted_by(\%row_data) | ||||||||||
| 141 | |||||||||||
| 142 | That frame, or undef. | ||||||||||
| 143 | |||||||||||
| 144 | my $debug = DBIx::QuickORM::Debug->new; | ||||||||||
| 145 | my $data = { | ||||||||||
| 146 | id => 1, | ||||||||||
| 147 | name => 'Ann', | ||||||||||
| 148 | }; | ||||||||||
| 149 | { | ||||||||||
| 150 | local $debug->{caller} = ['main', 'app.pl', 7, 'DBIx::QuickORM::Row::delete']; | ||||||||||
| 151 | $debug->note_deleted($data); | ||||||||||
| 152 | } | ||||||||||
| 153 | my $by = $debug->deleted_by($data); # ['main', 'app.pl', 7, 'DBIx::QuickORM::Row::delete'] | ||||||||||
| 154 | |||||||||||
| 155 | $debug->note_deleted($data); # again, with no call in progress | ||||||||||
| 156 | my $now = $debug->deleted_by($data); # undef: the older frame is gone | ||||||||||
| 157 | |||||||||||
| 158 | =back | ||||||||||
| 159 | |||||||||||
| 160 | =cut | ||||||||||
| 161 | |||||||||||
| 162 | 7 | ▶ | sub deleted_by { my $n = $_[0]->{+NOTES}->{$_[1]} or return undef; $n->{deleted_by} } | ||||||||
|
Subroutine
deleted_by: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 163 | |||||||||||
| 164 | 7 | ▶ | sub note_deleted { | ||||||||
|
Subroutine
note_deleted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 165 | 7 | my $self = shift; | |||||||||
| 166 | 7 | my ($data) = @_; | |||||||||
| 167 | 7 | $self->{+NOTES}->{$data}->{deleted_by} = $self->{+CALLER}; # a delete a rollback undid must not lend its frame to a later one | |||||||||
| 168 | 7 | return; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | =over 4 | ||||||||||
| 172 | |||||||||||
| 173 | =item $debug->note_clear | ||||||||||
| 174 | |||||||||||
| 175 | =item $debug->note_clear($table) | ||||||||||
| 176 | |||||||||||
| 177 | Records the C<caller> slot as the call that cleared everything (which resets | ||||||||||
| 178 | every table's record) or the table, even when the slot is empty. | ||||||||||
| 179 | |||||||||||
| 180 | =item $frame = $debug->last_clear($table) | ||||||||||
| 181 | |||||||||||
| 182 | The call that last cleared the table or everything, or undef. | ||||||||||
| 183 | |||||||||||
| 184 | my $debug = DBIx::QuickORM::Debug->new; | ||||||||||
| 185 | { | ||||||||||
| 186 | local $debug->{caller} = ['main', 'app.pl', 3, 'DBIx::QuickORM::clear_cache']; | ||||||||||
| 187 | $debug->note_clear; # everything | ||||||||||
| 188 | } | ||||||||||
| 189 | $debug->note_clear('posts'); # one table, with no call in progress | ||||||||||
| 190 | |||||||||||
| 191 | my $users = $debug->last_clear('users'); # ['main', 'app.pl', 3, 'DBIx::QuickORM::clear_cache'] | ||||||||||
| 192 | my $posts = $debug->last_clear('posts'); # undef: a table's own record wins over an older clear of everything | ||||||||||
| 193 | |||||||||||
| 194 | =back | ||||||||||
| 195 | |||||||||||
| 196 | =cut | ||||||||||
| 197 | |||||||||||
| 198 | 16 | ▶ | sub last_clear { | ||||||||
|
Subroutine
last_clear: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 199 | 16 | my $self = shift; | |||||||||
| 200 | 16 | my ($table) = @_; | |||||||||
| 201 | 16 | my $clears = $self->{+CLEARS}; | |||||||||
| 202 | 16 | ▶ | return exists $clears->{$table} ? $clears->{$table} : $clears->{'*'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | 8 | ▶ | sub note_clear { | ||||||||
|
Subroutine
note_clear: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 206 | 8 | my $self = shift; | |||||||||
| 207 | 8 | my ($table) = @_; | |||||||||
| 208 | 8 | ▶ | if (defined $table) { $self->{+CLEARS}->{$table} = $self->{+CALLER} } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 209 | 3 | else { $self->{+CLEARS} = {'*' => $self->{+CALLER}} } | |||||||||
| 210 | 8 | return; | |||||||||
| 211 | } | ||||||||||
| 212 | |||||||||||
| 213 | 5 | 1; | |||||||||
| 214 | |||||||||||
| 215 | __END__ | ||||||||||