]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DB/ResultSet/Maintainer.pm
switch to compatibility level 12
[debbugs.git] / Debbugs / DB / ResultSet / Maintainer.pm
1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
4 # Copyright 2016 by Don Armstrong <don@donarmstrong.com>.
5 use utf8;
6 package Debbugs::DB::ResultSet::Maintainer;
7
8 =head1 NAME
9
10 Debbugs::DB::ResultSet::Maintainer - Package maintainer result set operations
11
12 =head1 SYNOPSIS
13
14
15
16 =head1 DESCRIPTION
17
18
19
20 =cut
21
22 use strict;
23 use warnings;
24
25 use base 'DBIx::Class::ResultSet';
26
27 use Debbugs::DB::Util qw(select_one);
28
29
30 =over
31
32 =item get_maintainers 
33
34      $s->resultset('Maintainers')->get_maintainers();
35
36      $s->resultset('Maintainers')->get_maintainers(@maints);
37
38 Retrieve a HASHREF of all maintainers with the maintainer name as the key and
39 the id of the database as the value. If given an optional list of maintainers,
40 adds those maintainers to the database if they do not already exist in the
41 database.
42
43 =cut
44 sub get_maintainers {
45     my ($self,@maints) = @_;
46     my %maints;
47     for my $m ($self->result_source->schema->resultset('Maintainer')->
48                search(undef,
49                      {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
50                       columns => [qw[id name] ]
51                      })->all()) {
52         $maints{$m->{name}} = $m->{id};
53     }
54     my @maint_names = grep {not exists $maints{$_}} @maints;
55     my @maint_ids = $self->result_source->schema->
56         txn_do(sub {
57                    my @ids;
58                    for my $name (@_) {
59                        push @ids,
60                            $self->result_source->schema->
61                            resultset('Maintainer')->get_maintainer_id($name);
62                    }
63                    return @ids;
64                },@maint_names);
65     @maints{@maint_names} = @maint_ids;
66     return \%maints;
67 }
68
69 =item get_maintainer_id
70
71      $s->resultset('Maintainer')->get_maintainer_id('Foo Bar <baz@example.com>')
72
73 Given a maintainer name returns the maintainer id, possibly inserting the
74 maintainer (and correspondent) if either do not exist in the database.
75
76
77 =cut
78
79 sub get_maintainer_id {
80     my ($self,$maint) = @_;
81     my $rs =
82         $self->
83         search({name => $maint},
84               {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
85               }
86               )->first();
87     if (defined $rs) {
88         return $rs->{id};
89     }
90     my $ci =
91         $self->result_source->schema->resultset('Correspondent')->
92         get_correspondent_id($maint);
93     return $self->result_source->schema->storage->
94         dbh_do(sub {
95                    my ($s,$dbh,$maint,$ci) = @_;
96                    return select_one($dbh,<<'SQL',$maint,$ci,$maint);
97 WITH ins AS (
98 INSERT INTO maintainer (name,correspondent) VALUES (?,?)
99 ON CONFLICT (name) DO NOTHING RETURNING id
100 )
101 SELECT id FROM ins
102 UNION ALL
103 SELECT id FROM maintainer WHERE name = ?
104 LIMIT 1;
105 SQL
106                },
107                $maint,$ci
108               );
109 }
110
111 =back
112
113 =cut
114
115 1;
116
117 __END__