]> git.donarmstrong.com Git - debbugs.git/blob - lib/Debbugs/DB/ResultSet/Maintainer.pm
Debbugs::DB::Util is now a component of Debbugs::DB
[debbugs.git] / lib / 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 =over
28
29 =item get_maintainers 
30
31      $s->resultset('Maintainers')->get_maintainers();
32
33      $s->resultset('Maintainers')->get_maintainers(@maints);
34
35 Retrieve a HASHREF of all maintainers with the maintainer name as the key and
36 the id of the database as the value. If given an optional list of maintainers,
37 adds those maintainers to the database if they do not already exist in the
38 database.
39
40 =cut
41 sub get_maintainers {
42     my ($self,@maints) = @_;
43     my %maints;
44     for my $m ($self->result_source->schema->resultset('Maintainer')->
45                search(undef,
46                      {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
47                       columns => [qw[id name] ]
48                      })->all()) {
49         $maints{$m->{name}} = $m->{id};
50     }
51     my @maint_names = grep {not exists $maints{$_}} @maints;
52     my @maint_ids = $self->result_source->schema->
53         txn_do(sub {
54                    my @ids;
55                    for my $name (@_) {
56                        push @ids,
57                            $self->result_source->schema->
58                            resultset('Maintainer')->get_maintainer_id($name);
59                    }
60                    return @ids;
61                },@maint_names);
62     @maints{@maint_names} = @maint_ids;
63     return \%maints;
64 }
65
66 =item get_maintainer_id
67
68      $s->resultset('Maintainer')->get_maintainer_id('Foo Bar <baz@example.com>')
69
70 Given a maintainer name returns the maintainer id, possibly inserting the
71 maintainer (and correspondent) if either do not exist in the database.
72
73
74 =cut
75
76 sub get_maintainer_id {
77     my ($self,$maint) = @_;
78     my $rs =
79         $self->
80         search({name => $maint},
81               {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
82               }
83               )->first();
84     if (defined $rs) {
85         return $rs->{id};
86     }
87     my $ci =
88         $self->result_source->schema->resultset('Correspondent')->
89         get_correspondent_id($maint);
90     return $self->result_source->schema->
91         select_one(<<'SQL',$maint,$ci,$maint);
92 WITH ins AS (
93 INSERT INTO maintainer (name,correspondent) VALUES (?,?)
94 ON CONFLICT (name) DO NOTHING RETURNING id
95 )
96 SELECT id FROM ins
97 UNION ALL
98 SELECT id FROM maintainer WHERE name = ?
99 LIMIT 1;
100 SQL
101 }
102
103 =back
104
105 =cut
106
107 1;
108
109 __END__