logo
down
shadow

Can Haskell's Opaleye DSL generate any given SQL?


Can Haskell's Opaleye DSL generate any given SQL?

By : Douglas N. BK
Date : November 21 2020, 03:00 PM
wish helps you According to the Opaleye tutorial, creating tables and databases is currently unsupported:
code :


Share : facebook icon twitter icon
Opaleye query by String to Maybe

Opaleye query by String to Maybe


By : maryam b
Date : March 29 2020, 07:55 AM
Hope that helps I'm not familiar with OpalEye, but I think you might just need a judicious use of listToMaybe (import Data.Maybe (listToMaybe))
code :
getUserByIden :: String -> PGS.Connection -> IO (Maybe User)
getUserByIden iden c = do
  listToMaybe <$> runQuery c (userByIdenQuery $ pgString iden)
Opaleye newtype

Opaleye newtype


By : Namuwaya Betty Mebra
Date : March 29 2020, 07:55 AM
this one helps.
the issue appears to be that Opaleye doesn't know how to convert ItemId into a PGUuid but I know it can convert UUID to PGUuid
code :
instance O.QueryRunnerColumnDefault O.PGUuid ItemId where
  queryRunnerColumnDefault =
         O.queryRunnerColumn id ItemId queryRunnerColumnDefault
newtype ItemId' a = ItemId a
$(makeAdaptorAndInstance "pItemId" ''ItemId')
Left join in Opaleye

Left join in Opaleye


By : hellostarfish
Date : March 29 2020, 07:55 AM
Hope that helps There are a couple of misunderstandings here. I produced a full working version below.
Firstly, the return type of the leftJoin is not
code :
Query (ModelAColumn, Maybe ModelBColumn)
type ModelBNullableColumn = ModelB' (Column (Nullable PGUuid))
                                    (Column (Nullable PGText))
Query (ModelAColumn, ModelBNullableColumn)
IO [(ModelA, Maybe ModelB)]
type ModelBMaybe = ModelB' (Maybe UUID) (Maybe String)
IO [(ModelA, ModelBMaybe)]
ModelA { tableColumn "uuid", tableColumn "foreign" }
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}

import           Opaleye hiding (table)
import qualified Opaleye
import Data.Profunctor.Product.TH
import Database.PostgreSQL.Simple hiding (Query)
import Data.UUID

data ModelA' a b = ModelA { primA :: a, foreignA :: b }
type ModelA = ModelA' UUID (Maybe UUID)
type ModelAColumn = ModelA' (Column PGUuid) (Column (Nullable PGUuid))

$(makeAdaptorAndInstance "pModelA" ''ModelA')

modelAtable :: Table ModelAColumn ModelAColumn
modelAtable = Opaleye.table "model_a" $ pModelA ModelA { primA = tableColumn "uuid", foreignA = tableColumn "foreign" }

data ModelB' a b = ModelB { primB :: a, valB :: b }
type ModelB = ModelB' UUID String
type ModelBMaybe = ModelB' (Maybe UUID) (Maybe String)
type ModelBColumn = ModelB' (Column PGUuid) (Column PGText)
type ModelBNullableColumn = ModelB' (Column (Nullable PGUuid)) (Column (Nullable PGText))

$(makeAdaptorAndInstance "pModelB" ''ModelB')

modelBtable :: Table ModelBColumn ModelBColumn
modelBtable = Opaleye.table "model_b" $ pModelB ModelB { primB = tableColumn "uuid", valB = tableColumn "val" }

doJoin :: Connection -> IO [(ModelA, ModelBMaybe)]
doJoin conn = runQuery conn query
  where
    query :: Query (ModelAColumn, ModelBNullableColumn)
    query = leftJoin (queryTable modelAtable) (queryTable modelBtable) (\(ma, mb) -> matchNullable (pgBool False) (.== primB mb) (foreignA ma))

main :: IO ()
main = return ()
Declaring an Opaleye table without using TemplateHaskell

Declaring an Opaleye table without using TemplateHaskell


By : Juan C R
Date : March 29 2020, 07:55 AM
this one helps. GHC has a -ddump-splices option to see the code generated with TH. I think that should be useful as it probably doesn't look too bad. (With -ddump-to-file and -dumpdir to control the output location.)
Here's one way to write it:
code :
instance (ProductProfunctor p, Default p a a', Default p b b') => Default p (Birthday' a b) (Birthday' a' b') where
  def :: p (Birthday' a b) (Birthday' a' b')
  def = pBirthday (Birthday def def)


pBirthday :: ProductProfunctor p =>
  Birthday' (p a a') (p b b') -> p (Birthday a b) (Birthday a' b')
pBirthday (Birthday pa pb) =
  Birthday `rmap` lmap bdName pa **** lmap bdDay pb
  -- It generalizes the applicative construct
  --   "Birthday <$> pa <*> pb"
leftJoinF in Opaleye and IfPP

leftJoinF in Opaleye and IfPP


By : joe
Date : March 29 2020, 07:55 AM
this one helps. You can't return a Maybe out of a functional join. Functional join allows you to avoid Nullable in the sense that it hides the IS NULL check from you. It doesn't allow you to "lift the nullability" onto the Haskell side.
Related Posts Related Posts :
  • Understanding Why MVar Isn't Updating?
  • Traverse/Rewrite a JSON Value
  • Threepenny-gui: get file path via 'file' input
  • How can I return a list from list of list Haskell
  • Making Monad type showable
  • The usage of monad transformers
  • How to create a generic Complex type in haskell?
  • EAN13 Barcode Encoding in Haskell: How to test it?
  • How to implement the Arbitrary instance for data type like this?
  • How to use Parsers from Aeson with IO
  • Split a tuple into n parts
  • Xmonad - How do I move mouse pointer as part of ManageHook?
  • Idiomatic way to have many of the same generators in a list comprehension
  • Tree construction from a node string (Can't understand this Haskell code)
  • Give a default value for fields not available in json using aeson
  • grouping a list of lists by their first element
  • Reading Lines Asynchronously in Haskell
  • How do I enforce run-time conditions on data in Haskell?
  • Filtering by length of lines in file giving unexpected result
  • Sharing vs. non-sharing fixed-point combinator
  • What does "ignoring (possibly broken) abi-depends field for packages" mean?
  • What is "proxy a" in printtype function?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org