1 #ifndef ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
2 #define ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
16 template <
typename ALLOC>
20 using Statement = std::unique_ptr<sqlite3_stmt, SqliteFinalizer>;
33 using TableSchema = std::map<string_type, ColumnDescription, std::less<string_type>,
52 sqlQuery +=
"SELECT count(*) FROM ";
53 if (!attachedDbName.
empty())
55 sqlQuery += attachedDbName;
58 sqlQuery += tableName;
61 const int result = sqlite3_step(statement.get());
62 if (result != SQLITE_ROW)
64 throw SqliteException(
"ValidationSqliteUtils.getNumberOfTableRows: sqlite3_step() failed: ")
68 return static_cast<size_t>(sqlite3_column_int64(statement.get(), 0));
84 sqlQuery +=
"PRAGMA ";
85 if (!attachedDbName.
empty())
87 sqlQuery += attachedDbName;
90 sqlQuery +=
"table_info(";
91 sqlQuery += tableName;
96 int result = SQLITE_OK;
97 while ((result = sqlite3_step(statement.get())) == SQLITE_ROW)
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));
101 tableSchema.emplace(
string_type(columnName, allocator),
104 sqlite3_column_int(statement.get(), 3) != 0,
105 sqlite3_column_int(statement.get(), 5) != 0
109 if (result != SQLITE_DONE)
111 throw SqliteException(
"ValidationSqliteUtils.getTableSchema: sqlite3_step() failed: ")
130 StringView columnName,
const ALLOC& allocator)
134 sqlQuery +=
"SELECT ";
135 sqlQuery += columnName;
136 sqlQuery +=
" FROM ";
137 if (!attachedDbName.
empty())
139 sqlQuery += attachedDbName;
142 sqlQuery += tableName;
143 sqlQuery +=
" LIMIT 0";
148 return sqlite3_step(statement.get()) == SQLITE_DONE;
constexpr bool empty() const noexcept
sqlite3_stmt * prepareStatement(StringView sqlQuery)
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
std::basic_string< char, std::char_traits< char >, RebindAlloc< ALLOC, char > > string
static size_t getNumberOfTableRows(SqliteConnection &connection, StringView attachedDbName, StringView tableName, const ALLOC &allocator)
static void getTableSchema(SqliteConnection &connection, StringView attachedDbName, StringView tableName, TableSchema &tableSchema, const ALLOC &allocator)
string< ALLOC > string_type
static const char * sqliteColumnTypeName(int columnType)
std::map< string_type, ColumnDescription, std::less< string_type >, RebindAlloc< ALLOC, std::pair< const string_type, ColumnDescription > >> TableSchema
static bool isColumnInTable(SqliteConnection &connection, StringView attachedDbName, StringView tableName, StringView columnName, const ALLOC &allocator)
std::unique_ptr< sqlite3_stmt, SqliteFinalizer > Statement