ygrek / sqlgg

SQL Guided (code) Generator
https://ygrek.org/p/sqlgg/
GNU General Public License v2.0
62 stars 20 forks source link

Reuse inferred params types #119

Closed jongleb closed 11 months ago

jongleb commented 11 months ago

We let users work with a variable as if it has multiple types.

For example:

Our schema:

-- @create_some_table
CREATE TABLE IF NOT EXISTS `some_table` (
 `id` int NOT NULL,
 `some_field` TEXT NULL,
 `some_field_2` TEXT NULL DEFAULT NULL
) 

Our query

-- @create
INSERT INTO some_table (
  some_field,
  some_field_2
) VALUES (
  @some_field,
  case when @some_field = 'TEST' then 'A' else NULL end
);

And in this case it generates:

  let create db ~some_field =
    let set_params stmt =
      let p = T.start_params stmt (19) in
      begin match some_field with None -> T.set_param_null p | Some v -> T.set_param_Text p v end;
      T.set_param_Text p some_field; 
      T.finish_params p
    in
    T.execute db ("INSERT INTO some_table (\n\
 some_field,
  some_field_2
) VALUES (
  ?,\n\
  ?,\n\
        case when ? like 'TEST' then 'A' else NULL end,\n\
)") set_params

This PR fixes it.

ygrek commented 11 months ago

example shown is already fixed in null branch but this helped uncover another problem so overall the change is not needed but thanks :)