Atlas Game Manager
A game manager for f95 and dlsite written in c++
Loading...
Searching...
No Matches
binders.hpp
Go to the documentation of this file.
1//
2// Created by kj16609 on 9/8/23.
3//
4
5#pragma once
6#ifndef ATLASGAMEMANAGER_BINDERS_HPP
7#define ATLASGAMEMANAGER_BINDERS_HPP
8
9#include <QByteArray>
10#include <QString>
11
12#include <concepts>
13#include <sqlite3.h>
14
15template < typename T >
16[[nodiscard]] int bindParameter( sqlite3_stmt*, const T, const int ) noexcept = delete;
17
18template < typename T >
19 requires std::is_integral_v< T >
20[[nodiscard]] int bindParameter( sqlite3_stmt* stmt, const T val, const int idx ) noexcept
21{
22 return sqlite3_bind_int64( stmt, idx, static_cast< sqlite3_int64 >( val ) );
23}
24
25template < typename T >
26 requires std::is_same_v< T, QString >
27[[nodiscard]] int bindParameter( sqlite3_stmt* stmt, const QString val, const int idx ) noexcept
28{
29 const QByteArray utf8_text { val.toUtf8() };
30 return sqlite3_bind_text( stmt, idx, utf8_text.data(), static_cast< int >( utf8_text.size() ), SQLITE_TRANSIENT );
31}
32
33template < typename T >
34 requires std::is_same_v< T, std::u8string >
35[[nodiscard]] int bindParameter( sqlite3_stmt* stmt, const std::u8string val, const int idx ) noexcept
36{
37 return sqlite3_bind_text(
38 stmt, idx, reinterpret_cast< const char* >( val.c_str() ), static_cast< int >( val.size() ), SQLITE_TRANSIENT );
39}
40
41template < typename T >
42 requires std::is_same_v< T, std::string_view >
43[[nodiscard]] int bindParameter( sqlite3_stmt* stmt, const std::string_view val, const int idx ) noexcept
44{
45#ifdef __linux__
46 return sqlite3_bind_text( stmt, idx, val.data(), static_cast< int >( val.size() ), SQLITE_TRANSIENT );
47#else
48 QString str { QString::fromLocal8Bit( val.data(), static_cast< qsizetype >( val.size() ) ) };
49 return bindParameter< QString >( stmt, std::move( str ), idx );
50#endif
51}
52
53template < typename T >
54 requires std::is_same_v< T, std::filesystem::path >
55[[nodiscard]] int bindParameter( sqlite3_stmt* stmt, const std::filesystem::path val, const int idx ) noexcept
56{
57 return bindParameter< std::u8string >( stmt, val.u8string(), idx );
58}
59
60template < typename T >
61 requires std::is_same_v< T, std::vector< std::byte > >
62[[nodiscard]] int bindParameter( sqlite3_stmt* stmt, const std::vector< std::byte > val, const int idx ) noexcept
63{
64 return sqlite3_bind_blob( stmt, idx, val.data(), static_cast< int >( val.size() ), nullptr );
65}
66
67template < typename T >
68 requires std::is_same_v< T, std::nullopt_t >
69[[nodiscard]] int
70 bindParameter( sqlite3_stmt* stmt, [[maybe_unused]] const std::nullopt_t nullopt, const int idx ) noexcept
71{
72 return sqlite3_bind_null( stmt, idx );
73}
74
75template < typename T >
76 requires std::is_floating_point_v< T >
77[[nodiscard]] int bindParameter( sqlite3_stmt* stmt, const T val, const int idx ) noexcept
78{
79 return sqlite3_bind_double( stmt, idx, val );
80}
81
82template < typename T >
83 requires std::is_same_v< std::string, T >
84[[nodiscard]] int bindParameter( sqlite3_stmt* stmt, const T val, const int idx ) noexcept
85{
86 return bindParameter< QString >( stmt, QString::fromStdString( val ), idx );
87}
88
89#endif //ATLASGAMEMANAGER_BINDERS_HPP
int bindParameter(sqlite3_stmt *, const T, const int) noexcept=delete
Definition binders.hpp:20