[core] Move lastInsertRowId/changes to sqlite::Statement
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user