huntlabs / hunt-database

Database abstraction layer library using pure D programing language, support PostgreSQL and MySQL.
https://www.huntlabs.net
Apache License 2.0
48 stars 5 forks source link

Return value of statment.execute is always 2 in mysql. #56

Open vinoakash opened 3 years ago

vinoakash commented 3 years ago

Hi All,

The below code always returns value 2(result = stmt.execute()) but the data in the tables are getting inserted/ updated perfectly, so shoudnt the below code return the value 0 instead of 2. please correct me if my understanding is wrong.

Code

 auto con = new GetConnections();
ini result;
 Statement stmt = con.db.prepare("INSERT INTO test (Hostname,Location,Rack,Updated)
                                  VALUES(:Hostname,:Location,:Location,:Updated)
                                  ON DUPLICATE KEY UPDATE
                                  Location = IF(Location != VALUES(Location), VALUES(Location), :Location),
                                  Rack = IF(Rack != VALUES(Rack), VALUES(Rack), :Rack),
                                  Updated = :Updated");
 foreach(i,ref h,ref l,ref r; lockstep(data["Hostname"][], data["Location"][], data["Rack"][])) {
    stmt.setParameter("Hostname", data["Hostname"][i]);
    stmt.setParameter("Location", data["Location"][i]);
    stmt.setParameter("Rack", data["Rack"][i]);
    stmt.setParameter("Updated", Clock.currTime().toISOExtString()[0..19]);
    result =  stmt.execute();
 }
 con.db.close();
Heromyth commented 3 years ago

The execcute() returns the affected rows count, see here.