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 unsigned char* columnNameText = sqlite3_column_text(statement.get(), 1);
100 const char* columnName =
static_cast<const char*
>(
static_cast<const void*
>(columnNameText));
101 const unsigned char* columnTypeText = sqlite3_column_text(statement.get(), 2);
102 const char* columnType =
static_cast<const char*
>(
static_cast<const void*
>(columnTypeText));
103 tableSchema.emplace(
string_type(columnName, allocator),
106 sqlite3_column_int(statement.get(), 3) != 0,
107 sqlite3_column_int(statement.get(), 5) != 0
111 if (result != SQLITE_DONE)
113 throw SqliteException(
"ValidationSqliteUtils.getTableSchema: sqlite3_step() failed: ")
132 StringView columnName,
const ALLOC& allocator)
136 sqlQuery +=
"SELECT ";
137 sqlQuery += columnName;
138 sqlQuery +=
" FROM ";
139 if (!attachedDbName.
empty())
141 sqlQuery += attachedDbName;
144 sqlQuery += tableName;
145 sqlQuery +=
" LIMIT 0";
150 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