Zserio C++17 runtime library 1.3.0
Built for Zserio 2.20.0
Loading...
Searching...
No Matches
ValidationSqliteUtil.h
Go to the documentation of this file.
1#ifndef ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
2#define ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
3
4#include <map>
5#include <string_view>
6
10#include "zserio/String.h"
11
12namespace zserio
13{
14
16template <typename ALLOC>
18{
20 using Statement = std::unique_ptr<sqlite3_stmt, SqliteFinalizer>;
21
32
33 using TableSchema = std::map<StringType, ColumnDescription, std::less<StringType>,
35
48 static size_t getNumberOfTableRows(SqliteConnection& connection, std::string_view attachedDbName,
49 std::string_view tableName, const ALLOC& allocator)
50 {
52 sqlQuery += "SELECT count(*) FROM ";
53 if (!attachedDbName.empty())
54 {
56 sqlQuery += ".";
57 }
59
61 const int result = sqlite3_step(statement.get());
62 if (result != SQLITE_ROW)
63 {
64 throw SqliteException("ValidationSqliteUtils.getNumberOfTableRows: sqlite3_step() failed: ")
66 }
67
68 return static_cast<size_t>(sqlite3_column_int64(statement.get(), 0));
69 }
70
80 static void getTableSchema(SqliteConnection& connection, std::string_view attachedDbName,
81 std::string_view tableName, TableSchema& tableSchema, const ALLOC& allocator)
82 {
84 sqlQuery += "PRAGMA ";
85 if (!attachedDbName.empty())
86 {
88 sqlQuery += ".";
89 }
90 sqlQuery += "table_info(";
92 sqlQuery += ")";
93
95
96 int result = SQLITE_OK;
97 while ((result = sqlite3_step(statement.get())) == SQLITE_ROW)
98 {
99 const char* columnName = reinterpret_cast<const char*>(sqlite3_column_text(statement.get(), 1));
100 const char* columnType = reinterpret_cast<const char*>(sqlite3_column_text(statement.get(), 2));
104 sqlite3_column_int(statement.get(), 3) != 0, // is not null
105 sqlite3_column_int(statement.get(), 5) != 0 // is primary key
106 });
107 }
108
109 if (result != SQLITE_DONE)
110 {
111 throw SqliteException("ValidationSqliteUtils.getTableSchema: sqlite3_step() failed: ")
113 }
114 }
115
129 static bool isColumnInTable(SqliteConnection& connection, std::string_view attachedDbName,
130 std::string_view tableName, std::string_view columnName, const ALLOC& allocator)
131 {
132 // try select to check if hidden column exists
134 sqlQuery += "SELECT ";
136 sqlQuery += " FROM ";
137 if (!attachedDbName.empty())
138 {
140 sqlQuery += ".";
141 }
143 sqlQuery += " LIMIT 0";
144
145 try
146 {
148 return sqlite3_step(statement.get()) == SQLITE_DONE;
149 }
150 catch (const SqliteException&)
151 {
152 return false;
153 }
154 }
155
163 static const char* sqliteColumnTypeName(int columnType)
164 {
165 switch (columnType)
166 {
167 case SQLITE_INTEGER:
168 return "INTEGER";
169 case SQLITE_FLOAT:
170 return "REAL";
171 case SQLITE_TEXT:
172 return "TEXT";
173 case SQLITE_BLOB:
174 return "BLOB";
175 default:
176 return "NULL";
177 }
178 }
179};
180
181} // namespace zserio
182
183#endif // ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
sqlite3_stmt * prepareStatement(std::string_view sqlQuery)
static const char * sqliteColumnTypeName(int columnType)
static bool isColumnInTable(SqliteConnection &connection, std::string_view attachedDbName, std::string_view tableName, std::string_view columnName, const ALLOC &allocator)
BasicString< RebindAlloc< ALLOC, char > > StringType
static size_t getNumberOfTableRows(SqliteConnection &connection, std::string_view attachedDbName, std::string_view tableName, const ALLOC &allocator)
static void getTableSchema(SqliteConnection &connection, std::string_view attachedDbName, std::string_view tableName, TableSchema &tableSchema, const ALLOC &allocator)
std::map< StringType, ColumnDescription, std::less< StringType >, RebindAlloc< ALLOC, std::pair< const StringType, ColumnDescription > > > TableSchema
std::unique_ptr< sqlite3_stmt, SqliteFinalizer > Statement