diff options
author | louiz’ <louiz@louiz.org> | 2017-08-26 17:40:54 +0200 |
---|---|---|
committer | louiz’ <louiz@louiz.org> | 2017-08-26 17:42:59 +0200 |
commit | 2c717d347d796a2b007331c42d78146e156eaea0 (patch) | |
tree | f9143de9de6d8b0470c527df6118f2270c0e1cd9 /src/database/index.hpp | |
parent | 655151d88a6ab948949b73682c3a76a0274eb10c (diff) | |
download | biboumi-2c717d347d796a2b007331c42d78146e156eaea0.tar.gz biboumi-2c717d347d796a2b007331c42d78146e156eaea0.tar.bz2 biboumi-2c717d347d796a2b007331c42d78146e156eaea0.tar.xz biboumi-2c717d347d796a2b007331c42d78146e156eaea0.zip |
Add an index for the muc_log_line table
This immensely speeds up the archive select queries.
fix #3292
Diffstat (limited to 'src/database/index.hpp')
-rw-r--r-- | src/database/index.hpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/database/index.hpp b/src/database/index.hpp new file mode 100644 index 0000000..5924779 --- /dev/null +++ b/src/database/index.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include <sqlite3.h> + +#include <string> +#include <tuple> + +namespace +{ +template <std::size_t N=0, typename... T> +typename std::enable_if<N == sizeof...(T), void>::type +add_column_name(std::string&) +{ } + +template <std::size_t N=0, typename... T> +typename std::enable_if<N < sizeof...(T), void>::type +add_column_name(std::string& out) +{ + using ColumnType = typename std::remove_reference<decltype(std::get<N>(std::declval<std::tuple<T...>>()))>::type; + out += ColumnType::name; + if (N != sizeof...(T) - 1) + out += ","; + add_column_name<N+1, T...>(out); +} +} + +template <typename... Columns> +void create_index(sqlite3* db, const std::string& name, const std::string& table) +{ + std::string res{"CREATE INDEX IF NOT EXISTS "}; + res += name + " ON " + table + "("; + add_column_name<0, Columns...>(res); + res += ")"; + + char* error; + const auto result = sqlite3_exec(db, res.data(), nullptr, nullptr, &error); + if (result != SQLITE_OK) + { + log_error("Error executing query: ", error); + sqlite3_free(error); + } +} |