Store expression trees in a database

I need to store logical conditions in a database. for example: (condition1 || condition2) && condition3 should be stored in the database.

I plan to create a table [ ExpressionTree ] to handle the structure:

 Id condition combinationId nextId(FK->[Condition2Combination.Id]) operator (AND, OR, null) 

If (condition 1 || condition2) && condition3 in the [ExpressionTree] table, the entries should be:

 Id conditionId combinationId nextId operator 1 condition1 combination1 2 OR 2 condition2 combination1 3 AND 3 condition3 combination1 null null 

But the decision is not good, what is your suggestion? Thanks!

+6
java design database database-design
source share
2 answers

If you were not actually executing database queries on expression trees, I would be inclined to store the expressions as a text column ... and parse them on the client side as needed.

+8
source share

I do not see a tree structure in your decision. In particular, I don't think your nextId column is capable of handling expressions that have parentheses. But I could have missed something.

I suggest you take a look at the method of expressing tree structures, known as the nested set method. In this method, the "next identifier" column is replaced by two columns called "left id" and "right id", which is able to express which subitems are inside other subtrees. This is a more simplified summary of what you are about to find.

Using nested sets, it is easy to get a query that shows a subtree under any given node, or the path from any given node to the root.

Why is this data in the database?

+2
source share

All Articles