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