]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/DB/ResultSet/Correspondent.pm
7638a522361e3cd49ee64ca8d1dcf331bc3a042a
[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
32 sub get_correspondent_id {
33     my ($self,$addr) = @_;
34     my $full_name;
35     if ($addr =~ /</) {
36         $addr = getparsedaddrs($addr);
37         $full_name = $addr->phrase();
38         $full_name =~ s/^\"|\"$//g;
39         $full_name =~ s/^\s+|\s+$//g;
40         $addr = $addr->address();
41     }
42     my $rs =
43         $self->
44         search({addr => $addr},
45               {result_class => 'DBIx::Class::ResultClass::HashRefInflator',
46               }
47               )->first();
48     if (defined $rs) {
49         return $rs->{id};
50     }
51     return $self->result_source->schema->storage->
52         dbh_do(sub {
53                    my ($s,$dbh,$addr,$full_name) = @_;
54                    my $ci = select_one($dbh,<<'SQL',$addr,$addr);
55 WITH ins AS (
56 INSERT INTO correspondent (addr) VALUES (?)
57  ON CONFLICT (addr) DO NOTHING RETURNING id
58 )
59 SELECT id FROM ins
60 UNION ALL
61 SELECT id FROM correspondent WHERE addr = ?
62 LIMIT 1;
63 SQL
64                    select_one($dbh,<<'SQL',$ci,$full_name,$ci,$full_name);
65 WITH ins AS (
66 INSERT INTO correspondent_full_name (correspondent,full_name)
67    VALUES (?,?) ON CONFLICT (correspondent,full_name) DO NOTHING RETURNING id
68 )
69 SELECT id FROM ins
70 UNION ALL
71 SELECT id FROM correspondent_full_name WHERE correspondent=? AND full_name = ?
72 LIMIT 1;
73 SQL
74                    return $ci;
75 },
76                $addr,
77                $full_name
78               );
79
80 }
81
82
83
84 1;
85
86 __END__