File: odbc_describe_parameter.t

package info (click to toggle)
libdbd-odbc-perl 1.37-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,272 kB
  • sloc: perl: 7,932; ansic: 5,991; makefile: 33; sql: 8
file content (203 lines) | stat: -rw-r--r-- 4,578 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
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
#!/usr/bin/perl -w -I./t
# $Id: odbc_describe_parameter.t 15144 2012-02-13 09:23:24Z mjevans $
#
# Test odbc_describe_parameters
# Should default to on but you can turn it off in the prepare or at the
# connection level.
#
use Test::More;
use strict;
$| = 1;

my $has_test_nowarnings = 1;
eval "require Test::NoWarnings";
$has_test_nowarnings = undef if $@;
my $tests = 17;
$tests += 1 if $has_test_nowarnings;
plan tests => $tests;

use DBI qw(:sql_types);
#1
use_ok('ODBCTEST');

my $dbh;

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

END {
    if ($dbh) {
        eval {
            local $dbh->{PrintWarn} = 0;
            local $dbh->{PrintError} = 0;
            $dbh->do(q/drop table PERL_DBD_drop_me/);
        };
        $dbh->disconnect;
    }
    Test::NoWarnings::had_no_warnings()
          if ($has_test_nowarnings);
}

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


my ($ev, $sth);

eval {
    local $dbh->{PrintWarn} = 0;
    local $dbh->{PrintError} = 0;
    $dbh->do('drop table PERL_DBD_drop_me');
};

eval {
    $dbh->do('create table PERL_DBD_drop_me (a integer)');
};
$ev = $@;
#2
diag($ev) if $ev;
ok(!$ev, 'create test table with integer');

BAIL_OUT("Failed to create test table") if $ev;

sub default
{
    eval {
        $sth = $dbh->prepare('INSERT into PERL_DBD_drop_me VALUES (?)');
    };
    $ev = $@;
    diag($ev) if $ev;
    #3
    ok($sth && !$@, "prepare insert");

  SKIP: {
        skip "Failed to prepare", 1 if ($ev);

        eval {
            $sth->execute(1);
        };
        $ev = $@;
        diag($ev) if $ev;
        #4
        ok(!$@, "execute ok");
    };

  SKIP: {
        skip "Failed to execute", 1 if ($ev);

        my $pts = $sth->{ParamTypes};
        #5
        is(ref($pts), 'HASH', 'ParamTypes is a hash');
        my @params = keys %$pts;
        #6
        is(scalar(@params), 1, 'one parameter');
        #use Data::Dumper;
        #diag(Dumper($pts->{$params[0]}));
        #7
        # for drivers which don't have SQLDescribeParam the type will
        # be defaulted to SQL_VARCHAR or SQL_WVARCHAR
        ok(($pts->{$params[0]}->{TYPE} == SQL_INTEGER) ||
           ($pts->{$params[0]}->{TYPE} == SQL_LONGVARCHAR) ||
           ($pts->{$params[0]}->{TYPE} == SQL_WLONGVARCHAR) ||
           ($pts->{$params[0]}->{TYPE} == SQL_WVARCHAR) ||
           ($pts->{$params[0]}->{TYPE} == SQL_VARCHAR), 'integer parameter')
            or diag("Param type: " . $pts->{$params[0]}->{TYPE});
    };


}

sub on_prepare
{
    eval {
        $sth = $dbh->prepare('INSERT into PERL_DBD_drop_me VALUES (?)',
                             {
                                 odbc_describe_parameters => 0});
    };
    $ev = $@;
    diag($ev) if $ev;
    #8
    ok($sth && !$@, "prepare insert");

  SKIP: {
        skip "Failed to prepare", 1 if ($ev);

        eval {
            $sth->execute(1);
        };
        $ev = $@;
        diag($ev) if $ev;
        #9
        ok(!$@, "execute ok");
    };

  SKIP: {
        skip "Failed to execute", 1 if ($ev);

        my $pts = $sth->{ParamTypes};
        #10
        is(ref($pts), 'HASH', 'ParamTypes is a hash');
        my @params = keys %$pts;
        #11
        is(scalar(@params), 1, 'one parameter');
        #use Data::Dumper;
        #diag(Dumper($pts->{$params[0]}));
        #12
        ok(($pts->{$params[0]}->{TYPE} == 12) ||
               ($pts->{$params[0]}->{TYPE} == -9), 'char parameter (prepare)') or
	       diag($pts->{$params[0]}->{TYPE});
    };
}

sub on_connect
{
    $dbh->{odbc_describe_parameters} = 0;

    eval {
        $sth = $dbh->prepare('INSERT into PERL_DBD_drop_me VALUES (?)');
    };
    $ev = $@;
    diag($ev) if $ev;
    #8
    ok($sth && !$@, "prepare insert");

  SKIP: {
        skip "Failed to prepare", 1 if ($ev);

        eval {
            $sth->execute(1);
        };
        $ev = $@;
        diag($ev) if $ev;
        #9
        ok(!$@, "execute ok");
    };

  SKIP: {
        skip "Failed to execute", 1 if ($ev);

        my $pts = $sth->{ParamTypes};
        #10
        is(ref($pts), 'HASH', 'ParamTypes is a hash');
        my @params = keys %$pts;
        #11
        is(scalar(@params), 1, 'one parameter');
        #use Data::Dumper;
        #diag(Dumper($pts->{$params[0]}));
        #12
        ok(($pts->{$params[0]}->{TYPE} == 12) ||
               ($pts->{$params[0]}->{TYPE} == -9), 'char parameter (connect)');
    };
}

default();
on_prepare();
on_connect();