| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test::More; |
| 6 |
|
| 7 |
BEGIN { use_ok ("DBI"); } |
| 8 |
do "./t/lib.pl"; |
| 9 |
|
| 10 |
my @tbl_def = ( |
| 11 |
[ "id", "INTEGER", 4, &COL_KEY ], |
| 12 |
[ "str", "CHAR", 64, &COL_NULLABLE ], |
| 13 |
[ "name", "CHAR", 64, &COL_NULLABLE ], |
| 14 |
); |
| 15 |
|
| 16 |
sub DbFile; |
| 17 |
|
| 18 |
my $dir = "output$$"; |
| 19 |
my $fqd = File::Spec->rel2abs ($dir); |
| 20 |
my $abs = Cwd::abs_path ($dir); |
| 21 |
|
| 22 |
ok (my $dbh = Connect (), "connect"); |
| 23 |
|
| 24 |
ok ($dbh->{f_dir} eq $dir || $dbh->{f_dir} eq $abs || |
| 25 |
$dbh->{f_dir} eq $fqd, "default dir"); |
| 26 |
ok ($dbh->{f_dir} = $dir, "set f_dir"); |
| 27 |
|
| 28 |
ok (my $tbl = FindNewTable ($dbh), "find new test table"); |
| 29 |
ok (!-f DbFile ($tbl), "does not exist"); |
| 30 |
|
| 31 |
ok (my $tbl2 = FindNewTable ($dbh), "find new test table"); |
| 32 |
ok (!-f DbFile ($tbl2), "does not exist"); |
| 33 |
|
| 34 |
ok (my $tbl3 = FindNewTable ($dbh), "find new test table"); |
| 35 |
ok (!-f DbFile ($tbl3), "does not exist"); |
| 36 |
|
| 37 |
ok (my $tbl4 = FindNewTable ($dbh), "find new test table"); |
| 38 |
ok (!-f DbFile ($tbl4), "does not exist"); |
| 39 |
|
| 40 |
isnt ($tbl, $tbl2, "different 1 2"); |
| 41 |
isnt ($tbl, $tbl3, "different 1 3"); |
| 42 |
isnt ($tbl, $tbl4, "different 1 4"); |
| 43 |
isnt ($tbl2, $tbl3, "different 2 3"); |
| 44 |
isnt ($tbl2, $tbl4, "different 2 4"); |
| 45 |
isnt ($tbl3, $tbl4, "different 3 4"); |
| 46 |
|
| 47 |
like (my $def = TableDefinition ($tbl, @tbl_def), |
| 48 |
qr{^create table $tbl}i, "table definition"); |
| 49 |
ok ($dbh->do ($def), "create table 1"); |
| 50 |
ok (-f DbFile ($tbl), "does exists"); |
| 51 |
|
| 52 |
ok ($dbh->do ("drop table $tbl"), "drop table"); |
| 53 |
ok (!-f DbFile ($tbl), "does not exist"); |
| 54 |
|
| 55 |
ok ($dbh->disconnect, "disconnect"); |
| 56 |
undef $dbh; |
| 57 |
|
| 58 |
my $dsn = "DBI:CSV:f_dir=$dir;csv_eol=\015\012;csv_sep_char=\\;;"; |
| 59 |
ok ($dbh = Connect ($dsn), "connect"); |
| 60 |
|
| 61 |
ok ($dbh->do ($def), "create table"); |
| 62 |
ok (-f DbFile ($tbl), "does exists"); |
| 63 |
|
| 64 |
ok ($dbh->do ("insert into $tbl values (1, 1, ?)", undef, "joe"), "insert 1"); |
| 65 |
ok ($dbh->do ("insert into $tbl values (2, 2, ?)", undef, "Jochen;"), "insert 2"); |
| 66 |
|
| 67 |
ok (my $sth = $dbh->prepare ("select * from $tbl"), "prepare"); |
| 68 |
ok ($sth->execute, "execute"); |
| 69 |
ok (my $row = $sth->fetch, "fetch 1"); |
| 70 |
is_deeply ($row, [ 1, "1", "joe" ], "content"); |
| 71 |
ok ( $row = $sth->fetch, "fetch 2"); |
| 72 |
is_deeply ($row, [ 2, "2", "Jochen;" ], "content"); |
| 73 |
ok ($sth->finish, "finish"); |
| 74 |
undef $sth; |
| 75 |
|
| 76 |
ok ($dbh->do ("drop table $tbl"), "drop table"); |
| 77 |
ok (!-f DbFile ($tbl), "does not exist"); |
| 78 |
|
| 79 |
ok ($dbh->disconnect, "disconnect"); |
| 80 |
undef $dbh; |
| 81 |
|
| 82 |
$dsn = "DBI:CSV:"; |
| 83 |
ok ($dbh = Connect ($dsn), "connect"); |
| 84 |
|
| 85 |
# Check, whether the csv_tables->{$tbl}{file} attribute works |
| 86 |
like (my $def4 = TableDefinition ($tbl4, @tbl_def), |
| 87 |
qr{^create table $tbl4}i, "table definition"); |
| 88 |
ok ($dbh->{csv_tables}{$tbl4}{file} = DbFile ($tbl4), "set table/file"); |
| 89 |
ok ($dbh->do ($def4), "create table"); |
| 90 |
ok (-f DbFile ($tbl4), "does exists"); |
| 91 |
|
| 92 |
ok ($dbh->do ("drop table $tbl4"), "drop table"); |
| 93 |
|
| 94 |
ok ($dbh->disconnect, "disconnect"); |
| 95 |
undef $dbh; |
| 96 |
|
| 97 |
ok ($dbh = DBI->connect ("dbi:CSV:", "", "", { |
| 98 |
f_dir => DbDir (), |
| 99 |
f_ext => ".csv", |
| 100 |
dbd_verbose => 8, |
| 101 |
csv_sep_char => ";", |
| 102 |
csv_blank_is_undef => 1, |
| 103 |
csv_always_quote => 1, |
| 104 |
}), "connect with attr"); |
| 105 |
|
| 106 |
is ($dbh->{dbd_verbose}, 8, "dbd_verbose set"); |
| 107 |
is ($dbh->{f_ext}, ".csv", "f_ext set"); |
| 108 |
is ($dbh->{csv_sep_char}, ";", "sep_char set"); |
| 109 |
is ($dbh->{csv_blank_is_undef}, 1, "blank_is_undef set"); |
| 110 |
|
| 111 |
ok ($dbh->do ($def), "create table"); |
| 112 |
ok (-f DbFile ($tbl).".csv", "does exists"); |
| 113 |
#is ($sth->{blank_is_undef}, 1, "blank_is_undef"); |
| 114 |
eval { |
| 115 |
local $SIG{__WARN__} = sub { }; |
| 116 |
|
| 117 |
ok ($sth = $dbh->prepare ("insert into $tbl values (?, ?, ?)"), "prepare"); |
| 118 |
is ($sth->execute (1, ""), undef, "not enough values"); |
| 119 |
like ($dbh->errstr, qr/passed 2 parameters where 3 required/, "error message"); |
| 120 |
|
| 121 |
# Cannot use the same handle twice. SQL::Statement bug |
| 122 |
ok ($sth = $dbh->prepare ("insert into $tbl values (?, ?, ?)"), "prepare"); |
| 123 |
is ($sth->execute (1, "", 1, ""), undef, "too many values"); |
| 124 |
like ($dbh->errstr, qr/passed 4 parameters where 3 required/, "error message"); |
| 125 |
}; |
| 126 |
ok ($sth->execute ($_, undef, "Code $_"), "insert $_") for 0 .. 9; |
| 127 |
|
| 128 |
ok ($dbh->do ("drop table $tbl"), "drop table"); |
| 129 |
ok (!-f DbFile ($tbl), "does not exist"); |
| 130 |
ok (!-f DbFile ($tbl).".csv", "does not exist"); |
| 131 |
|
| 132 |
ok ($dbh->disconnect, "disconnect"); |
| 133 |
undef $dbh; |
| 134 |
|
| 135 |
done_testing (); |