~ubuntu-branches/ubuntu/feisty/libdbd-csv-perl/feisty

« back to all changes in this revision

Viewing changes to t/40bindparam.t

  • Committer: Bazaar Package Importer
  • Author(s): Stephen Zander
  • Date: 2002-04-12 16:10:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412161047-7aiszzv9vwkgz8rk
Tags: upstream-0.2002
Import upstream version 0.2002

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/local/bin/perl
 
2
#
 
3
#   $Id: 40bindparam.t,v 1.1.1.1 1999/06/13 12:59:35 joe Exp $
 
4
#
 
5
#   This is a skeleton test. For writing new tests, take this file
 
6
#   and modify/extend it.
 
7
#
 
8
 
 
9
$^W = 1;
 
10
 
 
11
 
 
12
#
 
13
#   Make -w happy
 
14
#
 
15
$test_dsn = '';
 
16
$test_user = '';
 
17
$test_password = '';
 
18
 
 
19
 
 
20
#
 
21
#   Include lib.pl
 
22
#
 
23
require DBI;
 
24
use vars qw($COL_NULLABLE);
 
25
$mdriver = "";
 
26
foreach $file ("lib.pl", "t/lib.pl") {
 
27
    do $file; if ($@) { print STDERR "Error while executing lib.pl: $@\n";
 
28
                           exit 10;
 
29
                      }
 
30
    if ($mdriver ne '') {
 
31
        last;
 
32
    }
 
33
}
 
34
if ($mdriver eq 'pNET') {
 
35
    print "1..0\n";
 
36
    exit 0;
 
37
}
 
38
 
 
39
sub ServerError() {
 
40
    my $err = $DBI::errstr;  # Hate -w ...
 
41
    print STDERR ("Cannot connect: ", $DBI::errstr, "\n",
 
42
        "\tEither your server is not up and running or you have no\n",
 
43
        "\tpermissions for acessing the DSN $test_dsn.\n",
 
44
        "\tThis test requires a running server and write permissions.\n",
 
45
        "\tPlease make sure your server is running and you have\n",
 
46
        "\tpermissions, then retry.\n");
 
47
    exit 10;
 
48
}
 
49
 
 
50
if (!defined(&SQL_VARCHAR)) {
 
51
    eval "sub SQL_VARCHAR { 12 }";
 
52
}
 
53
if (!defined(&SQL_INTEGER)) {
 
54
    eval "sub SQL_INTEGER { 4 }";
 
55
}
 
56
 
 
57
#
 
58
#   Main loop; leave this untouched, put tests after creating
 
59
#   the new table.
 
60
#
 
61
while (Testing()) {
 
62
    #
 
63
    #   Connect to the database
 
64
    Test($state or $dbh = DBI->connect($test_dsn, $test_user, $test_password),
 
65
         'connect')
 
66
        or ServerError();
 
67
 
 
68
    #
 
69
    #   Find a possible new table name
 
70
    #
 
71
    Test($state or $table = FindNewTable($dbh), 'FindNewTable')
 
72
        or DbiError($dbh->err, $dbh->errstr);
 
73
 
 
74
    #
 
75
    #   Create a new table; EDIT THIS!
 
76
    #
 
77
    Test($state or ($def = TableDefinition($table,
 
78
                                           ["id",   "INTEGER",  4, 0],
 
79
                                           ["name", "CHAR",    64, $COL_NULLABLE]) and
 
80
                    $dbh->do($def)), 'create', $def)
 
81
        or DbiError($dbh->err, $dbh->errstr);
 
82
 
 
83
 
 
84
    Test($state or $cursor = $dbh->prepare("INSERT INTO $table"
 
85
                                           . " VALUES (?, ?)"), 'prepare')
 
86
        or DbiError($dbh->err, $dbh->errstr);
 
87
 
 
88
    #
 
89
    #   Insert some rows
 
90
    #
 
91
 
 
92
    # Automatic type detection
 
93
    my $numericVal = 1;
 
94
    my $charVal = "Alligator Descartes";
 
95
    Test($state or $cursor->execute($numericVal, $charVal), 'execute insert 1')
 
96
        or DbiError($dbh->err, $dbh->errstr);
 
97
 
 
98
    # Does the driver remember the automatically detected type?
 
99
    Test($state or $cursor->execute("3", "Jochen Wiedmann"),
 
100
         'execute insert num as string')
 
101
        or DbiError($dbh->err, $dbh->errstr);
 
102
    $numericVal = 2;
 
103
    $charVal = "Tim Bunce";
 
104
    Test($state or $cursor->execute($numericVal, $charVal), 'execute insert 2')
 
105
        or DbiError($dbh->err, $dbh->errstr);
 
106
 
 
107
    # Now try the explicit type settings
 
108
    Test($state or $cursor->bind_param(1, " 4", SQL_INTEGER()), 'bind 1')
 
109
        or DbiError($dbh->err, $dbh->errstr);
 
110
    Test($state or $cursor->bind_param(2, "Andreas K�nig"), 'bind 2')
 
111
        or DbiError($dbh->err, $dbh->errstr);
 
112
    Test($state or $cursor->execute, 'execute binds')
 
113
        or DbiError($dbh->err, $dbh->errstr);
 
114
 
 
115
    # Works undef -> NULL?
 
116
    Test($state or $cursor->bind_param(1, 5, SQL_INTEGER()))
 
117
        or DbiError($dbh->err, $dbh->errstr);
 
118
    Test($state or $cursor->bind_param(2, undef))
 
119
        or DbiError($dbh->err, $dbh->errstr);
 
120
    Test($state or $cursor->execute)
 
121
        or DbiError($dbh->err, $dbh->errstr);
 
122
  
 
123
 
 
124
    Test($state or $cursor -> finish, 'finish');
 
125
 
 
126
    Test($state or undef $cursor  ||  1, 'undef cursor');
 
127
 
 
128
    Test($state or $dbh -> disconnect, 'disconnect');
 
129
 
 
130
    Test($state or undef $dbh  ||  1, 'undef dbh');
 
131
 
 
132
    #
 
133
    #   And now retreive the rows using bind_columns
 
134
    #
 
135
    #
 
136
    #   Connect to the database
 
137
    #
 
138
    Test($state or $dbh = DBI->connect($test_dsn, $test_user, $test_password),
 
139
         'connect for read')
 
140
        or ServerError();
 
141
 
 
142
    Test($state or $cursor = $dbh->prepare("SELECT * FROM $table"
 
143
                                           . " ORDER BY id"))
 
144
           or DbiError($dbh->err, $dbh->errstr);
 
145
 
 
146
    Test($state or $cursor->execute)
 
147
           or DbiError($dbh->err, $dbh->errstr);
 
148
 
 
149
    Test($state or $cursor->bind_columns(undef, \$id, \$name))
 
150
           or DbiError($dbh->err, $dbh->errstr);
 
151
 
 
152
    Test($state or ($ref = $cursor->fetch)  &&  $id == 1  &&
 
153
         $name eq 'Alligator Descartes')
 
154
        or printf("Query returned id = %s, name = %s, ref = %s, %d\n",
 
155
                  $id, $name, $ref, scalar(@$ref));
 
156
 
 
157
    Test($state or (($ref = $cursor->fetch)  &&  $id == 2  &&
 
158
                    $name eq 'Tim Bunce'))
 
159
        or printf("Query returned id = %s, name = %s, ref = %s, %d\n",
 
160
                  $id, $name, $ref, scalar(@$ref));
 
161
 
 
162
    Test($state or (($ref = $cursor->fetch)  &&  $id == 3  &&
 
163
                    $name eq 'Jochen Wiedmann'))
 
164
        or printf("Query returned id = %s, name = %s, ref = %s, %d\n",
 
165
                  $id, $name, $ref, scalar(@$ref));
 
166
 
 
167
    Test($state or (($ref = $cursor->fetch)  &&  $id == 4  &&
 
168
                    $name eq 'Andreas K�nig'))
 
169
        or printf("Query returned id = %s, name = %s, ref = %s, %d\n",
 
170
                  $id, $name, $ref, scalar(@$ref));
 
171
 
 
172
    Test($state or (($ref = $cursor->fetch)  &&  $id == 5  &&
 
173
                    !defined($name)))
 
174
        or printf("Query returned id = %s, name = %s, ref = %s, %d\n",
 
175
                  $id, $name, $ref, scalar(@$ref));
 
176
 
 
177
    Test($state or undef $cursor  or  1);
 
178
 
 
179
    #
 
180
    #   Finally drop the test table.
 
181
    #
 
182
    Test($state or $dbh->do("DROP TABLE $table"))
 
183
           or DbiError($dbh->err, $dbh->errstr);
 
184
}