1 #ifndef ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
2 #define ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
17 template <
typename ALLOC>
21 using Statement = std::unique_ptr<sqlite3_stmt, SqliteFinalizer>;
34 using TableSchema = std::map<string_type, ColumnDescription, std::less<string_type>,
53 sqlQuery +=
"SELECT count(*) FROM ";
54 if (!attachedDbName.
empty())
56 sqlQuery += attachedDbName;
59 sqlQuery += tableName;
62 const int result = sqlite3_step(statement.get());
63 if (result != SQLITE_ROW)
65 throw SqliteException(
"ValidationSqliteUtils.getNumberOfTableRows: sqlite3_step() failed: ")
69 return static_cast<size_t>(sqlite3_column_int64(statement.get(), 0));
85 sqlQuery +=
"PRAGMA ";
86 if (!attachedDbName.
empty())
88 sqlQuery += attachedDbName;
91 sqlQuery +=
"table_info(";
92 sqlQuery += tableName;
97 int result = SQLITE_OK;
98 while ((result = sqlite3_step(statement.get())) == SQLITE_ROW)
100 const char* columnName =
reinterpret_cast<const char*
>(sqlite3_column_text(statement.get(), 1));
101 const char* columnType =
reinterpret_cast<const char*
>(sqlite3_column_text(statement.get(), 2));
102 tableSchema.emplace(
string_type(columnName, allocator),
105 sqlite3_column_int(statement.get(), 3) != 0,
106 sqlite3_column_int(statement.get(), 5) != 0
110 if (result != SQLITE_DONE)
112 throw SqliteException(
"ValidationSqliteUtils.getTableSchema: sqlite3_step() failed: ")
131 StringView columnName,
const ALLOC& allocator)
135 sqlQuery +=
"SELECT ";
136 sqlQuery += columnName;
137 sqlQuery +=
" FROM ";
138 if (!attachedDbName.
empty())
140 sqlQuery += attachedDbName;
143 sqlQuery += tableName;
144 sqlQuery +=
" LIMIT 0";
149 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