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