cbmi/harvest-vocab — comment on commit 8ba55a3a6bfcc562d463e2da4cb1d0d895638db3
File:  line 0
By bruth:

A few things:

- What happens when `values` is empty?
- Table and column names must be quoted to ensure there are no conflicts with SQL reserved words
- There are backend specific ways to do it, so you might need to look around here: https://github.com/django/django/tree/master/django/db/backends

Also, think about the variables between these three operations:
- The `CASE` statement boolean values
- The aggregated value for the `WHERE` clause
- The logical operator between `WHERE` conditions

Knowing these three things allows for defining a SQL _template_ string to be used which can use these variables at runtime. This is mainly for clarity of the implementation. For example:

```python
SELECT * FROM (
SELECT %{target_column} AS id, %{case_statements} FROM %{through_table} GROUP BY %{target_column}
) AS T WHERE %{conditions}
```

Within constructing the `n`-length variables (i.e. case statements and conditions) the variables mentioned above can be used in sub templates, e.g:

```python
case_statement_template = 'SUM(CASE WHEN %{source_column} = %{value} THEN %{true_value} ELSE %{false_value} END) AS %{column_alias}'
```