Skip to content

HStore support #309

@saurabhnanda

Description

@saurabhnanda

Planning to raise a PR for this, once one of lpsmith/postgresql-simple#214 or lpsmith/postgresql-simple#215 is merged into PG-Simple.

However, in my development branch, I'm facing a very strange issue. Here's how I've defined the PGHStore data type:

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}

module Opaleye.PGHStore where

import Opaleye
import Data.Profunctor.Product.Default
import VacationLabs.Database.PostgreSQL.Simple.HStore
import Opaleye.Internal.RunQuery ()

data PGHStore

instance IsSqlType PGHStore where
  showPGType _ = "hstore"

pgHStore :: HStoreMap -> Column PGHStore
pgHStore h = unsafeCoerceColumn $ pgLazyByteString $ toLazyByteString (toHStore h)

instance QueryRunnerColumnDefault PGHStore HStoreMap where
  queryRunnerColumnDefault = fieldQueryRunnerColumn

instance Default Constant HStoreMap (Column PGHStore) where
  def = Constant pgHStore

A simple test fails:

testHStoreDB :: Connection -> Assertion
testHStoreDB conn = void $ withTransaction conn $ do
  _ <- execute_ conn "create table if not exists hstore_test(hstore_col hstore)"
  -- h <- generate (arbitrary :: Gen HStoreMap)
  let h = HStoreMap $ fromList $ [("a", "b")]
  (h_:_) <- runInsertManyReturning conn hstoreTestTable [constant h] Prelude.id
  -- _ <- execute conn "insert into hstore_test(hstore_col) values(?)" (Only h)
  assertEqual "not equal" h h_
  execute_ conn "drop table if exists hstore_test"

Generated SQL query:

INSERT INTO "hstore_test" ("hstore_col")
	VALUES (E'\\x2261223d3e226222')
	RETURNING "hstore_col"

Expected SQL query:

INSERT INTO "hstore_test" ("hstore_col") VALUES ('"a"=>"b"') RETURNING "hstore_col"

I've already validated that the pg-simple machinery is working almost correctly. So, I've done something really stupid one while defining PGHStore above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions