File: t65updcur.t

package info (click to toggle)
libdbd-informix-perl 2003.04-3
  • links: PTS
  • area: contrib
  • in suites: etch, etch-m68k, sarge
  • size: 1,232 kB
  • ctags: 467
  • sloc: perl: 7,349; ansic: 5,340; sh: 184; makefile: 58
file content (129 lines) | stat: -rwxr-xr-x 3,273 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
#!/usr/bin/perl -w
#
#   @(#)$Id: t65updcur.t,v 2003.3 2003/01/04 00:36:38 jleffler Exp $
#
#   Test $sth->{CursorName} and cursors FOR UPDATE for DBD::Informix
#
#   Copyright 1997,1999 Jonathan Leffler
#   Copyright 2000      Informix Software Inc
#   Copyright 2002-03   IBM

use DBD::Informix::TestHarness;
use strict;

# Test install...
my $dbh = &connect_to_test_database();

&stmt_note("1..16\n");
&stmt_ok();

my $table = "DBD_IX_TestTable";
my $select = "SELECT * FROM $table";

stmt_test $dbh, qq{
CREATE TEMP TABLE $table
(
	Col01	SERIAL NOT NULL PRIMARY KEY,
	Col02	CHAR(30) NOT NULL,
	Col03	DATE NOT NULL,
	Col04	DATETIME YEAR TO FRACTION(5) NOT NULL
)
};

my $date = &date_as_string($dbh, 12, 8, 1940);
my $time = '1940-12-08 06:45:32.54321';
my $raw1 = 'Mornington Crescent';
my $tag1 = $dbh->quote($raw1);
my $raw2 = "King's Cross / St Pancras";
my $tag2 = $dbh->quote($raw2);
my $raw3 = "ABC $raw1";
my $insert01 = qq{INSERT INTO $table VALUES(0, $tag1, '$date', '$time')};

# Insert two rows of data
stmt_test $dbh, $insert01;
$insert01 =~ s/$tag1/$tag2/;
stmt_test $dbh, $insert01;

my $sel = $dbh->prepare($select) or &stmt_fail;
&stmt_ok;

my $row1 = { 'col01' => 1, 'col02' => $raw1, 'col03' => $date, 'col04' => $time };
my $row2 = { 'col01' => 2, 'col02' => $raw2, 'col03' => $date, 'col04' => $time };
my $row3 = { 'col01' => 1, 'col02' => $raw3, 'col03' => $date, 'col04' => $time };
my $res1 = { 1 => $row1, 2 => $row2 };
my $res2 = { 1 => $row3 };

# Check that there is some data
$sel->execute ? validate_unordered_unique_data($sel, 'col01', $res1) : &stmt_nok;

my $selupd = $select . " FOR UPDATE";
my $st1 = $dbh->prepare($selupd) or &stmt_fail;
&stmt_ok;

# Check that attribute caching is still working!
my $name = $st1->{CursorName};
my $i;
for ($i = 0; $i < 3; $i++)
{
	my $x = ($name eq $st1->{CursorName}) ? "OK" : "** BROKEN **";
	print "# Cursor name $i: $st1->{CursorName} $x\n";
}

$name = $st1->{CursorName};
my $updstmt = "UPDATE $table SET Col02 = ? WHERE CURRENT OF $name";
print "# $updstmt\n";
my $st2 = $dbh->prepare($updstmt) or &stmt_fail;
&stmt_ok();

my $delstmt = "DELETE FROM $table WHERE CURRENT OF $name";
print "# $delstmt\n";
my $st3 = $dbh->prepare($delstmt) or &stmt_fail;
&stmt_ok();

# In a logged database, must be in a transaction
# Given new AutoCommit behaviour, must set AutoCommit Off.
$dbh->{AutoCommit} = 0
	unless (!$dbh->{ix_LoggedDatabase});

my $n = 0;
&stmt_fail() unless ($st1->execute());
&stmt_ok();

# Fetch first row
my $data;
&stmt_fail() unless ($data = $st1->fetch);
&stmt_ok();
$n++;
my @row;@row = @{$data};
for ($i = 0; $i <= $#row; $i++)
{
	print "Row $n: Field $i: <<$row[$i]>>\n";
}

# Update current row
$row[1] = "ABC " . $row[1];
&stmt_fail() unless ($st2->execute($row[1]));

# Fetch second row
&stmt_fail() unless ($data = $st1->fetch);
&stmt_ok();
$n++;
@row = @{$data};
for ($i = 0; $i <= $#row; $i++)
{
	print "Row $n: Field $i: <<$row[$i]>>\n";
}

# Delete it
&stmt_fail() unless ($st3->execute);
&stmt_ok;

# In a logged database, must be in a transaction
$dbh->commit unless (!$dbh->{ix_LoggedDatabase});

# Check that there is some data
$sel->execute ? validate_unordered_unique_data($sel, 'col01', $res2) : &stmt_nok;

$dbh->disconnect ? stmt_ok : stmt_nok;

&all_ok();