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

[#19] Implement returningNamed #20

Closed
wants to merge 1 commit into from

Conversation

vrom911
Copy link
Contributor

@vrom911 vrom911 commented Jul 26, 2019

Resolves #19

By the docs returning should receive the list of qs, not a single row as execute, so I just put args into the list. I'm not sure if in such form it's fully valid. Please advice.

@vrom911 vrom911 added the enhancement New feature or request label Jul 26, 2019
@vrom911 vrom911 requested a review from chshersh July 26, 2019 10:19
@vrom911 vrom911 self-assigned this Jul 26, 2019
@vrom911 vrom911 requested a review from arbus July 26, 2019 10:21
-> m [res] -- ^ Returning result
returningNamed conn qNamed params =
withNamedArgs qNamed params >>= \(q, actions) ->
liftIO $ PG.returning conn q [toList actions]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vrom911 Yeah, this is a difficult problem. The same problem is for the executeMany function. That's why we didn't manage to implement it in the mysql-not-so-simple library. The problem is in the fact, as you correctly noticed, that returning and executeMany work with multiple rows, not a single one. So they allow to INSERT/UPDATE many values at once. And with plain returning we would write code like this:

returning conn [sql|...|] [User "foo" 42, User "bar" 69]

But with named version we need to write like this:

```haskell
returning conn [sql|...|] [ ["name" =? "foo", age =? 42], ["name" =? "bar", age =? 69] ]

The only way to implement this that I managed to think of is to pass a function a -> [NamedParam] as an argument so the calls will look like this:

returningNamed
    conn
    [sql|...|] 
    (\User{..} -> [ "name" =? userName, "age" =? userAge ])
    someUsers

But I'm not sure whether this is actually works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another possible way could be:

returningNamed
    conn
    [sql|...|]
    [ "name" =? [user1, user2]
    , "age" =? [age1, age2]
    ]

Slightly easier but introduces an illegal state where the lists are not of the same length.

@vrom911 vrom911 closed this Oct 7, 2019
@vrom911 vrom911 deleted the vrom911/19-Implement-returningNamed branch October 7, 2019 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement returningNamed
3 participants