如何在EAV模型中将字符串比较为int

时间:2019-02-03 11:25:37

标签: sql postgresql hibernate spring-data-jpa entity-attribute-value

我已经将EAV db模式实现为postgres中poc的一部分。这是下面的插图:

how do I check whether an attribute with label = 'marks12' has value <
100

现在,我面临的问题是在比较联接表中的值时。 例如,

value::int

比较gender并不能解决问题,因为还有其他属性,例如int无法转换为this.props.match.params.beerId值。 如何在上述eav设计模型中执行这种以价值为中心的条件。

PS:我对其他允许在RDBMS中映射/存储动态属性的数据库设计持开放态度。

1 个答案:

答案 0 :(得分:0)

您需要一个dynamic command.函数才能完成工作:

create or replace function eval_condition(attr text, a_type text, op text, val text)
returns boolean language plpgsql immutable as $$
declare
    result boolean;
begin
    execute format(
        'select %L::%s %s %L::%s',
        attr, a_type, op, val, a_type)
    into result;
    return result;
end $$;

示例:

with example(attr, a_type, op, val) as (
values
    ('100',        'int',     '>',    '50'),
    ('2019-01-01', 'date',    '>',    '2019-01-02'),
    ('some text',  'text',    'like', 'some%'),
    ('99.99',      'numeric', '<',    '99.99')
)
select
    attr, a_type, op, val,
    eval_condition(attr, a_type, op, val)
from example;

    attr    | a_type  |  op  |    val     | eval_condition 
------------+---------+------+------------+----------------
 100        | int     | >    | 50         | t
 2019-01-01 | date    | >    | 2019-01-02 | f
 some text  | text    | like | some%      | t
 99.99      | numeric | <    | 99.99      | f
(4 rows)    
相关问题