summaryrefslogtreecommitdiff
path: root/lib/Multi/API.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-12-03 18:22:25 +0100
committerYorhel <git@yorhel.nl>2019-12-03 18:22:25 +0100
commit3900df57ce8681909862172697475f5533ac0489 (patch)
treeff2f30caaf30f972514a49068cba1989b946aa09 /lib/Multi/API.pm
parent8676d8ed8028cf7efb415c3f3dd1e4d4be9c91f8 (diff)
API: Add labels field to "set ulist"
Diffstat (limited to 'lib/Multi/API.pm')
-rw-r--r--lib/Multi/API.pm16
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/Multi/API.pm b/lib/Multi/API.pm
index e58f3972..5cb68c1b 100644
--- a/lib/Multi/API.pm
+++ b/lib/Multi/API.pm
@@ -1515,9 +1515,23 @@ sub set_ulist {
}
}
+ if(exists $opt->{labels}) {
+ return cerr $c, badarg => "Labels field expects an array", field => 'labels' if ref $opt->{labels} ne 'ARRAY';
+ return cerr $c, badarg => "Invalid label: '$_'", field => 'labels' for grep !defined($_) || ref($_) || !/^[0-9]+$/, $opt->{labels}->@*;
+ my %l = map +($_,1), grep $_ != 7, $opt->{labels}->@*;
+ # XXX: This is ugly. Errors (especially: unknown labels) are ignored and
+ # the entire set operation ought to run in a single transaction.
+ pg_cmd 'SELECT lbl FROM ulist_vns_labels WHERE uid = $1 AND vid = $2', [ $c->{uid}, $obj->{id} ], sub {
+ return if pg_expect $_[0];
+ my %ids = map +($_->{lbl}, 1), $_[0]->rowsAsHashes;
+ pg_cmd 'INSERT INTO ulist_vns_labels (uid, vid, lbl) VALUES ($1,$2,$3)', [ $c->{uid}, $obj->{id}, $_ ] for grep !$ids{$_}, keys %l;
+ pg_cmd 'DELETE FROM ulist_vns_labels WHERE uid = $1 AND vid = $2 AND lbl = $3', [ $c->{uid}, $obj->{id}, $_ ] for grep !$l{$_}, keys %ids;
+ };
+ }
+
+ push @set, 'lastmod = NOW()' if @set || $opt->{labels};
return cerr $c, missing => 'No fields to change' if !@set;
- push @set, 'lastmod = NOW()';
cpg $c, 'INSERT INTO ulist_vns (uid, vid) VALUES ($1, $2) ON CONFLICT (uid, vid) DO NOTHING', [ $c->{uid}, $obj->{id} ], sub {
setpg $obj, 'UPDATE ulist_vns SET '.join(',', @set).' WHERE uid = $1 AND vid = $2', \@bind;
};