Is it possible in PL / pgSQL to evaluate a string as an expression rather than an instruction?

I have two database tables:

# \d table_1
      Table "public.table_1"
   Column   |  Type   | Modifiers
------------+---------+-----------
 id         | integer |
 value      | integer |
 date_one   | date    |
 date_two   | date    |
 date_three | date    |

# \d table_2
     Table "public.table_2"
   Column   |  Type   | Modifiers
------------+---------+-----------
 id         | integer |
 table_1_id | integer |
 selector   | text    |

The values ​​in table_2.selectorcan be one of one, twoor threeand are used to select one of the date columns in table_1.

In my first implementation I used CASE:

SELECT value
FROM table_1
INNER JOIN table_2 ON table_2.table_1_id = table_1.id
WHERE CASE table_2.selector
      WHEN 'one' THEN
        table_1.date_one
      WHEN 'two' THEN
        table_1.date_two
      WHEN 'three' THEN
        table_1.date_three
      ELSE
        table_1.date_one
      END BETWEEN ? AND ?

The values ​​for selectorare such that I could identify the column of interest as eval(date_#{table_2.selector})if PL / pgSQL allows me to evaluate rows as expressions.

The closest I could find is EXECUTE stringone that evaluates whole statements. Is there any way to evaluate expressions?

+4
source share
1 answer

plpgsql . , . , .

- . , , .

0

All Articles