]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DB/ResultSet/Correspondent.pm
switch to compatibility level 12
[debbugs.git] / Debbugs / DB / ResultSet / Correspondent.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 2017 by Don Armstrong <don@donarmstrong.com>.
5 use utf8;
6 package Debbugs::DB::ResultSet::Correspondent;
7
8 =head1 NAME
9
10 Debbugs::DB::ResultSet::Correspondent - Correspondent table actions
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 use Debbugs::Common qw(getparsedaddrs);
30 use Debbugs::DB::Util qw(select_one);
31 use Scalar::Util qw(blessed);
32
33 sub get_correspondent_id {
34     my ($self,$addr) = @_;
35     my $full_name;
36     if (blessed($addr)) {
37         $full_name = $addr->phrase();
38         $addr = $addr->address();
39     } elsif ($addr =~ /</) {
40         $addr = getparsedaddrs($addr);
41         $full_name = $addr->phrase();
42         $addr = $addr->address();
43     }
44     if (defined $full_name) {
45         $full_name =~ s/^\"|\"$//g;
46         $full_name =~ s/^\s+|\s+$//g;
47     }
48     my $rs =
49         $self->
50         search({addr => $addr},
51               {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
52               }
53               )->first();
54     if (defined $rs) {
55         return $rs->{id};
56     }
57     return $self->result_source->schema->storage->
58         dbh_do(sub {
59                    my ($s,$dbh,$addr,$full_name) = @_;
60                    my $ci = select_one($dbh,<<'SQL',$addr,$addr);
61 WITH ins AS (
62 INSERT INTO correspondent (addr) VALUES (?)
63  ON CONFLICT (addr) DO NOTHING RETURNING id
64 )
65 SELECT id FROM ins
66 UNION ALL
67 SELECT id FROM correspondent WHERE addr = ?
68 LIMIT 1;
69 SQL
70                    if (defined $full_name) {
71                        select_one($dbh,<<'SQL',$ci,$full_name);
72 WITH ins AS (
73 INSERT INTO correspondent_full_name (correspondent,full_name)
74    VALUES (?,?) ON CONFLICT (correspondent,full_name) DO NOTHING RETURNING 1
75 ) SELECT 1 FROM ins
76 UNION ALL
77 SELECT 1;
78 SQL
79                    }
80                    return $ci;
81 },
82                $addr,
83                $full_name
84               );
85
86 }
87
88
89
90 1;
91
92 __END__