diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-10-17 22:11:44 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-10-20 01:40:55 +0100 |
commit | 5ceff5fb8d1f1c9edcd53f73fe4bcc4029fc45a0 (patch) | |
tree | 676b2a3d320412b5e4b2cb28ac20e1c22c00add6 | |
parent | Add override attributes (diff) | |
download | libdbpp-mysql-5ceff5fb8d1f1c9edcd53f73fe4bcc4029fc45a0.tar.bz2 libdbpp-mysql-5ceff5fb8d1f1c9edcd53f73fe4bcc4029fc45a0.tar.xz libdbpp-mysql-5ceff5fb8d1f1c9edcd53f73fe4bcc4029fc45a0.zip |
Add support for getting last insert value
-rw-r--r-- | libmysqlpp/connection.cpp | 6 | ||||
-rw-r--r-- | libmysqlpp/connection.h | 2 | ||||
-rw-r--r-- | libmysqlpp/unittests/Jamfile.jam | 1 | ||||
-rw-r--r-- | libmysqlpp/unittests/mysqlschema.sql | 4 | ||||
-rw-r--r-- | libmysqlpp/unittests/testmysql.cpp | 17 |
5 files changed, 30 insertions, 0 deletions
diff --git a/libmysqlpp/connection.cpp b/libmysqlpp/connection.cpp index d3457f5..fe3f51f 100644 --- a/libmysqlpp/connection.cpp +++ b/libmysqlpp/connection.cpp @@ -261,3 +261,9 @@ MySQL::Connection::bulkUploadData(const char * data, size_t len) const return len; } +int64_t +MySQL::Connection::insertId() const +{ + return mysql_insert_id(&conn); +} + diff --git a/libmysqlpp/connection.h b/libmysqlpp/connection.h index a1ea6ae..3ea6be8 100644 --- a/libmysqlpp/connection.h +++ b/libmysqlpp/connection.h @@ -29,6 +29,8 @@ namespace MySQL { void endBulkUpload(const char *) const override; size_t bulkUploadData(const char *, size_t) const override; + int64_t insertId() const override; + mutable MYSQL conn; private: diff --git a/libmysqlpp/unittests/Jamfile.jam b/libmysqlpp/unittests/Jamfile.jam index ed3e78e..167939a 100644 --- a/libmysqlpp/unittests/Jamfile.jam +++ b/libmysqlpp/unittests/Jamfile.jam @@ -17,6 +17,7 @@ run <library>boost_utf <library>boost_system <library>boost_filesystem + <dependency>mysqlschema.sql : testmysql ; diff --git a/libmysqlpp/unittests/mysqlschema.sql b/libmysqlpp/unittests/mysqlschema.sql index 58724c8..5ca3203 100644 --- a/libmysqlpp/unittests/mysqlschema.sql +++ b/libmysqlpp/unittests/mysqlschema.sql @@ -12,3 +12,7 @@ CREATE TABLE bulktest( intcol int, stringcol varchar(3000)); +CREATE TABLE inserts( + id int auto_increment, + num int, + primary key(id)); diff --git a/libmysqlpp/unittests/testmysql.cpp b/libmysqlpp/unittests/testmysql.cpp index bcbe528..9b0eae2 100644 --- a/libmysqlpp/unittests/testmysql.cpp +++ b/libmysqlpp/unittests/testmysql.cpp @@ -140,5 +140,22 @@ BOOST_AUTO_TEST_CASE( bigIterate ) delete ro; } +BOOST_AUTO_TEST_CASE( insertId ) +{ + auto ro = DB::MockDatabase::openConnectionTo("mysqlmock"); + auto ins = ro->newModifyCommand("INSERT INTO inserts(num) VALUES(?)"); + ins->bindParamI(0, 4); + ins->execute(); + BOOST_REQUIRE_EQUAL(1, ro->insertId()); + ins->bindParamI(0, 40); + ins->execute(); + BOOST_REQUIRE_EQUAL(2, ro->insertId()); + ins->bindParamI(0, -4); + ins->execute(); + BOOST_REQUIRE_EQUAL(3, ro->insertId()); + delete ins; + delete ro; +} + BOOST_AUTO_TEST_SUITE_END(); |