working textradius export
[freeside.git] / FS / FS / part_export / sqlmail.pm
1 package FS::part_export::sqlmail;
2
3 use vars qw(@ISA %fs_mail_table %fields);
4 use FS::part_export;
5
6 @ISA = qw(FS::part_export);
7
8 %fs_mail_table = ( svc_acct => 'user',
9                    svc_domain => 'domain' );
10
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 )];
14
15 sub rebless { shift; }
16
17 sub _export_insert {
18   my($self, $svc) = (shift, shift);
19   # this is a svc_something.
20
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',
24       $table, @attrib );
25   return $error if $error;
26   '';
27 }
28
29 sub _export_replace {
30   my( $self, $new, $old ) = (shift, shift, shift);
31
32   my $table = $fs_mail_table{$new->cust_svc->part_svc->svcdb};
33
34   my @old = ($old->svcnum, 'delete', $table, $old->svcnum);
35   my @narf = map {$new->$_} @{$fields{$table}};
36   $self->sqlmail_queue($new->svcnum, 'replace', $table, 
37       $new->svcnum, @narf);
38
39   return $error if $error;
40   '';
41 }
42
43 sub _export_delete {
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,
47     $svc->svcnum );
48 }
49
50 sub sqlmail_queue {
51   my( $self, $svcnum, $method, $table ) = (shift, shift, shift);
52   my $queue = new FS::queue {
53     'svcnum' => $svcnum,
54     'job'    => "FS::part_export::sqlmail::sqlmail_$method",
55   };
56   $queue->insert(
57     $self->option('datasrc'),
58     $self->option('username'),
59     $self->option('password'),
60     @_,
61   );
62 }
63
64 sub sqlmail_insert { #subroutine, not method
65   my $dbh = sqlmail_connect(shift, shift, shift);
66   my( $table, @attrib ) = @_;
67
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;
73
74   $dbh->disconnect;
75 }
76
77 sub sqlmail_delete { #subroutine, not method
78   my $dbh = sqlmail_connect(shift, shift, shift);
79   my( $table, $svcnum ) = @_;
80
81   my $sth = $dbh->prepare(
82     "DELETE FROM $table WHERE svcnum = $svcnum"
83   ) or die $dbh->errstr;
84   $sth->execute() or die $sth->errstr;
85
86   $dbh->disconnect;
87 }
88
89 sub sqlmail_replace {
90   my $dbh = sqlmail_connect(shift, shift, shift);
91   my( $table, $svcnum, @attrib ) = @_;
92
93   my %data;
94   @data{@{$fields{$table}}} = @attrib;
95
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;
102
103   $dbh->disconnect;
104 }
105
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;
110 }
111