Skip to content
Advertisement

How to retrieve the properties stored in SQL with multiple inheritance

I’m storing the records in SQL that represent a multiple inheritance relationship similar to the one in C++. Like that:

The classes have properties of two types. These properties are inherited by the classes, but in different ways. The first type type of property whenever defined for the class overrides the value of the same property used in any of base classes. The other type accumulates the value: the property is actually a set of values, each class inherits all values of it’s base classes, plus may add an additional (single) value to this set:

The caveat with OverridableValues: there are no cases when the same property is overridden on different paths of multiple inheritance.

I’m trying to design queries using common table expressions that would return the value/values for a given property and class.

The approach that I’m trying to use is to start from the root (assume for simplicity that there is a single root class), and then to build the tree of paths from the root to every other class. The problem is how to pass the information about properties from the parents to children. For example below is an incorrect attempt to do that:

I feel like I need one more UNION ALL inside the CTE, which is not allowed. But without it I either miss proper values or inherited ones. So far I’ve failed to design the query for both types of properties.

I’m using SQLite as my database engine.

Advertisement

Answer

Finally I’ve found a solution. I’m describing it below, but more efficient ones are still welcomed.

Let’s start with the Accumulable property. My problem was that I tried to add more than one UNION ALL into a single CTE. I’ve solved that with adding additional CTE (see the AcquiresFrom)

The AcquiresFrom means the way to aquire the value: the class either introduces a new value (the first clause) or to inherits it (the second clause). The ClassProperty incrementally propagates the values from base classes to derived. The only thing left to do is to eliminate duplicates and NULL values (the last clause SELECT DISTINCT / WHERE value IS NOT NULL).

The overridable property is more complex.

The Roots is obviously the list of classes that have no parents. The PossibleValues CTE propagates/overrides the values from roots to final classes, and breaks multiple inheritance cycles making the structure a tree-like. All valid id/value pairs are present in the result of this query, however some invalid values are present as well. These invalid values are those that were overridden on one of the branches, but this fact is not known on another branch. The acquired_from_id allows us to reconstruct who was that class that first introduced this value (that may be useful whenever two different classes intruduce the same value).

The last thing left is to resolve the ambiguity caused by multiple inheritance. Knowing the class and two possible values we need to know whether one value overrides the other. That is resolved with the Ancestors expression.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement