Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to use insert_ with ON CONFLICT #63

Open
i-am-the-slime opened this issue Jul 13, 2021 · 3 comments
Open

Is there a way to use insert_ with ON CONFLICT #63

i-am-the-slime opened this issue Jul 13, 2021 · 3 comments

Comments

@i-am-the-slime
Copy link

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

@Kamirus
Copy link
Owner

Kamirus commented Jul 13, 2021

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

genericShowInsert { ph } table rs =
let
cols = joinWith ", " $ tableColumnNames (Proxy Proxy rl)
len = rowListLength (Proxy Proxy rl)
placeholders = mkPlaceholders ph 1 len $ Array.length rs
in
["INSERT INTO ", tableName table, " (", cols, ") VALUES ", placeholders, ";"]
# joinWith ""

genericInsert_ { ph, exec } b table rs = do
let
q = genericShowInsert { ph } table rs
l = rs >>= hfoldl (RecordToArrayForeign b) ([] Array Foreign)
exec q l

GenericInsert BackendPGClass m t r where
genericInsert = genericInsert_ { exec, ph: "$" }
where
exec q l =
when (not $ null l) do
conn ← ask
PostgreSQL.PG.execute conn (PostgreSQL.Query q) l

insert_ = genericInsert (Proxy Proxy BackendSQLite3Class)

Let me know if you have any questions about the implementation

@i-am-the-slime
Copy link
Author

Thanks for the quick response.

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

@Kamirus
Copy link
Owner

Kamirus commented Jul 15, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants