Coverage Report

Created: 2024-12-05 10:39

src/zserio/SqliteException.h
Line
Count
Source
1
#ifndef ZSERIO_SQLITE_EXCEPTION_H_INC
2
#define ZSERIO_SQLITE_EXCEPTION_H_INC
3
4
#include "zserio/CppRuntimeException.h"
5
6
#include "sqlite3.h"
7
8
namespace zserio
9
{
10
11
/** Wrapper class to work with SQLite error code. */
12
class SqliteErrorCode
13
{
14
public:
15
    /**
16
     * Constructor.
17
     *
18
     * \param sqliteCode SQLite error code.
19
     */
20
    explicit SqliteErrorCode(int sqliteCode) :
21
            m_code(sqliteCode)
22
9
    {}
23
24
    /**
25
     * Gets SQLite error string appropriate to the error code.
26
     *
27
     * \return English language text that describes the error code. Memory to hold the error message string is
28
     *         managed by SQLite.
29
     */
30
    const char* getErrorString() const
31
9
    {
32
9
        return sqlite3_errstr(m_code);
33
9
    }
34
35
private:
36
    int m_code;
37
};
38
39
/** Exception thrown when an error in an SQLite operation occurs. */
40
class SqliteException : public CppRuntimeException
41
{
42
public:
43
    using CppRuntimeException::CppRuntimeException;
44
};
45
46
/**
47
 * Allow to append SqliteErrorCode to CppRuntimeException.
48
 *
49
 * \param exception Exception to modify.
50
 * \param code SQLite error code.
51
 */
52
inline CppRuntimeException& operator<<(CppRuntimeException& exception, SqliteErrorCode code)
53
9
{
54
9
    return exception << code.getErrorString();
55
9
}
56
57
} // namespace zserio
58
59
#endif // ifndef ZSERIO_SQLITE_EXCEPTION_H_INC