-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_tab_to_db.pl
executable file
·219 lines (174 loc) · 4.61 KB
/
add_tab_to_db.pl
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env perl
=head1 NAME
add_tab_to_db.pl - add tab separated file to Database
=head1 SYNOPSIS
perl add_tab_to_db.pl [OPTION]...
-v, --verbose use verbose mode
--help print this help message
--file tab separated file
--action create: create table
select: select from table
drop: drop table
Examples:
#insert data
perl add_tab_to_db.pl --file clients_banners.csv
#create table
perl add_tab_to_db.pl --action create
#select from table
perl add_tab_to_db.pl --action select
#drop table
perl add_tab_to_db.pl --action drop
=head1 DESCRIPTION
This program add tab separated file to table banners
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
=over 1
=item * Nikolay Mishin (L<MISHIN|https://metacpan.org/author/MISHIN>)
=back
=cut
use strict;
use warnings;
use 5.010;
use utf8;
use open qw/:std :utf8/;
use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use Cwd;
use FindBin '$RealBin';
use Log::Log4perl qw(:easy);
use Text::CSV;
use Data::Dumper;
use DBI;
exit main();
sub main {
# Argument parsing
my $verbose = 0; # frequently referred
my $log_file = $RealBin . "/add_tab_to_db.log";
#Init logging
Log::Log4perl->easy_init(
{
level => $DEBUG,
file => ":utf8>>$log_file",
layout => '%d %p> %m%n'
}
);
my %options = ( 'verbose' => $verbose, );
GetOptions( \%options, 'verbose', 'file=s', 'action=s' ) or pod2usage(1);
if ( !exists $options{file} && !exists $options{action} ) {
if ( defined $ARGV[0] ) {
$options{file} = $ARGV[0];
}
else {
pod2usage(1);
}
}
my $dbh = make_connect();
if ( exists $options{file} ) {
add_file_to_db( \%options, $dbh );
}
if ( exists $options{action} ) {
my $disp = make_dispatch_table();
if ( defined $disp->{ $options{action} } ) {
$disp->{ $options{action} }->($dbh);
}
else {
die "$options{action}:no such action!";
}
}
$dbh->disconnect();
return 0;
}
sub make_dispatch_table {
my $dispatch = {
create => \&create_table,
select => \&select_from_table,
drop => \&drop_table
};
return $dispatch;
}
sub add_file_to_db {
my ( $options, $dbh ) = @_;
my $filename = $options->{file};
my $tab_data = read_csv($filename);
add_data_to_banners( $tab_data, $dbh )
or die "Unable to add data to banners table $!";
my $msg = qq{read $filename with data } . Dumper($tab_data);
INFO($msg);
}
sub add_data_to_banners {
my ( $data, $dbh ) = @_;
my $insert_handle = $dbh->prepare('INSERT INTO banners VALUES (?,?,?)');
# start new transaction #
$dbh->begin_work();
foreach my $row (@$data) {
$insert_handle->execute(@$row);
}
# end the transaction #
$dbh->commit();
my $cnt = @$data + 0;
print "$cnt rows added to table banners\n";
my $rc = $dbh->disconnect;
return $rc;
}
sub read_csv {
my ($file) = @_;
my $csv = Text::CSV->new(
{
binary => 1,
auto_diag => 1,
sep_char => "\t",
}
);
my @result = ();
open( my $data, '<:encoding(utf8)', $file )
or die "Could not open '$file' $!\n";
while ( my $fields = $csv->getline($data) ) {
push @result, $fields;
}
if ( not $csv->eof ) {
$csv->error_diag();
}
close $data;
return \@result;
}
sub make_connect {
my $dbh =
DBI->connect( "dbi:SQLite:dbname=test.db", "", "", { RaiseError => 1 } )
or die $DBI::errstr;
return $dbh;
}
sub create_table {
my ($dbh) = @_;
$dbh->do(<<'END_SQL');
create table banners (
banner_id int unsigned not null primary key,
title varchar(200),
url varchar(4000)
)
END_SQL
print "table banners created\n";
}
sub select_from_table {
my ($dbh) = @_;
my $stmt = qq(SELECT banner_id, title, url from banners;);
my $sth = $dbh->prepare($stmt);
my $rv = $sth->execute() or die $DBI::errstr;
if ( $rv < 0 ) {
print $DBI::errstr;
}
while ( my @row = $sth->fetchrow_array() ) {
print "banner_id = " . $row[0] . "\n";
print "title = " . $row[1] . "\n";
print "url = " . $row[2] . "\n\n";
}
print "Select done successfully\n";
}
sub drop_table {
my ($dbh) = @_;
$dbh->do(<<'END_SQL');
drop table banners
END_SQL
print "table banners dropped\n";
}