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

Add Any type to handle values without knowing their types #138

Open
zouppen opened this issue May 17, 2024 · 0 comments
Open

Add Any type to handle values without knowing their types #138

zouppen opened this issue May 17, 2024 · 0 comments

Comments

@zouppen
Copy link

zouppen commented May 17, 2024

I have a code which I'd like to keep compatible if key type changes. For example the primary key of a table can be changed from integer to text without any code changes and I can use the value to insert rows to another table which references that column by doing only database schema changes without affecting the code.

I made a small module to demonstrate the usage. If you'd like to include it to the library, I can make a PR for it.

-- |Implements type which is unparsed. The Any type is intentionally
-- opaque to prevent SQL injections. It's primary purpose is to get a
-- value which is used for future inserts without knowing anything
-- about its type.
module Any (Any) where

import Data.ByteString (ByteString)
import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.FromField
import Database.PostgreSQL.Simple.ToField
import Data.ByteString.Builder (byteString)

newtype Any = Any ByteString

instance FromField Any where
  fromField f Nothing = returnError ConversionFailed f ""
  fromField _ (Just bs) = pure $ Any bs

instance ToField Any where
  toField (Any bs) = Plain (byteString bs)
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