1 package FS::part_export::sqlmail;
3 use vars qw(@ISA %fs_mail_table %fields);
6 @ISA = qw(FS::part_export);
8 %fs_mail_table = ( svc_acct => 'user',
9 svc_domain => 'domain' );
11 # fields that need to be copied into the fs_mail tables
12 $fields{user} = [qw(username _password finger domsvc svcnum )];
13 $fields{domain} = [qw(domain svcnum catchall )];
15 sub rebless { shift; }
18 my($self, $svc) = (shift, shift);
19 # this is a svc_something.
21 my $table = $fs_mail_table{$svc->cust_svc->part_svc->svcdb};
22 my @attrib = map {$svc->$_} @{$fields{$table}};
23 my $error = $self->sqlmail_queue( $svc->svcnum, 'insert',
25 return $error if $error;
30 my( $self, $new, $old ) = (shift, shift, shift);
32 my $table = $fs_mail_table{$new->cust_svc->part_svc->svcdb};
34 my @old = ($old->svcnum, 'delete', $table, $old->svcnum);
35 my @narf = map {$new->$_} @{$fields{$table}};
36 $self->sqlmail_queue($new->svcnum, 'replace', $table,
39 return $error if $error;
44 my( $self, $svc ) = (shift, shift);
45 my $table = $fs_mail_table{$new->cust_svc->part_svc->svcdb};
46 $self->sqlmail_queue( $svc->svcnum, 'delete', $table,
51 my( $self, $svcnum, $method, $table ) = (shift, shift, shift);
52 my $queue = new FS::queue {
54 'job' => "FS::part_export::sqlmail::sqlmail_$method",
57 $self->option('datasrc'),
58 $self->option('username'),
59 $self->option('password'),
64 sub sqlmail_insert { #subroutine, not method
65 my $dbh = sqlmail_connect(shift, shift, shift);
66 my( $table, @attrib ) = @_;
68 my $sth = $dbh->prepare(
69 "INSERT INTO $table (" . join (',', @{$fields{$table}}) .
70 ") VALUES ('" . join ("','", @attrib) . "')"
71 ) or die $dbh->errstr;
72 $sth->execute() or die $sth->errstr;
77 sub sqlmail_delete { #subroutine, not method
78 my $dbh = sqlmail_connect(shift, shift, shift);
79 my( $table, $svcnum ) = @_;
81 my $sth = $dbh->prepare(
82 "DELETE FROM $table WHERE svcnum = $svcnum"
83 ) or die $dbh->errstr;
84 $sth->execute() or die $sth->errstr;
90 my $dbh = sqlmail_connect(shift, shift, shift);
91 my( $table, $svcnum, @attrib ) = @_;
94 @data{@{$fields{$table}}} = @attrib;
96 my $sth = $dbh->prepare(
97 "UPDATE $table SET " .
98 ( join ',', map {$_ . "='" . $data{$_} . "'"} keys(%data) ) .
99 " WHERE svcnum = $svcnum"
100 ) or die $dbh->errstr;
101 $sth->execute() or die $sth->errstr;
106 sub sqlmail_connect {
107 #my($datasrc, $username, $password) = @_;
108 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
109 DBI->connect(@_) or die $DBI::errstr;