tizoc / ppx_pgsql

Syntax extension for embedded SQL queries using PG'OCaml.
BSD 3-Clause "New" or "Revised" License
52 stars 9 forks source link

Allow generating functions with parameters of option type instead of optional parameters. #10

Closed SylvainBoilard closed 4 years ago

SylvainBoilard commented 4 years ago

Hi,

I find using functions with optional parameters very unwieldy when it is not known in advance whether a parameter will be null or not, for example when parsing a JSON file where some values may be entirely omitted.

Currently I have resorted to changing the type of the generated functions with Obj.magic by turning any optional argument into a "mandatory" named argument with an option type (fortunately this works), and I think this is actually the best solution for me in the current state of affairs: I can then simply use None as parameter when inserting a record with a NULL value, and I can do the actual insertion in one line of code.

It would be great if I had the option to tell ppx_pgsql to directly generate functions that way.

Cheers,

tizoc commented 4 years ago

Hi @SylvainBoilard, can you give me a concrete example? not sure I follow. Doesn't using $?name placeholder variables in the query and passing optional arguments as func ?optional ~other solve your problem?

SylvainBoilard commented 4 years ago

It does actually, I didn’t know I could use options using ?label instead of ~label when calling a function with optional arguments.

Thank you for your answer, and sorry for the noise.

tizoc commented 4 years ago

No worries! glad that helped.