diff options
author | Robin Krahl <me@robin-krahl.de> | 2015-07-25 18:54:06 +0200 |
---|---|---|
committer | Robin Krahl <me@robin-krahl.de> | 2015-07-25 18:54:06 +0200 |
commit | 6a00fdf8076537def530e635fbd2a6bbb1827094 (patch) | |
tree | 57380e590ceb511388badfd9b2998c79f27ea762 | |
parent | e04b08fdf55e6e964f6fc2198b882d07ee463456 (diff) | |
download | sqlitepp-6a00fdf8076537def530e635fbd2a6bbb1827094.tar.gz sqlitepp-6a00fdf8076537def530e635fbd2a6bbb1827094.tar.bz2 |
Add lastInsertRowId, minor improvements.
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | .travis.yml | 7 | ||||
-rw-r--r-- | CMakeLists.txt | 22 | ||||
-rw-r--r-- | include/sqlitepp/sqlitepp.h | 11 | ||||
-rw-r--r-- | src/sqlitepp/sqlitepp.cc | 7 | ||||
-rw-r--r-- | src/sqlitepp/sqlitepp_test.cc | 5 |
6 files changed, 38 insertions, 15 deletions
@@ -32,3 +32,4 @@ build # IDE project files .idea +bin diff --git a/.travis.yml b/.travis.yml index 00a860a..4e0d348 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,10 +11,11 @@ addons: packages: - libsqlite3-dev - libgtest-dev + - g++-4.8 before_script: - wget http://www.cmake.org/files/v3.2/cmake-3.2.3-Linux-x86_64.tar.gz -O /tmp/cmake.tar.gz - - tar -xvf /tmp/cmake.tar.gz + - tar -xf /tmp/cmake.tar.gz - export CMAKE=$PWD/cmake-3.2.3-Linux-x86_64/bin/cmake - mkdir gtest && cd gtest - cmake -DCMAKE_BUILD_TYPE=RELEASE /usr/src/gtest/ @@ -24,7 +25,5 @@ before_script: script: - mkdir bin && cd bin - - $CMAKE .. - - make - - make test + - $CMAKE .. && make && make test - make check diff --git a/CMakeLists.txt b/CMakeLists.txt index ad1abd8..95f0341 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,6 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/") add_definitions(-std=c++11) project(sqlitepp) -enable_testing() set(SOURCES src/sqlitepp/sqlitepp.cc) set(TEST_SOURCES src/sqlitepp/sqlitepp_test.cc) @@ -15,24 +14,27 @@ include(StyleCheck) include_directories(${INCLUDES}) add_library(sqlitepp ${SOURCES}) -add_executable(sqlitepp_test ${TEST_SOURCES}) find_package(Doxygen) -find_package(GTest REQUIRED) find_package(Sqlite3 REQUIRED) set(DEP_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIRS}) -set(DEP_LIBRARIES ${SQLITE3_LIBRARIES}) -set(TEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIRS}) -set(TEST_LIBRARIES ${GTEST_BOTH_LIBRARIES} pthread sqlitepp) +set(DEP_LIBRARIES PUBLIC ${SQLITE3_LIBRARIES}) include_directories(${DEP_INCLUDE_DIRS}) -include_directories(${TEST_INCLUDE_DIRS}) target_link_libraries(sqlitepp ${DEP_LIBRARIES}) -target_link_libraries(sqlitepp_test ${TEST_LIBRARIES}) -set(GTEST_ARGS "") -gtest_add_tests(sqlitepp_test "${GTEST_ARGS}" ${TEST_SOURCES}) +find_package(GTest) +if(GTEST_FOUND) + enable_testing() + set(TEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIRS}) + set(TEST_LIBRARIES ${GTEST_BOTH_LIBRARIES} pthread sqlitepp) + add_executable(sqlitepp_test ${TEST_SOURCES}) + include_directories(${TEST_INCLUDE_DIRS}) + target_link_libraries(sqlitepp_test ${TEST_LIBRARIES}) + set(GTEST_ARGS "") + gtest_add_tests(sqlitepp_test "${GTEST_ARGS}" ${TEST_SOURCES}) +endif(GTEST_FOUND) add_style_check_target(check "${LINT_FILES}") diff --git a/include/sqlitepp/sqlitepp.h b/include/sqlitepp/sqlitepp.h index 7ef3726..8e5bc0c 100644 --- a/include/sqlitepp/sqlitepp.h +++ b/include/sqlitepp/sqlitepp.h @@ -387,6 +387,17 @@ class Database : private Uncopyable, public Openable { /// \throws DatabaseError if an error occurred during the execution void execute(const std::string& sql); + /// \brief Returns the row ID of the last element that was inserted. + /// + /// If no entry has been inserted into the database, this method returns + /// zero. + /// + /// \returns the index of the last element inserted into the database or + /// zero + /// \throws std::logic_error if the database is not open + /// \throws DatabaseError if an error occurred during the execution + int lastInsertRowId() const; + /// \brief Opens the given database file. /// /// The given file must either be a valid SQLite3 database file or may not diff --git a/src/sqlitepp/sqlitepp.cc b/src/sqlitepp/sqlitepp.cc index d64b5ba..ef0dfa8 100644 --- a/src/sqlitepp/sqlitepp.cc +++ b/src/sqlitepp/sqlitepp.cc @@ -187,6 +187,11 @@ void Database::execute(const std::string& sql) { statement->step(); } +int Database::lastInsertRowId() const { + requireOpen(); + return sqlite3_last_insert_rowid(m_handle); +} + void Database::open(const std::string& file) { if (isOpen()) { throw std::logic_error("sqlitepp::Database::open(std::string&): " @@ -235,7 +240,7 @@ bool ResultSet::canRead() const { int ResultSet::columnCount() const { m_statement->requireOpen(); m_statement->requireCanRead(); - return sqlite3_column_count(m_statement->m_handle); + return sqlite3_data_count(m_statement->m_handle); } double ResultSet::readDouble(const int column) const { diff --git a/src/sqlitepp/sqlitepp_test.cc b/src/sqlitepp/sqlitepp_test.cc index b7ea5f6..dfa46d4 100644 --- a/src/sqlitepp/sqlitepp_test.cc +++ b/src/sqlitepp/sqlitepp_test.cc @@ -66,10 +66,15 @@ TEST(Database, insert) { statement->bind(":id", 1); statement->bind(2, "test value"); statement->execute(); + const int rowId1 = database.lastInsertRowId(); + EXPECT_NE(0, rowId1); statement->reset(); statement->bind(":id", 2); statement->bind(2, "other value"); statement->execute(); + const int rowId2 = database.lastInsertRowId(); + EXPECT_NE(0, rowId2); + EXPECT_NE(rowId1, rowId2); } TEST(Database, query) { |