File: 82_table_info.t

package info (click to toggle)
libdbd-odbc-perl 1.50-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,392 kB
  • ctags: 496
  • sloc: perl: 8,818; ansic: 6,376; makefile: 33; sql: 8
file content (147 lines) | stat: -rwxr-xr-x 3,800 bytes parent folder | download
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
#!/usr/bin/perl -w -I./t
#
# Test type_info
#
use strict;
use warnings;
use DBI;
use Test::More;
use Data::Dumper;
use DBI::Const::GetInfoType;

my $has_test_nowarnings = 1;
eval "require Test::NoWarnings";
$has_test_nowarnings = undef if $@;

my $dbh;

BEGIN {
   if (!defined $ENV{DBI_DSN}) {
      plan skip_all => "DBI_DSN is undefined";
   }
}

END {
    if ($dbh) {
        local $dbh->{PrintWarn} = 0;
        local $dbh->{PrintError} = 0;
        $dbh->do(q/drop table PERL_DBD_DROP_ME/);
    }
};

$dbh = DBI->connect();
unless($dbh) {
   BAIL_OUT("Unable to connect to the database ($DBI::errstr)\nTests skipped.\n");
   exit 0;
}
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 0;

$dbh->do(q/create table PERL_DBD_DROP_ME (a char(10))/);

if ($dbh->get_info($GetInfoType{SQL_CATALOG_NAME}) ne 'N') {

    # test type_info('%','','') which should return catalogs only
    my $s = $dbh->table_info('%', '', '');
    my $r = $s->fetchall_arrayref;
    if ($r && scalar(@$r)) {    # assuming we get something back
        my $pass = 1;
        foreach my $row (@$r) {
            if (!defined($row->[0])) {
                $pass = 0;
                diag("Catalog is not defined");
            }

            if (defined($row->[1])) {
                $pass = 0;
                diag("Schema is defined as $row->[1]");
            }

            if (defined($row->[2])) {
                $pass = 0;
                diag("Table is defined as $row->[2]");
            }
        }
        ok($pass, "catalogs only") or diag(Dumper($r));
    }
}

if ($dbh->get_info($GetInfoType{SQL_SCHEMA_USAGE}) != 0) {
    # test type_info('','%','') which should return schema only
    my $s = $dbh->table_info('', '%', '');
    my $r = $s->fetchall_arrayref;
    if ($r && scalar(@$r)) {    # assuming we get something back
        my $pass = 1;
        foreach my $row (@$r) {
            if (defined($row->[0])) {
                $pass = 0;
                diag("Catalog is defined as $row->[0]");
            }

            if (!defined($row->[1])) {
                $pass = 0;
                diag("Schema is not defined");
            }

            if (defined($row->[2])) {
                $pass = 0;
                diag("Table is defined as $row->[2]");
            }
        }
        ok($pass, "schema only") or diag(Dumper($r));
    }
}

{
    # test type_info() - returns tables
    my $s = $dbh->table_info(undef, undef, 'PERL_DBD_DROP_ME');
    my $r = $s->fetchall_arrayref;
    ok(scalar(@$r) > 0, 'table found');

    if ($r && scalar(@$r)) {    # assuming we get something back
        my $pass = 0;
        foreach my $row (@$r) {
            $pass = 1;
        }
        ok($pass, "table only") or diag(Dumper($r));
    }
}

# test type_info('','','', '%')  which should return table types only
if ($dbh->get_info($GetInfoType{SQL_DRIVER_NAME}) =~ /sqlite/) {
    diag("SQLite is known to fail the next test because catalog, schema and table are returned as '' instead of undef");
}

my $s = $dbh->table_info('', '', '', '%');
my $r = $s->fetchall_arrayref;
if ($r && scalar(@$r)) {        # assuming we get something back
    my $pass = 1;
    foreach my $row (@$r) {
        if (defined($row->[0])) {
            $pass = 0;
            diag("Catalog is defined as $row->[0]");
        }

        if (defined($row->[1])) {
            $pass = 0;
            diag("Schema is defined as $row->[1]");
        }

        if (defined($row->[2])) {
            $pass = 0;
            diag("Table is defined as $row->[2]");
        }

        if (!defined($row->[3])) {
            $pass = 0;
            diag("table type is not defined");
        }
    }
    ok($pass, "table type only") or diag(Dumper($r));
}


Test::NoWarnings::had_no_warnings()
  if ($has_test_nowarnings);

done_testing();