summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno de Oliveira Abinader <bruno@mapbox.com>2017-01-04 11:09:45 -0400
committerBruno de Oliveira Abinader <bruno@mapbox.com>2017-01-23 19:06:53 +0200
commit80d72123dfe3cb9f560b6a150a998eeda480b9de (patch)
treef002717a45c7179e66b02c630b94eca5dbb5f047
parent97b433edc99f5b7212915aab483e119f60fde101 (diff)
downloadqtlocation-mapboxgl-80d72123dfe3cb9f560b6a150a998eeda480b9de.tar.gz
[core] Move lastInsertRowId/changes to sqlite::Statement
-rw-r--r--platform/default/mbgl/storage/offline_database.cpp14
-rw-r--r--platform/default/sqlite3.cpp25
-rw-r--r--platform/default/sqlite3.hpp6
-rw-r--r--test/storage/offline_database.test.cpp19
4 files changed, 44 insertions, 20 deletions
diff --git a/platform/default/mbgl/storage/offline_database.cpp b/platform/default/mbgl/storage/offline_database.cpp
index 49359dbd39..cdbb940057 100644
--- a/platform/default/mbgl/storage/offline_database.cpp
+++ b/platform/default/mbgl/storage/offline_database.cpp
@@ -313,7 +313,7 @@ bool OfflineDatabase::putResource(const Resource& resource,
}
update->run();
- if (db->changes() != 0) {
+ if (update->changes() != 0) {
transaction.commit();
return false;
}
@@ -502,7 +502,7 @@ bool OfflineDatabase::putTile(const Resource::TileData& tile,
}
update->run();
- if (db->changes() != 0) {
+ if (update->changes() != 0) {
transaction.commit();
return false;
}
@@ -567,7 +567,7 @@ OfflineRegion OfflineDatabase::createRegion(const OfflineRegionDefinition& defin
stmt->bindBlob(2, metadata);
stmt->run();
- return OfflineRegion(db->lastInsertRowid(), definition, metadata);
+ return OfflineRegion(stmt->lastInsertRowId(), definition, metadata);
}
OfflineRegionMetadata OfflineDatabase::updateMetadata(const int64_t regionID, const OfflineRegionMetadata& metadata) {
@@ -656,7 +656,7 @@ bool OfflineDatabase::markUsed(int64_t regionID, const Resource& resource) {
insert->bind(6, tile.z);
insert->run();
- if (db->changes() == 0) {
+ if (insert->changes() == 0) {
return false;
}
@@ -693,7 +693,7 @@ bool OfflineDatabase::markUsed(int64_t regionID, const Resource& resource) {
insert->bind(2, resource.url);
insert->run();
- if (db->changes() == 0) {
+ if (insert->changes() == 0) {
return false;
}
@@ -830,7 +830,7 @@ bool OfflineDatabase::evict(uint64_t neededFreeSize) {
// clang-format on
stmt1->bind(1, accessed);
stmt1->run();
- uint64_t changes1 = db->changes();
+ uint64_t changes1 = stmt1->changes();
// clang-format off
Statement stmt2 = getStatement(
@@ -845,7 +845,7 @@ bool OfflineDatabase::evict(uint64_t neededFreeSize) {
// clang-format on
stmt2->bind(1, accessed);
stmt2->run();
- uint64_t changes2 = db->changes();
+ uint64_t changes2 = stmt2->changes();
// The cached value of offlineTileCount does not need to be updated
// here because only non-offline tiles can be removed by eviction.
diff --git a/platform/default/sqlite3.cpp b/platform/default/sqlite3.cpp
index 8e882f4455..251433638a 100644
--- a/platform/default/sqlite3.cpp
+++ b/platform/default/sqlite3.cpp
@@ -56,6 +56,8 @@ public:
}
sqlite3_stmt* stmt = nullptr;
+ int64_t lastInsertRowId = 0;
+ int64_t changes = 0;
};
template <typename T>
@@ -133,16 +135,6 @@ Statement Database::prepare(const char *query) {
return Statement(this, query);
}
-int64_t Database::lastInsertRowid() const {
- assert(impl);
- return sqlite3_last_insert_rowid(impl->db);
-}
-
-uint64_t Database::changes() const {
- assert(impl);
- return sqlite3_changes(impl->db);
-}
-
Statement::Statement(Database *db, const char *sql)
: impl(std::make_unique<StatementImpl>(db->impl->db, sql))
{
@@ -292,6 +284,8 @@ void Statement::bind(
bool Statement::run() {
assert(impl);
const int err = sqlite3_step(impl->stmt);
+ impl->lastInsertRowId = sqlite3_last_insert_rowid(sqlite3_db_handle(impl->stmt));
+ impl->changes = sqlite3_changes(sqlite3_db_handle(impl->stmt));
if (err == SQLITE_DONE) {
return false;
} else if (err == SQLITE_ROW) {
@@ -390,6 +384,17 @@ void Statement::clearBindings() {
sqlite3_clear_bindings(impl->stmt);
}
+int64_t Statement::lastInsertRowId() const {
+ assert(impl);
+ return impl->lastInsertRowId;
+}
+
+uint64_t Statement::changes() const {
+ assert(impl);
+ auto changes = impl->changes;
+ return (changes < 0 ? 0 : changes);
+}
+
Transaction::Transaction(Database& db_, Mode mode)
: db(db_) {
switch (mode) {
diff --git a/platform/default/sqlite3.hpp b/platform/default/sqlite3.hpp
index dac7b6f650..02113b829f 100644
--- a/platform/default/sqlite3.hpp
+++ b/platform/default/sqlite3.hpp
@@ -46,9 +46,6 @@ public:
void exec(const std::string &sql);
Statement prepare(const char *query);
- int64_t lastInsertRowid() const;
- uint64_t changes() const;
-
private:
std::unique_ptr<DatabaseImpl> impl;
@@ -86,6 +83,9 @@ public:
void reset();
void clearBindings();
+ int64_t lastInsertRowId() const;
+ uint64_t changes() const;
+
private:
std::unique_ptr<StatementImpl> impl;
};
diff --git a/test/storage/offline_database.test.cpp b/test/storage/offline_database.test.cpp
index 2e25835d80..5e8da106da 100644
--- a/test/storage/offline_database.test.cpp
+++ b/test/storage/offline_database.test.cpp
@@ -80,6 +80,25 @@ private:
} // namespace
+TEST(OfflineDatabase, Statement) {
+ using namespace mbgl;
+
+ mapbox::sqlite::Database db(":memory:", mapbox::sqlite::ReadWrite | mapbox::sqlite::Create);
+ db.exec("CREATE TABLE test (id INTEGER)");
+ mapbox::sqlite::Statement stmt1 = db.prepare("INSERT INTO test (id) VALUES (?1)");
+ stmt1.bind(1, 0);
+ stmt1.run();
+ ASSERT_EQ(stmt1.lastInsertRowId(), 1);
+ ASSERT_EQ(stmt1.changes(), 1);
+
+ mapbox::sqlite::Statement stmt2 = db.prepare("INSERT INTO test (id) VALUES (?1)");
+ stmt2.bind(1, 0);
+ stmt2.run();
+ ASSERT_EQ(stmt1.lastInsertRowId(), 1);
+ ASSERT_EQ(stmt2.lastInsertRowId(), 2);
+ ASSERT_EQ(stmt2.changes(), 1);
+}
+
TEST(OfflineDatabase, TEST_REQUIRES_WRITE(Create)) {
using namespace mbgl;