summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/sqlmail.pm
blob: 4194daf0c7bf82770895e4d193b973c176c1401a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package FS::part_export::sqlmail;

use vars qw(@ISA %fs_mail_table %fields);
use FS::part_export;

@ISA = qw(FS::part_export);

%fs_mail_table = ( svc_acct => 'user',
                   svc_domain => 'domain' );

# fields that need to be copied into the fs_mail tables
$fields{user} = [qw(username _password finger domsvc svcnum )];
$fields{domain} = [qw(domain svcnum catchall )];

sub rebless { shift; }

sub _export_insert {
  my($self, $svc) = (shift, shift);
  # this is a svc_something.

  my $table = $fs_mail_table{$svc->cust_svc->part_svc->svcdb};
  my @attrib = map {$svc->$_} @{$fields{$table}};
  my $error = $self->sqlmail_queue( $svc->svcnum, 'insert',
      $table, @attrib );
  return $error if $error;
  '';
}

sub _export_replace {
  my( $self, $new, $old ) = (shift, shift, shift);

  my $table = $fs_mail_table{$new->cust_svc->part_svc->svcdb};

  my @old = ($old->svcnum, 'delete', $table, $old->svcnum);
  my @narf = map {$new->$_} @{$fields{$table}};
  $self->sqlmail_queue($new->svcnum, 'replace', $table, 
      $new->svcnum, @narf);

  return $error if $error;
  '';
}

sub _export_delete {
  my( $self, $svc ) = (shift, shift);
  my $table = $fs_mail_table{$new->cust_svc->part_svc->svcdb};
  $self->sqlmail_queue( $svc->svcnum, 'delete', $table,
    $svc->svcnum );
}

sub sqlmail_queue {
  my( $self, $svcnum, $method, $table ) = (shift, shift, shift);
  my $queue = new FS::queue {
    'svcnum' => $svcnum,
    'job'    => "FS::part_export::sqlmail::sqlmail_$method",
  };
  $queue->insert(
    $self->option('datasrc'),
    $self->option('username'),
    $self->option('password'),
    @_,
  );
}

sub sqlmail_insert { #subroutine, not method
  my $dbh = sqlmail_connect(shift, shift, shift);
  my( $table, @attrib ) = @_;

  my $sth = $dbh->prepare(
    "INSERT INTO $table (" . join (',', @{$fields{$table}}) .
    ") VALUES ('" . join ("','", @attrib) . "')"
  ) or die $dbh->errstr;
  $sth->execute() or die $sth->errstr;

  $dbh->disconnect;
}

sub sqlmail_delete { #subroutine, not method
  my $dbh = sqlmail_connect(shift, shift, shift);
  my( $table, $svcnum ) = @_;

  my $sth = $dbh->prepare(
    "DELETE FROM $table WHERE svcnum = $svcnum"
  ) or die $dbh->errstr;
  $sth->execute() or die $sth->errstr;

  $dbh->disconnect;
}

sub sqlmail_replace {
  my $dbh = sqlmail_connect(shift, shift, shift);
  my( $table, $svcnum, @attrib ) = @_;

  my %data;
  @data{@{$fields{$table}}} = @attrib;

  my $sth = $dbh->prepare(
    "UPDATE $table SET " .
    ( join ',',  map {$_ . "='" . $data{$_} . "'"} keys(%data) ) .
    " WHERE svcnum = $svcnum"
    ) or die $dbh->errstr;
  $sth->execute() or die $sth->errstr;

  $dbh->disconnect;
}

sub sqlmail_connect {
  #my($datasrc, $username, $password) = @_;
  #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
  DBI->connect(@_) or die $DBI::errstr;
}