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 {
113 my ( $self, $svc_acct ) = (shift, shift);
114 my %map = $self->_map;
115 my $keymap = $map{$self->option('primary_key')};
116 my $err_or_queue = $self->acct_sql_queue(
119 $self->option('table'),
120 $self->option('primary_key') => $svc_acct->$keymap(),
122 return $err_or_queue unless ref($err_or_queue);
127 my( $self, $svcnum, $method ) = (shift, shift, shift);
128 my $queue = new FS::queue {
130 'job' => "FS::part_export::acct_sql::acct_sql_$method",
133 $self->option('datasrc'),
134 $self->option('username'),
135 $self->option('password'),
140 sub acct_sql_insert { #subroutine, not method
141 my $dbh = acct_sql_connect(shift, shift, shift);
142 my( $table, %record ) = @_;
144 my $sth = $dbh->prepare(
145 "INSERT INTO $table ( ". join(", ", keys %record).
146 " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
147 ) or die $dbh->errstr;
149 $sth->execute( map $record{$_}, keys %record )
150 or die "can't insert into $table table: ". $sth->errstr;
155 sub acct_sql_delete { #subroutine, not method
156 my $dbh = acct_sql_connect(shift, shift, shift);
157 my( $table, %record ) = @_;
159 my $sth = $dbh->prepare(
160 "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
161 ) or die $dbh->errstr;
163 $sth->execute( map $record{$_}, keys %record )
164 or die "can't delete from $table table: ". $sth->errstr;
169 sub acct_sql_connect {
170 #my($datasrc, $username, $password) = @_;
171 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
172 DBI->connect(@_) or die $DBI::errstr;