| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# lib.pl is the file where database specific things should live, |
| 4 |
# wherever possible. For example, you define certain constants |
| 5 |
# here and the like. |
| 6 |
|
| 7 |
use strict; |
| 8 |
use warnings; |
| 9 |
|
| 10 |
use File::Spec; |
| 11 |
|
| 12 |
my $testname = "output$$"; |
| 13 |
my $base_dir = File::Spec->rel2abs (File::Spec->curdir ()); |
| 14 |
my $test_dir = File::Spec->rel2abs ($testname); |
| 15 |
my $test_dsn = $ENV{DBI_DSN} || ""; |
| 16 |
$test_dsn =~ m/csv/i or $test_dsn = "dbi:CSV:f_dir=$testname"; |
| 17 |
my $test_user = $ENV{DBI_USER} || ""; |
| 18 |
my $test_pass = $ENV{DBI_PASS} || ""; |
| 19 |
|
| 20 |
DBI->import (":sql_types"); |
| 21 |
defined &SQL_CHAR or *SQL_CHAR = sub { 1 }; |
| 22 |
defined &SQL_VARCHAR or *SQL_VARCHAR = sub { 12 }; |
| 23 |
defined &SQL_INTEGER or *SQL_INTEGER = sub { 4 }; |
| 24 |
|
| 25 |
sub COL_NULLABLE () { 1 } |
| 26 |
sub COL_KEY () { 2 } |
| 27 |
|
| 28 |
my %v; |
| 29 |
{ my @req = qw( DBI SQL::Statement Text::CSV_XS DBD::CSV ); |
| 30 |
my $req = join ";\n" => map { qq{require $_;\n\$v{"$_"} = $_->VERSION ()} } @req; |
| 31 |
eval $req; |
| 32 |
|
| 33 |
if ($@) { |
| 34 |
my @missing = grep { !exists $v{$_} } @req; |
| 35 |
print STDERR "\n\nYOU ARE MISSING REQUIRED MODULES: [ @missing ]\n\n"; |
| 36 |
exit 0; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
sub AnsiTypeToDb { |
| 41 |
my ($type, $size) = @_; |
| 42 |
my $uctype = uc $type; |
| 43 |
|
| 44 |
if ($uctype eq "CHAR" || $uctype eq "VARCHAR") { |
| 45 |
$size ||= 1; |
| 46 |
return "$uctype ($size)"; |
| 47 |
} |
| 48 |
|
| 49 |
$uctype eq "BLOB" || $uctype eq "REAL" || $uctype eq "INTEGER" and |
| 50 |
return $uctype; |
| 51 |
|
| 52 |
$uctype eq "INT" and |
| 53 |
return "INTEGER"; |
| 54 |
|
| 55 |
warn "Unknown type $type\n"; |
| 56 |
return $type; |
| 57 |
} # AnsiTypeToDb |
| 58 |
|
| 59 |
# This function generates a table definition based on an input list. The input |
| 60 |
# list consists of references, each reference referring to a single column. The |
| 61 |
# column reference consists of column name, type, size and a bitmask of certain |
| 62 |
# flags, namely |
| 63 |
# |
| 64 |
# COL_NULLABLE - true, if this column may contain NULL's |
| 65 |
# COL_KEY - true, if this column is part of the table's primary key |
| 66 |
|
| 67 |
sub TableDefinition { |
| 68 |
my ($tablename, @cols) = @_; |
| 69 |
|
| 70 |
my @keys = (); |
| 71 |
foreach my $col (@cols) { |
| 72 |
$col->[2] & COL_KEY and push @keys, $col->[0]; |
| 73 |
} |
| 74 |
|
| 75 |
my @colDefs; |
| 76 |
foreach my $col (@cols) { |
| 77 |
my $colDef = $col->[0] . " " . AnsiTypeToDb ($col->[1], $col->[2]); |
| 78 |
$col->[3] & COL_NULLABLE or $colDef .= " NOT NULL"; |
| 79 |
push @colDefs, $colDef; |
| 80 |
} |
| 81 |
my $keyDef = @keys ? ", PRIMARY KEY (" . join (", ", @keys) . ")" : ""; |
| 82 |
my $tq = $tablename =~ m/^\w+\./ ? qq{"$tablename"} : $tablename; |
| 83 |
return sprintf "CREATE TABLE %s (%s%s)", $tq, |
| 84 |
join (", ", @colDefs), $keyDef; |
| 85 |
} # TableDefinition |
| 86 |
|
| 87 |
# This function generates a list of tables associated to a given DSN. |
| 88 |
sub ListTables { |
| 89 |
my $dbh = shift or return; |
| 90 |
|
| 91 |
my @tables = $dbh->func ("list_tables"); |
| 92 |
my $msg = $dbh->errstr || $DBI::errstr; |
| 93 |
$msg and die "Cannot create table list: $msg"; |
| 94 |
@tables; |
| 95 |
} # ListTables |
| 96 |
|
| 97 |
sub DbCleanup { |
| 98 |
chdir $base_dir; |
| 99 |
-d $testname or return; |
| 100 |
chdir $testname or BAIL_OUT ("Cleanup failed"); |
| 101 |
unlink glob "*"; |
| 102 |
chdir $base_dir; |
| 103 |
rmdir $testname; |
| 104 |
} # DbCleanup |
| 105 |
|
| 106 |
mkdir $testname, 0755; |
| 107 |
END { DbCleanup (); } |
| 108 |
|
| 109 |
# This functions generates a list of possible DSN's aka |
| 110 |
# databases and returns a possible table name for a new |
| 111 |
# table being created. |
| 112 |
# |
| 113 |
# Problem is, we have two different situations here: Test scripts |
| 114 |
# call us by pasing a dbh, which is fine for most situations. |
| 115 |
{ my $listTablesHook; |
| 116 |
|
| 117 |
my $testtable = "testaa"; |
| 118 |
|
| 119 |
sub FindNewTable |
| 120 |
{ |
| 121 |
my $dbh = shift; |
| 122 |
|
| 123 |
my @tables = defined $listTablesHook ? $listTablesHook->($dbh) |
| 124 |
: defined &ListTables ? ListTables ($dbh) |
| 125 |
: die "Fatal: ListTables not implemented.\n"; |
| 126 |
|
| 127 |
my $table; |
| 128 |
while (grep { $_ eq $testtable } @tables) { |
| 129 |
$testtable++; |
| 130 |
} |
| 131 |
$table = $testtable; |
| 132 |
$testtable++; |
| 133 |
return $table; |
| 134 |
} # FindNewTable |
| 135 |
} |
| 136 |
|
| 137 |
sub isSaneCase { |
| 138 |
my @f = glob "??????.???"; |
| 139 |
foreach my $try (qw( FrUbLl BlURgH wOngOs )) { |
| 140 |
my $fn = "$try.csv"; |
| 141 |
grep m{^$fn$}i => @f and next; |
| 142 |
open my $fh, ">", $fn or return 1; |
| 143 |
close $fh; |
| 144 |
my $sane = (-f $fn && ! -f lc $fn && ! -f uc $fn); |
| 145 |
unlink $fn; |
| 146 |
return $sane; |
| 147 |
} |
| 148 |
# Assume insane |
| 149 |
return 0; |
| 150 |
} # isSaneCase |
| 151 |
|
| 152 |
sub ServerError { |
| 153 |
die "# Cannot connect: $DBI::errstr\n"; |
| 154 |
} # ServerError |
| 155 |
|
| 156 |
sub Connect { |
| 157 |
my $attr = @_ && ref $_[-1] eq "HASH" ? pop @_ : {}; |
| 158 |
my ($dsn, $usr, $pass) = @_; |
| 159 |
$dsn ||= $test_dsn; |
| 160 |
$usr ||= $test_user; |
| 161 |
$pass ||= $test_pass; |
| 162 |
my $dbh = DBI->connect ($dsn, $usr, $pass, $attr) or ServerError; |
| 163 |
$dbh; |
| 164 |
} # Connect |
| 165 |
|
| 166 |
sub DbDir { |
| 167 |
@_ and $test_dir = File::Spec->catdir ($base_dir, shift); |
| 168 |
$test_dir; |
| 169 |
} # DbDir |
| 170 |
|
| 171 |
sub DbFile { |
| 172 |
my $file = shift or return; |
| 173 |
File::Spec->catdir ($test_dir, $file); |
| 174 |
} # DbFile |
| 175 |
|
| 176 |
1; |