iwongu / sqlite3pp

SQLite3++ - C++ wrapper of SQLite3 API
MIT License
609 stars 177 forks source link

Throw from destructors #33

Closed andreygorbachev closed 8 years ago

andreygorbachev commented 8 years ago

Hi,

Looks like the sqlite3pp::statement destructor is just processing the result of sqlite3_finalize call. Would it no be safer just to ignore the non SQLITE_OK return, rather then throw an exception from destructor? Looking at documentation for sqlite3_finalize:

“If the most recent evaluation of statement S failed, then sqlite3_finalize(S) returns the appropriate error code or extended error code.”

I guess the same does not apply to the sqlite3pp::transaction destructor. Modifying your example a bit:

sqlite3pp::transaction xct(db, true); { sqlite3pp::command cmd1( db, "INSERT INTO contacts (name, phone) VALUES (:user, :phone)"); cmd1.bind(":user", "Mike", sqlite3pp::nocopy); cmd1.bind(":phone", "555-1234", sqlite3pp::nocopy); cmd1.execute();

... more code ...

sqlite3pp::command cmd2( db, "INSERT INTO contacts (name, phone) VALUES (:user, :phone)"); cmd2.bind(":user", "Dave", sqlite3pp::nocopy); cmd2.bind(":phone", "555-5678", sqlite3pp::nocopy); cmd2.execute(); } xct.rollback();

If “... more code ...” throws would the xct try to commit the cmd1 part only?

Best Regards, Gorby

iwongu commented 8 years ago

Fixed by https://github.com/iwongu/sqlite3pp/commit/aa23baaffe7fae7953327006d716d9899bf9fd18

iwongu commented 8 years ago

And by https://github.com/iwongu/sqlite3pp/commit/9a594a62978f4c80b0e14ae996c08b456819cc75