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

No instance for ToField Cursor #113

Open
CGenie opened this issue May 8, 2023 · 3 comments
Open

No instance for ToField Cursor #113

CGenie opened this issue May 8, 2023 · 3 comments

Comments

@CGenie
Copy link

CGenie commented May 8, 2023

Hello,

I'd like to use cursors to perform an update of a very large table. Postgres allows for this kind of query:

UPDATE foo SET dataval = myval WHERE CURRENT OF curs1;

(see https://www.postgresql.org/docs/current/plpgsql-cursors.html#PLPGSQL-CURSOR-USING)

However, I can't just do

execute [sql| UPDATE foo SET dataval = ? WHERE CURRENT OF ?|] ( myval, cursor )

because Cursor doesn't have a ToField instance. Also, I see that only the datatype of Cursor is exported

https://hackage.haskell.org/package/postgresql-simple-0.6.5/docs/src/Database.PostgreSQL.Simple.Cursor.html#declareCursor

and not the constructor, so I can't access the name field.

@CGenie
Copy link
Author

CGenie commented May 8, 2023

I was able to successfully run the above query with this instance:

import qualified Data.Text.Encoding as T

instance ToField Cursor where
  toField (Cursor name _conn) = toField $ Identifier $ T.decodeUtf8 $ fromQuery name

@CGenie
Copy link
Author

CGenie commented May 9, 2023

As a side note, it would be nice if the declareCursor function allowed to accept query parameters as well.

CGenie added a commit to CGenie/postgresql-simple that referenced this issue May 10, 2023
@CGenie
Copy link
Author

CGenie commented May 10, 2023

This branch seems to work for me: https://github.com/CGenie/postgresql-simple/tree/cursor-to-field

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

1 participant