composite-opaleye 0.6.1.0 → 0.6.2.0
raw patch · 3 files changed
+36/−23 lines, 3 filesdep ~vinylPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependency ranges changed: vinyl
API changes (from Hackage documentation)
- Composite.Opaleye.Util: constantColumnUsing :: Constant haskell (Column pgType) -> (haskell' -> haskell) -> Constant haskell' (Column pgType')
+ Composite.Opaleye.Util: constantColumnUsing :: ToFields haskell (Column sqlType) -> (haskell' -> haskell) -> ToFields haskell' (Column sqlType')
Files
- composite-opaleye.cabal +5/−5
- src/Composite/Opaleye/TH.hs +26/−13
- src/Composite/Opaleye/Util.hs +5/−5
composite-opaleye.cabal view
@@ -1,13 +1,13 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.33.0.+-- This file has been generated from package.yaml by hpack version 0.32.0. -- -- see: https://github.com/sol/hpack ----- hash: 796e0fc2311d9cc8ced5a68d57d144a537cc8ff6435a72cf8b9cec9928a2deca+-- hash: ecbbdea7379111313db5ef320f24808e4d9059d8b3d6a247f526168424461fe5 name: composite-opaleye-version: 0.6.1.0+version: 0.6.2.0 synopsis: Opaleye SQL for Frames records description: Integration between Frames records and Opaleye SQL, allowing records to be stored, retrieved, and queried from PostgreSQL. category: Records@@ -43,7 +43,7 @@ , profunctors >=5.2.1 && <5.4 , template-haskell >=2.11.1.0 && <2.15 , text >=1.2.2.2 && <1.3- , vinyl >=0.5.3 && <0.12+ , vinyl >=0.5.3 && <0.13 default-language: Haskell2010 test-suite composite-opaleye-test@@ -70,5 +70,5 @@ , profunctors >=5.2.1 && <5.4 , template-haskell >=2.11.1.0 && <2.15 , text >=1.2.2.2 && <1.3- , vinyl >=0.5.3 && <0.12+ , vinyl >=0.5.3 && <0.13 default-language: Haskell2010
src/Composite/Opaleye/TH.hs view
@@ -1,7 +1,6 @@ {-# LANGUAGE CPP #-} module Composite.Opaleye.TH where -import Composite.Opaleye.Util (constantColumnUsing) import Control.Lens ((<&>)) import qualified Data.ByteString.Char8 as BSC8 import Data.Maybe (fromMaybe)@@ -24,13 +23,15 @@ ) import Language.Haskell.TH.Syntax (lift) import Opaleye- ( Column, Constant, QueryRunnerColumnDefault, PGText, fieldQueryRunnerColumn, queryRunnerColumnDefault+ ( Column, Constant(..), QueryRunnerColumnDefault, ToFields, fieldQueryRunnerColumn, queryRunnerColumnDefault )+import Opaleye.Internal.PGTypes (IsSqlType, showSqlType, literalColumn)+import Opaleye.Internal.HaskellDB.PrimQuery (Literal(StringLit)) -- |Derive the various instances required to make a Haskell enumeration map to a PostgreSQL @enum@ type. ----- In @deriveOpaleyeEnum ''HaskellType "sqltype" hsConToSqlValue@, @''HaskellType@ is the sum type (data declaration) to make instances for, @"sqltype"@ is--- the PostgreSQL type name, and @hsConToSqlValue@ is a function to map names of constructors to SQL values.+-- In @deriveOpaleyeEnum ''HaskellType "schema.sqltype" hsConToSqlValue@, @''HaskellType@ is the sum type (data declaration) to make instances for, +-- @"schema.sqltype"@ is the PostgreSQL type name, and @hsConToSqlValue@ is a function to map names of constructors to SQL values. -- -- The function @hsConToSqlValue@ is of the type @String -> Maybe String@ in order to make using 'stripPrefix' convenient. The function is applied to each -- constructor name and for @Just value@ that value is used, otherwise for @Nothing@ the constructor name is used.@@ -50,7 +51,7 @@ -- The splice: -- -- @--- deriveOpaleyeEnum ''MyEnum "myenum" ('stripPrefix' "my" . 'map' 'toLower')+-- deriveOpaleyeEnum ''MyEnum "myschema.myenum" ('stripPrefix' "my" . 'map' 'toLower') -- @ -- -- Will create @PGMyEnum@ and instances required to use @MyEnum@ / @Column MyEnum@ in Opaleye.@@ -60,6 +61,9 @@ -- @ -- data PGMyEnum --+-- instance 'IsSqlType' PGMyEnum where+-- 'showSqlType' _ = "myschema.myenum"+-- -- instance 'FromField' MyEnum where -- 'fromField' f mbs = do -- tname <- 'typename' f@@ -67,16 +71,17 @@ -- _ | tname /= "myenum" -> 'returnError' 'Incompatible' f "" -- Just "foo" -> pure MyFoo -- Just "bar" -> pure MyBar--- Just other -> 'returnError' 'ConversionFailed' f ("Unexpected myenum value: " <> 'BSC8.unpack' other)+-- Just other -> 'returnError' 'ConversionFailed' f ("Unexpected myschema.myenum value: " <> 'BSC8.unpack' other) -- Nothing -> 'returnError' 'UnexpectedNull' f "" -- -- instance 'QueryRunnerColumnDefault' PGMyEnum MyEnum where -- queryRunnerColumnDefault = 'fieldQueryRunnerColumn' ----- instance 'Default' 'Constant' MyEnum ('Column' PGMyEnum) where--- def = 'constantColumnUsing' (def :: 'Constant' String ('Column' 'PGText')) $ \ case--- MyFoo -> "foo"--- MyBar -> "bar"+-- instance 'Default' 'ToFields' MyEnum ('Column' PGMyEnum) where+-- def = 'Constant' $ \ a ->+-- 'literalColumn' . 'stringLit' $ case a of+-- MyFoo -> "foo"+-- MyBar -> "bar" -- @ deriveOpaleyeEnum :: Name -> String -> (String -> Maybe String) -> Q [Dec] deriveOpaleyeEnum hsName sqlName hsConToSqlValue = do@@ -112,6 +117,14 @@ (cxt []) #endif + isSqlTypeInst <- instanceD (cxt []) [t| IsSqlType $sqlType |] . (:[]) $ do+ funD 'showSqlType+ [ clause+ [wildP]+ (normalB (lift sqlName))+ []+ ]+ fromFieldInst <- instanceD (cxt []) [t| FromField $hsType |] . (:[]) $ do field <- newName "field" mbs <- newName "mbs"@@ -161,7 +174,7 @@ [] ] - defaultInst <- instanceD (cxt []) [t| Default Constant $hsType (Column $sqlType) |] . (:[]) $ do+ defaultInst <- instanceD (cxt []) [t| Default ToFields $hsType (Column $sqlType) |] . (:[]) $ do s <- newName "s" let body = lamE [varP s] $ caseE (varE s) $@@ -174,9 +187,9 @@ funD 'def [ clause []- (normalB [| constantColumnUsing (def :: Constant String (Column PGText)) $body |])+ (normalB [| Constant (literalColumn . StringLit . $body) |]) [] ] - pure [sqlTypeDecl, fromFieldInst, queryRunnerColumnDefaultInst, defaultInst]+ pure [sqlTypeDecl, isSqlTypeInst, fromFieldInst, queryRunnerColumnDefaultInst, defaultInst]
src/Composite/Opaleye/Util.hs view
@@ -1,11 +1,11 @@ module Composite.Opaleye.Util where import Data.Profunctor (dimap)-import Opaleye (Column, Constant, unsafeCoerceColumn)+import Opaleye (Column, ToFields, unsafeCoerceColumn) --- |Coerce one type of 'Column' 'Constant' profunctor to another using by just asserting the changed type on the column side and using the given function+-- |Coerce one type of 'Column' 'ToFields' profunctor to another using by just asserting the changed type on the column side and using the given function -- on the Haskell side. Useful when the PG value representation is the same but the Haskell type changes, e.g. for enums.-constantColumnUsing :: Constant haskell (Column pgType)+constantColumnUsing :: ToFields haskell (Column sqlType) -> (haskell' -> haskell)- -> Constant haskell' (Column pgType')-constantColumnUsing oldConstant f = dimap f unsafeCoerceColumn oldConstant+ -> ToFields haskell' (Column sqlType')+constantColumnUsing oldToFields f = dimap f unsafeCoerceColumn oldToFields