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>;
52 string_type sqlQuery(allocator);
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));
84 string_type sqlQuery(allocator);
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),
107 sqlite3_column_int(statement.get(), 3) != 0,
108 sqlite3_column_int(statement.get(), 5) != 0
112 if (result != SQLITE_DONE)
114 throw SqliteException(
"ValidationSqliteUtils.getTableSchema: sqlite3_step() failed: ") <<
136 string_type sqlQuery(allocator);
137 sqlQuery +=
"SELECT ";
138 sqlQuery += columnName;
139 sqlQuery +=
" FROM ";
140 if (!attachedDbName.
empty())
142 sqlQuery += attachedDbName;
145 sqlQuery += tableName;
146 sqlQuery +=
" LIMIT 0";
151 return sqlite3_step(statement.get()) == SQLITE_DONE;
186 #endif // ZSERIO_VALIDATION_SQLITE_UTIL_H_INC
std::map< string_type, ColumnDescription, std::less< string_type >, RebindAlloc< ALLOC, std::pair< const string_type, ColumnDescription >>> TableSchema
sqlite3_stmt * prepareStatement(StringView sqlQuery)
static const char * sqliteColumnTypeName(int columnType)
static size_t getNumberOfTableRows(SqliteConnection &connection, StringView attachedDbName, StringView tableName, const ALLOC &allocator)
std::map< KEY, T, COMPARE, PropagatingPolymorphicAllocator< std::pair< const KEY, T >>> map
std::basic_string< char, std::char_traits< char >, RebindAlloc< ALLOC, char >> string
constexpr bool empty() const noexcept
static void getTableSchema(SqliteConnection &connection, StringView attachedDbName, StringView tableName, TableSchema &tableSchema, const ALLOC &allocator)
static bool isColumnInTable(SqliteConnection &connection, StringView attachedDbName, StringView tableName, StringView columnName, const ALLOC &allocator)
std::unique_ptr< sqlite3_stmt, SqliteFinalizer > Statement
typename std::allocator_traits< ALLOC >::template rebind_alloc< T > RebindAlloc
string< ALLOC > string_type