Skip to content

Commit dddfba3

Browse files
committed
Use .dispose when needed
1 parent f174fa6 commit dddfba3

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

lib/DBIish/CommonTesting.pm6

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ method run-tests {
136136

137137
is $rc, 1, "execute one with one integer parameter should return 1 row affected";
138138
is $sth.rows, 1, '$sth.rows for execute one with one integer parameter should report 1 row affected';
139+
$sth.dispose;
139140

140141
ok $sth = $dbh.prepare( "
141142
INSERT INTO nom (price)
@@ -144,6 +145,7 @@ method run-tests {
144145
ok $rc = $sth.execute(4.85), "execute one with one float parameter";
145146
is $rc, 1, "execute one with one float parameter should return 1 row affected";
146147
is $sth.rows, 1, '$sth.rows for execute one with one float parameter should report 1 row affected';
148+
$sth.dispose;
147149

148150
ok $sth = $dbh.prepare( "
149151
INSERT INTO nom (name, description, quantity, price)
@@ -337,30 +339,37 @@ method run-tests {
337339
}
338340
else { skip 'fetchrow_arrayref not implemented', 2 }
339341

340-
$sth.finish;
342+
$sth.dispose;
341343

342344
# test quotes and so on
343345
{
344346
$sth = $dbh.prepare(q[INSERT INTO nom (name, description) VALUES (?, ?)]);
345347
my $lived;
346-
lives-ok { $sth.execute("quot", q["';]); $lived = 1 }, 'can insert single and double quotes';
348+
lives-ok {
349+
$sth.execute("quot", q["';]); $lived = 1
350+
}, 'can insert single and double quotes';
351+
$sth.dispose;
347352
if $lived {
348-
$sth = $dbh.prepare(q[SELECT description FROM nom where name = ?]);
349-
lives-ok { $sth.execute('quot') }, 'lived while retrieving result';
353+
$sth = $dbh.prepare(q[SELECT description FROM nom WHERE name = ?]);
354+
lives-ok {
355+
$sth.execute('quot');
356+
}, 'lived while retrieving result';
350357
is $sth.fetchrow.join, q["';], 'got the right string back';
351-
$sth.finish;
358+
$sth.dispose;
352359
}
353360
else {
354361
skip('dependent tests', 2);
355362
}
356363

357364
$lived = 0;
358-
lives-ok { $dbh.do(q[INSERT INTO nom (name, description) VALUES(?, '?"')], 'mark'); $lived = 1}, 'can use question mark in quoted strings';
365+
lives-ok {
366+
$dbh.do(q[INSERT INTO nom (name, description) VALUES(?, '?"')], 'mark'); $lived = 1
367+
}, 'can use question mark in quoted strings';
359368
if $lived {
360369
my $sth = $dbh.prepare(q[SELECT description FROM nom WHERE name = 'mark']);
361370
$sth.execute;
362371
is $sth.fetchrow.join, '?"', 'correctly retrieved question mark';
363-
$sth.finish;
372+
$sth.dispose;
364373
}
365374
else {
366375
skip('dependent test', 1);
@@ -375,6 +384,7 @@ method run-tests {
375384
my $row = $sth.fetchrow-hash;
376385

377386
ok !?$row, 'a query with no results should have a falsy value';
387+
$sth.dispose;
378388
}
379389

380390
# test that a query that's exhausted its result set has a falsy value
@@ -386,6 +396,7 @@ method run-tests {
386396
$row = $sth.fetchrow-hash;
387397

388398
ok !?$row, 'a query with no more results should have a falsy value';
399+
$sth.dispose;
389400
}
390401

391402
# test that an integer >= 2**31 still works as an argument to execute
@@ -402,7 +413,7 @@ method run-tests {
402413
is $row[1], 'many', 'The contents of the row fetched via a large integer are correct';
403414
is $row[2], $large-int, 'The contents of the row fetched via a large integer are correct';
404415

405-
$sth.finish;
416+
$sth.dispose;
406417
}
407418

408419
# Drop the table when finished, and disconnect

t/26-mysql-blob.t

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ v6;
22
use Test;
33
use DBIish;
44

5-
plan 18;
5+
plan 19;
66

77
my %con-parms = :database<dbdishtest>, :user<testuser>, :password<testpass>;
88
my $dbh;
@@ -31,20 +31,31 @@ lives-ok {
3131
}, 'Table created';
3232
my $blob = Buf.new(^256);
3333
my $query = 'INSERT INTO test VALUES(?, ?)';
34-
ok (my $sth = $dbh.prepare($query)), "Prepared '$query'";
35-
ok $sth.execute(1, $blob), 'Executed with buf';
36-
ok $sth.execute(2, Buf), 'Executed without buf';
37-
ok $sth = $dbh.prepare('SELECT name FROM test WHERE id = ?'), 'SELECT prepared';
38-
ok $sth.execute(1), 'Executed for 1';
39-
ok (my @res = $sth.row), 'Get a row';
40-
is @res.elems, 1, 'One field';
41-
ok (my $data = @res[0]), 'With data at 0';
42-
ok $data ~~ Buf, 'Data is-a Buf';
43-
is $data, $blob, 'Data in Buf match with original';
44-
ok $sth.execute(2), 'Executed for 2';
45-
ok (@res = $sth.row), 'Get a row';
46-
is @res.elems, 1, 'One field';
47-
$data = @res[0];
48-
ok $data ~~ Buf, 'Data is-a Buf';
49-
ok not $data.defined, 'But is NULL';
50-
$dbh.do('DROP TABLE IF EXISTS test');
34+
35+
with $dbh.prepare($query) {
36+
LEAVE { .dispose }
37+
ok $_, "Prepared '$query'";
38+
ok .execute(1, $blob), 'Executed with buf';
39+
ok .execute(2, Buf), 'Executed without buf';
40+
} else { .fail }
41+
42+
with $dbh.prepare('SELECT name FROM test WHERE id = ?') {
43+
LEAVE { .dispose }
44+
ok $_, 'SELECT prepared';
45+
ok .execute(1), 'Executed for 1';
46+
ok (my @res = .row), 'Get a row';
47+
is @res.elems, 1, 'One field';
48+
ok (my $data = @res[0]), 'With data at 0';
49+
ok $data ~~ Buf, 'Data is-a Buf';
50+
is $data, $blob, 'Data in Buf match with original';
51+
ok .execute(2), 'Executed for 2';
52+
ok (@res = .row), 'Get a row';
53+
is @res.elems, 1, 'One field';
54+
$data = @res[0];
55+
ok $data ~~ Buf, 'Data is-a Buf';
56+
ok not $data.defined, 'But is NULL';
57+
} else { .fail }
58+
59+
lives-ok {
60+
$dbh.do('DROP TABLE IF EXISTS test');
61+
}, 'All clean';

0 commit comments

Comments
 (0)