updated build system

This commit is contained in:
2024-10-09 09:15:18 -05:00
parent 13a55bff61
commit 8bb291bbd9
14 changed files with 107 additions and 85 deletions

View File

@ -0,0 +1,52 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "utils/db/sqlite/db_common.hpp"
#if defined(PROJECT_ENABLE_SQLITE)
namespace repertory::utils::db::sqlite {
auto execute_sql(sqlite3 &db3, const std::string &sql,
std::string &err) -> bool {
char *err_msg{nullptr};
auto res = sqlite3_exec(&db3, sql.c_str(), nullptr, nullptr, &err_msg);
if (err_msg != nullptr) {
err = err_msg;
sqlite3_free(err_msg);
err_msg = nullptr;
}
if (res == SQLITE_OK) {
return true;
}
err = "failed to execute sql|" + sql + "|" + std::to_string(res) + '|' +
(err.empty() ? sqlite3_errstr(res) : err);
return false;
}
void set_journal_mode(sqlite3 &db3) {
sqlite3_exec(&db3, "PRAGMA journal_mode = WAL;PRAGMA synchronous = NORMAL;",
nullptr, nullptr, nullptr);
}
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)

View File

@ -0,0 +1,99 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "utils/db/sqlite/db_delete.hpp"
#if defined(PROJECT_ENABLE_SQLITE)
namespace repertory::utils::db::sqlite {
auto db_delete::dump() const -> std::string {
std::stringstream query;
query << "DELETE FROM \"" << context_->table_name << "\"";
if (context_->where.has_value()) {
std::int32_t idx{};
query << " WHERE " << context_->where->dump(idx);
}
query << ';';
return query.str();
}
auto db_delete::go() const -> db_result<context> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
sqlite3_stmt *stmt_ptr{nullptr};
auto query_str = dump();
auto res = sqlite3_prepare_v2(&context_->db3, query_str.c_str(), -1,
&stmt_ptr, nullptr);
if (res != SQLITE_OK) {
utils::error::raise_error(function_name,
"failed to prepare|" + std::to_string(res) + '|' +
sqlite3_errstr(res) + '|' + query_str);
return {context_, res};
}
context_->stmt.reset(stmt_ptr);
for (std::int32_t idx = 0;
idx < static_cast<std::int32_t>(context_->where_values.size()); idx++) {
res = std::visit(
overloaded{
[this, &idx](std::int64_t data) -> std::int32_t {
return sqlite3_bind_int64(context_->stmt.get(), idx + 1, data);
},
[this, &idx](const std::string &data) -> std::int32_t {
return sqlite3_bind_text(context_->stmt.get(), idx + 1,
data.c_str(), -1, nullptr);
},
},
context_->where_values.at(static_cast<std::size_t>(idx)));
if (res != SQLITE_OK) {
utils::error::raise_error(function_name,
"failed to bind|" + std::to_string(res) + '|' +
sqlite3_errstr(res) + '|' + query_str);
return {context_, res};
}
}
return {context_, res};
}
auto db_delete::group(context::w_t::group_func_t func) -> context::w_t::wn_t {
if (not context_->where.has_value()) {
context_->where = context::w_t{context_};
}
return context_->where->group(std::move(func));
}
auto db_delete::where(std::string column_name) const -> context::w_t::cn_t {
if (not context_->where.has_value()) {
context_->where = context::w_t{context_};
}
return context_->where->where(column_name);
}
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)

View File

@ -0,0 +1,104 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "utils/db/sqlite/db_insert.hpp"
#if defined(PROJECT_ENABLE_SQLITE)
namespace repertory::utils::db::sqlite {
auto db_insert::column_value(std::string column_name,
db_types_t value) -> db_insert & {
context_->values[column_name] = value;
return *this;
}
auto db_insert::dump() const -> std::string {
std::stringstream query;
query << "INSERT ";
if (context_->or_replace) {
query << "OR REPLACE ";
}
query << "INTO \"" << context_->table_name << "\" (";
for (std::int32_t idx = 0;
idx < static_cast<std::int32_t>(context_->values.size()); idx++) {
if (idx > 0) {
query << ", ";
}
query << '"' << std::next(context_->values.begin(), idx)->first << '"';
}
query << ") VALUES (";
for (std::int32_t idx = 0;
idx < static_cast<std::int32_t>(context_->values.size()); idx++) {
if (idx > 0) {
query << ", ";
}
query << "?" << (idx + 1);
}
query << ");";
return query.str();
}
auto db_insert::go() const -> db_result<context> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
sqlite3_stmt *stmt_ptr{nullptr};
auto query_str = dump();
auto res = sqlite3_prepare_v2(&context_->db3, query_str.c_str(), -1,
&stmt_ptr, nullptr);
if (res != SQLITE_OK) {
utils::error::raise_error(function_name, "failed to prepare|" +
std::to_string(res) + '|' +
sqlite3_errstr(res));
return {context_, res};
}
context_->stmt.reset(stmt_ptr);
for (std::int32_t idx = 0;
idx < static_cast<std::int32_t>(context_->values.size()); idx++) {
res = std::visit(
overloaded{
[this, &idx](std::int64_t data) -> std::int32_t {
return sqlite3_bind_int64(context_->stmt.get(), idx + 1, data);
},
[this, &idx](const std::string &data) -> std::int32_t {
return sqlite3_bind_text(context_->stmt.get(), idx + 1,
data.c_str(), -1, nullptr);
},
},
std::next(context_->values.begin(), idx)->second);
if (res != SQLITE_OK) {
utils::error::raise_error(function_name, "failed to bind|" +
std::to_string(res) + '|' +
sqlite3_errstr(res));
return {context_, res};
}
}
return {context_, res};
}
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)

View File

@ -0,0 +1,156 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "utils/db/sqlite/db_select.hpp"
#if defined(PROJECT_ENABLE_SQLITE)
namespace repertory::utils::db::sqlite {
auto db_select::column(std::string column_name) -> db_select & {
context_->columns.push_back(column_name);
return *this;
}
auto db_select::count(std::string column_name,
std::string as_column_name) -> db_select & {
context_->count_columns[column_name] = as_column_name;
return *this;
}
auto db_select::dump() const -> std::string {
std::stringstream query;
query << "SELECT ";
bool has_column{false};
if (context_->columns.empty()) {
if (context_->count_columns.empty()) {
query << "*";
has_column = true;
}
} else {
has_column = not context_->columns.empty();
for (std::size_t idx = 0U; idx < context_->columns.size(); idx++) {
if (idx > 0U) {
query << ", ";
}
query << context_->columns.at(idx);
}
}
for (std::int32_t idx = 0U;
idx < static_cast<std::int32_t>(context_->count_columns.size()); idx++) {
if (has_column || idx > 0) {
query << ", ";
}
query << "COUNT(\"";
auto &count_column = *std::next(context_->count_columns.begin(), idx);
query << count_column.first << "\") AS \"" << count_column.second << '"';
}
query << " FROM \"" << context_->table_name << "\"";
if (context_->where.has_value()) {
std::int32_t idx{};
query << " WHERE " << context_->where->dump(idx);
}
if (context_->order_by.has_value()) {
query << " ORDER BY \"" << context_->order_by.value().first << "\" ";
query << (context_->order_by.value().second ? "ASC" : "DESC");
}
if (context_->limit.has_value()) {
query << " LIMIT " << context_->limit.value();
}
query << ';';
return query.str();
}
auto db_select::go() const -> db_result<context> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
sqlite3_stmt *stmt_ptr{nullptr};
auto query_str = dump();
auto res = sqlite3_prepare_v2(&context_->db3, query_str.c_str(), -1,
&stmt_ptr, nullptr);
if (res != SQLITE_OK) {
utils::error::raise_error(function_name,
"failed to prepare|" + std::to_string(res) + '|' +
sqlite3_errstr(res) + '|' + query_str);
return {context_, res};
}
context_->stmt.reset(stmt_ptr);
for (std::int32_t idx = 0;
idx < static_cast<std::int32_t>(context_->where_values.size()); idx++) {
res = std::visit(
overloaded{
[this, &idx](std::int64_t data) -> std::int32_t {
return sqlite3_bind_int64(context_->stmt.get(), idx + 1, data);
},
[this, &idx](const std::string &data) -> std::int32_t {
return sqlite3_bind_text(context_->stmt.get(), idx + 1,
data.c_str(), -1, nullptr);
},
},
context_->where_values.at(static_cast<std::size_t>(idx)));
if (res != SQLITE_OK) {
utils::error::raise_error(function_name,
"failed to bind|" + std::to_string(res) + '|' +
sqlite3_errstr(res) + '|' + query_str);
return {context_, res};
}
}
return {context_, res};
}
auto db_select::group(context::w_t::group_func_t func) -> context::w_t::wn_t {
if (not context_->where.has_value()) {
context_->where = context::w_t{context_};
}
return context_->where->group(std::move(func));
}
auto db_select::limit(std::int32_t value) -> db_select & {
context_->limit = value;
return *this;
}
auto db_select::order_by(std::string column_name,
bool ascending) -> db_select & {
context_->order_by = {column_name, ascending};
return *this;
}
auto db_select::where(std::string column_name) const -> context::w_t::cn_t {
if (not context_->where.has_value()) {
context_->where = context::w_t{context_};
}
return context_->where->where(column_name);
}
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)

View File

@ -0,0 +1,166 @@
/*
Copyright <2018-2024> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "utils/db/sqlite/db_update.hpp"
#if defined(PROJECT_ENABLE_SQLITE)
namespace repertory::utils::db::sqlite {
auto db_update::column_value(std::string column_name,
db_types_t value) -> db_update & {
context_->column_values[column_name] = value;
return *this;
}
auto db_update::dump() const -> std::string {
std::stringstream query;
query << "UPDATE \"" << context_->table_name << "\" SET ";
for (std::int32_t idx = 0;
idx < static_cast<std::int32_t>(context_->column_values.size()); idx++) {
if (idx > 0) {
query << ", ";
}
auto column = std::next(context_->column_values.begin(), idx);
query << '"' << column->first << "\"=?" + std::to_string(idx + 1);
}
if (context_->where.has_value()) {
auto idx{static_cast<std::int32_t>(context_->column_values.size())};
query << " WHERE " << context_->where->dump(idx);
}
if (context_->order_by.has_value()) {
query << " ORDER BY \"" << context_->order_by.value().first << "\" ";
query << (context_->order_by.value().second ? "ASC" : "DESC");
}
if (context_->limit.has_value()) {
query << " LIMIT " << context_->limit.value();
}
query << ';';
return query.str();
}
auto db_update::go() const -> db_result<context> {
static constexpr const std::string_view function_name{
static_cast<const char *>(__FUNCTION__),
};
sqlite3_stmt *stmt_ptr{nullptr};
auto query_str = dump();
auto res = sqlite3_prepare_v2(&context_->db3, query_str.c_str(), -1,
&stmt_ptr, nullptr);
if (res != SQLITE_OK) {
utils::error::raise_error(function_name,
"failed to prepare|" + std::to_string(res) + '|' +
sqlite3_errstr(res) + '|' + query_str);
return {context_, res};
}
context_->stmt.reset(stmt_ptr);
for (std::int32_t idx = 0;
idx < static_cast<std::int32_t>(context_->column_values.size()); idx++) {
res = std::visit(
overloaded{
[this, &idx](std::int64_t data) -> std::int32_t {
return sqlite3_bind_int64(context_->stmt.get(), idx + 1, data);
},
[this, &idx](const std::string &data) -> std::int32_t {
return sqlite3_bind_text(context_->stmt.get(), idx + 1,
data.c_str(), -1, nullptr);
},
},
std::next(context_->column_values.begin(), idx)->second);
if (res != SQLITE_OK) {
utils::error::raise_error(function_name, "failed to bind|" +
std::to_string(res) + '|' +
sqlite3_errstr(res));
return {context_, res};
}
}
for (std::int32_t idx = 0;
idx < static_cast<std::int32_t>(context_->where_values.size()); idx++) {
res = std::visit(overloaded{
[this, &idx](std::int64_t data) -> std::int32_t {
return sqlite3_bind_int64(
context_->stmt.get(),
idx +
static_cast<std::int32_t>(
context_->column_values.size()) +
1,
data);
},
[this, &idx](const std::string &data) -> std::int32_t {
return sqlite3_bind_text(
context_->stmt.get(),
idx +
static_cast<std::int32_t>(
context_->column_values.size()) +
1,
data.c_str(), -1, nullptr);
},
},
context_->where_values.at(static_cast<std::size_t>(idx)));
if (res != SQLITE_OK) {
utils::error::raise_error(function_name,
"failed to bind|" + std::to_string(res) + '|' +
sqlite3_errstr(res) + '|' + query_str);
return {context_, res};
}
}
return {context_, res};
}
auto db_update::group(context::w_t::group_func_t func) -> context::w_t::wn_t {
if (not context_->where.has_value()) {
context_->where = context::w_t{context_};
}
return context_->where->group(std::move(func));
}
auto db_update::limit(std::int32_t value) -> db_update & {
context_->limit = value;
return *this;
}
auto db_update::order_by(std::string column_name,
bool ascending) -> db_update & {
context_->order_by = {column_name, ascending};
return *this;
}
auto db_update::where(std::string column_name) const -> context::w_t::cn_t {
if (not context_->where.has_value()) {
context_->where = context::w_t{context_};
}
return context_->where->where(column_name);
}
} // namespace repertory::utils::db::sqlite
#endif // defined(PROJECT_ENABLE_SQLITE)