1 package FS::part_export::acct_sql;
3 use vars qw(@ISA %info @saltset);
5 #use Digest::MD5 qw(md5_hex);
6 use FS::Record; #qw(qsearchs);
9 @ISA = qw(FS::part_export);
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.',
20 'primary_key' => { label => 'Database primary key' },
23 tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash',
24 'username' => 'email',
25 'password' => '_password',
26 'crypt' => 'crypt_password',
28 'maildir' => 'virtual_maildir',
32 my $postfix_courierimap_mailbox_map =
33 join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}",
34 keys %postfix_courierimap_mailbox_map );
36 tie my %postfix_courierimap_alias_map, 'Tie::IxHash',
42 my $postfix_courierimap_alias_map =
43 join('\n', map "$_ $postfix_courierimap_alias_map{$_}",
44 keys %postfix_courierimap_alias_map );
48 'desc' => 'Real-time export of accounts to SQL databases '.
49 '(Postfix+Courier IMAP, others?)',
50 'options' => \%options,
53 Export accounts (svc_acct records) to SQL databases. Written for
54 Postfix+Courier IMAP but intended to be generally useful for generic SQL
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.
64 <BR><BR>Use these buttons for some useful presets:
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";
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";
82 map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option('schema') );
85 sub rebless { shift; }
88 my($self, $svc_acct) = (shift, shift);
90 my %map = $self->_map;
92 my %record = map { my $value = $map{$_};
93 $_ => $svc_acct->$value();
97 $self->acct_sql_queue(
100 $self->option('table'),
103 return $err_or_queue unless ref($err_or_queue);
109 sub _export_replace {
110 my($self, $new, $old) = (shift, shift, shift);
112 my %map = $self->_map;
113 my $keymap = $map{$self->option('primary_key')};
115 my %record = map { my $value = $map{$_};
116 $_ => $new->$value();
119 my $err_or_queue = $self->acct_sql_queue(
122 $self->option('table'),
123 $self->option('primary_key') => $old->$keymap(),
126 return $err_or_queue unless ref($err_or_queue);
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(
137 $self->option('table'),
138 $self->option('primary_key') => $svc_acct->$keymap(),
140 return $err_or_queue unless ref($err_or_queue);
145 my( $self, $svcnum, $method ) = (shift, shift, shift);
146 my $queue = new FS::queue {
148 'job' => "FS::part_export::acct_sql::acct_sql_$method",
151 $self->option('datasrc'),
152 $self->option('username'),
153 $self->option('password'),
158 sub acct_sql_insert { #subroutine, not method
159 my $dbh = acct_sql_connect(shift, shift, shift);
160 my( $table, %record ) = @_;
162 my $sth = $dbh->prepare(
163 "INSERT INTO $table ( ". join(", ", keys %record).
164 " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
165 ) or die $dbh->errstr;
167 $sth->execute( values(%record) )
168 or die "can't insert into $table table: ". $sth->errstr;
173 sub acct_sql_delete { #subroutine, not method
174 my $dbh = acct_sql_connect(shift, shift, shift);
175 my( $table, %record ) = @_;
177 my $sth = $dbh->prepare(
178 "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
179 ) or die $dbh->errstr;
181 $sth->execute( map $record{$_}, keys %record )
182 or die "can't delete from $table table: ". $sth->errstr;
187 sub acct_sql_replace { #subroutine, not method
188 my $dbh = acct_sql_connect(shift, shift, shift);
189 my( $table, $pkey, $old_pkey, %record ) = @_;
191 my $sth = $dbh->prepare(
192 "UPDATE $table SET ". join(', ', map "$_ = ?", keys %record ).
194 ) or die $dbh->errstr;
196 $sth->execute( values(%record), $old_pkey );
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;