libnick 2025.9.1
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
sqlitestatement.h
Go to the documentation of this file.
1
22
23#ifndef SQLITESTATEMENT_H
24#define SQLITESTATEMENT_H
25
26#include "sqlite.h"
27#include "sqlitestepresult.h"
28
30{
35 {
36 public:
43 SqliteStatement(sqlite3* database, const std::string& command);
53 ~SqliteStatement() noexcept;
66 template<SupportedSqliteValue T>
67 bool bind(int index, T value) noexcept
68 {
69 if(!m_statement)
70 {
71 return false;
72 }
73 if constexpr (std::is_same_v<T, int>)
74 {
75 return sqlite3_bind_int(m_statement, index, value) == SQLITE_OK;
76 }
77 else if constexpr (std::is_same_v<T, std::int64_t>)
78 {
79 return sqlite3_bind_int64(m_statement, index, value) == SQLITE_OK;
80 }
81 else if constexpr (std::is_same_v<T, double>)
82 {
83 return sqlite3_bind_double(m_statement, index, value) == SQLITE_OK;
84 }
85 else if constexpr (std::is_same_v<T, bool>)
86 {
87 return sqlite3_bind_int(m_statement, index, value ? 1 : 0) == SQLITE_OK;
88 }
89 else if constexpr (std::is_same_v<T, std::string>)
90 {
91 return sqlite3_bind_text(m_statement, index, value.c_str(), -1, SQLITE_TRANSIENT) == SQLITE_OK;
92 }
93 }
94
100 template<SupportedSqliteValue T>
101 T getColumn(int index) noexcept
102 {
103 if constexpr (std::is_same_v<T, int>)
104 {
105 if(!m_statement)
106 {
107 return 0;
108 }
109 return sqlite3_column_int(m_statement, index);
110 }
111 else if constexpr (std::is_same_v<T, std::int64_t>)
112 {
113 if(!m_statement)
114 {
115 return 0;
116 }
117 return sqlite3_column_int64(m_statement, index);
118 }
119 else if constexpr (std::is_same_v<T, double>)
120 {
121 if(!m_statement)
122 {
123 return 0.0;
124 }
125 return sqlite3_column_double(m_statement, index);
126 }
127 else if constexpr (std::is_same_v<T, bool>)
128 {
129 if(!m_statement)
130 {
131 return false;
132 }
133 return static_cast<bool>(sqlite3_column_int(m_statement, index));
134 }
135 else if constexpr (std::is_same_v<T, std::string>)
136 {
137 if(!m_statement)
138 {
139 return "";
140 }
141 return { reinterpret_cast<const char*>(sqlite3_column_text(m_statement, index)), static_cast<size_t>(sqlite3_column_bytes(m_statement, index)) };
142 }
143 }
155 operator bool() const noexcept;
156
157 private:
158 sqlite3_stmt* m_statement;
159 };
160}
161
162#endif //SQLITESTATEMENT_H
SqliteStatement & operator=(const SqliteStatement &)=delete
SqliteStatement(sqlite3 *database, const std::string &command)
Constructs a SqliteStatement.
T getColumn(int index) noexcept
Gets the sqlite column value as a specific type.
Definition sqlitestatement.h:101
SqliteStatement(SqliteStatement &&other) noexcept
Constructs a SqliteStatement via move.
SqliteStatement(const SqliteStatement &)=delete
SqliteStepResult step() noexcept
Steps through the statement.
SqliteStatement & operator=(SqliteStatement &&other) noexcept
Assigns a SqliteStatement via move.
~SqliteStatement() noexcept
Destructs a SqliteStatement.
bool bind(int index, T value) noexcept
Binds a value to a sqlite parameter.
Definition sqlitestatement.h:67
Definition sqlite.h:40
SqliteStepResult
Definition sqlitestepresult.h:26