brouznouf / fivem-mysql-async

MySql Async Library for FiveM
MIT License
111 stars 106 forks source link

Async callback rows matched vs. rows affected #189

Closed panki27 closed 2 years ago

panki27 commented 2 years ago

Describe the bug MySQL.Async.execute() is supposed to pass rowsAffected to the callback function. Yet it appears to be passing rowsMatched.

To Reproduce

CREATE TABLE foo (id INTEGER, highscore BIGINT DEFAULT 0);
INSERT INTO foo (id) VALUES (1);

Run the following from MySQL shell vs. with the library:

UPDATE foo SET highscore = greatest(highscore, 1337); -- this is supposed to return 1 rows affected
UPDATE foo SET highscore = greatest(highscore, 42); -- this is supposed to return 0 rows affected (no change)

Set up callbackfunction like:

function(rowsAffected)
    print(rowsAffected)
end

Expected behavior An output of

1
0

instead of

1
1

Software:

Additional context I think the library is returning how many rows were matched, as MySQL Client reports 1 for that. Also, I found this line in the MySQL documentation:

For UPDATE statements, the affected-rows value by default is the number of rows actually changed. If you specify the CLIENT_FOUND_ROWS flag to mysql_real_connect() when connecting to mysqld, the affected-rows value is the number of rows “found”; that is, matched by the WHERE clause.

So I guess this is the cause? Not sure.

panki27 commented 2 years ago

Ok I was able to fix this. RTFM...

You gotta supply flags=-FOUND_ROWS in the connection string to get it.