| 1 | package App::DrivePlayer::DB; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use App::DrivePlayer::Setup; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use File::Path qw( make_path ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use App::DrivePlayer::Schema; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | has path => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | isa => Str, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | has schema => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | is => 'lazy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | isa => InstanceOf['App::DrivePlayer::Schema'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | builder => '_build_schema', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 36 | ▶ | sub _build_schema { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_schema: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 36 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 36 | ▶ | make_path(dirname($self->path)) unless -d dirname($self->path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 36 | return App::DrivePlayer::Schema->connect_and_deploy($self->path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # Trigger schema build (and thus table creation) at construction time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 36 | ▶ | sub BUILD { $_[0]->schema } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # ---- Helpers ---- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # Maximum character lengths for text fields written to the database. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Long values are silently truncated; integers are never truncated. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | my %MAX_LEN = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | title => 500, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | artist => 255, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | album => 255, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | genre => 100, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | comment => 500, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | mime_type => 100, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | modified_time => 50, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | name => 500, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | path => 500, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | folder_path => 500, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 508 | ▶ | sub _trunc { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_trunc: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 508 | my ($val, $field) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 508 | ▶ | return $val unless defined $val && exists $MAX_LEN{$field}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $val and exists $MAX_LEN{$field}
MC/DC100%2/2
defined $val and exists $MAX_LEN{$field}
defined $val
exists $MAX_LEN{$field}
Truth table100%3/3
A and B
A= defined $valB= exists $MAX_LEN{$field}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 307 | my $max = $MAX_LEN{$field}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 307 | ▶ | return length($val) > $max ? substr($val, 0, $max) : $val; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # Convert a DBIC Row object to a plain hashref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 90 | ▶ | sub _row_to_hash { my $row = shift; return $row ? { $row->get_columns() } : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_row_to_hash: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Return a resultset shorthand. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 239 | ▶ | sub _rs { $_[0]->schema->resultset($_[1]) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_rs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # ---------- scan_folders ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 30 | ▶ | sub upsert_scan_folder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
upsert_scan_folder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 30 | my ($self, $drive_id, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 30 | my $row = $self->_rs('ScanFolder')->update_or_create( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | { drive_id => $drive_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | name => _trunc($name, 'name') }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | { key => 'unique_drive_id' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 30 | return _row_to_hash($row); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub get_scan_folder_by_drive_id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_scan_folder_by_drive_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | my ($self, $drive_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | return _row_to_hash( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | $self->_rs('ScanFolder')->find({ drive_id => $drive_id }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | sub all_scan_folders { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_scan_folders: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | $self->_rs('ScanFolder')->search({}, { order_by => 'name' })->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | sub delete_scan_folder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete_scan_folder: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | my ($self, $drive_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # cascade_delete on the has_many relationship removes child folders+tracks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | my $row = $self->_rs('ScanFolder')->find({ drive_id => $drive_id }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | $row->delete if $row; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # ---------- folders ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 26 | ▶ | sub upsert_folder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
upsert_folder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 26 | my ($self, %f) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | my $row = $self->_rs('Folder')->update_or_create( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | drive_id => $f{drive_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | name => _trunc($f{name}, 'name'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | parent_drive_id => $f{parent_drive_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | path => _trunc($f{path}, 'path'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | scan_folder_id => $f{scan_folder_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 26 | { key => 'unique_drive_id' }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 26 | return _row_to_hash($row); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # ---------- tracks ---------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 2 | my @STRUCTURAL_FIELDS = qw( folder_id folder_path mime_type size modified_time ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | my @METADATA_FIELDS = qw( title artist album track_number year duration_ms genre comment ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 52 | ▶ | sub upsert_track { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
upsert_track: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 52 | my ($self, %t) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 52 | my $existing = $self->_rs('Track')->find({ drive_id => $t{drive_id} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 52 | ▶ | if ($existing) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # Always refresh structural fields from the Drive scan. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # Preserve metadata fields that are already populated; only fill in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | # nulls from the scan's filename-derived values. This ensures that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | # metadata loaded from the sheet (or set via AcoustID) is never | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # silently overwritten by a re-scan. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 2 | my %update = map { $_ => _trunc($t{$_}, $_) } @STRUCTURAL_FIELDS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | for my $f (@METADATA_FIELDS) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 16 | my $cur = $existing->get_column($f); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | # Treat the drive_id placeholder as absent for the title field. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 16 | ▶ | my $absent = !defined $cur || $cur eq '' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition88%8/9
!defined($cur) || $cur eq '' || $f eq 'title' && $cur eq $existing->drive_id
not defined $cur or $cur eq ""
$f eq "title" and $cur eq $existing->drive_id
MC/DC75%3/4
!defined($cur) || $cur eq '' || $f eq 'title' && $cur eq $existing->drive_id
not defined $cur
$cur eq ""
$f eq "title"
$cur eq $existing->drive_id
Truth table80%4/5
not(A) or B || C and D
A= not defined $curB= $cur eq ""C= $f eq "title"D= $cur eq $existing->drive_id
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | || ($f eq 'title' && $cur eq $existing->drive_id); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 16 | ▶ | $update{$f} = _trunc($t{$f}, $f) if $absent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | $existing->update(\%update); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | $self->_rs('Track')->create({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | drive_id => $t{drive_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | title => _trunc($t{title}, 'title'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | artist => _trunc($t{artist}, 'artist'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | album => _trunc($t{album}, 'album'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | track_number => $t{track_number}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | year => $t{year}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | duration_ms => $t{duration_ms}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | size => $t{size}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | mime_type => _trunc($t{mime_type}, 'mime_type'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | modified_time => _trunc($t{modified_time}, 'modified_time'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | folder_id => $t{folder_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | folder_path => _trunc($t{folder_path}, 'folder_path'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | genre => _trunc($t{genre}, 'genre'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 50 | comment => _trunc($t{comment}, 'comment'), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | sub update_track_metadata { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_track_metadata: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | my ($self, $id, %fields) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my $row = $self->_rs('Track')->find($id) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my %allowed = map { $_ => 1 } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | qw( title artist album track_number year duration_ms genre comment ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my %update = map { $_ => $fields{$_} } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | grep { $allowed{$_} } keys %fields; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | $row->update(\%update) if %update; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | # Count tracks whose $field exactly equals $value, optionally excluding one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # track id. Used to decide whether an edit should offer to propagate a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | # rename to siblings (e.g. cleaning up mojibake artist/album/genre values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # without having to revisit every track). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub count_tracks_with_field { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count_tracks_with_field: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my ($self, $field, $value, $exclude_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | return 0 unless defined $value && length $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $value and length $value
MC/DC0%0/2
defined $value and length $value
defined $value
length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | my %where = ($field => $value); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | $where{id} = { '!=' => $exclude_id } if defined $exclude_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | return $self->_rs('Track')->search(\%where)->count; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # Bulk-rename: set $field = $new for every track where $field = $old. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # Returns the number of rows updated. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub rename_field { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rename_field: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my ($self, $field, $old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | return 0 unless defined $old && length $old; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $old and length $old
MC/DC0%0/2
defined $old and length $old
defined $old
length $old
Truth table0%0/3
A and B
A= defined $oldB= length $old
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | return $self->_rs('Track')->search({ $field => $old }) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | ->update({ $field => $new }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | # Track ids whose $field exactly equals $value. Used to drive a targeted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | # sheet sync after a bulk rename so we don't rewrite the whole spreadsheet. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub track_ids_with_field { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
track_ids_with_field: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my ($self, $field, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | return () unless defined $value && length $value; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $value and length $value
MC/DC0%0/2
defined $value and length $value
defined $value
length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | return $self->_rs('Track')->search({ $field => $value }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | { columns => ['id'] }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | ->get_column('id')->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | # Insert or update a track using only the metadata fields available from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | # sheet (drive_id + title/artist/album etc.). Structural fields (folder_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | # mime_type, size, …) are left null or at their placeholder values so that a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | # subsequent Drive scan can fill them in via upsert_track. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 1 | ▶ | sub upsert_track_from_metadata { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
upsert_track_from_metadata: called CC=7 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | my ($self, %fields) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | $self->_rs('Track')->update_or_create( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | drive_id => $fields{drive_id}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | title => _trunc($fields{title} || $fields{drive_id}, 'title'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | mime_type => _trunc($fields{mime_type} || 'audio/', 'mime_type'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | folder_id => $fields{folder_id} // undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | artist => _trunc($fields{artist}, 'artist'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | album => _trunc($fields{album}, 'album'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | track_number => $fields{track_number} // undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | year => $fields{year} // undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | duration_ms => $fields{duration_ms} // undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | genre => _trunc($fields{genre}, 'genre'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | ▶ | comment => _trunc($fields{comment}, 'comment'), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition46%6/13
$fields{'title'} || $fields{'drive_id'}
$fields{'mime_type'} || 'audio/'
$fields{"folder_id"} // undef
$fields{"track_number"} // undef
$fields{"year"} // undef
$fields{"duration_ms"} // undef
MC/DC0%0/2
$fields{'title'} || $fields{'drive_id'}
$fields{'title'}
$fields{'drive_id'}
MC/DC0%0/1
$fields{'mime_type'} || 'audio/'
$fields{'mime_type'}
MC/DC0%0/1
$fields{"folder_id"} // undef
$fields{"folder_id"}
MC/DC0%0/1
$fields{"track_number"} // undef
$fields{"track_number"}
MC/DC0%0/1
$fields{"year"} // undef
$fields{"year"}
MC/DC0%0/1
$fields{"duration_ms"} // undef
$fields{"duration_ms"}
Truth table33%1/3
A || B
A= $fields{'title'}B= $fields{'drive_id'}
Truth table50%1/2
A
A= $fields{'mime_type'}
Truth table50%1/2
A
A= $fields{"folder_id"}
Truth table50%1/2
A
A= $fields{"track_number"}
Truth table50%1/2
A
A= $fields{"year"}
Truth table50%1/2
A
A= $fields{"duration_ms"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | { key => 'unique_drive_id' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 2 | ▶ | sub get_track { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_track: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 2 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | return _row_to_hash( $self->_rs('Track')->find($id) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # Walk Track → Folder → ScanFolder and return the scan folder hashref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | # Used by targeted single-row sheet sync so it knows which tab to update. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | sub scan_folder_for_track { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
scan_folder_for_track: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | my ($self, $track_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 0 | ▶ | my $track = $self->_rs('Track')->find($track_id) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | my $folder = $track->folder or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | ▶ | my $sf = $folder->scan_folder or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | return _row_to_hash($sf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 6 | ▶ | sub get_track_by_drive_id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_track_by_drive_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 6 | my ($self, $drive_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 6 | return _row_to_hash( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | $self->_rs('Track')->find({ drive_id => $drive_id }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 2 | my @TRACK_ORDER = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | \['LOWER(artist) NULLS LAST'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | \['LOWER(album) NULLS LAST'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | \['track_number NULLS LAST'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | \['LOWER(title)'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 2 | ▶ | sub all_tracks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_tracks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | $self->_rs('Track')->search({}, { order_by => \@TRACK_ORDER })->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 2 | ▶ | sub tracks_by_artist { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tracks_by_artist: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 2 | my ($self, $artist) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | $self->_rs('Track')->search( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | \[ 'LOWER(artist) = LOWER(?)', $artist ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | { order_by => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | \['LOWER(album) NULLS LAST'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | \['track_number NULLS LAST'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | \['LOWER(title)'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | ]}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | )->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 2 | ▶ | sub tracks_by_album { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tracks_by_album: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 2 | my ($self, $album) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 2 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | $self->_rs('Track')->search( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | \[ 'LOWER(album) = LOWER(?)', $album ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | { order_by => [ \['track_number NULLS LAST'], \['LOWER(title)'] ] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | )->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 3 | ▶ | sub tracks_by_folder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tracks_by_folder: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 3 | my ($self, $folder_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 3 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | $self->_rs('Track')->search( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | { folder_id => $folder_id }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | { order_by => [ \['track_number NULLS LAST'], \['LOWER(title)'] ] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | )->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | sub tracks_by_scan_folder { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tracks_by_scan_folder: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | my ($self, $scan_folder_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | $self->_rs('Track')->search( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | { 'folder.scan_folder_id' => $scan_folder_id }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | { join => 'folder', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | order_by => \@TRACK_ORDER }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | )->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 4 | ▶ | sub search_tracks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search_tracks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 4 | my ($self, $query) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 4 | my $like = "%$query%"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 4 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | $self->_rs('Track')->search( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | { -or => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | title => { like => $like }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | artist => { like => $like }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | album => { like => $like }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | ]}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | { order_by => \@TRACK_ORDER }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | )->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | # Distinct, non-empty values of a single text column, ordered case-insensitively. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 2 | ▶ | sub _distinct_nonempty { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_distinct_nonempty: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 2 | my ($self, $col) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 2 | my @vals = $self->_rs('Track')->search( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | { $col => [ -and => { '!=' => undef }, { '!=' => '' } ] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | { columns => [$col], distinct => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | )->get_column($col)->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 2 | my @sorted = sort { lc($a) cmp lc($b) } @vals; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 2 | return @sorted; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | sub all_genres { $_[0]->_distinct_nonempty('genre') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_genres: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 1 | ▶ | sub all_artists { $_[0]->_distinct_nonempty('artist') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_artists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | ▶ | sub all_albums { $_[0]->_distinct_nonempty('album') } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
all_albums: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | sub tracks_by_genre { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tracks_by_genre: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | my ($self, $genre) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | $self->_rs('Track')->search( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | \[ 'LOWER(genre) = LOWER(?)', $genre ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | { order_by => \@TRACK_ORDER }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | )->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 15 | ▶ | sub track_count { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
track_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 15 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 15 | return $self->_rs('Track')->count; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | sub tracks_needing_metadata { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tracks_needing_metadata: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | my ($self, $scan_folder_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | my %cond = ( metadata_fetched => 0 ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | my %attr = ( order_by => \@TRACK_ORDER ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | if (defined $scan_folder_id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | $cond{'folder.scan_folder_id'} = $scan_folder_id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | $attr{join} = 'folder'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | return map { _row_to_hash($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | $self->_rs('Track')->search(\%cond, \%attr)->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | sub mark_metadata_fetched { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mark_metadata_fetched: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | my $row = $self->_rs('Track')->find($id) or return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | $row->update({ metadata_fetched => 1 }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | sub reset_metadata_fetched { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_metadata_fetched: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | $self->_rs('Track')->update_all({ metadata_fetched => 0 }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | sub reset_metadata_fetched_incomplete { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_metadata_fetched_incomplete: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | $self->_rs('Track')->search({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | metadata_fetched => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | -or => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | genre => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | genre => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | artist => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | artist => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | album => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | album => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | year => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | })->update_all({ metadata_fetched => 0 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 1 | ▶ | sub clear_scan_folder_tracks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
clear_scan_folder_tracks: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 1 | my ($self, $scan_folder_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | # Collect folder IDs, then bulk-delete tracks and folders. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | # (Relies on foreign_keys=ON cascade for correctness; explicit here for speed.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 1 | my @folder_ids = $self->_rs('Folder') | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | ->search({ scan_folder_id => $scan_folder_id }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | ->get_column('id')->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 1 | ▶ | if (@folder_ids) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 1 | $self->_rs('Track')->search({ folder_id => \@folder_ids })->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 1 | $self->_rs('Folder')->search({ scan_folder_id => $scan_folder_id })->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | # Resultset of tracks under $scan_folder_id whose drive_id is NOT in %$seen. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | # Returns undef if the scan folder has no folders (nothing to consider). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 14 | ▶ | sub _unseen_tracks_rs { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unseen_tracks_rs: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 14 | my ($self, $scan_folder_id, $seen) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 14 | my @keep = keys %$seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 14 | my @folder_ids = $self->_rs('Folder') | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | ->search({ scan_folder_id => $scan_folder_id }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | ->get_column('id')->all; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 14 | ▶ | return unless @folder_ids; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 14 | ▶ | return $self->_rs('Track')->search({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | folder_id => { -in => \@folder_ids }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | (@keep ? (drive_id => { -not_in => \@keep }) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 7 | ▶ | sub count_unseen_tracks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count_unseen_tracks: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 7 | my ($self, $scan_folder_id, $seen) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 7 | ▶ | my $rs = $self->_unseen_tracks_rs($scan_folder_id, $seen) or return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 7 | return $rs->count; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 7 | ▶ | sub remove_unseen_tracks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_unseen_tracks: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 7 | my ($self, $scan_folder_id, $seen) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 7 | ▶ | my $rs = $self->_unseen_tracks_rs($scan_folder_id, $seen) or return 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | 7 | return $rs->delete; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 7 | ▶ | sub remove_unseen_folders { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remove_unseen_folders: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 7 | my ($self, $scan_folder_id, $seen) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 7 | my @keep = keys %$seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 7 | ▶ | return $self->_rs('Folder')->search({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | scan_folder_id => $scan_folder_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | (@keep ? (drive_id => { -not_in => \@keep }) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | })->delete; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||