Lightweight 0.20260921.0
Loading...
Searching...
No Matches
TableFilter.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include "../Api.hpp"
5
6#include <optional>
7#include <string>
8#include <string_view>
9#include <vector>
10
11namespace Lightweight::SqlBackup
12{
13
14#if defined(_MSC_VER)
15 #pragma warning(push)
16 #pragma warning(disable : 4251) // STL types in DLL interface
17#endif
18
19/// @ingroup Backup
20/// Filters tables by name patterns with glob-style wildcards.
21///
22/// Supports:
23/// - Exact table names: "Users", "Products"
24/// - Wildcard suffix: "User*" (matches UserAccounts, Users, etc.)
25/// - Wildcard prefix: "*_log" (matches audit_log, error_log, etc.)
26/// - Wildcard anywhere: "*audit*" (matches any table containing "audit")
27/// - Single char wildcard: "User?" (matches Users, User1, etc.)
28/// - Schema.table notation: "dbo.Users", "sales.*"
29/// - Comma-separated patterns: "Users,Products,*_log"
30class LIGHTWEIGHT_API TableFilter
31{
32 public:
33 /// Parses a filter specification string.
34 ///
35 /// @param filterSpec Comma-separated patterns like "table1,table2,foo*,schema.table"
36 /// Empty string or "*" means match all tables.
37 static TableFilter Parse(std::string_view filterSpec);
38
39 /// Checks if a table matches any of the patterns.
40 ///
41 /// @param schema The schema name (can be empty).
42 /// @param tableName The table name.
43 /// @return true if the table matches at least one pattern.
44 [[nodiscard]] bool Matches(std::string_view schema, std::string_view tableName) const;
45
46 /// Returns true if the filter matches all tables (no filtering applied).
47 [[nodiscard]] bool MatchesAll() const noexcept
48 {
49 return _matchesAll;
50 }
51
52 /// Returns the number of patterns in this filter.
53 [[nodiscard]] size_t PatternCount() const noexcept
54 {
55 return _patterns.size();
56 }
57
58 private:
59 struct Pattern
60 {
61 std::optional<std::string> schema; ///< Schema pattern (nullopt = any schema)
62 std::string table; ///< Table name pattern
63 };
64
65 std::vector<Pattern> _patterns;
66 bool _matchesAll = true;
67
68 /// Performs glob-style pattern matching.
69 ///
70 /// @param pattern The pattern with * and ? wildcards.
71 /// @param text The text to match against.
72 /// @return true if text matches the pattern.
73 static bool GlobMatch(std::string_view pattern, std::string_view text);
74};
75
76#if defined(_MSC_VER)
77 #pragma warning(pop)
78#endif
79
80} // namespace Lightweight::SqlBackup
static TableFilter Parse(std::string_view filterSpec)
bool MatchesAll() const noexcept
Returns true if the filter matches all tables (no filtering applied).
size_t PatternCount() const noexcept
Returns the number of patterns in this filter.
bool Matches(std::string_view schema, std::string_view tableName) const