/src/proj/src/sqlite3_utils.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * Project: PROJ |
3 | | * Purpose: SQLite3 related utilities |
4 | | * Author: Even Rouault, <even.rouault at spatialys.com> |
5 | | * |
6 | | ****************************************************************************** |
7 | | * Copyright (c) 2019, Even Rouault, <even.rouault at spatialys.com> |
8 | | * |
9 | | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | | * copy of this software and associated documentation files (the "Software"), |
11 | | * to deal in the Software without restriction, including without limitation |
12 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
13 | | * and/or sell copies of the Software, and to permit persons to whom the |
14 | | * Software is furnished to do so, subject to the following conditions: |
15 | | * |
16 | | * The above copyright notice and this permission notice shall be included |
17 | | * in all copies or substantial portions of the Software. |
18 | | * |
19 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
20 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
22 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
25 | | * DEALINGS IN THE SOFTWARE. |
26 | | *****************************************************************************/ |
27 | | |
28 | | #ifndef SQLITE3_HPP_INCLUDED |
29 | | #define SQLITE3_HPP_INCLUDED |
30 | | |
31 | | #include <memory> |
32 | | |
33 | | #include <sqlite3.h> |
34 | | |
35 | | #include "proj.h" |
36 | | #include "proj/util.hpp" |
37 | | |
38 | | NS_PROJ_START |
39 | | |
40 | | //! @cond Doxygen_Suppress |
41 | | |
42 | | // --------------------------------------------------------------------------- |
43 | | |
44 | | struct pj_sqlite3_vfs { |
45 | | sqlite3_vfs base{}; |
46 | | std::string namePtr{}; |
47 | | |
48 | | virtual ~pj_sqlite3_vfs(); |
49 | | }; |
50 | | |
51 | | // --------------------------------------------------------------------------- |
52 | | |
53 | | class SQLite3VFS { |
54 | | pj_sqlite3_vfs *vfs_ = nullptr; |
55 | | |
56 | | explicit SQLite3VFS(pj_sqlite3_vfs *vfs); |
57 | | |
58 | | SQLite3VFS(const SQLite3VFS &) = delete; |
59 | | SQLite3VFS &operator=(const SQLite3VFS &) = delete; |
60 | | |
61 | | public: |
62 | | ~SQLite3VFS(); |
63 | | |
64 | | static std::unique_ptr<SQLite3VFS> create(bool fakeSync, bool fakeLock, |
65 | | bool skipStatJournalAndWAL); |
66 | | |
67 | | #ifdef EMBED_RESOURCE_FILES |
68 | | static std::unique_ptr<SQLite3VFS> createMem(const void *membuffer, |
69 | | size_t bufferSize); |
70 | | #endif |
71 | | |
72 | | const char *name() const; |
73 | 0 | sqlite3_vfs *raw() { return &(vfs_->base); } |
74 | | }; |
75 | | |
76 | | // --------------------------------------------------------------------------- |
77 | | |
78 | | class SQLiteStatement { |
79 | | sqlite3_stmt *hStmt = nullptr; |
80 | | int iBindIdx = 1; |
81 | | int iResIdx = 0; |
82 | | SQLiteStatement(const SQLiteStatement &) = delete; |
83 | | SQLiteStatement &operator=(const SQLiteStatement &) = delete; |
84 | | |
85 | | public: |
86 | | explicit SQLiteStatement(sqlite3_stmt *hStmtIn); |
87 | 0 | ~SQLiteStatement() { sqlite3_finalize(hStmt); } |
88 | | |
89 | 0 | int execute() { return sqlite3_step(hStmt); } |
90 | | |
91 | 0 | void bindNull() { |
92 | 0 | sqlite3_bind_null(hStmt, iBindIdx); |
93 | 0 | iBindIdx++; |
94 | 0 | } |
95 | | |
96 | 0 | void bindText(const char *txt) { |
97 | 0 | sqlite3_bind_text(hStmt, iBindIdx, txt, -1, nullptr); |
98 | 0 | iBindIdx++; |
99 | 0 | } |
100 | | |
101 | 0 | void bindInt64(sqlite3_int64 v) { |
102 | 0 | sqlite3_bind_int64(hStmt, iBindIdx, v); |
103 | 0 | iBindIdx++; |
104 | 0 | } |
105 | | |
106 | 0 | void bindBlob(const void *blob, size_t blob_size) { |
107 | 0 | sqlite3_bind_blob(hStmt, iBindIdx, blob, static_cast<int>(blob_size), |
108 | 0 | nullptr); |
109 | 0 | iBindIdx++; |
110 | 0 | } |
111 | | |
112 | 0 | const char *getText() { |
113 | 0 | auto ret = sqlite3_column_text(hStmt, iResIdx); |
114 | 0 | iResIdx++; |
115 | 0 | return reinterpret_cast<const char *>(ret); |
116 | 0 | } |
117 | | |
118 | 0 | sqlite3_int64 getInt64() { |
119 | 0 | auto ret = sqlite3_column_int64(hStmt, iResIdx); |
120 | 0 | iResIdx++; |
121 | 0 | return ret; |
122 | 0 | } |
123 | | |
124 | 0 | const void *getBlob(int &size) { |
125 | 0 | size = sqlite3_column_bytes(hStmt, iResIdx); |
126 | 0 | auto ret = sqlite3_column_blob(hStmt, iResIdx); |
127 | 0 | iResIdx++; |
128 | 0 | return ret; |
129 | 0 | } |
130 | | |
131 | 0 | void reset() { |
132 | 0 | sqlite3_reset(hStmt); |
133 | 0 | iBindIdx = 1; |
134 | 0 | iResIdx = 0; |
135 | 0 | } |
136 | | |
137 | 0 | void resetResIndex() { iResIdx = 0; } |
138 | | }; |
139 | | |
140 | | //! @endcond Doxygen_Suppress |
141 | | |
142 | | NS_PROJ_END |
143 | | |
144 | | #endif // SQLITE3_HPP_INCLUDED |