| 1 | package DBIx::ProcedureCall::CLI; | ||
| 2 | |||
| 3 | require Exporter; | ||
| 4 | @ISA = qw[ Exporter]; | ||
| 5 | @EXPORT = qw [ procedure function ]; | ||
| 6 | |||
| 7 | use DBI; | ||
| 8 | use DBIx::ProcedureCall; | ||
| 9 | use Data::Dumper; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our $VERSION = '0.08'; | ||
| 14 | |||
| 15 | sub conn{ | ||
| 16 | my ($dsn) = @_; | ||
| 17 | my $conn = DBI->connect($dsn, undef, undef, { | ||
| 18 | RaiseError => 1, AutoCommit => 1, PrintError => 0, | ||
| 19 | }); | ||
| 20 | |||
| 21 | } | ||
| 22 | |||
| 23 | |||
| 24 | sub get_bind_array{ | ||
| 25 | my @a = @ARGV; | ||
| 26 | foreach (@a){ | ||
| 27 | # if it starts with a colon, make a reference | ||
| 28 | if (index ($_, ':') == 0 ){ | ||
| 29 | my $var; | ||
| 30 | ($_ , $var ) = split '=', $_, 2; | ||
| 31 | $_ = \$var; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | return @a; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub procedure{ | ||
| 38 | my $dsn; | ||
| 39 | $dsn = shift @ARGV if $ARGV[0] =~ /^dbi:/; | ||
| 40 | $ARGV[0] =~ s/:fetch\(\)/:fetch[]/; | ||
| 41 | my $dbh = conn($dsn); | ||
| 42 | my @a = get_bind_array; | ||
| 43 | eval{ | ||
| 44 | DBIx::ProcedureCall::run($dbh, @a); | ||
| 45 | print "executed procedure '$ARGV[0]'. \n"; | ||
| 46 | }; | ||
| 47 | if ($@){ | ||
| 48 | warn "failed to execute procedure '$ARGV[0]':\n"; | ||
| 49 | warn "-----------------------\n"; | ||
| 50 | warn $@; | ||
| 51 | warn "-----------------------\n"; | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | print_out_params(@a); | ||
| 55 | |||
| 56 | } | ||
| 57 | |||
| 58 | sub function{ | ||
| 59 | my $dsn; | ||
| 60 | $dsn = shift @ARGV if $ARGV[0] =~ /^dbi:/; | ||
| 61 | my $dbh = conn($dsn); | ||
| 62 | my @a = get_bind_array; | ||
| 63 | # change :fetch() to :fetch[] | ||
| 64 | $ARGV[0] =~ s/:fetch\(\)/:fetch[]/; | ||
| 65 | my $result; | ||
| 66 | eval{ | ||
| 67 | $result = DBIx::ProcedureCall::run($dbh, @a); | ||
| 68 | print "executed function '$ARGV[0]'. \n"; | ||
| 69 | }; | ||
| 70 | if ($@){ | ||
| 71 | warn "failed to execute function '$ARGV[0]':\n"; | ||
| 72 | warn "-----------------------\n"; | ||
| 73 | warn $@; | ||
| 74 | warn "-----------------------\n"; | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | print "------ result -----------\n"; | ||
| 79 | if (ref $result ){ | ||
| 80 | print Dumper $result; | ||
| 81 | if (ref $result eq 'ARRAY'){ | ||
| 82 | print scalar @$result; | ||
| 83 | print " rows returned \n"; | ||
| 84 | } | ||
| 85 | }else{ | ||
| 86 | print $result; | ||
| 87 | print "\n"; | ||
| 88 | } | ||
| 89 | print "------------------------\n"; | ||
| 90 | print_out_params(@a); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub print_out_params{ | ||
| 95 | # skip procedure name | ||
| 96 | shift; | ||
| 97 | my $output = ""; | ||
| 98 | my $i = 0; | ||
| 99 | foreach (@_){ | ||
| 100 | $i++; | ||
| 101 | next unless ref $_; | ||
| 102 | $ARGV[$i] =~ s/=.+//; | ||
| 103 | $output .= "$ARGV[$i] = "; | ||
| 104 | $output .= defined $$_ ? $$_ : '<null>'; | ||
| 105 | $output .= "\n"; | ||
| 106 | } | ||
| 107 | return unless $output; | ||
| 108 | print "------ parameters -------\n"; | ||
| 109 | print $output; | ||
| 110 | print "------------------------\n"; | ||
| 111 | } | ||
| 112 | 1; | ||
| 113 | |||
| 114 | __END__ |