diff options
author | louiz’ <louiz@louiz.org> | 2017-06-16 09:49:08 +0200 |
---|---|---|
committer | louiz’ <louiz@louiz.org> | 2017-06-16 09:53:49 +0200 |
commit | 4a963cc480bb5a78e20380131ba886a7a23b0782 (patch) | |
tree | c09b97d8f9a240499262d0f870c004762b1fc8dc /src/database/table.cpp | |
parent | a77c982f6325408cbcc0afc9876edf28d095b3aa (diff) | |
download | biboumi-4a963cc480bb5a78e20380131ba886a7a23b0782.tar.gz biboumi-4a963cc480bb5a78e20380131ba886a7a23b0782.tar.bz2 biboumi-4a963cc480bb5a78e20380131ba886a7a23b0782.tar.xz biboumi-4a963cc480bb5a78e20380131ba886a7a23b0782.zip |
At startup, upgrade all database tables by adding missing columns
Diffstat (limited to 'src/database/table.cpp')
-rw-r--r-- | src/database/table.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/database/table.cpp b/src/database/table.cpp new file mode 100644 index 0000000..5929f33 --- /dev/null +++ b/src/database/table.cpp @@ -0,0 +1,25 @@ +#include <database/table.hpp> + +std::set<std::string> get_all_columns_from_table(sqlite3* db, const std::string& table_name) +{ + std::set<std::string> result; + char* errmsg; + std::string query{"PRAGMA table_info("s + table_name + ")"}; + log_debug(query); + int res = sqlite3_exec(db, query.data(), [](void* param, int columns_nb, char** columns, char**) -> int { + constexpr int name_column = 1; + std::set<std::string>* result = static_cast<std::set<std::string>*>(param); + log_debug("Table has column ", columns[name_column]); + if (name_column < columns_nb) + result->insert(columns[name_column]); + return 0; + }, &result, &errmsg); + + if (res != SQLITE_OK) + { + log_error("Error executing ", query, ": ", errmsg); + sqlite3_free(errmsg); + } + + return result; +} |