Lightweight 0.20251202.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 { return _matchesAll; }
47
48 /// Returns the number of patterns in this filter.
49 [[nodiscard]] size_t PatternCount() const noexcept { return _patterns.size(); }
50
51 private:
52 struct Pattern
53 {
54 std::optional<std::string> schema; ///< Schema pattern (nullopt = any schema)
55 std::string table; ///< Table name pattern
56 };
57
58 std::vector<Pattern> _patterns;
59 bool _matchesAll = true;
60
61 /// Performs glob-style pattern matching.
62 ///
63 /// @param pattern The pattern with * and ? wildcards.
64 /// @param text The text to match against.
65 /// @return true if text matches the pattern.
66 static bool GlobMatch(std::string_view pattern, std::string_view text);
67};
68
69#if defined(_MSC_VER)
70#pragma warning(pop)
71#endif
72
73} // 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