« blib/lib/App/RecordStream/Operation/fromatomfeed.pm
↑ Summary
blib/lib/App/RecordStream/Operation/fromxml.pm »
| 1 | package App::RecordStream::Operation::fromdb; | ||
| 2 | |||
| 3 | our $VERSION = "4.0.23"; | ||
| 4 | |||
| 5 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | ▶ | use base qw(App::RecordStream::Operation); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | use App::RecordStream::OptionalRequire 'DBI'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | BEGIN { App::RecordStream::OptionalRequire::require_done() } |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | use App::RecordStream::DBHandle; | ||
| 14 | use App::RecordStream::Record; | ||
| 15 | |||
| 16 | sub init { | ||
| 17 | my $this = shift; | ||
| 18 | my $args = shift; | ||
| 19 | |||
| 20 | my ($table_name, $sql); | ||
| 21 | my $spec = { | ||
| 22 | 'table=s' => \$table_name, | ||
| 23 | 'sql=s' => \$sql, | ||
| 24 | }; | ||
| 25 | |||
| 26 | $this->parse_options($args, $spec, ['pass_through']); | ||
| 27 | |||
| 28 | $this->{'TABLE_NAME'} = $table_name; | ||
| 29 | |||
| 30 | my $dbh = App::RecordStream::DBHandle::get_dbh($args); | ||
| 31 | $this->{'DBH'} = $dbh; | ||
| 32 | |||
| 33 | die("Must define --table or --sql\n") unless ( $table_name || $sql ); | ||
| 34 | |||
| 35 | unless ( $sql ) { | ||
| 36 | $sql = "SELECT * FROM $table_name"; | ||
| 37 | } | ||
| 38 | |||
| 39 | $this->{'SQL'} = $sql; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub wants_input { | ||
| 43 | return 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub stream_done { | ||
| 47 | my $this = shift; | ||
| 48 | |||
| 49 | my $sth = $this->{'DBH'}->prepare($this->{'SQL'}); | ||
| 50 | $sth->execute(); | ||
| 51 | |||
| 52 | while ( my $row = $sth->fetchrow_hashref() ) { | ||
| 53 | my $record = App::RecordStream::Record->new(%$row); | ||
| 54 | $this->push_record($record); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub usage { | ||
| 59 | my $this = shift; | ||
| 60 | |||
| 61 | my $options = [ | ||
| 62 | [ 'table', 'Name of the table to dump, this is a shortcut for --sql \'SELECT * from tableName\''], | ||
| 63 | [ 'sql', 'SQL select statement to run'], | ||
| 64 | ]; | ||
| 65 | |||
| 66 | my $args_string = $this->options_string($options); | ||
| 67 | |||
| 68 | my $usage = <<USAGE; | ||
| 69 | __FORMAT_TEXT__ | ||
| 70 | Recs from DB will execute a select statement on a database of your choice, | ||
| 71 | and create a record stream from the results. The keys of the record will be | ||
| 72 | the column names and the values the row values. | ||
| 73 | __FORMAT_TEXT__ | ||
| 74 | |||
| 75 | $args_string | ||
| 76 | |||
| 77 | USAGE | ||
| 78 | |||
| 79 | return $usage . App::RecordStream::DBHandle::usage() . <<EXAMPLES; | ||
| 80 | Examples: | ||
| 81 | # Dump a table | ||
| 82 | recs-fromdb --type sqlite --dbfile testDb --table recs | ||
| 83 | |||
| 84 | # Run a select statement | ||
| 85 | recs-fromdb --dbfile testDb --sql 'SELECT * FROM recs WHERE id > 9' | ||
| 86 | EXAMPLES | ||
| 87 | } | ||
| 88 | |||
| 89 | 1; | ||