| 1 | package MojoX::MojoDbWrap; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use v5.24; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | { our $VERSION = '0.100' } | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use experimental qw< signatures >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Ouch qw< :trytiny_var >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 77 | ▶ | sub assert_condition ($name, $condition, $mdb) { | ||||||||||||||||||||||||||
|
Subroutine
assert_condition: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 11 | 77 | ▶ | ouch 400, "$name: undefined condition matches everything" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | unless defined($condition); | ||||||||||||||||||||||||||||
| 13 | 75 | ▶ | ouch 400, "$name: invalid condition, expected a reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | unless ref($condition); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 70 | ▶ | if ($mdb->can('abstract')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 55 | my $where = $mdb->abstract->where($condition); | |||||||||||||||||||||||||||
| 18 | 55 | ▶ | ouch 400, "$name: empty condition matches everything" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | if length($where) == 0; | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 58 | return $condition; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 217 | ▶ | sub assert_hash_reference ($name, $candidate, $msg = undef) { | ||||||||||||||||||||||||||
|
Subroutine
assert_hash_reference: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 26 | 217 | ▶ | return $candidate if ref($candidate) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 11 | ▶ | ouch 400, $name . ': ' | ||||||||||||||||||||||||||
|
Condition100%2/2
$msg // "invalid input, expected a hash reference"
MC/DC100%1/1
$msg // "invalid input, expected a hash reference"
$msg
Truth table100%2/2
A
A= $msg
| |||||||||||||||||||||||||||||
| 28 | . ($msg // 'invalid input, expected a hash reference'); | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 142 | ▶ | sub assert_new_style_opts ($name, $opts) { | ||||||||||||||||||||||||||
|
Subroutine
assert_new_style_opts: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 32 | 142 | assert_hash_reference($name, $opts); | |||||||||||||||||||||||||||
| 33 | ouch 400, "$name: old-style sort hashes are unsupported, use order_by" | ||||||||||||||||||||||||||||
| 34 | 138 | ▶ | if exists($opts->{-asc}) || exists($opts->{-desc}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $opts->{"-asc"} or exists $opts->{"-desc"}
MC/DC100%2/2
exists $opts->{"-asc"} or exists $opts->{"-desc"}
exists $opts->{"-asc"}
exists $opts->{"-desc"}
Truth table100%3/3
A or B
A= exists $opts->{"-asc"}B= exists $opts->{"-desc"}
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 21 | ▶ | sub assert_valid_keys ($name, $href, @keys) { | ||||||||||||||||||||||||||
|
Subroutine
assert_valid_keys: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 38 | 21 | ▶ | return unless defined($href); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 16 | assert_hash_reference($name, $href); | |||||||||||||||||||||||||||
| 40 | 15 | my %is_valid = map { $_ => 1 } @keys; | |||||||||||||||||||||||||||
| 41 | 15 | ▶ | my @bad = grep { ! $is_valid{$_} } keys($href->%*) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | my $what = @bad == 1 ? 'option' : 'options'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | ouch 400, "$name: unknown $what " . join(', ', sort @bad); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 19 | ▶ | sub coerce_wrappers ($x) { | ||||||||||||||||||||||||||
|
Subroutine
coerce_wrappers: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 47 | 19 | ▶ | $x //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$x //= []
MC/DC100%1/1
$x //= []
$x
Truth table100%2/2
A
A= $x
| |||||||||||||||||||||||||||||
| 48 | 19 | ▶ | ouch 400, 'invalid value for wrappers, need undef or array ref' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | unless ref($x) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 50 | my @wrappers = map { | ||||||||||||||||||||||||||||
| 51 | 18 | my $w = $x->[$_]; | |||||||||||||||||||||||||||
| 52 | 15 | assert_hash_reference('(' . __PACKAGE__ . ' constructor)', | |||||||||||||||||||||||||||
| 53 | $w, "item $_ in wrappers is not a hash ref"); | ||||||||||||||||||||||||||||
| 54 | # copy, so that the caller's data is not touched | ||||||||||||||||||||||||||||
| 55 | 14 | ▶ | +{ $w->%*, insert => $w->{insert} // \&default_inserter }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$w->{"insert"} // \&MojoX::MojoDbWrap::default_inserter
MC/DC100%1/1
$w->{"insert"} // \&MojoX::MojoDbWrap::default_inserter
$w->{"insert"}
Truth table100%2/2
A
A= $w->{"insert"}
| |||||||||||||||||||||||||||||
| 56 | } 0 .. $x->$#*; | ||||||||||||||||||||||||||||
| 57 | return [ | ||||||||||||||||||||||||||||
| 58 | @wrappers, | ||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | class => 'Mojo::Pg', | ||||||||||||||||||||||||||||
| 61 | 7 | ▶ | create => sub ($class, $url) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=10.5
| |||||||||||||||||||||||||||||
| 62 | 7 | ▶ | return unless $url =~ m{\A postgres (?:ql)? ://}imxs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | require Mojo::Pg; | |||||||||||||||||||||||||||
| 64 | 0 | return Mojo::Pg->new($url); | |||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||
| 66 | insert => \&default_inserter, | ||||||||||||||||||||||||||||
| 67 | can_limit => 1, | ||||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | class => 'Mojo::SQLite', | ||||||||||||||||||||||||||||
| 71 | 7 | ▶ | create => sub ($class, $url) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 7 | local $@; # don't clobber $EVAL_ERROR, we don't use it! | |||||||||||||||||||||||||||
| 73 | 7 | require Mojo::SQLite; | |||||||||||||||||||||||||||
| 74 | 5 | return eval { Mojo::SQLite->new($url) }; | |||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||
| 76 | 17 | insert => \&default_inserter, | |||||||||||||||||||||||||||
| 77 | can_limit => 1, | ||||||||||||||||||||||||||||
| 78 | }, | ||||||||||||||||||||||||||||
| 79 | ]; | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 67 | ▶ | sub explode_tablish ($t) { | ||||||||||||||||||||||||||
|
Subroutine
explode_tablish: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 83 | 67 | ▶ | my ($tbl, $idf) = ref($t) eq 'ARRAY' ? $t->@* : $t; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 67 | ▶ | return ($tbl, $idf // 'id'); | ||||||||||||||||||||||||||
|
Condition100%2/2
$idf // "id"
MC/DC100%1/1
$idf // "id"
$idf
Truth table100%2/2
A
A= $idf
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 17 | ▶ | sub isa_wrappers ($x) { | ||||||||||||||||||||||||||
|
Subroutine
isa_wrappers: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 88 | 17 | for my $i (0 .. $x->$#*) { | |||||||||||||||||||||||||||
| 89 | 39 | my $w = $x->[$i]; | |||||||||||||||||||||||||||
| 90 | 39 | ▶ | ouch 400, "item $i lacks class name" unless defined($w->{class}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | ouch 400, "item $i lacks create sub" | ||||||||||||||||||||||||||||
| 92 | 38 | ▶ | unless ref($w->{create}) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | ouch 400, "item $i lacks insert sub" | ||||||||||||||||||||||||||||
| 94 | 36 | ▶ | unless ref($w->{insert}) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 13 | return; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 129 | ▶ | sub sub_name ($default, $name, $level = 2) { | ||||||||||||||||||||||||||
|
Subroutine
sub_name: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 100 | 129 | ▶ | return $name if defined($name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 105 | my $subroutine = (caller($level))[3]; | |||||||||||||||||||||||||||
| 102 | 105 | ▶ | return $default if !defined($subroutine); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 105 | ▶ | return $default if $subroutine eq '(eval)'; # this would be... *meh* | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 104 | return ($subroutine =~ s{\A.*::}{}rmxs); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 5 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # actual input stuff | ||||||||||||||||||||||||||||
| 110 | 5 | has db_url => (is => 'ro', required => 1); | |||||||||||||||||||||||||||
| 111 | 11 | ▶ | has migrations_for => (is => 'ro', default => sub { return {} }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 5 | has _wrapped => (is => 'lazy', init_arg => undef); | |||||||||||||||||||||||||||
| 113 | 5 | has _wrappers => ( | |||||||||||||||||||||||||||
| 114 | is => 'ro', | ||||||||||||||||||||||||||||
| 115 | init_arg => 'wrappers', | ||||||||||||||||||||||||||||
| 116 | default => undef, | ||||||||||||||||||||||||||||
| 117 | coerce => \&coerce_wrappers, | ||||||||||||||||||||||||||||
| 118 | isa => \&isa_wrappers, | ||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 11 | ▶ | sub _build__wrapped ($self) { | ||||||||||||||||||||||||||
|
Subroutine
_build__wrapped: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 122 | 11 | my $url = $self->db_url; | |||||||||||||||||||||||||||
| 123 | 11 | for my $candidate ($self->_wrappers->@*) { | |||||||||||||||||||||||||||
| 124 | 19 | my $class = $candidate->{class}; | |||||||||||||||||||||||||||
| 125 | 19 | ▶ | my $instance = $candidate->{create}->($class, $url) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 8 | return { $candidate->%*, instance => $instance }; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 1 | ouch 400, 'unsupported DB URL'; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # handy accessors | ||||||||||||||||||||||||||||
| 132 | 169 | ▶ | sub mdb ($self) { return $self->_wrapped->{instance} } | ||||||||||||||||||||||||||
|
Subroutine
mdb: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 133 | 7 | ▶ | sub mdb_class ($self) { return $self->_wrapped->{class} } | ||||||||||||||||||||||||||
|
Subroutine
mdb_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 1 | ▶ | sub mdb_module ($self) { return $self->_wrapped->{class} } | ||||||||||||||||||||||||||
|
Subroutine
mdb_module: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 84 | ▶ | sub db ($self) { return $self->mdb->db } | ||||||||||||||||||||||||||
|
Subroutine
db: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 129 | ▶ | sub db_and_opts ($self, $ext_opts, $name = undef) { | ||||||||||||||||||||||||||
|
Subroutine
db_and_opts: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 129 | $name = sub_name(db_and_opts => $name, 2); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # SQL::Abstract::Pg is nice to old-style sort hashes; we are not. | ||||||||||||||||||||||||||||
| 141 | 129 | ▶ | assert_new_style_opts($name, $ext_opts //= {}); | ||||||||||||||||||||||||||
|
Condition100%2/2
$ext_opts //= {}
MC/DC100%1/1
$ext_opts //= {}
$ext_opts
Truth table100%2/2
A
A= $ext_opts
| |||||||||||||||||||||||||||||
| 142 | 123 | my $opts = { $ext_opts->%* }; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 123 | my $db; | |||||||||||||||||||||||||||
| 145 | 123 | ▶ | if (exists($opts->{db})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 71 | $db = delete($opts->{db}); | |||||||||||||||||||||||||||
| 147 | 71 | ▶ | ouch 400, "$name: db is present but undefined" unless defined($db); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 118 | ▶ | return ($db // $self->db, $opts); | ||||||||||||||||||||||||||
|
Condition66%2/3
$db // $self->db
MC/DC0%0/2
$db // $self->db
$db
$self->db
Truth table66%2/3
A // B
A= $dbB= $self->db
| |||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # RETURNING (Pg, SQLite 3.35+) instead of last_insert_id, which is wrong | ||||||||||||||||||||||||||||
| 153 | # when on_conflict turns the insert into an update | ||||||||||||||||||||||||||||
| 154 | 30 | ▶ | sub default_inserter ($self, $tablish, $data, $ext_opts = undef) { | ||||||||||||||||||||||||||
|
Subroutine
default_inserter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | 30 | my ($tbl, $idf) = explode_tablish($tablish); | |||||||||||||||||||||||||||
| 156 | 30 | my ($db, $opts) = $self->db_and_opts($ext_opts); | |||||||||||||||||||||||||||
| 157 | 28 | $opts->{returning} = $idf; | |||||||||||||||||||||||||||
| 158 | 28 | my $row = $db->insert($tbl, $data, $opts)->hash; | |||||||||||||||||||||||||||
| 159 | 27 | ▶ | return $row ? $row->{$idf} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 47 | ▶ | sub id_of ($self, $tablish, $cond, $ext_opts = undef) { | ||||||||||||||||||||||||||
|
Subroutine
id_of: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 47 | assert_condition(id_of => $cond, $self->mdb); | |||||||||||||||||||||||||||
| 164 | 37 | my ($tbl, $idf) = explode_tablish($tablish); | |||||||||||||||||||||||||||
| 165 | 37 | my ($db, $opts) = $self->db_and_opts($ext_opts); | |||||||||||||||||||||||||||
| 166 | 34 | ▶ | $opts->{limit} = 1 if $self->_can_limit; # we fetch only the first one | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 34 | my $hash = $db->select($tbl, [$idf], $cond, $opts)->hash; | |||||||||||||||||||||||||||
| 168 | 34 | ▶ | return $hash ? $hash->{$idf} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 34 | ▶ | sub _can_limit ($self) { return $self->_wrapped->{can_limit} } | ||||||||||||||||||||||||||
|
Subroutine
_can_limit: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 172 | 32 | ▶ | sub _inserter ($self) { return $self->_wrapped->{insert} } | ||||||||||||||||||||||||||
|
Subroutine
_inserter: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 30 | ▶ | sub id_or_insert ($self, $tablish, $condition, $default, $e_opts = undef) { | ||||||||||||||||||||||||||
|
Subroutine
id_or_insert: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 30 | assert_condition(id_or_insert => $condition, $self->mdb); | |||||||||||||||||||||||||||
| 176 | 21 | assert_valid_keys(id_or_insert => $e_opts, qw< db insert select >); | |||||||||||||||||||||||||||
| 177 | 18 | my ($db, $opts) = $self->db_and_opts($e_opts); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # get options and validate them on the fly. Use shallow copies so that | ||||||||||||||||||||||||||||
| 180 | # we can fiddle with them later. | ||||||||||||||||||||||||||||
| 181 | my ($sel_opts, $ins_opts) = map { | ||||||||||||||||||||||||||||
| 182 | 17 | ▶ | my $href = $opts->{$_} // {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{$_} // {}
MC/DC100%1/1
$opts->{$_} // {}
$opts->{$_}
Truth table100%2/2
A
A= $opts->{$_}
| |||||||||||||||||||||||||||||
| 183 | 32 | assert_hash_reference(id_or_insert => $href, | |||||||||||||||||||||||||||
| 184 | "invalid input $_, expected a hash reference"); | ||||||||||||||||||||||||||||
| 185 | 28 | +{ $href->%* }; | |||||||||||||||||||||||||||
| 186 | } qw< select insert >; | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # SQL::Abstract::Pg is nice to old-style sort hashes; we are not. | ||||||||||||||||||||||||||||
| 189 | 13 | assert_new_style_opts(id_or_insert => $sel_opts); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # use the same 'db' down the line | ||||||||||||||||||||||||||||
| 192 | 11 | $sel_opts->{db} = $ins_opts->{db} = $db; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # if not set by the caller, we settle for DO NOTHING | ||||||||||||||||||||||||||||
| 195 | 11 | ▶ | $ins_opts->{on_conflict} //= undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$ins_opts->{"on_conflict"} //= undef
MC/DC100%1/1
$ins_opts->{"on_conflict"} //= undef
$ins_opts->{"on_conflict"}
Truth table100%2/2
A
A= $ins_opts->{"on_conflict"}
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 11 | my $inserter = $self->_inserter; | |||||||||||||||||||||||||||
| 198 | 11 | for (1 .. 3) { # paranoia for quick insert/delete | |||||||||||||||||||||||||||
| 199 | 14 | my $id = $self->id_of($tablish, $condition, $sel_opts); | |||||||||||||||||||||||||||
| 200 | 14 | ▶ | return $id if defined($id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 8 | $id = $inserter->($self, $tablish, $default, $ins_opts); | |||||||||||||||||||||||||||
| 203 | 8 | ▶ | return $id if defined($id); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # insert did nothing three times: almost surely $default conflicts on | ||||||||||||||||||||||||||||
| 207 | # a unique key that $condition does not cover, so a 409 is better than | ||||||||||||||||||||||||||||
| 208 | # a generic 500, even though it might be a problem on the library side | ||||||||||||||||||||||||||||
| 209 | 1 | ouch 409, 'id_or_insert: no record matches the condition, but ' | |||||||||||||||||||||||||||
| 210 | . 'the default record conflicts with an existing one'; | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 23 | ▶ | sub insert ($self, $tablish, $data, $ext_opts = undef) { | ||||||||||||||||||||||||||
|
Subroutine
insert: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 214 | # call it here to ensure checks over $ext_opts | ||||||||||||||||||||||||||||
| 215 | 23 | my ($db, $opts) = $self->db_and_opts($ext_opts, 'insert'); | |||||||||||||||||||||||||||
| 216 | 21 | $opts->{db} = $db; | |||||||||||||||||||||||||||
| 217 | 21 | return $self->_inserter->($self, $tablish, $data, $opts); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 19 | ▶ | sub upsert ($self, $tablish, $data, $ext_opts = undef) { | ||||||||||||||||||||||||||
|
Subroutine
upsert: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 221 | # call it here to ensure checks over $ext_opts | ||||||||||||||||||||||||||||
| 222 | 19 | my ($db, $opts) = $self->db_and_opts($ext_opts); | |||||||||||||||||||||||||||
| 223 | 17 | $opts->{db} = $db; | |||||||||||||||||||||||||||
| 224 | 17 | ▶ | if (defined(my $conflicting = delete($opts->{conflicting}))) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 225 | ouch 400, 'upsert: cannot use both conflicting and on_conflict' | ||||||||||||||||||||||||||||
| 226 | 14 | ▶ | if exists($opts->{on_conflict}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 13 | ▶ | my @keys = ref($conflicting) eq 'ARRAY' ? $conflicting->@* : $conflicting; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 13 | ▶ | ouch 400, 'upsert: empty conflicting' unless @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | # SQL::Abstract(::*) accept $data as an array reference, but in this | ||||||||||||||||||||||||||||
| 231 | # "DWIM" branch we insist on getting a hash reference | ||||||||||||||||||||||||||||
| 232 | 12 | assert_hash_reference(upsert => $data, | |||||||||||||||||||||||||||
| 233 | 'data must be a hash reference when using key "conflicting"'); | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # NULLs never conflict, so this would insert a new record every time | ||||||||||||||||||||||||||||
| 236 | 11 | my @missing = grep { !defined($data->{$_}) } @keys; | |||||||||||||||||||||||||||
| 237 | 11 | ▶ | ouch 400, "upsert: no value for conflicting column(s) @missing" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | if @missing; | ||||||||||||||||||||||||||||
| 239 | 8 | my %is_key = map { $_ => 1 } @keys; | |||||||||||||||||||||||||||
| 240 | my %reduced_data = ( | ||||||||||||||||||||||||||||
| 241 | 4 | map { $_ => $data->{$_} } | |||||||||||||||||||||||||||
| 242 | 8 | grep { !$is_key{$_} } | |||||||||||||||||||||||||||
| 243 | keys($data->%*) | ||||||||||||||||||||||||||||
| 244 | ); | ||||||||||||||||||||||||||||
| 245 | # nothing else to set: no-op update, so RETURNING still gives the id | ||||||||||||||||||||||||||||
| 246 | 8 | ▶ | %reduced_data = ($keys[0] => $data->{$keys[0]}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | unless %reduced_data; | ||||||||||||||||||||||||||||
| 248 | 8 | $opts->{on_conflict} = [ $conflicting => \%reduced_data ]; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | elsif (!exists($opts->{on_conflict})) { | ||||||||||||||||||||||||||||
| 251 | 2 | ouch 400, 'upsert: need either conflicting or on_conflict'; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | 9 | return $self->insert($tablish, $data, $opts); | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 3 | ▶ | sub init ($self, $name = 'migrations') { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 257 | 3 | ▶ | my $migrations = $self->migrations_for // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->migrations_for // {}
MC/DC0%0/1
$self->migrations_for // {}
$self->migrations_for
Truth table50%1/2
A
A= $self->migrations_for
| |||||||||||||||||||||||||||||
| 258 | 3 | ▶ | if (my $migration = $migrations->{$self->mdb_class}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 2 | $self->mdb->migrations | |||||||||||||||||||||||||||
| 260 | ->name($name) | ||||||||||||||||||||||||||||
| 261 | ->from_string($migration) | ||||||||||||||||||||||||||||
| 262 | ->migrate; | ||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | 3 | return $self; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 5 | 1; | |||||||||||||||||||||||||||