| 1 | package DBIx::dbMan::Extension::SQLOutputInsert; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.06'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000070-000006"; } | ||
| 11 | |||
| 12 | sub preference { return 0; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/SQL_OUTPUT/ ]; } | ||
| 15 | |||
| 16 | sub init { | ||
| 17 | my $obj = shift; | ||
| 18 | $obj->{-mempool}->register('output_format','insert'); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub done { | ||
| 22 | my $obj = shift; | ||
| 23 | $obj->{-mempool}->deregister('output_format','insert'); | ||
| 24 | if ($obj->{-mempool}->get('output_format') eq 'insert') { | ||
| 25 | my @all_formats = $obj->{-mempool}->get_register('output_format'); | ||
| 26 | $obj->{-mempool}->set('output_format', @all_formats ? $all_formats[0] : ''); | ||
| 27 | } | ||
| 28 | } | ||
| 29 | |||
| 30 | sub handle_action { | ||
| 31 | my ($obj,%action) = @_; | ||
| 32 | |||
| 33 | $action{processed} = 1; | ||
| 34 | if ($action{action} eq 'SQL_OUTPUT') { | ||
| 35 | if ($obj->{-mempool}->get('output_format') eq 'insert') { | ||
| 36 | my $begin = 'INSERT INTO new_table ('.join(',',@{$action{fieldnames}}).') VALUES ('; | ||
| 37 | my @types = @{$action{fieldtypes}}; | ||
| 38 | my @litp = (); my @lits = (); | ||
| 39 | my $output = 'CREATE TABLE new_table ('.join(',',map { my $temp = $obj->{-dbi}->type_info(shift @types); my %th = (defined $temp)?%$temp:(); my $cp = $th{CREATE_PARAMS}; $cp =~ s/max length|precision/$th{COLUMN_SIZE}/g; $cp =~ s/scale/$th{MINIMUM_SCALE}/g; push @litp,$th{LITERAL_PREFIX}||''; push @lits,$th{LITERAL_SUFFIX}||''; $_.' '.$th{TYPE_NAME}.($cp?"($cp)":'').($th{NULLABLE} == 1?'':' NOT NULL'); } @{$action{fieldnames}}).");\n"; | ||
| 40 | for (@{$action{result}}) { | ||
| 41 | my @lp = @litp; my @ls = @lits; | ||
| 42 | $output .= $begin . join ',',map { my $lm = shift @lp; my $rm = shift @ls; defined($_)?"$lm$_$rm":"NULL" } @$_; | ||
| 43 | $output .= ");\n"; | ||
| 44 | } | ||
| 45 | $action{action} = 'OUTPUT'; | ||
| 46 | $action{output} = $output; | ||
| 47 | delete $action{processed}; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | return %action; | ||
| 52 | } |