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 param_types in the usage below the body #118

Closed jongleb closed 11 months ago

jongleb commented 1 year 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.

Khady commented 1 year ago

could you please add a test?

jongleb commented 1 year ago

could you please add a test?

Done!