Skip to content

Commit f5eb73a

Browse files
committed
Merge pull request #130
Fix return value of $sth->rows when error occurred
2 parents 750cdb3 + f4e3a3f commit f5eb73a

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

MariaDB.xs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ rows(sth)
236236
XSRETURN_UNDEF;
237237
}
238238
}
239+
if (imp_sth->row_num == (my_ulonglong)-1)
240+
XSRETURN_IV(-1);
239241
RETVAL = my_ulonglong2sv(imp_sth->row_num);
240242
OUTPUT:
241243
RETVAL

dbdimp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3777,7 +3777,7 @@ mariadb_st_prepare_sv(
37773777
imp_sth->done_desc = FALSE;
37783778
imp_sth->result = NULL;
37793779
imp_sth->currow = 0;
3780-
imp_sth->row_num = 0;
3780+
imp_sth->row_num = (my_ulonglong)-1;
37813781

37823782
if (DBIc_TRACE_LEVEL(imp_xxh) >= 2)
37833783
PerlIO_printf(DBIc_LOGPIO(imp_xxh),
@@ -4139,7 +4139,7 @@ bool mariadb_st_more_results(SV* sth, imp_sth_t* imp_sth)
41394139

41404140
imp_sth->done_desc = FALSE;
41414141
imp_sth->currow = 0;
4142-
imp_sth->row_num = 0;
4142+
imp_sth->row_num = (my_ulonglong)-1;
41434143

41444144
/* clear NUM_OF_FIELDS attribute */
41454145
DBIc_DBISTATE(imp_sth)->set_attr_k(sth, sv_2mortal(newSVpvs("NUM_OF_FIELDS")), 0, sv_2mortal(newSViv(0)));

t/35prepare.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ for (my $i = 0 ; $i < 10; $i++)
6464
# save these values for later testing
6565
$testInsertVals->{$i}= $random_chars;
6666
ok($rows= $sth->execute($i, $random_chars), "Testing insert row");
67-
ok($rows= 1, "Should have inserted one row");
67+
is($rows, 1, "Should have inserted one row");
6868
}
6969

7070
ok($sth= $dbh->prepare("SELECT * FROM dbd_mysql_t35prepare WHERE id = ? OR id = ?"),

t/40numrows.t

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require 'lib.pl';
1010
my ($dbh, $sth, $aref);
1111
$dbh = DbiTestConnect($test_dsn, $test_user, $test_password,
1212
{ RaiseError => 1, PrintError => 0, AutoCommit => 0 });
13-
plan tests => 2*29 + 1;
13+
plan tests => 2*36 + 1;
1414

1515
for my $mariadb_server_prepare (0, 1) {
1616

@@ -29,6 +29,8 @@ ok $dbh->do("INSERT INTO dbd_mysql_t40numrows VALUES( 1, 'Alligator Descartes' )
2929

3030
ok ($sth = $dbh->prepare("SELECT * FROM dbd_mysql_t40numrows WHERE id = 1"));
3131

32+
is $sth->rows, -1, '$sth->rows prior execute is unknown (-1)';
33+
3234
ok $sth->execute;
3335

3436
is $sth->rows, 1, '$sth->rows should be 1';
@@ -43,6 +45,8 @@ ok $dbh->do("INSERT INTO dbd_mysql_t40numrows VALUES( 2, 'Jochen Wiedmann' )"),
4345

4446
ok ($sth = $dbh->prepare("SELECT * FROM dbd_mysql_t40numrows WHERE id >= 1"));
4547

48+
is $sth->rows, -1, '$sth->rows prior execute is unknown (-1)';
49+
4650
ok $sth->execute;
4751

4852
is $sth->rows, 2, '$sth->rows should be 2';
@@ -57,6 +61,8 @@ ok $dbh->do("INSERT INTO dbd_mysql_t40numrows VALUES(3, 'Tim Bunce')"), "inserti
5761

5862
ok ($sth = $dbh->prepare("SELECT * FROM dbd_mysql_t40numrows WHERE id >= 2"));
5963

64+
is $sth->rows, -1, '$sth->rows prior execute is unknown (-1)';
65+
6066
ok $sth->execute;
6167

6268
is $sth->rows, 2, 'rows should be 2';
@@ -69,6 +75,8 @@ is $sth->rows, 2, 'rows still should be 2';
6975

7076
ok ($sth = $dbh->prepare("SELECT * FROM dbd_mysql_t40numrows"));
7177

78+
is $sth->rows, -1, '$sth->rows prior execute is unknown (-1)';
79+
7280
ok $sth->execute;
7381

7482
is $sth->rows, 3, 'rows should be 3';
@@ -79,6 +87,15 @@ is scalar @$aref, 3, 'Verified rows should be 3';
7987

8088
is $sth->rows, 3, 'rows still should be 3';
8189

90+
my $sth = eval { $dbh->prepare("SYNTAX ERROR") };
91+
if ($sth) {
92+
is $sth->rows, -1, '$sth->rows prior execute is unknown (-1)';
93+
ok !eval { $sth->execute() }, '$sth->execute for SYNTAX ERROR failed';
94+
is $sth->rows, -1, '$sth->rows for SYNTAX ERROR is unknown (-1)';
95+
} else {
96+
pass '$dbh->prepare for SYNTAX ERROR failed' for 1..3;
97+
}
98+
8299
ok $dbh->do("DROP TEMPORARY TABLE dbd_mysql_t40numrows"), "drop table dbd_mysql_t40numrows";
83100

84101
}

t/76multi_statement.t

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ my $dbh = DbiTestConnect($test_dsn, $test_user, $test_password,
1414
{ RaiseError => 1, PrintError => 0, AutoCommit => 0,
1515
mariadb_multi_statements => 1 });
1616

17-
plan tests => 71;
17+
plan tests => 77;
1818

1919
ok (defined $dbh, "Connected to database with multi statement support");
2020

@@ -34,6 +34,7 @@ $dbh->{mariadb_server_prepare}= 0;
3434
# Check that more_results works for non-SELECT results too
3535
my $sth;
3636
ok($sth = $dbh->prepare("UPDATE dbd_mysql_t76multi SET a=5 WHERE a=1; UPDATE dbd_mysql_t76multi SET a='6suffix' WHERE a<4"));
37+
is($sth->rows, -1);
3738
ok($sth->execute(), "Execute updates");
3839
is($sth->rows, 1, "First update affected 1 row");
3940
is($sth->{mariadb_warning_count}, 0, "First update had no warnings");
@@ -98,25 +99,30 @@ $dbh->{mariadb_server_prepare}= 0;
9899
# Check that statement attributes are correct after calling more_results
99100
ok($dbh->do("CREATE TEMPORARY TABLE dbd_mysql_t76multi3 (name_id1 INT, name_id2 INT)"));
100101
ok($sth = $dbh->prepare("SELECT name_id1 FROM dbd_mysql_t76multi3; INSERT INTO dbd_mysql_t76multi2 VALUES(11); SELECT name_id1, name_id2 FROM dbd_mysql_t76multi3; SYNTAX ERROR"));
102+
is($sth->rows, -1);
101103
ok($sth->execute());
102104
is($sth->{NUM_OF_FIELDS}, 1);
103105
is($sth->{NUM_OF_PARAMS}, 0);
106+
is($sth->rows, 0);
104107
is_deeply($sth->{NAME}, [ 'name_id1' ]);
105108
is_deeply($sth->{mariadb_type}, [ DBD::MariaDB::TYPE_LONG ]);
106109
ok($sth->more_results());
107110
is($sth->{NUM_OF_FIELDS}, 0);
108111
is($sth->{NUM_OF_PARAMS}, 0);
112+
is($sth->rows, 1);
109113
ok(!eval { $sth->{NAME} });
110114
ok(!eval { $sth->{mariadb_type} });
111115
ok($sth->more_results());
112116
is($sth->{NUM_OF_FIELDS}, 2);
113117
is($sth->{NUM_OF_PARAMS}, 0);
118+
is($sth->rows, 0);
114119
is_deeply($sth->{NAME}, [ 'name_id1', 'name_id2' ]);
115120
is_deeply($sth->{mariadb_type}, [ DBD::MariaDB::TYPE_LONG, DBD::MariaDB::TYPE_LONG ]);
116121
ok(!eval { $sth->more_results() });
117122
is($sth->{NUM_OF_FIELDS}, 0);
118123
is($sth->{NUM_OF_PARAMS}, 0);
119124
ok(!eval { $sth->{NAME} });
120125
ok(!eval { $sth->{mariadb_type} });
126+
is($sth->rows, -1);
121127

122128
$dbh->disconnect();

0 commit comments

Comments
 (0)