Skip to content

Commit 1785c1e

Browse files
committed
Add tests to verify $sth->{Active} and statement attributes
1 parent 4f47a06 commit 1785c1e

File tree

3 files changed

+208
-23
lines changed

3 files changed

+208
-23
lines changed

t/40sth_attr.t

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,125 @@ use warnings;
33

44
use Test::More;
55
use DBI;
6+
use DBD::MariaDB;
67

78
use vars qw($test_dsn $test_user $test_password);
89
use lib 't', '.';
910
require "lib.pl";
1011

1112
my $dbh = DbiTestConnect($test_dsn, $test_user, $test_password, { PrintError => 0, RaiseError => 1 });
1213

13-
plan tests => 11;
14+
plan tests => 90;
1415

1516
ok($dbh->do("CREATE TEMPORARY TABLE t(id INT)"));
1617
ok($dbh->do("INSERT INTO t(id) VALUES(1)"));
1718

18-
my $sth = $dbh->prepare("SELECT * FROM t");
19-
ok($sth->execute());
20-
ok($sth->fetchall_arrayref());
21-
is_deeply($sth->{NAME}, ["id"]);
22-
ok($sth->finish());
19+
my $sth1 = $dbh->prepare("SELECT * FROM t");
20+
ok(!$sth1->{Active});
21+
ok($sth1->execute());
22+
ok($sth1->{Active});
23+
is_deeply($sth1->{NAME}, ["id"]);
24+
is_deeply($sth1->fetchall_arrayref(), [ [ 1 ] ]);
25+
ok(!$sth1->{Active});
26+
is_deeply($sth1->{mariadb_type}, [ DBD::MariaDB::TYPE_LONG ]);
2327

2428
my $sth2 = $dbh->prepare("SELECT * FROM t", { mariadb_server_prepare => 1 });
29+
ok(!$sth2->{Active});
2530
ok($sth2->execute());
26-
ok($sth2->fetchall_arrayref());
31+
ok($sth2->{Active});
2732
is_deeply($sth2->{NAME}, ["id"]);
28-
ok($sth2->finish());
33+
is_deeply($sth2->fetchall_arrayref(), [ [ 1 ] ]);
34+
ok(!$sth2->{Active});
35+
is_deeply($sth2->{mariadb_type}, [ DBD::MariaDB::TYPE_LONG ]);
36+
37+
ok($dbh->do("INSERT INTO t(id) VALUES(2)"));
38+
39+
my $sth3 = $dbh->prepare("SELECT * FROM t");
40+
ok(!$sth3->{Active});
41+
ok($sth3->execute());
42+
ok($sth3->{Active});
43+
is_deeply($sth3->{NAME}, ["id"]);
44+
is_deeply($sth3->fetchrow_arrayref(), [ 1 ]);
45+
ok($sth3->{Active});
46+
is_deeply($sth3->{NAME}, ["id"]);
47+
is_deeply($sth3->fetchrow_arrayref(), [ 2 ]);
48+
ok(!$sth3->{Active});
49+
is_deeply($sth3->{NAME}, ["id"]);
50+
is_deeply($sth3->{mariadb_type}, [ DBD::MariaDB::TYPE_LONG ]);
51+
52+
my $sth4 = $dbh->prepare("SELECT * FROM t", { mariadb_server_prepare => 1 });
53+
ok(!$sth4->{Active});
54+
ok($sth4->execute());
55+
ok($sth4->{Active});
56+
is_deeply($sth4->{NAME}, ["id"]);
57+
is_deeply($sth4->fetchrow_arrayref(), [ 1 ]);
58+
ok($sth4->{Active});
59+
is_deeply($sth4->{NAME}, ["id"]);
60+
is_deeply($sth4->fetchrow_arrayref(), [ 2 ]);
61+
ok(!$sth4->{Active});
62+
is_deeply($sth4->{NAME}, ["id"]);
63+
is_deeply($sth4->{mariadb_type}, [ DBD::MariaDB::TYPE_LONG ]);
64+
65+
my $sth5 = $dbh->prepare("SELECT * FROM t");
66+
ok(!$sth5->{Active});
67+
ok($sth5->execute());
68+
ok($sth5->{Active});
69+
is_deeply($sth5->{NAME}, ["id"]);
70+
is_deeply($sth5->fetchrow_arrayref(), [ 1 ]);
71+
ok($sth5->{Active});
72+
is_deeply($sth5->{NAME}, ["id"]);
73+
ok($sth5->finish);
74+
ok(!$sth5->{Active});
75+
is_deeply($sth5->{NAME}, ["id"]);
76+
is_deeply($sth5->{mariadb_type}, [ DBD::MariaDB::TYPE_LONG ]);
77+
78+
my $sth6 = $dbh->prepare("SELECT * FROM t", { mariadb_server_prepare => 1 });
79+
ok(!$sth6->{Active});
80+
ok($sth6->execute());
81+
ok($sth6->{Active});
82+
is_deeply($sth6->{NAME}, ["id"]);
83+
is_deeply($sth6->fetchrow_arrayref(), [ 1 ]);
84+
ok($sth6->{Active});
85+
is_deeply($sth6->{NAME}, ["id"]);
86+
ok($sth6->finish);
87+
ok(!$sth6->{Active});
88+
is_deeply($sth6->{NAME}, ["id"]);
89+
is_deeply($sth6->{mariadb_type}, [ DBD::MariaDB::TYPE_LONG ]);
90+
91+
my $sth7 = $dbh->prepare("SELECT NULL as my_name LIMIT 0");
92+
ok(!$sth7->{Active});
93+
ok($sth7->execute());
94+
ok(!$sth7->{Active});
95+
is_deeply($sth7->{NAME}, ["my_name"]);
96+
ok(!$sth7->fetchrow_arrayref());
97+
ok(!$sth7->{Active});
98+
is_deeply($sth7->{mariadb_type}, [ DBD::MariaDB::TYPE_NULL ]);
99+
100+
my $sth8 = $dbh->prepare("SELECT NULL as my_name LIMIT 0");
101+
ok(!$sth8->{Active});
102+
ok($sth8->execute());
103+
ok(!$sth8->{Active});
104+
is_deeply($sth8->{NAME}, ["my_name"]);
105+
ok(!$sth8->fetchrow_arrayref());
106+
ok(!$sth8->{Active});
107+
is_deeply($sth8->{mariadb_type}, [ DBD::MariaDB::TYPE_NULL ]);
108+
109+
my $sth9 = $dbh->prepare("SELECT NULL as my_name LIMIT 0");
110+
ok(!$sth9->{Active});
111+
ok($sth9->execute());
112+
ok(!$sth9->{Active});
113+
is_deeply($sth9->{NAME}, ["my_name"]);
114+
is_deeply($sth9->fetchall_arrayref(), []);
115+
ok(!$sth9->{Active});
116+
is_deeply($sth9->{mariadb_type}, [ DBD::MariaDB::TYPE_NULL ]);
117+
118+
my $sth10 = $dbh->prepare("SELECT NULL as my_name LIMIT 0");
119+
ok(!$sth10->{Active});
120+
ok($sth10->execute());
121+
ok(!$sth10->{Active});
122+
is_deeply($sth10->{NAME}, ["my_name"]);
123+
is_deeply($sth10->fetchall_arrayref(), []);
124+
ok(!$sth10->{Active});
125+
is_deeply($sth10->{mariadb_type}, [ DBD::MariaDB::TYPE_NULL ]);
29126

30127
ok($dbh->disconnect());

t/87async.t

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ my $dbh = DbiTestConnect($test_dsn, $test_user, $test_password,
1616
if ($dbh->{mariadb_serverversion} < 50012) {
1717
plan skip_all => "Servers < 5.0.12 do not support SLEEP()";
1818
}
19-
plan tests => 96;
19+
plan tests => 137;
2020

2121
is $dbh->get_info($GetInfoType{'SQL_ASYNC_MODE'}), 2; # statement-level async
2222
is $dbh->get_info($GetInfoType{'SQL_MAX_ASYNC_CONCURRENT_STATEMENTS'}), 1;
@@ -93,23 +93,31 @@ is $b, 1;
9393
is $c, 2;
9494

9595
$sth = $dbh->prepare('SELECT SLEEP(2)');
96+
ok !$sth->{Active};
9697
ok !defined($sth->mariadb_async_ready);
9798
$start = Time::HiRes::gettimeofday();
9899
ok $sth->execute;
99100
$end = Time::HiRes::gettimeofday();
100101
cmp_ok(($end - $start), '>=', 1.9);
102+
ok $sth->{Active};
103+
ok $sth->finish;
104+
ok !$sth->{Active};
101105

102106
$sth = $dbh->prepare('SELECT SLEEP(2)', { mariadb_async => 1 });
107+
ok !$sth->{Active};
103108
ok !defined($sth->mariadb_async_ready);
104109
$start = Time::HiRes::gettimeofday();
105110
ok $sth->execute;
106111
ok defined($sth->mariadb_async_ready);
107112
$end = Time::HiRes::gettimeofday();
108113
cmp_ok(($end - $start), '<', 2);
114+
ok $sth->{Active};
109115

110116
sleep 1 until $sth->mariadb_async_ready;
111117

118+
ok $sth->{Active};
112119
my $row = $sth->fetch;
120+
ok !$sth->{Active};
113121
$end = Time::HiRes::gettimeofday();
114122
ok $row;
115123
is $row->[0], 0;
@@ -126,16 +134,19 @@ ok $dbh->errstr;
126134
$dbh->do('DELETE FROM async_test');
127135

128136
$sth = $dbh->prepare('INSERT INTO async_test VALUES(SLEEP(2), ?, ?)', { mariadb_async => 1 });
137+
ok !$sth->{Active};
129138
$start = Time::HiRes::gettimeofday();
130139
$rows = $sth->execute(1, 2);
131140
$end = Time::HiRes::gettimeofday();
132141
cmp_ok(($end - $start), '<', 2);
142+
ok $sth->{Active};
133143
ok $rows;
134144
is $rows, '0E0';
135145

136146
$rows = $sth->mariadb_async_result;
137147
$end = Time::HiRes::gettimeofday();
138148
cmp_ok(($end - $start), '>=', 1.9);
149+
ok !$sth->{Active};
139150
is $rows, 1;
140151

141152
( $a, $b, $c ) = $dbh->selectrow_array('SELECT * FROM async_test');
@@ -164,10 +175,13 @@ ok $rows;
164175
is $rows, '0E0';
165176

166177
$sth = $dbh->prepare('UPDATE async_test SET value0 = 0 WHERE value0 = 999', { mariadb_async => 1 });
178+
ok !$sth->{Active};
167179
$rows = $sth->execute;
180+
ok $sth->{Active};
168181
ok $rows;
169182
is $rows, '0E0';
170183
$rows = $sth->mariadb_async_result;
184+
ok !$sth->{Active};
171185
ok $rows;
172186
is $rows, '0E0';
173187

@@ -179,7 +193,9 @@ $rows = $dbh->do('INSERT INTO async_test VALUES(1, 2, 3)');
179193
is $rows, 1;
180194

181195
$sth = $dbh->prepare('SELECT 1, value0, value1, value2 FROM async_test WHERE value0 = ?', { mariadb_async => 1 });
196+
ok !$sth->{Active};
182197
$sth->execute(1);
198+
ok $sth->{Active};
183199
is $sth->{'NUM_OF_FIELDS'}, undef;
184200
is $sth->{'NUM_OF_PARAMS'}, 1;
185201
is $sth->{'NAME'}, undef;
@@ -195,6 +211,7 @@ is $sth->{'NULLABLE'}, undef;
195211
is $sth->{'Database'}, $dbh;
196212
is $sth->{'Statement'}, 'SELECT 1, value0, value1, value2 FROM async_test WHERE value0 = ?';
197213
$sth->mariadb_async_result;
214+
ok $sth->{Active};
198215
is $sth->{'NUM_OF_FIELDS'}, 4;
199216
is $sth->{'NUM_OF_PARAMS'}, 1;
200217
cmp_bag $sth->{'NAME'}, [qw/1 value0 value1 value2/];
@@ -210,31 +227,60 @@ is ref($sth->{'NULLABLE'}), 'ARRAY';
210227
is $sth->{'Database'}, $dbh;
211228
is $sth->{'Statement'}, 'SELECT 1, value0, value1, value2 FROM async_test WHERE value0 = ?';
212229
$sth->finish;
230+
ok !$sth->{Active};
213231

214232
$sth->execute(1);
233+
ok $sth->{Active};
215234
$row = $sth->fetch;
235+
ok !$sth->{Active};
236+
is_deeply $row, [1, 1, 2, 3];
237+
is $sth->rows, 1;
238+
239+
$sth->execute(1);
240+
ok $sth->{Active};
241+
$sth->mariadb_async_result;
242+
ok $sth->{Active};
243+
$row = $sth->fetch;
244+
ok !$sth->{Active};
216245
is_deeply $row, [1, 1, 2, 3];
217246
is $sth->rows, 1;
218-
$sth->finish;
219247

220248
$sth->execute(1);
249+
ok $sth->{Active};
221250
$row = $sth->fetchrow_arrayref;
251+
ok !$sth->{Active};
222252
is_deeply $row, [1, 1, 2, 3];
223253
is $sth->rows, 1;
224-
$sth->finish;
225254

226255
$sth->execute(1);
256+
ok $sth->{Active};
227257
my @row = $sth->fetchrow_array;
258+
ok !$sth->{Active};
228259
is_deeply \@row, [1, 1, 2, 3];
229260
is $sth->rows, 1;
230-
$sth->finish;
231261

232262
$sth->execute(1);
263+
ok $sth->{Active};
233264
$row = $sth->fetchrow_hashref;
265+
ok !$sth->{Active};
234266
cmp_bag [ keys %$row ], [qw/1 value0 value1 value2/];
235267
cmp_bag [ values %$row ], [1, 1, 2, 3];
236268
is $sth->rows, 1;
237-
$sth->finish;
238269

239-
undef $sth;
270+
$sth = $dbh->prepare('UPDATE async_test SET value0 = 2 WHERE value0 = 1', { mariadb_async => 1 });
271+
ok !$sth->{Active};
272+
ok $sth->execute();
273+
ok $sth->{Active};
274+
ok $sth->mariadb_async_result;
275+
ok !$sth->{Active};
276+
277+
$sth = $dbh->prepare('SYNTAX ERROR', { mariadb_async => 1 });
278+
ok !$sth->{Active};
279+
ok $sth->execute();
280+
ok $sth->{Active};
281+
ok !$sth->mariadb_async_result;
282+
ok !$sth->{Active};
283+
284+
local $SIG{__WARN__} = sub { die @_ };
285+
240286
ok $dbh->disconnect;

0 commit comments

Comments
 (0)