From 99040eb2bf5ab50bf3e4af138b5d5a7d8d04a333 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 13 Jun 2011 21:07:49 +0000 Subject: [PATCH] forward_sql export, RT#13247 --- FS/FS/part_export.pm | 2 +- FS/FS/part_export/acct_sql.pm | 224 +++--------------------------------------- 2 files changed, 14 insertions(+), 212 deletions(-) diff --git a/FS/FS/part_export.pm b/FS/FS/part_export.pm index c0d3c54a2..d3068df81 100644 --- a/FS/FS/part_export.pm +++ b/FS/FS/part_export.pm @@ -462,7 +462,7 @@ foreach my $INC ( @INC ) { } unless ( keys %$info ) { warn "no %info hash found in FS::part_export::$mod, skipping\n" - unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck + unless $mod =~ /^(passwdfile|null|.+_Common)$/; #hack but what the heck next; } warn "got export info from FS::part_export::$mod: $info\n" if $DEBUG; diff --git a/FS/FS/part_export/acct_sql.pm b/FS/FS/part_export/acct_sql.pm index d561c80c1..fa17afe16 100644 --- a/FS/FS/part_export/acct_sql.pm +++ b/FS/FS/part_export/acct_sql.pm @@ -1,32 +1,15 @@ package FS::part_export::acct_sql; +use base qw( FS::part_export::sql_Common ); -use vars qw(@ISA %info); -use Tie::IxHash; -#use Digest::MD5 qw(md5_hex); +use strict; +use vars qw( %info ); use FS::Record; #qw(qsearchs); -use FS::part_export; -@ISA = qw(FS::part_export); - -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' }, - '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', @@ -118,192 +101,11 @@ to be configured for different mail server setups. END ); -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 rebless { shift; } - -sub _export_insert { - my($self, $svc_acct) = (shift, shift); - - my %schema = $self->_schema_map; - my %static = $self->_static_map; - - my %record = ( - - ( map { $_ => $static{$_} } keys %static ), - - ( map { my $value = $schema{$_}; - my @arg = (); - push @arg, $self->option('crypt') - if $value eq 'crypt_password' && $self->option('crypt'); - $_ => $svc_acct->$value(@arg); - } keys %schema - ), - - ); - - my $err_or_queue = - $self->acct_sql_queue( - $svc_acct->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 = (); - push @arg, $self->option('crypt') - if $value eq 'crypt_password' && $self->option('crypt'); - $_ => $new->$value(@arg); - } keys %schema - ), - - ); - - my $err_or_queue = $self->acct_sql_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_acct ) = (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_acct->$keymap(); - } - } else { - my $keymap = $schema{$self->option('primary_key')}; - $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(), - } - - my $err_or_queue = $self->acct_sql_queue( - $svc_acct->svcnum, - 'delete', - $self->option('table'), - %primary_key, - #$self->option('primary_key') => $svc_acct->$keymap(), - ); - return $err_or_queue unless ref($err_or_queue); - ''; -} - -sub acct_sql_queue { - my( $self, $svcnum, $method ) = (shift, shift, shift); - my $queue = new FS::queue { - 'svcnum' => $svcnum, - 'job' => "FS::part_export::acct_sql::acct_sql_$method", - }; - $queue->insert( - $self->option('datasrc'), - $self->option('username'), - $self->option('password'), - @_, - ) or $queue; -} - -sub acct_sql_insert { #subroutine, not method - my $dbh = acct_sql_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 acct_sql_delete { #subroutine, not method - my $dbh = acct_sql_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 acct_sql_replace { #subroutine, not method - my $dbh = acct_sql_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 acct_sql_connect { - #my($datasrc, $username, $password) = @_; - #DBI->connect($datasrc, $username, $password) or die $DBI::errstr; - DBI->connect(@_) or die $DBI::errstr; +sub _map_arg_callback { + my($self, $field) = @_; + my $crypt = $self->option('crypt'); + return () unless $field eq 'crypt_password' && $crypt; + ($crypt); } 1; -- 2.11.0