maxscale使用awk重写过滤器。

huangapple go评论60阅读模式
英文:

maxscale rewrite filter with awk

问题

以下是翻译好的部分:

以下的重写规则按预期工作:

%%
regex_grammar: Awk
case_sensitive: false
ignore_whitespace: true
%
SELECT msg FROM mytable WHERE id = 123
%
SELECT msg FROM mytable WHERE id = sha1(123)

现在我的问题是,如何在 regex_grammar: Awk 中使用占位符编写此查询。如果我知道这一点,我将在下一个问题上有所突破。我的实际目标是重写以下查询:

Input:
SELECT msg FROM mytable WHERE id IN (123,456,769)

Output:
SELECT msg FROM mytable WHERE id IN (sha1(123),sha1(456),sha1(769))

我假设这只适用于 regex_grammar: Awk 而不适用于 Native。我是对的吗?

这是我找到的唯一文档:https://mariadb.com/kb/en/mariadb-maxscale-2208-rewrite-filter/

我也接受其他建议或工具!Maxscale 对我来说似乎是最有希望的解决方案。

英文:

The following rewrite rule works as expected:

%%
regex_grammar: Awk
case_sensitive: false
ignore_whitespace: true
%
SELECT msg FROM mytable WHERE id = 123
%
SELECT msg FROM mytable WHERE id = sha1(123)

My question now is, how can I write this query with placeholder in regex_grammer: Awk. If I knew that, I would have a breakthrough for the next problem. My actual goal is to rewrite the following query:

Input:
SELECT msg FROM mytable WHERE id IN (123,456,769)

Output:
SELECT msg FROM mytable WHERE id IN (sha1(123),sha1(456),sha1(769))

I assume that this only works with regex_grammar Awk and not with Native. Am I right?

This is the only documentation that I found: https://mariadb.com/kb/en/mariadb-maxscale-2208-rewrite-filter/

I also accept other suggestions or tools! Maxscale looked the most promising solution to me.

答案1

得分: 0

你正在进行的替换类型需要进行重复的替换,而替换的值数量是不固定的。重写过滤器适用于更简单的用例,其中模式是固定的,不需要复杂的匹配。

然而,你仍然可以使用MaxScale中的regexfilter来完成这个任务,它允许更自由地使用正则表达式。以下是应该处理IN列表中的简单值并将它们包装在SHA1函数调用中的regexfilter配置:

[regex]
type=filter
module=regexfilter
match=/(?i)(IN\s+\(|,)\s*([^,]+)\s*/
replace=$1 SHA1($2)

这是我用来测试的regex101.com页面。

请注意,这不适用于包含嵌入逗号的字符串,这意味着它并不是一个通用的解决方案。

英文:

The type of replacement you're doing requires repeated substitutions with no fixed amount of values. The rewrite filter is intended for simpler use-cases where the patterns are fixed and do not require complex matching.

However, this can still be done with the regexfilter in MaxScale that allows more free-form use of regular expressions. The following regexfilter configuration should handle simple values in an IN list and wrap them in a SHA1 function call.

[regex]
type=filter
module=regexfilter
match=/(?i)(IN\s+\(|,)\s*([^,]+)\s*/
replace=$1 SHA1($2)

Here's the regex101.com page I used to test it.

Note that this won't work with strings with embedded commas in them which means it's not really an universal solution.

huangapple
  • 本文由 发表于 2023年7月4日 21:55:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定