Storing conditional logic expressions/rules in a database

前端 未结 5 634
说谎
说谎 2021-02-05 17:13

How can I store logical expressions using a RDBMS?

I tag objects and would like to be able to build truth statements based on those tags. (These might be considered as

5条回答
  •  不知归路
    2021-02-05 17:37

    I would use one table

    tags(id,name,type,expression,order)
    
    • type would show if the tag is normal or calculated.
    • order is reordered if you add new calculated tags, it specifies the order of the calculation for these tags...
    • expression is parsed and checked before inserting a row, it could also be built using a GUI (something like how Oracle discoverer does these things).
    • You only link the normal tags to the items

    For your example second-hand-goods needs to be calculated before second-hand-offer, all the others can be calculated without any dependencies.

    1,'new',1,'',NULL
    2,'for_sale',1,'',NULL
    3,'used',1,'',NULL
    4,'offer',1,'',NULL
    5,'second_hand_goods',2,'(!new or used) and for_sale',1
    6,'new_offer',2,'new and offer',1
    7,'second_hand_offer',2,'second_hand_goods and offer',2
    

    An item could be tagged by only for_sale, calculating would give:

    second_hand_goods,second_hand_offer
    

    I would have a function that gives a list of all the tags for the item, including direct tags and calculated ones:

    for_sale,second_hand_goods,second_hand_offer
    

提交回复
热议问题