File Coverage

blib/lib/App/RecordStream/Operation/fromdb.pm
Criterion Covered Total %
statement 13 13 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 18 100.0


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::fromdb;
2              
3             our $VERSION = "4.0.25";
4              
5 2     2   359 use strict;
  2         3  
  2         41  
6 2     2   7 use warnings;
  2         4  
  2         39  
7              
8 2     2   7 use base qw(App::RecordStream::Operation);
  2         2  
  2         141  
9              
10 2     2   9 use App::RecordStream::OptionalRequire 'DBI';
  2         4  
  2         9  
11 2     2   6 BEGIN { App::RecordStream::OptionalRequire::require_done() }
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 = <
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() . <
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;