]> git.donarmstrong.com Git - debbugs.git/blob - lib/Debbugs/DB/ResultSet/CorrespondentFullName.pm
correct correspondent full name (db version now 13)
[debbugs.git] / lib / Debbugs / DB / ResultSet / CorrespondentFullName.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::CorrespondentFullName;
7
8 =head1 NAME
9
10 Debbugs::DB::ResultSet::CorrespondentFullName - 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 upsert_correspondent_id {
34     my ($self,$addr) = @_;
35     my $full_addr;
36     if (blessed($addr)) {
37         $full_addr = $addr->format();
38     } else {
39         $full_addr = $addr;
40         undef $addr;
41     }
42     my $rs = $self->
43         search({full_addr => $addr,
44                },
45               {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
46               }
47               )->first();
48     if (defined $rs) {
49         return $rs->{correspondent};
50     }
51     if (not defined $addr) {
52         $addr = getparsedaddrs($full_addr);
53     }
54     my $email = $addr->address();
55     my $name = $addr->phrase();
56     if (defined $name) {
57         $name =~ s/^\"|\"$//g;
58         $name =~ s/^\s+|\s+$//g;
59     } else {
60        $name = '';
61     }
62     my $ci = $self->result_source->schema->
63         select_one(<<'SQL',$addr,$addr);
64 WITH ins AS (
65 INSERT INTO correspondent (addr) VALUES (?)
66  ON CONFLICT (addr) DO NOTHING RETURNING id
67 )
68 SELECT id FROM ins
69 UNION ALL
70 SELECT id FROM correspondent WHERE addr = ?
71 LIMIT 1;
72 SQL
73     $self->result_source->schema->
74         select_one(<<'SQL',$ci,$full_addr,$name);
75 WITH ins AS (
76 INSERT INTO correspondent_full_name (correspondent,full_addr,name)
77    VALUES (?,?,?) ON CONFLICT (correspondent,full_addr) DO UPDATE SET last_seen=NOW() RETURNING correspondent
78 )
79 SELECT 1 FROM ins
80 UNION ALL
81 SELECT 1;
82 SQL
83     return $ci;
84 }
85
86
87
88 1;
89
90 __END__