1 package FS::part_export::domain_sql;
3 use vars qw(@ISA %info);
7 @ISA = qw(FS::part_export);
9 #quite a bit of false laziness w/acct_sql - some stuff should be generalized
10 #out to a "dababase base class"
12 tie my %options, 'Tie::IxHash',
13 'datasrc' => { label => 'DBI data source' },
14 'username' => { label => 'Database username' },
15 'password' => { label => 'Database password' },
16 'table' => { label => 'Database table' },
17 'schema' => { label =>
18 'Database schema mapping to Freeside methods.',
21 'static' => { label =>
22 'Database schema mapping to static values.',
25 'primary_key' => { label => 'Database primary key' },
28 tie my %postfix_transport_map, 'Tie::IxHash',
31 my $postfix_transport_map =
32 join('\n', map "$_ $postfix_transport_map{$_}",
33 keys %postfix_transport_map );
34 tie my %postfix_transport_static, 'Tie::IxHash',
35 'transport' => 'virtual:',
37 my $postfix_transport_static =
38 join('\n', map "$_ $postfix_transport_static{$_}",
39 keys %postfix_transport_static );
42 'svc' => 'svc_domain',
43 'desc' => 'Real time export of domains to SQL databases '.
45 'options' => \%options,
47 Export domains (svc_domain records) to SQL databases. Currently this is a
48 simple export with a default for Postfix, but it can be extended for other
51 <BR><BR>Use these buttons for useful presets:
53 <LI><INPUT TYPE="button" VALUE="postfix_transport" onClick='
54 this.form.table.value = "transport";
55 this.form.schema.value = "$postfix_transport_map";
56 this.form.static.value = "$postfix_transport_static";
57 this.form.primary_key.value = "domain";
63 sub _schema_map { shift->_map('schema'); }
64 sub _static_map { shift->_map('static'); }
68 map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option(shift) );
72 my($self, $svc_domain) = (shift, shift);
74 my %schema = $self->_schema_map;
75 my %static = $self->_static_map;
77 my %record = ( map { $_ => $static{$_} } keys %static ),
78 ( map { my $method = $schema{$_};
79 $_ => $svc_domain->$method();
84 $self->domain_sql_queue(
87 $self->option('table'),
90 return $err_or_queue unless ref($err_or_queue);
96 my($self, $new, $old) = (shift, shift, shift);
98 my %schema = $self->_schema_map;
99 my %static = $self->_static_map;
101 my @primary_key = ();
102 if ( $self->option('primary_key') =~ /,/ ) {
103 foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
104 my $keymap = $schema{$key};
105 push @primary_key, $old->$keymap();
108 my $keymap = $map{$self->option('primary_key')};
109 push @primary_key, $old->$keymap();
112 my %record = ( map { $_ => $static{$_} } keys %static ),
113 ( map { my $method = $schema{$_};
114 $_ => $new->$method();
118 my $err_or_queue = $self->domain_sql_queue(
121 $self->option('table'),
122 $self->option('primary_key'), @primary_key,
125 return $err_or_queue unless ref($err_or_queue);
130 my ( $self, $svc_domain ) = (shift, shift);
132 my %schema = $self->_schema_map;
133 my %static = $self->_static_map;
135 my %primary_key = ();
136 if ( $self->option('primary_key') =~ /,/ ) {
137 foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
138 my $keymap = $map{$key};
139 $primary_key{ $key } = $svc_domain->$keymap();
142 my $keymap = $map{$self->option('primary_key')};
143 $primary_key{ $self->option('primary_key') } = $svc_domain->$keymap(),
146 my $err_or_queue = $self->domain_sql_queue(
149 $self->option('table'),
151 #$self->option('primary_key') => $svc_domain->$keymap(),
153 return $err_or_queue unless ref($err_or_queue);
157 sub domain_sql_queue {
158 my( $self, $svcnum, $method ) = (shift, shift, shift);
159 my $queue = new FS::queue {
161 'job' => "FS::part_export::domain_sql::domain_sql_$method",
164 $self->option('datasrc'),
165 $self->option('username'),
166 $self->option('password'),
171 sub domain_sql_insert { #subroutine, not method
172 my $dbh = domain_sql_connect(shift, shift, shift);
173 my( $table, %record ) = @_;
175 my $sth = $dbh->prepare(
176 "INSERT INTO $table ( ". join(", ", keys %record).
177 " ) VALUES ( ". join(", ", map '?', keys %record ). " )"
178 ) or die $dbh->errstr;
180 $sth->execute( values(%record) )
181 or die "can't insert into $table table: ". $sth->errstr;
186 sub domain_sql_delete { #subroutine, not method
187 my $dbh = domain_sql_connect(shift, shift, shift);
188 my( $table, %record ) = @_;
190 my $sth = $dbh->prepare(
191 "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record )
192 ) or die $dbh->errstr;
194 $sth->execute( map $record{$_}, keys %record )
195 or die "can't delete from $table table: ". $sth->errstr;
200 sub domain_sql_replace { #subroutine, not method
201 my $dbh = domain_sql_connect(shift, shift, shift);
203 my( $table, $pkey ) = ( shift, shift );
205 my %primary_key = ();
206 if ( $pkey =~ /,/ ) {
207 foreach my $key ( split(/\s*,\s*/, $pkey ) ) {
208 $primary_key{$key} = shift;
211 $primary_key{$pkey} = shift;
216 my $sth = $dbh->prepare(
218 ' SET '. join(', ', map "$_ = ?", keys %record ).
219 ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key )
220 ) or die $dbh->errstr;
222 $sth->execute( values(%record), values(%primary_key) );
227 sub domain_sql_connect {
228 #my($datasrc, $username, $password) = @_;
229 #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
230 DBI->connect(@_) or die $DBI::errstr;