Storing Business Logic in Database

后端 未结 11 1435
感情败类
感情败类 2021-01-30 00:04

We want to write some business logic rules that work on top of certain data to build reports. Not sure which is the best to store them in the database MySQL.

11条回答
  •  臣服心动
    2021-01-30 00:13

    All I can give you is the way you should solve this problem, and not the answer itself.

    The general way to design a database to store complex data like this is to design the way you would keep them in memory as objects and then try and design the database accordingly. You will be evaluating the rules in a programming language after all. The procedure will be as follow: First the class diagram

    Class diagram

    Then it's time to convert it into an ERD:

    enter image description here

    Once you have a database structure to store/reload your object to/from, you can simply create your classes such that each object is responsible to load/store itself.

    [UPDATE]

    For instance if you want to store the statement a + b * -c into database, it could be translated as the following inserts:

    -- c
    INSERT INTO statement (statement_id) VALUES (1);
    INSERT INTO operand (statement_id, type) VALUES (1, 'double');
    -- - (minus)
    INSERT INTO statement (statement_id) VALUES (2);
    INSERT INTO operator (statement_id, type) VALUES (2, 'minus');
    -- -c
    INSERT INTO binary (operator_statement_id, operand_statement_id) VALUES (2, 1);
    -- b
    INSERT INTO statement (statement_id) VALUES (3);
    INSERT INTO operand (statement_id, type) VALUES (3, 'double');
    -- * (multiply)
    INSERT INTO statement (statement_id) VALUES (4);
    INSERT INTO operator (statement_id, type) VALUES (4, 'multiply');
    -- b * -c
    INSERT INTO unary (operator_statement_id, operand_statement_id1, operand_statement_id2) VALUES (4, 3, 2);
    -- a
    INSERT INTO statement (statement_id) VALUES (5);
    INSERT INTO operand (statement_id, type) VALUES (5, 'double');
    -- + (plus)
    INSERT INTO statement (statement_id) VALUES (6);
    INSERT INTO operator (statement_id, type) VALUES (6, 'sum');
    -- a + b * -c
    INSERT INTO unary (operator_statement_id, operand_statement_id1, operand_statement_id2) VALUES (6, 5, 4);
    

提交回复
热议问题