nikita-volkov / hasql-th

Template Haskell utilities for Hasql
http://hackage.haskell.org/package/hasql-th
MIT License
112 stars 8 forks source link

Inserting multiple records (example request) #28

Closed thales-maciel closed 1 year ago

thales-maciel commented 1 year ago

I have the following Statement:

insertX :: Statement (Int16, Int16, Int32, Int32) Int32
insertX =
  [TH.singletonStatement|
    insert into xs (a, b, c)
    values ($1 :: int2, $2 :: int2, $3 :: int4)
    returning id :: int4
    |]

how can I insert multiple Xs in the same Statement? I guess I'm missing something

nikita-volkov commented 1 year ago

See https://hackage.haskell.org/package/hasql-1.6.3/docs/Hasql-Statement.html#g:2

gfarrell commented 1 year ago

@nikita-volkov when I try to use unnest as per the hasql documentation but with hasql-th I get errors around missing instances in vector (I posted on SO about this).

MVE:

testQuery :: [Text] -> Session [Integer]
testQuery =
  flip statement $
    dimap 
      id
      (toList . fmap toInteger)
      [vectorStatement|
        insert into mytable (
          title
        ) (
          select * from unnest($1 :: text[])
        ) returning (id :: int4)
      |]

The error I get is:

• No instance for (Data.Vector.Generic.Base.Vector [] Text)
       arising from a use of ‘Data.Vector.Generic.foldl'’
   • In the first argument of ‘hasql-1.6.3:Hasql.Private.Encoders.dimension’, namely
       ‘Data.Vector.Generic.foldl'’
     In the first argument of ‘hasql-1.6.3:Hasql.Private.Encoders.array’, namely
       ‘((hasql-1.6.3:Hasql.Private.Encoders.dimension
            Data.Vector.Generic.foldl')
           (hasql-1.6.3:Hasql.Private.Encoders.element
              (hasql-1.6.3:Hasql.Private.Encoders.nonNullable
                 hasql-1.6.3:Hasql.Private.Encoders.text)))’
     In the first argument of ‘hasql-1.6.3:Hasql.Private.Encoders.nonNullable’, namely
       ‘(hasql-1.6.3:Hasql.Private.Encoders.array
           ((hasql-1.6.3:Hasql.Private.Encoders.dimension
               Data.Vector.Generic.foldl')
              (hasql-1.6.3:Hasql.Private.Encoders.element
                 (hasql-1.6.3:Hasql.Private.Encoders.nonNullable
                    hasql-1.6.3:Hasql.Private.Encoders.text))))’
gfarrell commented 1 year ago

Ah nvm it's because I'm mapping using a list not a vector.