Go to the documentation of this file.
31 #if __cplusplus > 201402L
32 #define CPP_SQLITE_NODISCARD [[nodiscard]]
34 #define CPP_SQLITE_NODISCARD
37 #if defined(CPP_SQLITE_NOTHROW)
38 #define CPP_SQLITE_THROW(...) return false
40 #define CPP_SQLITE_THROW(...) throw sqlite::Error(__VA_ARGS__)
45 class Error :
public std::runtime_error
48 explicit Error(
const char* message,
int errorCode = SQLITE_ERROR)
54 explicit Error(
const std::string& message,
int errorCode = SQLITE_ERROR)
74 if(code != SQLITE_OK && code != SQLITE_DONE)
76 const int extendedCode = sqlite3_extended_errcode(db);
77 std::string errstr = sqlite3_errstr(extendedCode);
78 std::string errmsg = sqlite3_errmsg(db);
88 if(code != SQLITE_OK && code != SQLITE_DONE)
90 std::string errstr = std::string(
"SQL error: ") + sqlite3_errstr(code);
105 this->
Open(filename);
113 other.m_connection =
nullptr;
135 other.m_connection =
nullptr;
141 bool Open(
const std::string& filename)
179 explicit Blob(std::vector<unsigned char> data)
241 inline void Append(sqlite3_stmt* statement,
int index,
const int32_t& data)
246 inline void Append(sqlite3_stmt* statement,
int index,
const int64_t& data)
251 inline void Append(sqlite3_stmt* statement,
int index,
const float& data)
256 inline void Append(sqlite3_stmt* statement,
int index,
const double& data)
261 inline void Append(sqlite3_stmt* statement,
int index,
const std::string& data)
266 inline void Append(sqlite3_stmt* statement,
int index,
const char* data)
281 template<
typename Arg>
282 inline void AppendToQuery(sqlite3_stmt* statement,
int index,
const Arg& arg)
287 template<
typename First,
typename ... Args>
288 inline void AppendToQuery(sqlite3_stmt* statement,
int index,
const First& first,
const Args&... args)
300 auto* db = connection.
GetPtr();
302 const int code = sqlite3_prepare_v2(
305 static_cast<int>(
command.size()),
325 other.handle =
nullptr;
333 const int code = sqlite3_step(
handle);
335 if(code == SQLITE_ROW)
374 #ifndef CPP_SQLITE_NOTHROW
379 std::string name = sqlite3_column_name(
handle, columnIndex);
383 #ifndef CPP_SQLITE_NOTHROW
397 static_assert(
sizeof(T) == -1,
"SQL error: invalid column data type");
406 return static_cast<float>(sqlite3_column_double(
handle, col));
412 return sqlite3_column_double(
handle, col);
418 return sqlite3_column_int(
handle, col);
424 return sqlite3_column_int64(
handle, col);
430 const unsigned char*
bytes = sqlite3_column_text(
handle, col);
431 const int size = sqlite3_column_bytes(
handle, col);
438 return {
reinterpret_cast<const char*
>(
bytes),
static_cast<std::string::size_type
>(
size)};
444 const void*
bytes = sqlite3_column_blob(
handle, col);
445 const int size = sqlite3_column_bytes(
handle, col);
533 template<
typename First,
typename ... Args>
541 template<
typename First,
typename ... Args>
557 template<
typename First,
typename ... Args>
564 return Result(std::move(statement));
572 return Result(std::move(statement));
577 sqlite3_backup* backup = sqlite3_backup_init(to.
GetPtr(),
"main", from.
GetPtr(),
"main");
constexpr auto size(const C &c) -> decltype(c.size())
virtual ~Connection() noexcept
void Append(sqlite3_stmt *statement, int index, const int32_t &data)
Statement & operator=(Statement &&other) noexcept
constexpr auto bytes
Matches N arbitrary bytes.
CPP_SQLITE_NODISCARD int ColumnCount() const
#define CPP_SQLITE_NODISCARD
ROSLIB_DECL std::string command(const std::string &cmd)
const CPP_SQLITE_NODISCARD unsigned char * GetData() const
const CPP_SQLITE_NODISCARD void * GetData() const
CPP_SQLITE_NODISCARD bool Next() const
Type(const sqlite::Priv::Statement &statement, int col)
CPP_SQLITE_NODISCARD bool HasData() const
CPP_SQLITE_NODISCARD int GetCode() const
Connection(Connection &&other) noexcept
CPP_SQLITE_NODISCARD uint32_t GetSize() const
bool CheckError(sqlite3 *db, int code)
CPP_SQLITE_NODISCARD unsigned char * GetData()
CPP_SQLITE_NODISCARD std::string GetColumnName(int columnIndex) const
Result(Result &&other) noexcept
Result & operator=(Result &&other) noexcept
void Statement(sqlite::Connection &connection, const std::string &command, const First &first, const Args... args)
Blob(std::vector< unsigned char > data)
constexpr auto count
Sink that counts all arguments.
sqlite::Priv::Statement m_statement
Connection & operator=(Connection &&other) noexcept
CPP_SQLITE_NODISCARD sqlite3 * GetPtr()
CPP_SQLITE_NODISCARD int ColumnCount() const
Error(const std::string &message, int errorCode=SQLITE_ERROR)
NOBlob(const void *ptr, uint32_t bytes)
CPP_SQLITE_NODISCARD std::string GetColumnName(int columnIndex) const
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Connection & operator=(const Connection &)=delete
Blob(const void *data, int32_t bytes)
Statement(sqlite::Connection &connection, const std::string &command)
Result(sqlite::Priv::Statement &&statement)
Statement(Statement &&other) noexcept
friend Result Query(sqlite::Connection &connection, const std::string &command, const First &first, const Args... args)
bool Open(const std::string &filename)
CPP_SQLITE_NODISCARD T Get(int) const
std::vector< unsigned char > m_data
CPP_SQLITE_NODISCARD int GetExtendedResult() const
void AppendToQuery(sqlite3_stmt *statement, int index, const Arg &arg)
const sqlite::Priv::Statement & m_statement
Error(const char *message, int errorCode=SQLITE_ERROR)
CPP_SQLITE_NODISCARD Type Get(int columnIndex) const
Expected< std::monostate > Result
bool Backup(sqlite::Connection &from, sqlite::Connection &to)
CPP_SQLITE_NODISCARD Result Query(sqlite::Connection &connection, const std::string &command, const First &first, const Args... args)
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name, cert-dcl58-cpp) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression, cppcoreguidelines-noexcept-swap, performance-noexcept-swap) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
CPP_SQLITE_NODISCARD bool Advance() const
CPP_SQLITE_NODISCARD uint32_t GetSize() const
Connection(const std::string &filename)
friend void Statement(sqlite::Connection &, const std::string &)
#define CPP_SQLITE_THROW(...)