purescript-selda icon indicating copy to clipboard operation
purescript-selda copied to clipboard

Is there a way to use `insert_` with `ON CONFLICT`

Open i-am-the-slime opened this issue 2 years ago • 3 comments

I would like to end my query with ON CONFLICT DO NOTHING. Could there be a special insertOnConflict_ function or something?

i-am-the-slime avatar Jul 13 '21 17:07 i-am-the-slime

Unfortunately there's no easy way to add this variant. Customization of statements like INSERT is not yet possible. For now you can write your own insert operation. You'd need to write your alternative of genericShowInsert by adding ON CONFLICT DO NOTHING, and then use it just like insert_ does

https://github.com/Kamirus/purescript-selda/blob/ae8bf03413e24a53f474a9f7f198b1c7b11bf6ff/src/Selda/Query/ShowStatement.purs#L77-L84

https://github.com/Kamirus/purescript-selda/blob/e2c56b046d88d4c9462a1a7eb177c05de502ce47/src/Selda/Query/Class.purs#L62-L66

https://github.com/Kamirus/purescript-selda/blob/e2c56b046d88d4c9462a1a7eb177c05de502ce47/src/Selda/PG/Class.purs#L150-L156

https://github.com/Kamirus/purescript-selda/blob/e2c56b046d88d4c9462a1a7eb177c05de502ce47/src/Selda/SQLite3/Class.purs#L73

Let me know if you have any questions about the implementation

Kamirus avatar Jul 13 '21 21:07 Kamirus

Thanks for the quick response.

Please tell me this is not what you meant: https://gist.github.com/i-am-the-slime/9b22e12a3670ae2f9501ca7c908100a4

i-am-the-slime avatar Jul 15 '21 17:07 i-am-the-slime

Yes, it looks like it. Does it work for you?

If you care only about postgresql then you can drop the type class GenericInsertOnConflict and merge the instance with insertOnConflict_ , in your case that would simplify the code a bit

I don't know what is the best way to integrate this change into selda - probably easiest would be to expose these basic building blocks for query-string generation in genericShowInsert (e.g. via a continuation that would take tableName table, cols and placeholders and produce a string), So the issue can stay open for the time being

Kamirus avatar Jul 15 '21 18:07 Kamirus