Skip to content

Commit

Permalink
added locking mode
Browse files Browse the repository at this point in the history
  • Loading branch information
fnc12 committed Dec 10, 2024
1 parent 0da82b0 commit 9d539a4
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 47 deletions.
29 changes: 29 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Run unit_tests",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/tests/unit_tests",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
},
{
"name": "Run amalgamate script",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/third_party/amalgamate/amalgamate.py",
"args": [
"-c", "${workspaceFolder}/third_party/amalgamate/config.json",
"-s", "${workspaceFolder}"
],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal"
}
]
}
78 changes: 78 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"files.associations": {
"*.c": "c",
"string": "cpp",
"__bit_reference": "cpp",
"__hash_table": "cpp",
"__split_buffer": "cpp",
"__tree": "cpp",
"array": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"initializer_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"span": "cpp",
"string_view": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"chrono": "cpp",
"format": "cpp",
"ranges": "cpp",
"text_encoding": "cpp",
"__locale": "cpp",
"__node_handle": "cpp",
"__verbose_abort": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"memory": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"locale": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"print": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"tuple": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"algorithm": "cpp",
"clocale": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"compare": "cpp",
"concepts": "cpp",
"exception": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"random": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"__config": "cpp"
}
}
70 changes: 70 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run unit_tests",
"type": "shell",
"command": "${workspaceFolder}/build/tests/unit_tests",
"options": {
"cwd": "${workspaceFolder}/build/tests"
},
"group": {
"kind": "test",
"isDefault": true
},
"problemMatcher": [],
"presentation": {
"reveal": "always",
"panel": "shared"
}
},
{
"label": "Run amalgamate script",
"type": "shell",
"command": "python3",
"args": [
"third_party/amalgamate/amalgamate.py",
"-c",
"third_party/amalgamate/config.json",
"-s",
"."
],
"group": {
"kind": "build",
"isDefault": false
},
"problemMatcher": [],
"presentation": {
"reveal": "always",
"panel": "shared"
}
},
{
"label": "Run clang-format",
"type": "shell",
"command": "clang-format",
"args": [
"-i",
"-style=file",
"include/sqlite_orm/*.h",
"tests/*.cpp",
"tests/*/*.cpp",
"tests/*/*/*.cpp",
"dev/*.h",
"dev/*/*.h",
"tests/*/*.h",
"examples/*.cpp",
"examples/*/src/*.cpp"
],
"group": {
"kind": "build",
"isDefault": false
},
"problemMatcher": [],
"presentation": {
"reveal": "always",
"panel": "shared"
}
}
]
}
1 change: 1 addition & 0 deletions dev/error_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace sqlite_orm {
cannot_start_a_transaction_within_a_transaction,
no_active_transaction,
incorrect_journal_mode_string,
incorrect_locking_mode_string,
invalid_collate_argument_enum,
failed_to_init_a_backup,
unknown_member_value,
Expand Down
24 changes: 13 additions & 11 deletions dev/journal_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <algorithm> // std::transform
#include <cctype> // std::toupper

#include "serialize_result_type.h"

#if defined(_WINNT_)
// DELETE is a macro defined in the Windows SDK (winnt.h)
#pragma push_macro("DELETE")
Expand All @@ -33,37 +35,37 @@ namespace sqlite_orm {

namespace internal {

inline const std::string& to_string(journal_mode j) {
static std::string res[] = {
inline const serialize_result_type& to_string(journal_mode value) {
static const std::array<serialize_result_type, 6> res = {
"DELETE",
"TRUNCATE",
"PERSIST",
"MEMORY",
"WAL",
"OFF",
};
return res[static_cast<int>(j)];
return res.at(static_cast<int>(value));
}

inline std::unique_ptr<journal_mode> journal_mode_from_string(const std::string& str) {
std::string upper_str;
std::transform(str.begin(), str.end(), std::back_inserter(upper_str), [](char c) {
inline std::pair<bool, journal_mode> journal_mode_from_string(const std::string& string) {
std::string upperString;
std::transform(string.begin(), string.end(), std::back_inserter(upperString), [](char c) {
return static_cast<char>(std::toupper(static_cast<int>(c)));
});
static std::array<journal_mode, 6> all = {{
static const std::array<journal_mode, 6> allValues = {{
journal_mode::DELETE,
journal_mode::TRUNCATE,
journal_mode::PERSIST,
journal_mode::MEMORY,
journal_mode::WAL,
journal_mode::OFF,
}};
for(auto j: all) {
if(to_string(j) == upper_str) {
return std::make_unique<journal_mode>(j);
for(auto journalMode: allValues) {
if(to_string(journalMode) == upperString) {
return {true, journalMode};
}
}
return {};
return {false, journal_mode::OFF};
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions dev/locking_mode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <array> // std::array
#include <string> // std::string
#include <utility> // std::pair
#include <iterator> // std::back_inserter

#include "serialize_result_type.h"

namespace sqlite_orm {
enum class locking_mode : signed char {
NORMAL = 0,
EXCLUSIVE = 1,
};

namespace internal {
inline const serialize_result_type& to_string(locking_mode value) {
static const std::array<serialize_result_type, 2> res = {
"NORMAL",
"EXCLUSIVE",
};
return res.at(static_cast<int>(value));
}

inline std::pair<bool, locking_mode> locking_mode_from_string(const std::string& string) {
std::string upperString;
std::transform(string.begin(), string.end(), std::back_inserter(upperString), [](char c) {
return static_cast<char>(std::toupper(static_cast<int>(c)));
});
static const std::array<locking_mode, 2> allValues = {{
locking_mode::NORMAL,
locking_mode::EXCLUSIVE,
}};
for(auto lockingMode: allValues) {
if(to_string(lockingMode) == upperString) {
return {true, lockingMode};
}
}
return {false, locking_mode::NORMAL};
}
}
}
35 changes: 25 additions & 10 deletions dev/pragma.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "error_code.h"
#include "row_extractor.h"
#include "journal_mode.h"
#include "locking_mode.h"
#include "connection_holder.h"
#include "util.h"
#include "serializing_util.h"
Expand Down Expand Up @@ -63,6 +64,14 @@ namespace sqlite_orm {
return this->get_pragma<int>("busy_timeout");
}

sqlite_orm::locking_mode locking_mode() {
return this->get_pragma<sqlite_orm::locking_mode>("locking_mode");
}

void locking_mode(sqlite_orm::locking_mode value) {
this->set_pragma("locking_mode", value);
}

sqlite_orm::journal_mode journal_mode() {
return this->get_pragma<sqlite_orm::journal_mode>("journal_mode");
}
Expand Down Expand Up @@ -231,23 +240,29 @@ namespace sqlite_orm {
*/
template<class T>
void set_pragma(const std::string& name, const T& value, sqlite3* db = nullptr) {
auto con = this->get_connection();
if(!db) {
db = con.get();
}
std::stringstream ss;
ss << "PRAGMA " << name << " = " << value << std::flush;
perform_void_exec(db, ss.str());
ss << "PRAGMA " << name << " = " << value;
this->set_pragma_impl(ss.str(), db);
}

void set_pragma(const std::string& name, const sqlite_orm::journal_mode& value, sqlite3* db = nullptr) {
std::stringstream ss;
ss << "PRAGMA " << name << " = " << to_string(value);
this->set_pragma_impl(ss.str(), db);
}

void set_pragma(const std::string& name, const sqlite_orm::locking_mode& value, sqlite3* db = nullptr) {
std::stringstream ss;
ss << "PRAGMA " << name << " = " << to_string(value);
this->set_pragma_impl(ss.str(), db);
}

void set_pragma_impl(const std::string& query, sqlite3* db = nullptr) {
auto con = this->get_connection();
if(!db) {
if(db == nullptr) {
db = con.get();
}
std::stringstream ss;
ss << "PRAGMA " << name << " = " << to_string(value) << std::flush;
perform_void_exec(db, ss.str());
perform_void_exec(db, query);
}
};
}
Expand Down
Loading

0 comments on commit 9d539a4

Please sign in to comment.