libnick 2025.10.0
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;
62 void reset() noexcept;
70 template<SupportedSqliteValue T>
71 bool bind(int index, T value) noexcept
72 {
73 if(!m_statement)
74 {
75 return false;
76 }
77 if constexpr (std::is_same_v<T, int>)
78 {
79 return sqlite3_bind_int(m_statement, index, value) == SQLITE_OK;
80 }
81 else if constexpr (std::is_same_v<T, std::int64_t>)
82 {
83 return sqlite3_bind_int64(m_statement, index, value) == SQLITE_OK;
84 }
85 else if constexpr (std::is_same_v<T, double>)
86 {
87 return sqlite3_bind_double(m_statement, index, value) == SQLITE_OK;
88 }
89 else if constexpr (std::is_same_v<T, bool>)
90 {
91 return sqlite3_bind_int(m_statement, index, value ? 1 : 0) == SQLITE_OK;
92 }
93 else if constexpr (std::is_same_v<T, std::string>)
94 {
95 return sqlite3_bind_text(m_statement, index, value.c_str(), -1, SQLITE_TRANSIENT) == SQLITE_OK;
96 }
97 }
98
104 template<SupportedSqliteValue T>
105 T getColumn(int index) noexcept
106 {
107 if constexpr (std::is_same_v<T, int>)
108 {
109 if(!m_statement)
110 {
111 return 0;
112 }
113 return sqlite3_column_int(m_statement, index);
114 }
115 else if constexpr (std::is_same_v<T, std::int64_t>)
116 {
117 if(!m_statement)
118 {
119 return 0;
120 }
121 return sqlite3_column_int64(m_statement, index);
122 }
123 else if constexpr (std::is_same_v<T, double>)
124 {
125 if(!m_statement)
126 {
127 return 0.0;
128 }
129 return sqlite3_column_double(m_statement, index);
130 }
131 else if constexpr (std::is_same_v<T, bool>)
132 {
133 if(!m_statement)
134 {
135 return false;
136 }
137 return static_cast<bool>(sqlite3_column_int(m_statement, index));
138 }
139 else if constexpr (std::is_same_v<T, std::string>)
140 {
141 if(!m_statement)
142 {
143 return "";
144 }
145 return { reinterpret_cast<const char*>(sqlite3_column_text(m_statement, index)), static_cast<size_t>(sqlite3_column_bytes(m_statement, index)) };
146 }
147 }
159 operator bool() const noexcept;
160
161 private:
162 sqlite3_stmt* m_statement;
163 };
164}
165
166#endif //SQLITESTATEMENT_H
SqliteStatement & operator=(const SqliteStatement &)=delete
void reset() noexcept
Resets the statement to its initial state, clearing any bindings as well.
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:105
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:71
Definition sqlite.h:40
SqliteStepResult
Definition sqlitestepresult.h:26