1 package FS::cust_main::_Marketgear;
4 use vars qw( $DEBUG $me $conf );
7 $me = '[FS::cust_main::_Marketgear]';
9 install_callback FS::UID sub {
16 return '' unless $conf->config('cust_main-skeleton_tables')
17 && $conf->config('cust_main-skeleton_custnum');
19 warn " inserting skeleton records\n"
20 if $DEBUG > 1 || $cust_main::DEBUG > 1;
22 #'mg_user_preference' => {},
23 #'mg_user_indicator_profile.user_indicator_profile_id' => { 'mg_profile_indicator.profile_indicator_id' => { 'mg_profile_details.profile_detail_id' }, },
24 #'mg_watchlist_header.watchlist_header_id' => { 'mg_watchlist_details.watchlist_details_id' },
25 #'mg_user_grid_header.grid_header_id' => { 'mg_user_grid_details.user_grid_details_id' },
26 #'mg_portfolio_header.portfolio_header_id' => { 'mg_portfolio_trades.portfolio_trades_id' => { 'mg_portfolio_trades_positions.portfolio_trades_positions_id' } },
27 my @tables = eval(join('\n',$conf->config('cust_main-skeleton_tables')));
30 _copy_skel( 'cust_main', #tablename
31 $conf->config('cust_main-skeleton_custnum'), #sourceid
32 $self->custnum, #destid
33 @tables, #child tables
37 #recursive subroutine, not a method
39 my( $table, $sourceid, $destid, %child_tables ) = @_;
42 if ( $table =~ /^(\w+)\.(\w+)$/ ) {
43 ( $table, $primary_key ) = ( $1, $2 );
45 my $dbdef_table = dbdef->table($table);
46 $primary_key = $dbdef_table->primary_key
47 or return "$table has no primary key".
48 " (or do you need to run dbdef-create?)";
51 warn " _copy_skel: $table.$primary_key $sourceid to $destid for ".
52 join (', ', keys %child_tables). "\n"
55 foreach my $child_table_def ( keys %child_tables ) {
59 if ( $child_table_def =~ /^(\w+)\.(\w+)$/ ) {
60 ( $child_table, $child_pkey ) = ( $1, $2 );
62 $child_table = $child_table_def;
64 $child_pkey = dbdef->table($child_table)->primary_key;
65 # or return "$table has no primary key".
66 # " (or do you need to run dbdef-create?)\n";
70 if ( keys %{ $child_tables{$child_table_def} } ) {
72 return "$child_table has no primary key".
73 " (run dbdef-create or try specifying it?)\n"
76 #false laziness w/Record::insert and only works on Pg
77 #refactor the proper last-inserted-id stuff out of Record::insert if this
78 # ever gets use for anything besides a quick kludge for one customer
79 my $default = dbdef->table($child_table)->column($child_pkey)->default;
80 $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i
81 or return "can't parse $child_table.$child_pkey default value ".
82 " for sequence name: $default";
87 my @sel_columns = grep { $_ ne $primary_key }
88 dbdef->table($child_table)->columns;
89 my $sel_columns = join(', ', @sel_columns );
91 my @ins_columns = grep { $_ ne $child_pkey } @sel_columns;
92 my $ins_columns = ' ( '. join(', ', $primary_key, @ins_columns ). ' ) ';
93 my $placeholders = ' ( ?, '. join(', ', map '?', @ins_columns ). ' ) ';
95 my $sel_st = "SELECT $sel_columns FROM $child_table".
96 " WHERE $primary_key = $sourceid";
99 my $sel_sth = dbh->prepare( $sel_st )
100 or return dbh->errstr;
102 $sel_sth->execute or return $sel_sth->errstr;
104 while ( my $row = $sel_sth->fetchrow_hashref ) {
106 warn " selected row: ".
107 join(', ', map { "$_=".$row->{$_} } keys %$row ). "\n"
111 "INSERT INTO $child_table $ins_columns VALUES $placeholders";
112 my $ins_sth =dbh->prepare($statement)
113 or return dbh->errstr;
114 my @param = ( $destid, map $row->{$_}, @ins_columns );
115 warn " $statement: [ ". join(', ', @param). " ]\n"
117 $ins_sth->execute( @param )
118 or return $ins_sth->errstr;
120 #next unless keys %{ $child_tables{$child_table} };
121 next unless $sequence;
123 #another section of that laziness
124 my $seq_sql = "SELECT currval('$sequence')";
125 my $seq_sth = dbh->prepare($seq_sql) or return dbh->errstr;
126 $seq_sth->execute or return $seq_sth->errstr;
127 my $insertid = $seq_sth->fetchrow_arrayref->[0];
129 # don't drink soap! recurse! recurse! okay!
131 _copy_skel( $child_table_def,
132 $row->{$child_pkey}, #sourceid
134 %{ $child_tables{$child_table_def} },
136 return $error if $error;