comparison DBD/t/12bind.t @ 0:cedccb7e0143

Copy of clients/perl directory without Tests from MonetDB changeset 4d2d4532228a.
author Sjoerd Mullender <sjoerd@acm.org>
date Mon, 19 Sep 2016 15:15:52 +0200 (2016-09-19)
parents
children a0ec9e080a5b
comparison
equal deleted inserted replaced
-1:000000000000 0:cedccb7e0143
1 #!perl -I./t
2
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 #
7 # Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.
8
9 $| = 1;
10
11 use strict;
12 use warnings;
13 use DBI();
14 use DBD_TEST();
15
16 use Test::More;
17
18 if (defined $ENV{DBI_DSN}) {
19 plan tests => 6;
20 } else {
21 plan skip_all => 'Cannot test without DB info';
22 }
23
24 my $dbh = DBI->connect or die "Connect failed: $DBI::errstr\n";
25 ok ( defined $dbh, 'Connection');
26
27 ok( DBD_TEST::tab_create( $dbh ),"Create the test table $DBD_TEST::table_name");
28
29 my $data =
30 [
31 [ 1,'foo' ,'me' x 120 ,'1998-05-13','1988-05-13 01:12:33']
32 , [ 2,'bar' ,'bar varchar' ,'1998-05-14','1998-05-14 01:25:33']
33 , [ 3,'bletch','bletch varchar','1998-05-15','1998-05-15 01:15:33']
34 , [ 4,'bletch','me' x 14 ,'1998-05-15','1998-05-15 01:15:33']
35 ];
36
37 ok( tab_insert( $dbh, $data ),'Insert test data');
38
39 ok( tab_select( $dbh ),'Select test data');
40
41 ok( DBD_TEST::tab_delete( $dbh ),'Drop test table');
42
43 ok( $dbh->disconnect,'Disconnect');
44
45
46 sub tab_select
47 {
48 my $dbh = shift;
49
50 my $sth = $dbh->prepare("SELECT A,B,C,D FROM $DBD_TEST::table_name WHERE a = ?")
51 or return undef;
52 my $ti = DBD_TEST::get_type_for_column( $dbh,'A');
53 for my $v ( 1, 3, 2, 4, 10 ) {
54 $sth->bind_param( 1, $v, { TYPE => $ti->{DATA_TYPE} } );
55 $sth->execute;
56 while ( my $row = $sth->fetch ) {
57 print "# -- $row->[0] length:", length $row->[1]," $row->[1] $row->[2] $row->[3]\n";
58 if ( $row->[0] != $v ) {
59 print "# Bind value failed! bind value = $v, returned value = $row->[0]\n";
60 return undef;
61 }
62 }
63 }
64 return 1;
65 }
66
67 sub tab_insert
68 {
69 my $dbh = shift;
70 my $data = shift;
71
72 my $sth = $dbh->prepare("INSERT INTO $DBD_TEST::table_name (A, B, C, D) VALUES (?, ?, ?, ?)");
73 unless ( $sth ) {
74 print $DBI::errstr;
75 return 0;
76 }
77 $sth->{PrintError} = 1;
78 for ( @$data ) {
79 my $ti;
80
81 $ti = DBD_TEST::get_type_for_column( $dbh,'A');
82 $sth->bind_param( 1, $_->[ 0], { TYPE => $ti->{DATA_TYPE} } );
83
84 $ti = DBD_TEST::get_type_for_column( $dbh,'B');
85 # $_->[1] = $_->[1] x (int( int( $ti->{COLUMN_SIZE} / 2 ) / length( $_->[1] ) ) ); # XXX
86 $sth->bind_param( 2, $_->[ 1], { TYPE => $ti->{DATA_TYPE} } );
87
88 $ti = DBD_TEST::get_type_for_column( $dbh,'C');
89 $sth->bind_param( 3, $_->[ 2], { TYPE => $ti->{DATA_TYPE} } );
90
91 $ti = DBD_TEST::get_type_for_column( $dbh,'D');
92 my $i = ( $ti->{DATA_TYPE} == DBI::SQL_TYPE_DATE || $ti->{DATA_TYPE} == DBI::SQL_DATE ) ? 3 : 4;
93 $sth->bind_param( 4, $_->[$i], { TYPE => $ti->{DATA_TYPE} } );
94
95 return 0 unless $sth->execute;
96 }
97 1;
98 }