src/zserio/ValidationSqliteUtil.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef ZSERIO_VALIDATION_SQLITE_UTIL_H_INC |
2 | | #define ZSERIO_VALIDATION_SQLITE_UTIL_H_INC |
3 | | |
4 | | #include <map> |
5 | | |
6 | | #include "zserio/RebindAlloc.h" |
7 | | #include "zserio/SqliteConnection.h" |
8 | | #include "zserio/SqliteFinalizer.h" |
9 | | #include "zserio/String.h" |
10 | | #include "zserio/StringView.h" |
11 | | #include "zserio/Types.h" |
12 | | |
13 | | namespace zserio |
14 | | { |
15 | | |
16 | | /** Sqlite utility for validation. */ |
17 | | template <typename ALLOC> |
18 | | struct ValidationSqliteUtil |
19 | | { |
20 | | using string_type = string<ALLOC>; |
21 | | using Statement = std::unique_ptr<sqlite3_stmt, SqliteFinalizer>; |
22 | | |
23 | | /** |
24 | | * Description of a single column. |
25 | | */ |
26 | | struct ColumnDescription |
27 | | { |
28 | | string_type name; /**< Column name. */ |
29 | | string_type type; /**< Column SQLite data type ("INTEGER", "REAL", "TEXT" or "BLOB"). */ |
30 | | bool isNotNull; /**< Is true if the column has "NOT NULL" constraint. */ |
31 | | bool isPrimaryKey; /**< Is true if the column is primary key. */ |
32 | | }; |
33 | | |
34 | | using TableSchema = std::map<string_type, ColumnDescription, std::less<string_type>, |
35 | | RebindAlloc<ALLOC, std::pair<const string_type, ColumnDescription>>>; |
36 | | |
37 | | /** |
38 | | * Gets number of rows in the given SQLite table. |
39 | | * |
40 | | * \param connection Database connection to use. |
41 | | * \param attachedDbName Attached database name if table is relocated in different database. |
42 | | * \param tableName Name of the table to get column types of. |
43 | | * \param allocator Allocator to use for the query string composition. |
44 | | * |
45 | | * \return Number of rows. |
46 | | * |
47 | | * \throw SqliteException if the table does not exist. |
48 | | */ |
49 | | static size_t getNumberOfTableRows(SqliteConnection& connection, StringView attachedDbName, |
50 | | StringView tableName, const ALLOC& allocator) |
51 | 7 | { |
52 | 7 | string_type sqlQuery(allocator); |
53 | 7 | sqlQuery += "SELECT count(*) FROM "; |
54 | 7 | if (!attachedDbName.empty()) |
55 | 1 | { |
56 | 1 | sqlQuery += attachedDbName; |
57 | 1 | sqlQuery += "."; |
58 | 1 | } |
59 | 7 | sqlQuery += tableName; |
60 | | |
61 | 7 | Statement statement(connection.prepareStatement(sqlQuery)); |
62 | 7 | const int result = sqlite3_step(statement.get()); |
63 | 7 | if (result != SQLITE_ROW) |
64 | 1 | { |
65 | 1 | throw SqliteException("ValidationSqliteUtils.getNumberOfTableRows: sqlite3_step() failed: ") |
66 | 1 | << SqliteErrorCode(result); |
67 | 1 | } |
68 | | |
69 | 6 | return static_cast<size_t>(sqlite3_column_int64(statement.get(), 0)); |
70 | 7 | } |
71 | | |
72 | | /** |
73 | | * Gets a map of column names to column description for given SQLite table. |
74 | | * |
75 | | * \param connection Database connection to use. |
76 | | * \param attachedDbName Attached database name if table is relocated in different database. |
77 | | * \param tableName Name of the table to get column types of. |
78 | | * \param tableSchema Schema to fill. |
79 | | * \param allocator Allocator to use for the query string composition. |
80 | | */ |
81 | | static void getTableSchema(SqliteConnection& connection, StringView attachedDbName, StringView tableName, |
82 | | TableSchema& tableSchema, const ALLOC& allocator) |
83 | 5 | { |
84 | 5 | string_type sqlQuery(allocator); |
85 | 5 | sqlQuery += "PRAGMA "; |
86 | 5 | if (!attachedDbName.empty()) |
87 | 1 | { |
88 | 1 | sqlQuery += attachedDbName; |
89 | 1 | sqlQuery += "."; |
90 | 1 | } |
91 | 5 | sqlQuery += "table_info("; |
92 | 5 | sqlQuery += tableName; |
93 | 5 | sqlQuery += ")"; |
94 | | |
95 | 5 | Statement statement(connection.prepareStatement(sqlQuery)); |
96 | | |
97 | 5 | int result = SQLITE_OK; |
98 | 13 | while ((result = sqlite3_step(statement.get())) == SQLITE_ROW) |
99 | 8 | { |
100 | 8 | const char* columnName = reinterpret_cast<const char*>(sqlite3_column_text(statement.get(), 1)); |
101 | 8 | const char* columnType = reinterpret_cast<const char*>(sqlite3_column_text(statement.get(), 2)); |
102 | 8 | tableSchema.emplace(string_type(columnName, allocator), |
103 | 8 | ColumnDescription{ |
104 | 8 | string_type(columnName, allocator), string_type(columnType, allocator), |
105 | 8 | sqlite3_column_int(statement.get(), 3) != 0, // is not null |
106 | 8 | sqlite3_column_int(statement.get(), 5) != 0 // is primary key |
107 | 8 | }); |
108 | 8 | } |
109 | | |
110 | 5 | if (result != SQLITE_DONE) |
111 | 0 | { |
112 | 0 | throw SqliteException("ValidationSqliteUtils.getTableSchema: sqlite3_step() failed: ") |
113 | 0 | << SqliteErrorCode(result); |
114 | 0 | } |
115 | 5 | } |
116 | | |
117 | | /** |
118 | | * Check if the column is present in the given SQLite table. Note that this method also detect |
119 | | * hidden SQLite columns, which are not visible using standard PRAGMA table_info query. |
120 | | * |
121 | | * \param connection Database connection to use. |
122 | | * \param attachedDbName Attached database name if table is relocated in different database. |
123 | | * \param tableName Name of the table to get column types of. |
124 | | * \param columnName Name of the column to check. |
125 | | * \param allocator Allocator to use for the query string composition. |
126 | | * |
127 | | * \return Returns true if the column is present in the table, even if the column is hidden. Otherwise |
128 | | * returns false. |
129 | | */ |
130 | | static bool isColumnInTable(SqliteConnection& connection, StringView attachedDbName, StringView tableName, |
131 | | StringView columnName, const ALLOC& allocator) |
132 | 7 | { |
133 | | // try select to check if hidden column exists |
134 | 7 | string_type sqlQuery(allocator); |
135 | 7 | sqlQuery += "SELECT "; |
136 | 7 | sqlQuery += columnName; |
137 | 7 | sqlQuery += " FROM "; |
138 | 7 | if (!attachedDbName.empty()) |
139 | 1 | { |
140 | 1 | sqlQuery += attachedDbName; |
141 | 1 | sqlQuery += "."; |
142 | 1 | } |
143 | 7 | sqlQuery += tableName; |
144 | 7 | sqlQuery += " LIMIT 0"; |
145 | | |
146 | 7 | try |
147 | 7 | { |
148 | 7 | Statement statement(connection.prepareStatement(sqlQuery)); |
149 | 7 | return sqlite3_step(statement.get()) == SQLITE_DONE; |
150 | 7 | } |
151 | 7 | catch (const SqliteException&) |
152 | 7 | { |
153 | 3 | return false; |
154 | 3 | } |
155 | 7 | } |
156 | | |
157 | | /** |
158 | | * Gets name of the given SQLite column type. |
159 | | * |
160 | | * \param columnType SQLite column type. |
161 | | * |
162 | | * \return SQLite column type name. |
163 | | */ |
164 | | static const char* sqliteColumnTypeName(int columnType) |
165 | 15 | { |
166 | 15 | switch (columnType) |
167 | 15 | { |
168 | 5 | case SQLITE_INTEGER: |
169 | 5 | return "INTEGER"; |
170 | 2 | case SQLITE_FLOAT: |
171 | 2 | return "REAL"; |
172 | 2 | case SQLITE_TEXT: |
173 | 2 | return "TEXT"; |
174 | 2 | case SQLITE_BLOB: |
175 | 2 | return "BLOB"; |
176 | 4 | default: |
177 | 4 | return "NULL"; |
178 | 15 | } |
179 | 15 | } |
180 | | }; |
181 | | |
182 | | } // namespace zserio |
183 | | |
184 | | #endif // ZSERIO_VALIDATION_SQLITE_UTIL_H_INC |