I’m trying to create a procedure in MySQL/MariaDB with optional filters for each participating table. Assuming the tables are $A, B, C, ldots$, each table may or may not be filtered, and the result is that of each table having their respective optional filters applied. $A$ and $B$ have the relation `A (1) -> (*) B`

, and similarly, `B (1) -> (*) C`

and so on.

With that, I’ve thought of the following function to filter & join each pair of tables:

```
/*
F(): filter
L: left side
R: right side
*/
if(L.filter is NULL and R.filter is NULL) {
return fullJoin(F(L), F(R))
} else if(L.filter not NULL and R.filter is NULL) {
return leftJoin(F(L), F(R))
} else if(L.filter is NULL and R.filter not NULL) {
return rightJoin(F(L), F(R))
} else { // both not NULL
return innerJoin(F(L), F(R))
}
```

The filter function `F()`

would be something like `SELECT * FROM table WHERE input == NULL or input == column`

. The function is first applied to 4 tables as follows:

```
fn(fn(fn(A,B),C),D)
```

where the result of said function `fn`

is considered to have a `not NULL`

filter.

Additionally, I think the function `fn`

can be packed into the following:

```
SELECT * FROM L FULL JOIN R
WHERE (R.input == NULL || R.input = R.column)
AND (L.input == NULL || L.input = L.column)
```

Is my function correct?