评估计算字符串:“ 2 * 3 + 4 * 5”;在Postgresql中 [英] Eval Calculation-string: "2 * 3 + 4 * 5" in Postgresql
本文介绍了评估计算字符串:“ 2 * 3 + 4 * 5”;在Postgresql中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Postgres中评估纯计算字符串。
I would like to evaluate a pure-calculation string in Postgres.
例如: eval(' 234 + 65 * 3')
该函数不是常量,因此也可以简单地是 2 + 2
The function is NOT constant, so could also simply be 2 + 2
期望类似于 SELECT eval('2 + 2')AS结果
我读到有关 eval()
函数的巨大安全问题,但这些问题似乎包含了 SELECT
语句。这里是纯计算要求。
I read about huge security issues regarding eval()
functions, but those seem to incorporate SELECT
statements. Here pure calculation requirements.
推荐答案
您需要 PL / pgSQL
:
create or replace function f(_s text)
returns numeric as $$
declare i numeric;
begin
execute format('select %s', _s) into i;
return i;
end;
$$ language plpgsql;
select f('1 + 1');
f
---
2
这篇关于评估计算字符串:“ 2 * 3 + 4 * 5”;在Postgresql中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文