From 80c705e3125aab7a6d2aa43ac5df8b25bb4f5f92 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 13 Jun 2011 21:08:23 +0000 Subject: [PATCH] forward_sql export, RT#13247 --- FS/FS/part_export/acct_sql.pm | 12 +-- FS/FS/part_export/forward_sql.pm | 24 +++++ FS/FS/part_export/sql_Common.pm | 215 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+), 6 deletions(-) create mode 100644 FS/FS/part_export/forward_sql.pm create mode 100644 FS/FS/part_export/sql_Common.pm diff --git a/FS/FS/part_export/acct_sql.pm b/FS/FS/part_export/acct_sql.pm index fa17afe16..0c0b4ac30 100644 --- a/FS/FS/part_export/acct_sql.pm +++ b/FS/FS/part_export/acct_sql.pm @@ -5,11 +5,11 @@ use strict; use vars qw( %info ); use FS::Record; #qw(qsearchs); -my %options = __PACKAGE__->sql_options; -$options{'crypt'} = { label => 'Password encryption', - type=>'select', options=>[qw(crypt md5 sha1_base64)], - default=>'crypt', - }; +my $options = __PACKAGE__->sql_options; +$options->{'crypt'} = { label => 'Password encryption', + type=>'select', options=>[qw(crypt md5 sha1_base64)], + default=>'crypt', + }; tie my %vpopmail_map, 'Tie::IxHash', 'pw_name' => 'username', @@ -62,7 +62,7 @@ my $postfix_native_mailbox_map = 'svc' => 'svc_acct', 'desc' => 'Real-time export of accounts to SQL databases '. '(vpopmail, Postfix+Courier IMAP, others?)', - 'options' => \%options, + 'options' => $options, 'nodomain' => '', 'notes' => < 'svc_forward', + 'desc' => 'Real-time export of forwards to SQL databases ', + #.' (vpopmail, Postfix+Courier IMAP, others?)', + 'options' => __PACKAGE__->sql_options, + 'notes' => <
In contrast to sqlmail, this is intended to export just svc_forward +records only, rather than a single export for svc_acct, svc_forward and +svc_domain records, to export in "default" database schemas rather than +configure the MTA or POP/IMAP server for a Freeside-specific schema, and +to be configured for different mail server setups. +END +); + +1; diff --git a/FS/FS/part_export/sql_Common.pm b/FS/FS/part_export/sql_Common.pm new file mode 100644 index 000000000..6aab7ec38 --- /dev/null +++ b/FS/FS/part_export/sql_Common.pm @@ -0,0 +1,215 @@ +package FS::part_export::sql_Common; +use base qw( FS::part_export ); + +use strict; +use Tie::IxHash; + +tie my %options, 'Tie::IxHash', + 'datasrc' => { label => 'DBI data source' }, + 'username' => { label => 'Database username' }, + 'password' => { label => 'Database password' }, + 'table' => { label => 'Database table' }, + 'schema' => { label => + 'Database schema mapping to Freeside methods.', + type => 'textarea', + }, + 'static' => { label => + 'Database schema mapping to static values.', + type => 'textarea', + }, + 'primary_key' => { label => 'Database primary key' }, +; + +sub sql_options { + \%options; +} +sub _schema_map { shift->_map('schema'); } +sub _static_map { shift->_map('static'); } + +sub _map { + my $self = shift; + map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option(shift) ); +} + +sub _map_arg_callback { + (); +} + +sub rebless { shift; } + +sub _export_insert { + my($self, $svc_x) = (shift, shift); + + my %schema = $self->_schema_map; + my %static = $self->_static_map; + + my %record = ( + + ( map { $_ => $static{$_} } keys %static ), + + ( map { my $value = $schema{$_}; + my @arg = $self->_map_arg_callback($value); + $_ => $svc_x->$value(@arg); + } keys %schema + ), + + ); + + my $err_or_queue = + $self->sql_Common_queue( + $svc_x->svcnum, + 'insert', + $self->option('table'), + %record + ); + return $err_or_queue unless ref($err_or_queue); + + ''; + +} + +sub _export_replace { + my($self, $new, $old) = (shift, shift, shift); + + my %schema = $self->_schema_map; + my %static = $self->_static_map; + + my @primary_key = (); + if ( $self->option('primary_key') =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) { + my $keymap = $schema{$key}; + push @primary_key, $old->$keymap(); + } + } else { + my $keymap = $schema{$self->option('primary_key')}; + push @primary_key, $old->$keymap(); + } + + my %record = ( + + ( map { $_ => $static{$_} } keys %static ), + + ( map { my $value = $schema{$_}; + my @arg = $self->_map_arg_callback($value); + $_ => $new->$value(@arg); + } keys %schema + ), + + ); + + my $err_or_queue = $self->sql_Common_queue( + $new->svcnum, + 'replace', + $self->option('table'), + $self->option('primary_key'), @primary_key, + %record, + ); + return $err_or_queue unless ref($err_or_queue); + ''; +} + +sub _export_delete { + my ( $self, $svc_x ) = (shift, shift); + + my %schema = $self->_schema_map; + + my %primary_key = (); + if ( $self->option('primary_key') =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) { + my $keymap = $schema{$key}; + $primary_key{ $key } = $svc_x->$keymap(); + } + } else { + my $keymap = $schema{$self->option('primary_key')}; + $primary_key{ $self->option('primary_key') } = $svc_x->$keymap(), + } + + my $err_or_queue = $self->sql_Common_queue( + $svc_x->svcnum, + 'delete', + $self->option('table'), + %primary_key, + #$self->option('primary_key') => $svc_x->$keymap(), + ); + return $err_or_queue unless ref($err_or_queue); + ''; +} + +sub sql_Common_queue { + my( $self, $svcnum, $method ) = (shift, shift, shift); + my $queue = new FS::queue { + 'svcnum' => $svcnum, + 'job' => "FS::part_export::sql_Common::sql_Common_$method", + }; + $queue->insert( + $self->option('datasrc'), + $self->option('username'), + $self->option('password'), + @_, + ) or $queue; +} + +sub sql_Common_insert { #subroutine, not method + my $dbh = sql_Common_connect(shift, shift, shift); + my( $table, %record ) = @_; + + my $sth = $dbh->prepare( + "INSERT INTO $table ( ". join(", ", keys %record). + " ) VALUES ( ". join(", ", map '?', keys %record ). " )" + ) or die $dbh->errstr; + + $sth->execute( values(%record) ) + or die "can't insert into $table table: ". $sth->errstr; + + $dbh->disconnect; +} + +sub sql_Common_delete { #subroutine, not method + my $dbh = sql_Common_connect(shift, shift, shift); + my( $table, %record ) = @_; + + my $sth = $dbh->prepare( + "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record ) + ) or die $dbh->errstr; + + $sth->execute( map $record{$_}, keys %record ) + or die "can't delete from $table table: ". $sth->errstr; + + $dbh->disconnect; +} + +sub sql_Common_replace { #subroutine, not method + my $dbh = sql_Common_connect(shift, shift, shift); + + my( $table, $pkey ) = ( shift, shift ); + + my %primary_key = (); + if ( $pkey =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $pkey ) ) { + $primary_key{$key} = shift; + } + } else { + $primary_key{$pkey} = shift; + } + + my %record = @_; + + my $sth = $dbh->prepare( + "UPDATE $table". + ' SET '. join(', ', map "$_ = ?", keys %record ). + ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key ) + ) or die $dbh->errstr; + + $sth->execute( values(%record), values(%primary_key) ); + + $dbh->disconnect; +} + +sub sql_Common_connect { + #my($datasrc, $username, $password) = @_; + #DBI->connect($datasrc, $username, $password) or die $DBI::errstr; + DBI->connect(@_) or die $DBI::errstr; +} + +1; + -- 2.11.0