| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
# sql_to_db.pm |
|
5
|
|
|
|
|
|
|
# Copyright (C) 2014 by John Heidemann |
|
6
|
|
|
|
|
|
|
# $Id: dd1b62c899ec2f2f31abaa9131d4f053b42c038e $ |
|
7
|
|
|
|
|
|
|
# |
|
8
|
|
|
|
|
|
|
# This program is distributed under terms of the GNU general |
|
9
|
|
|
|
|
|
|
# public license, version 2. See the file COPYING |
|
10
|
|
|
|
|
|
|
# in $dblib for details. |
|
11
|
|
|
|
|
|
|
# |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Fsdb::Filter::sqlselect_to_db; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sqlselect_to_db - convert MySQL or MariaDB selected tables to fsdb |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sqlselect_to_db dest.fsdb |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Converts a MySQL or MariaDB tables to Fsdb format. |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
The input is I fsdb. |
|
28
|
|
|
|
|
|
|
The first non-box row is taken to be the names of the columns. |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
The output is two-space-separated fsdb. |
|
31
|
|
|
|
|
|
|
(Someday more general field separators should be supported.) |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=for comment |
|
34
|
|
|
|
|
|
|
begin_standard_fsdb_options |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This module also supports the standard fsdb options: |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=over 4 |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=item B<-d> |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Enable debugging output. |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=item B<-i> or B<--input> InputSource |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Read from InputSource, typically a file name, or C<-> for standard input, |
|
47
|
|
|
|
|
|
|
or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item B<-o> or B<--output> OutputDestination |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Write to OutputDestination, typically a file name, or C<-> for standard output, |
|
52
|
|
|
|
|
|
|
or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects. |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item B<--autorun> or B<--noautorun> |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
By default, programs process automatically, |
|
57
|
|
|
|
|
|
|
but Fsdb::Filter objects in Perl do not run until you invoke |
|
58
|
|
|
|
|
|
|
the run() method. |
|
59
|
|
|
|
|
|
|
The C<--(no)autorun> option controls that behavior within Perl. |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=item B<--help> |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Show help. |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=item B<--man> |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Show full manual. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=back |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=for comment |
|
72
|
|
|
|
|
|
|
end_standard_fsdb_options |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 SAMPLE USAGE |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head2 Input: |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
+----------------+---------------+--------------------+------+-------------------------+ |
|
80
|
|
|
|
|
|
|
| username | firstname | lastname | id | email | |
|
81
|
|
|
|
|
|
|
+----------------+---------------+--------------------+------+-------------------------+ |
|
82
|
|
|
|
|
|
|
| johnh | John | Heidemann | 134 | johnh@isi.edu | |
|
83
|
|
|
|
|
|
|
+----------------+---------------+--------------------+------+-------------------------+ |
|
84
|
|
|
|
|
|
|
1 row in set (0.01 sec) |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 Command: |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sqlselect_to_db |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=head2 Output: |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
#fsdb -F S username firstname lastname id email |
|
93
|
|
|
|
|
|
|
johnh John Heidemann 134 johnh@isi.edu |
|
94
|
|
|
|
|
|
|
# | sqlselect_to_db |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
L. |
|
100
|
|
|
|
|
|
|
L. |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 CLASS FUNCTIONS |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=cut |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
@ISA = qw(Fsdb::Filter); |
|
108
|
|
|
|
|
|
|
$VERSION = 2.0; |
|
109
|
|
|
|
|
|
|
|
|
110
|
1
|
|
|
1
|
|
3406
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
25
|
|
|
111
|
1
|
|
|
1
|
|
3
|
use Pod::Usage; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
63
|
|
|
112
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
37
|
|
|
113
|
|
|
|
|
|
|
|
|
114
|
1
|
|
|
1
|
|
3
|
use Text::CSV_XS; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
23
|
|
|
115
|
|
|
|
|
|
|
|
|
116
|
1
|
|
|
1
|
|
2
|
use Fsdb::Filter; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
13
|
|
|
117
|
1
|
|
|
1
|
|
3
|
use Fsdb::IO::Writer; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
551
|
|
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 new |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$filter = new Fsdb::Filter::csv_to_db(@arguments); |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Create a new csv_to_db object, taking command-line arguments. |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=cut |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub new ($@) { |
|
129
|
0
|
|
|
0
|
1
|
|
my $class = shift @_; |
|
130
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(@_); |
|
131
|
0
|
|
|
|
|
|
bless $self, $class; |
|
132
|
0
|
|
|
|
|
|
$self->set_defaults; |
|
133
|
0
|
|
|
|
|
|
$self->parse_options(@_); |
|
134
|
0
|
|
|
|
|
|
$self->SUPER::post_new(); |
|
135
|
0
|
|
|
|
|
|
return $self; |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 set_defaults |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$filter->set_defaults(); |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Internal: set up defaults. |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub set_defaults ($) { |
|
148
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
|
149
|
0
|
|
|
|
|
|
$self->SUPER::set_defaults(); |
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 parse_options |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
$filter->parse_options(@ARGV); |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Internal: parse command-line arguments. |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub parse_options ($@) { |
|
161
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
|
162
|
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
my(@argv) = @_; |
|
164
|
|
|
|
|
|
|
$self->get_options( |
|
165
|
|
|
|
|
|
|
\@argv, |
|
166
|
0
|
|
|
0
|
|
|
'help|?' => sub { pod2usage(1); }, |
|
167
|
0
|
|
|
0
|
|
|
'man' => sub { pod2usage(-verbose => 2); }, |
|
168
|
|
|
|
|
|
|
'autorun!' => \$self->{_autorun}, |
|
169
|
|
|
|
|
|
|
'd|debug+' => \$self->{_debug}, |
|
170
|
0
|
|
|
0
|
|
|
'i|input=s' => sub { $self->parse_io_option('input', @_); }, |
|
171
|
|
|
|
|
|
|
'log!' => \$self->{_logprog}, |
|
172
|
0
|
|
|
0
|
|
|
'o|output=s' => sub { $self->parse_io_option('output', @_); }, |
|
173
|
0
|
0
|
|
|
|
|
) or pod2usage(2); |
|
174
|
0
|
0
|
|
|
|
|
pod2usage(2) if ($#argv >= 0); |
|
175
|
|
|
|
|
|
|
} |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head2 setup |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
$filter->setup(); |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Internal: setup, parse headers. |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub setup ($) { |
|
186
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
|
187
|
|
|
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
$self->finish_fh_io_option('input'); |
|
189
|
|
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
$self->{_lineno} = 0; |
|
191
|
0
|
|
|
|
|
|
my($header); |
|
192
|
0
|
|
|
|
|
|
for (;;) { |
|
193
|
0
|
|
|
|
|
|
$header = $self->{_in}->getline; |
|
194
|
0
|
|
|
|
|
|
$self->{_lineno}++; |
|
195
|
0
|
0
|
|
|
|
|
croak $self->{_prog} . ": cound not find header\n" |
|
196
|
|
|
|
|
|
|
if (!defined($header)); |
|
197
|
0
|
|
|
|
|
|
chomp($header); |
|
198
|
0
|
0
|
|
|
|
|
last if ($header !~ /^[-+]+$/); |
|
199
|
|
|
|
|
|
|
}; |
|
200
|
0
|
|
|
|
|
|
my(@raw_columns) = split(/\|/, $header); |
|
201
|
0
|
|
|
|
|
|
shift(@raw_columns); # REQUIRE leading | |
|
202
|
|
|
|
|
|
|
#if ($raw_columns[0] eq ''); |
|
203
|
0
|
|
|
|
|
|
my(@columns) = Fsdb::IO::clean_potential_columns(@raw_columns); |
|
204
|
0
|
|
|
|
|
|
$self->finish_io_option('output', -fscode => 'S', -cols => \@columns); |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 run |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
$filter->run(); |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Internal: run over each rows. |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=cut |
|
214
|
|
|
|
|
|
|
sub run ($) { |
|
215
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
|
216
|
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
my $write_fastpath_sub = $self->{_out}->fastpath_sub(); |
|
218
|
|
|
|
|
|
|
|
|
219
|
0
|
|
|
|
|
|
for (;;) { |
|
220
|
0
|
|
|
|
|
|
my $line = $self->{_in}->getline; |
|
221
|
0
|
|
|
|
|
|
$self->{_lineno}++; |
|
222
|
0
|
0
|
|
|
|
|
last if (!defined($line)); |
|
223
|
0
|
|
|
|
|
|
chomp $line; |
|
224
|
0
|
0
|
|
|
|
|
next if ($line =~ /^[-+]+$/); |
|
225
|
0
|
0
|
|
|
|
|
next if ($line =~ /\d+ rows? in set/); |
|
226
|
0
|
|
|
|
|
|
my(@row) = split(/\|/, $line); |
|
227
|
0
|
|
|
|
|
|
shift(@row); # require leading | |
|
228
|
0
|
|
|
|
|
|
grep { s/^ +//g; } @row; |
|
|
0
|
|
|
|
|
|
|
|
229
|
0
|
|
|
|
|
|
grep { s/ +$//g; } @row; |
|
|
0
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
grep { s/ +/ /g; } @row; # clean up for fsdb double-space separator |
|
|
0
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
grep { s/^ *$/-/g; } @row; # add null values for fields |
|
|
0
|
|
|
|
|
|
|
|
232
|
0
|
|
|
|
|
|
&{$write_fastpath_sub}(\@row); |
|
|
0
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
}; |
|
234
|
|
|
|
|
|
|
} |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head1 AUTHOR and COPYRIGHT |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Copyright (C) 2014 by John Heidemann |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
This program is distributed under terms of the GNU general |
|
242
|
|
|
|
|
|
|
public license, version 2. See the file COPYING |
|
243
|
|
|
|
|
|
|
with the distribution for details. |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=cut |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1; |