]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DB/ResultSet/Maintainer.pm
load_packages now only changes rows it has to
[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
31 sub get_maintainers {
32     my ($self,@maints) = @_;
33     my %maints;
34     for my $m ($self->result_source->schema->resultset('Maintainer')->
35                search(undef,
36                      {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
37                       columns => [qw[id name] ]
38                      })->all()) {
39         $maints{$m->{name}} = $m->{id};
40     }
41     my @maint_names = grep {not exists $maints{$_}} @maints;
42     my @maint_ids = $self->result_source->schema->
43         txn_do(sub {
44                    my @ids;
45                    for my $name (@_) {
46                        push @ids,
47                            $self->result_source->schema->
48                            resultset('Maintainer')->get_maintainer_id($name);
49                    }
50                    return @ids;
51                },@maint_names);
52     @maints{@maint_names} = @maint_ids;
53     return \%maints;
54 }
55
56 sub get_maintainer_id {
57     my ($self,$maint) = @_;
58     my $rs =
59         $self->
60         search({name => $maint},
61               {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
62               }
63               )->first();
64     if (defined $rs) {
65         return $rs->{id};
66     }
67     my $ci =
68         $self->result_source->schema->resultset('Correspondent')->
69         get_correspondent_id($maint);
70     return $self->result_source->schema->storage->
71         dbh_do(sub {
72                    my ($s,$dbh,$maint,$ci) = @_;
73                    return select_one($dbh,<<'SQL',$maint,$ci,$maint);
74 WITH ins AS (
75 INSERT INTO maintainer (name,correspondent) VALUES (?,?)
76 ON CONFLICT (name) DO NOTHING RETURNING id
77 )
78 SELECT id FROM ins
79 UNION ALL
80 SELECT id FROM maintainer WHERE name = ?
81 LIMIT 1;
82 SQL
83                },
84                $maint,$ci
85               );
86 }
87
88 1;
89
90 __END__