Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions exe-rel8-import/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{-# language DeriveGeneric #-}
{-# language DerivingStrategies #-}
{-# language DuplicateRecordFields #-}
{-# language DisambiguateRecordFields #-}
{-# language GeneralizedNewtypeDeriving #-}
{-# language LambdaCase #-}
{-# language NamedFieldPuns #-}
Expand Down Expand Up @@ -179,11 +178,11 @@ main = do
, ..
}

putStrLn $ HS.prettyPrint $ tablesToModule tables
putStrLn $ HS.prettyPrint $ tablesToModule schema tables


tablesToModule :: [Table Result] -> HS.Module ()
tablesToModule tables = HS.Module () Nothing pragmas imports allTableDecls
tablesToModule :: Text -> [Table Result] -> HS.Module ()
tablesToModule nameOfDatabaseSchema tables = HS.Module () Nothing pragmas imports allTableDecls
where
pragmas = [ deriveGeneric, deriveAnyClass, derivingStrategies, overloadedStrings ]
where
Expand Down Expand Up @@ -211,6 +210,27 @@ tablesToModule tables = HS.Module () Nothing pragmas imports allTableDecls
where
pascalName = HS.Ident () $ pascal $ unpack name

field Attribute{ attribute, typ } = (fieldDecl, fieldColumnMapping)
where
fieldDecl = HS.FieldDecl () [fieldName] $ columnF columnType

fieldColumnMapping = HS.FieldUpdate () (HS.UnQual () fieldName) columnName

fieldName = HS.Ident () $ camel $ unpack $ attname attribute

columnType =
HS.TyCon () $ HS.UnQual () $ HS.Ident () $
maybe (pascal $ unpack $ typname typ) unpack (lookup (typname typ) typeMapping)

columnF = HS.TyApp () (HS.TyApp () _Column f)
where
_Column = HS.TyCon () $ HS.UnQual () $ HS.Ident () "Column"
f = HS.TyVar () $ HS.Ident () "f"

columnName = HS.Lit () $ HS.String () str str
where
str = unpack $ attname attribute

rel8able = HS.DataDecl () (HS.DataType ()) Nothing declHead [constructor] [derivingGeneric, derivingRel8able]
where
declHead = HS.DHApp () tyName f
Expand All @@ -220,19 +240,7 @@ tablesToModule tables = HS.Module () Nothing pragmas imports allTableDecls

constructor = HS.QualConDecl () Nothing Nothing conDecl
where
conDecl = HS.RecDecl () pascalName $ map field columns
field Attribute{ attribute, typ } = HS.FieldDecl () [fieldName] $ columnF columnType
where
fieldName = HS.Ident () $ camel $ unpack $ attname attribute

columnType =
HS.TyCon () $ HS.UnQual () $ HS.Ident () $
maybe (pascal $ unpack $ typname typ) unpack (lookup (typname typ) typeMapping)

columnF = HS.TyApp () (HS.TyApp () _Column f)
where
_Column = HS.TyCon () $ HS.UnQual () $ HS.Ident () "Column"
f = HS.TyVar () $ HS.Ident () "f"
conDecl = HS.RecDecl () pascalName $ map (fst . field) columns

derivingGeneric = HS.Deriving () (Just (HS.DerivStock ())) [rule]
where
Expand Down Expand Up @@ -270,19 +278,12 @@ tablesToModule tables = HS.Module () Nothing pragmas imports allTableDecls
HS.App () (HS.Con () (HS.UnQual () (HS.Ident () "Just"))) $
HS.Lit () $ HS.String () str str
where
str = unpack name
str = unpack nameOfDatabaseSchema

columnsField = HS.FieldUpdate () columnsName columnsRecord
where
columnsName = HS.UnQual () (HS.Ident () "columns")
columnsRecord = HS.RecConstr () (HS.UnQual () pascalName) $ map field columns

field Attribute{ attribute, typ } = HS.FieldUpdate () (HS.UnQual () fieldName) columnName
where
fieldName = HS.Ident () $ camel $ unpack $ attname attribute
columnName = HS.Lit () $ HS.String () str str
where
str = unpack $ attname attribute
columnsRecord = HS.RecConstr () (HS.UnQual () pascalName) $ map (snd . field) columns


typeMapping :: [(Text, Text)]
Expand Down