Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions dev/pragma.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ namespace sqlite_orm {

pragma_t(get_connection_t get_connection_) : get_connection(std::move(get_connection_)) {}

void busy_timeout(int value) {
this->set_pragma("busy_timeout", value);
}

int busy_timeout() {
return this->get_pragma<int>("busy_timeout");
}

sqlite_orm::journal_mode journal_mode() {
return this->get_pragma<sqlite_orm::journal_mode>("journal_mode");
}
Expand Down
27 changes: 27 additions & 0 deletions dev/storage_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,19 @@ namespace sqlite_orm {
return this->connection->retain_count() > 0;
}

int busy_handler(std::function<int(int)> handler) {
_busy_handler = move(handler);
if(this->is_opened()) {
if(_busy_handler) {
return sqlite3_busy_handler(this->connection->get(), busy_handler_callback, this);
} else {
return sqlite3_busy_handler(this->connection->get(), nullptr, nullptr);
}
} else {
return SQLITE_OK;
}
}

protected:
storage_base(const std::string &filename_, int foreignKeysCount) :
pragma(std::bind(&storage_base::get_connection, this)),
Expand Down Expand Up @@ -339,6 +352,7 @@ namespace sqlite_orm {
std::unique_ptr<connection_holder> connection;
std::map<std::string, collating_function> collatingFunctions;
const int cachedForeignKeysCount;
std::function<int(int)> _busy_handler;

connection_ref get_connection() {
connection_ref res{*this->connection};
Expand Down Expand Up @@ -411,6 +425,10 @@ namespace sqlite_orm {
sqlite3_limit(db, p.first, p.second);
}

if(_busy_handler) {
sqlite3_busy_handler(this->connection->get(), busy_handler_callback, this);
}

if(this->on_open) {
this->on_open(db);
}
Expand Down Expand Up @@ -526,6 +544,15 @@ namespace sqlite_orm {
return f(leftLen, lhs, rightLen, rhs);
}

static int busy_handler_callback(void *selfPointer, int triesCount) {
auto &storage = *static_cast<storage_base *>(selfPointer);
if(storage._busy_handler) {
return storage._busy_handler(triesCount);
} else {
return 0;
}
}

// returns foreign keys count in storage definition
template<class T>
static int foreign_keys_count(T &storageImpl) {
Expand Down
35 changes: 35 additions & 0 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -8199,6 +8199,14 @@ namespace sqlite_orm {

pragma_t(get_connection_t get_connection_) : get_connection(std::move(get_connection_)) {}

void busy_timeout(int value) {
this->set_pragma("busy_timeout", value);
}

int busy_timeout() {
return this->get_pragma<int>("busy_timeout");
}

sqlite_orm::journal_mode journal_mode() {
return this->get_pragma<sqlite_orm::journal_mode>("journal_mode");
}
Expand Down Expand Up @@ -8879,6 +8887,19 @@ namespace sqlite_orm {
return this->connection->retain_count() > 0;
}

int busy_handler(std::function<int(int)> handler) {
_busy_handler = move(handler);
if(this->is_opened()) {
if(_busy_handler) {
return sqlite3_busy_handler(this->connection->get(), busy_handler_callback, this);
} else {
return sqlite3_busy_handler(this->connection->get(), nullptr, nullptr);
}
} else {
return SQLITE_OK;
}
}

protected:
storage_base(const std::string &filename_, int foreignKeysCount) :
pragma(std::bind(&storage_base::get_connection, this)),
Expand Down Expand Up @@ -8916,6 +8937,7 @@ namespace sqlite_orm {
std::unique_ptr<connection_holder> connection;
std::map<std::string, collating_function> collatingFunctions;
const int cachedForeignKeysCount;
std::function<int(int)> _busy_handler;

connection_ref get_connection() {
connection_ref res{*this->connection};
Expand Down Expand Up @@ -8988,6 +9010,10 @@ namespace sqlite_orm {
sqlite3_limit(db, p.first, p.second);
}

if(_busy_handler) {
sqlite3_busy_handler(this->connection->get(), busy_handler_callback, this);
}

if(this->on_open) {
this->on_open(db);
}
Expand Down Expand Up @@ -9103,6 +9129,15 @@ namespace sqlite_orm {
return f(leftLen, lhs, rightLen, rhs);
}

static int busy_handler_callback(void *selfPointer, int triesCount) {
auto &storage = *static_cast<storage_base *>(selfPointer);
if(storage._busy_handler) {
return storage._busy_handler(triesCount);
} else {
return 0;
}
}

// returns foreign keys count in storage definition
template<class T>
static int foreign_keys_count(T &storageImpl) {
Expand Down
19 changes: 19 additions & 0 deletions tests/pragma_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,22 @@ TEST_CASE("Auto vacuum") {
storage.pragma.auto_vacuum(2);
REQUIRE(storage.pragma.auto_vacuum() == 2);
}

TEST_CASE("busy_timeout") {
auto storage = make_storage({});

auto value = storage.pragma.busy_timeout();
REQUIRE(value == 0);

storage.pragma.busy_timeout(10);
value = storage.pragma.busy_timeout();
REQUIRE(value == 10);

storage.pragma.busy_timeout(20);
value = storage.pragma.busy_timeout();
REQUIRE(value == 20);

storage.pragma.busy_timeout(-1);
value = storage.pragma.busy_timeout();
REQUIRE(value == 0);
}
7 changes: 7 additions & 0 deletions tests/storage_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

using namespace sqlite_orm;

TEST_CASE("busy handler") {
auto storage = make_storage({});
storage.busy_handler([](int /*timesCount*/) {
return 0;
});
}

TEST_CASE("drop table") {
struct User {
int id = 0;
Expand Down