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