|
1 |
| -#!/usr/bin/perl |
| 1 | +#!/pro/bin/perl |
2 | 2 |
|
3 | 3 | use 5.18.2;
|
4 | 4 | use warnings;
|
5 | 5 |
|
6 |
| -use DP; |
| 6 | +our $VERSION = "0.02 - 20200727"; |
| 7 | +our $CMD = $0 =~ s{.*/}{}r; |
| 8 | + |
| 9 | +sub usage { |
| 10 | + my $err = shift and select STDERR; |
| 11 | + say "usage: $CMD [--devel]"; |
| 12 | + exit $err; |
| 13 | + } # usage |
| 14 | + |
| 15 | +use CSV; |
7 | 16 | use DBI;
|
8 |
| -use lib "/pro/3gl/CPAN/DBD-CSV/lib"; |
9 |
| -use lib "/pro/3gl/CPAN/DBD-CSV/blib/arch"; |
10 |
| -use lib "/pro/3gl/CPAN/DBD-CSV/blib/lib"; |
11 |
| - |
12 |
| -my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { |
13 |
| - f_schema => undef, |
14 |
| - f_dir => ".", |
15 |
| - f_ext => ".csv/r", |
16 |
| - |
17 |
| - RaiseError => 1, |
18 |
| - PrintError => 1, |
19 |
| - }) or die "$DBI::errstr\n" || $DBI::errstr; |
20 |
| - |
21 |
| -$dbh->{csv_tables}{tst} = { |
22 |
| - file => "test.csv", # alias to f_file |
23 |
| - eol => "\n", # alias to csv_eol |
24 |
| - sep_char => ";", # alias to csv_sep_char |
25 |
| - always_quote => 1, # alias to csv_always_quote |
26 |
| - col_names => [qw( c_tst s_tst )], |
27 |
| - }; |
28 |
| - |
29 |
| -#$dbh->{TraceLevel} = 99; |
30 |
| - |
31 |
| -say for $dbh->tables (undef, undef, undef, undef); |
32 |
| - |
33 |
| -$dbh->{csv_tables}{tools}{sep_char} = ";"; # should work |
34 |
| - |
35 |
| -foreach my $t (qw( tools fruit )) { |
36 |
| - say $t; |
37 |
| - my $sth = $dbh->prepare ("select * from $t"); |
38 |
| - $sth->execute; |
39 |
| - while (my @r = $sth->fetchrow_array) { |
40 |
| - printf "%4d %s\n", @r; |
41 |
| - } |
| 17 | +use Test::More; |
| 18 | +use Getopt::Long qw(:config bundling); |
| 19 | +GetOptions ( |
| 20 | + "help|?" => sub { usage (0); }, |
| 21 | + "V|version" => sub { say "$CMD [$VERSION]"; exit 0; }, |
| 22 | + |
| 23 | + "d|devel!" => \ my $opt_d, |
| 24 | + |
| 25 | + "v|verbose:1" => \(my $opt_v = 0), |
| 26 | + ) or usage (1); |
| 27 | + |
| 28 | +if ($opt_d) { |
| 29 | + unshift @INC => "/pro/3gl/CPAN/DBD-CSV/lib"; |
| 30 | + unshift @INC => "/pro/3gl/CPAN/DBD-CSV/blib/arch"; |
| 31 | + unshift @INC => "/pro/3gl/CPAN/DBD-CSV/blib/lib"; |
42 | 32 | }
|
43 | 33 |
|
44 |
| -open my $fh, ">", "test.csv";close $fh; |
45 |
| -# If empty should insert "c_tst";"s_tst" |
46 |
| -$dbh->do ("insert into tst values (42, 'Test')"); # "42";"Test" |
47 |
| -$dbh->do ("update tst set s_tst = 'Done' where c_tst = 42"); # "42";"Done" |
| 34 | +foreach my $x (0, 1) { |
| 35 | + my ($fpfx, $cpfx) = $x ? ("f_", "csv_") : ("", ""); |
| 36 | + my $dbh = DBI->connect ("dbi:CSV:", undef, undef, { |
| 37 | + "${fpfx}schema" => undef, |
| 38 | + "${fpfx}dir" => "files", |
| 39 | + "${fpfx}ext" => ".csv/r", |
| 40 | + |
| 41 | + "${cpfx}eol" => "\n", # alias to csv_eol |
| 42 | + "${cpfx}always_quote" => 1, # alias to csv_always_quote |
| 43 | + "${cpfx}sep_char" => ";", # alias to csv_sep_char |
| 44 | + |
| 45 | + RaiseError => 1, |
| 46 | + PrintError => 1, |
| 47 | + }) or die "$DBI::errstr\n" || $DBI::errstr; |
| 48 | + |
| 49 | + unlink "test.csv"; |
| 50 | + $dbh->{csv_tables}{tst} = { |
| 51 | + file => "test.csv", # alias to f_file |
| 52 | + col_names => [qw( c_tst s_tst )], |
| 53 | + }; |
| 54 | + |
| 55 | + is_deeply ( |
| 56 | + [ sort $dbh->tables (undef, undef, undef, undef) ], |
| 57 | + [qw( fruit tools )], "Tables"); |
| 58 | + is_deeply ( |
| 59 | + [ sort keys %{$dbh->{csv_tables}} ], |
| 60 | + [qw( fruit tools tst )], "Mixed tables"); |
| 61 | + |
| 62 | + $dbh->{csv_tables}{fruit}{sep_char} = ","; # should work |
| 63 | + |
| 64 | + is_deeply ($dbh->selectall_arrayref ("select * from tools order by c_tool"), |
| 65 | + [ [ 1, "Hammer" ], |
| 66 | + [ 2, "Screwdriver" ], |
| 67 | + [ 3, "Drill" ], |
| 68 | + [ 4, "Saw" ], |
| 69 | + [ 5, "Router" ], |
| 70 | + [ 6, "Hobbyknife" ], |
| 71 | + ], "Sorted tools"); |
| 72 | + is_deeply ($dbh->selectall_arrayref ("select * from fruit order by c_fruit"), |
| 73 | + [ [ 1, "Apple" ], |
| 74 | + [ 2, "Blueberry" ], |
| 75 | + [ 3, "Orange" ], |
| 76 | + [ 4, "Melon" ], |
| 77 | + ], "Sorted fruit"); |
| 78 | + |
| 79 | + open my $fh, ">", "test.csv";close $fh; |
| 80 | + # If empty should insert "c_tst";"s_tst" |
| 81 | + $dbh->do ("insert into tst values (42, 'Test')"); # "42";"Test" |
| 82 | + $dbh->do ("update tst set s_tst = 'Done' where c_tst = 42"); # "42";"Done" |
| 83 | + |
| 84 | + $dbh->disconnect; |
| 85 | + |
| 86 | + open $fh, "<", "test.csv" or die "test.csv: $!\n"; |
| 87 | + my @dta = <$fh>; |
| 88 | + close $fh; |
| 89 | + is ($dta[-1], qq{"42";"Done"\n}, "Table tst written to test.csv"); |
| 90 | + unlink "test.csv"; |
| 91 | + } |
48 | 92 |
|
49 |
| -$dbh->disconnect; |
| 93 | +done_testing; |
0 commit comments