finish modification
[freeside.git] / FS / FS / part_export / acct_sql.pm
1 package FS::part_export::acct_sql;
2
3 use vars qw(@ISA %info @saltset);
4 use Tie::IxHash;
5 #use Digest::MD5 qw(md5_hex);
6 use FS::Record; #qw(qsearchs);
7 use FS::part_export;
8
9 @ISA = qw(FS::part_export);
10
11 tie my %options, 'Tie::IxHash',
12   'datasrc'            => { label => 'DBI data source' },
13   'username'           => { label => 'Database username' },
14   'password'           => { label => 'Database password' },
15   'table'              => { label => 'Database table' },
16   'schema'             => { label =>
17                               'Database schema mapping to Freeside methods.',
18                             type  => 'textarea',
19                           },
20   'primary_key'        => { label => 'Database primary key' },
21 ;
22
23 tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash',
24   'username' => 'email',
25   'password' => '_password',
26   'crypt'    => 'crypt_password',
27   'name'     => 'finger',
28   'maildir'  => 'virtual_maildir',
29   'domain'   => 'domain',
30   'svcnum'   => 'svcnum',
31 ;
32 my $postfix_courierimap_mailbox_map =
33   join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}",
34                  keys %postfix_courierimap_mailbox_map      );
35
36 tie my %postfix_courierimap_alias_map, 'Tie::IxHash',
37   'address' => 'email',
38   'goto'    => 'email',
39   'domain'  => 'domain',
40   'svcnum'  => 'svcnum',
41 ;
42 my $postfix_courierimap_alias_map =
43   join('\n', map "$_ $postfix_courierimap_alias_map{$_}",
44                  keys %postfix_courierimap_alias_map      );
45
46 %info = (
47   'svc'      => 'svc_acct',
48   'desc'     => 'Real-time export of accounts to SQL databases '.
49                 '(Postfix+Courier IMAP, others?)',
50   'options'  => \%options,
51   'nodomain' => '',
52   'notes'    => <<END
53 Export accounts (svc_acct records) to SQL databases.  Written for
54 Postfix+Courier IMAP but intended to be generally useful for generic SQL
55 exports, eventually.
56
57 <BR><BR>In contrast to sqlmail, this is newer and less well tested, and
58 currently less flexible.  It is intended to export just svc_acct records only,
59 rather than a single export for svc_acct, svc_forward and svc_domain records,
60 to export in "default" formats rather than configure the MTA or POP/IMAP server
61 for a Freeside-specific schema, and possibly to be configured for different
62 mail server setups through some subclassing rather than options.
63
64 <BR><BR>Use these buttons for some useful presets:
65 <UL>
66   <LI><INPUT TYPE="button" VALUE="postfix_courierimap_mailbox" onClick='
67     this.form.table.value = "mailbox";
68     this.form.schema.value = "$postfix_courierimap_mailbox_map";
69     this.form.primary_key.value = "username";
70   '>
71   <LI><INPUT TYPE="button" VALUE="postfix_courierimap_alias" onClick='
72     this.form.table.value = "alias";
73     this.form.schema.value = "$postfix_courierimap_alias_map";
74     this.form.primary_key.value = "address";
75   '>
76 </UL>
77 END
78 );
79
80 sub _map {
81   my $self = shift;
82   map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option('schema') );
83 }
84
85 sub rebless { shift; }
86
87 sub _export_insert {
88   my($self, $svc_acct) = (shift, shift);
89
90   my %map = $self->_map;
91
92   my %record = map { my $value = $map{$_};
93                      $_ => $svc_acct->$value();
94                    } keys %map;
95
96   my $err_or_queue =
97     $self->acct_sql_queue(
98       $svc_acct->svcnum,
99       'insert',
100       $self->option('table'),
101       %record
102     );
103   return $err_or_queue unless ref($err_or_queue);
104
105   '';
106
107 }
108
109 sub _export_replace {
110   my($self, $new, $old) = (shift, shift, shift);
111
112   my %map = $self->_map;
113   my $keymap = $map{$self->option('primary_key')};
114
115   my %record = map { my $value = $map{$_};
116                      $_ => $new->$value();
117                    } keys %map;
118
119   my $err_or_queue = $self->acct_sql_queue(
120     $new->svcnum,
121     'replace',
122     $self->option('table'),
123     $self->option('primary_key') => $old->$keymap(),
124     %record,
125   );
126   return $err_or_queue unless ref($err_or_queue);
127   '';
128 }
129
130 sub _export_delete {
131   my ( $self, $svc_acct ) = (shift, shift);
132   my %map = $self->_map;
133   my $keymap = $map{$self->option('primary_key')};
134   my $err_or_queue = $self->acct_sql_queue(
135     $svc_acct->svcnum,
136     'delete',
137     $self->option('table'),
138     $self->option('primary_key') => $svc_acct->$keymap(),
139   );
140   return $err_or_queue unless ref($err_or_queue);
141   '';
142 }
143
144 sub acct_sql_queue {
145   my( $self, $svcnum, $method ) = (shift, shift, shift);
146   my $queue = new FS::queue {
147     'svcnum' => $svcnum,
148     'job'    => "FS::part_export::acct_sql::acct_sql_$method",
149   };
150   $queue->insert(
151     $self->option('datasrc'),
152     $self->option('username'),
153     $self->option('password'),
154     @_,
155   ) or $queue;
156 }
157
158 sub acct_sql_insert { #subroutine, not method
159   my $dbh = acct_sql_connect(shift, shift, shift);
160   my( $table, %record ) = @_;
161
162   my $sth = $dbh->prepare(
163     "INSERT INTO $table ( ". join(", ", keys %record).
164     " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
165   ) or die $dbh->errstr;
166
167   $sth->execute( values(%record) )
168     or die "can't insert into $table table: ". $sth->errstr;
169
170   $dbh->disconnect;
171 }
172
173 sub acct_sql_delete { #subroutine, not method
174   my $dbh = acct_sql_connect(shift, shift, shift);
175   my( $table, %record ) = @_;
176
177   my $sth = $dbh->prepare(
178     "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
179   ) or die $dbh->errstr;
180
181   $sth->execute( map $record{$_}, keys %record )
182     or die "can't delete from $table table: ". $sth->errstr;
183
184   $dbh->disconnect;
185 }
186
187 sub acct_sql_replace { #subroutine, not method
188   my $dbh = acct_sql_connect(shift, shift, shift);
189   my( $table, $pkey, $old_pkey, %record ) = @_;
190
191   my $sth = $dbh->prepare(
192     "UPDATE $table SET ". join(', ', map "$_ = ?", keys %record ).
193     "WHERE $pkey = ?"
194   ) or die $dbh->errstr;
195
196   $sth->execute( values(%record), $old_pkey );
197
198   $dbh->disconnect;
199 }
200
201 sub acct_sql_connect {
202   #my($datasrc, $username, $password) = @_;
203   #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
204   DBI->connect(@_) or die $DBI::errstr;
205 }
206
207 1;
208