1-
21{-# language BlockArguments #-}
3- {-# language LambdaCase #-}
4- {-# language RecordWildCards #-}
5- {-# language RankNTypes #-}
6- {-# language DuplicateRecordFields #-}
7- {-# language DerivingStrategies #-}
8- {-# language OverloadedRecordDot #-}
9- {-# language TypeApplications #-}
10- {-# language NamedFieldPuns #-}
11- {-# language ScopedTypeVariables #-}
12- {-# language StandaloneDeriving #-}
132{-# language DeriveAnyClass #-}
3+ {-# language DeriveGeneric #-}
4+ {-# language DerivingStrategies #-}
5+ {-# language DuplicateRecordFields #-}
146{-# language FlexibleContexts #-}
157{-# language FlexibleInstances #-}
16- {-# language DeriveGeneric #-}
8+ {-# language GADTs #-}
179{-# language GeneralizedNewtypeDeriving #-}
10+ {-# language LambdaCase #-}
11+ {-# language NamedFieldPuns #-}
12+ {-# language OverloadedRecordDot #-}
1813{-# language OverloadedStrings #-}
19- {-# language GADTs #-}
14+ {-# language RankNTypes #-}
15+ {-# language RecordWildCards #-}
16+ {-# language ScopedTypeVariables #-}
17+ {-# language StandaloneDeriving #-}
18+ {-# language TypeApplications #-}
19+ {-# options_ghc -Wno-partial-fields #-}
2020
2121module Rel8.Table.Verify
22- ( getSchemaErrors
23- , SomeTableSchema (.. )
24- , showCreateTable
25- , checkedShowCreateTable
26- ) where
22+ ( getSchemaErrors
23+ , SomeTableSchema (.. )
24+ , showCreateTable
25+ , checkedShowCreateTable
26+ )
27+ where
2728
2829-- base
29- import Control.Monad
3030import Data.Bits (shiftR , (.&.) )
31- import Data.Either (lefts )
32- import Data.Function
31+ import Data.Function ((&) )
3332import Data.Functor ((<&>) )
3433import Data.Functor.Const
3534import Data.Functor.Contravariant ( (>$<) )
@@ -48,32 +47,45 @@ import qualified Prelude as P
4847import qualified Data.Map as M
4948
5049-- hasql
51- import Hasql.Connection
5250import qualified Hasql.Statement as HS
5351
5452-- rel8
55- import Rel8 -- not importing this seems to cause a type error???
5653import Rel8.Column ( Column )
5754import Rel8.Column.List ( HList )
5855import Rel8.Expr ( Expr )
56+ import Rel8.Expr.Eq ((==.) )
57+ import Rel8.Expr.Ord ((>.) )
58+ import Rel8.Expr.Order (asc )
5959import Rel8.Generic.Rel8able (GFromExprs , Rel8able )
6060import Rel8.Query ( Query )
61+ import Rel8.Query.Each (each )
62+ import Rel8.Query.Filter (filter )
63+ import Rel8.Query.List (many )
64+ import Rel8.Query.Order (orderBy )
6165import Rel8.Schema.HTable
6266import Rel8.Schema.Name ( Name (Name ) )
6367import Rel8.Schema.Null hiding (nullable )
64- import qualified Rel8.Schema.Null as Null
65- import qualified Rel8.Statement.Run as RSR
66- import Rel8.Schema.Table ( TableSchema (.. ) )
67- import Rel8.Schema.Spec
68- import Rel8.Schema.Result ( Result )
6968import Rel8.Schema.QualifiedName ( QualifiedName (.. ) )
70- import Rel8.Table ( Columns )
69+ import Rel8.Schema.Result ( Result )
70+ import Rel8.Schema.Spec (Spec (Spec ))
71+ import qualified Rel8.Schema.Spec
72+ import Rel8.Schema.Table ( TableSchema (.. ) )
73+ import Rel8.Statement.Run (run1 )
74+ import Rel8.Statement.Select (select )
75+ import Rel8.Table (Columns , toColumns )
7176import Rel8.Table.List ( ListTable )
72- import Rel8.Table.Serialize ( ToExprs )
77+ import Rel8.Table.Name (namesFromLabelsWith )
78+ import Rel8.Table.Rel8able ()
79+ import Rel8.Table.Serialize (ToExprs , lit )
7380import Rel8.Type ( DBType (.. ) )
7481import Rel8.Type.Eq ( DBEq )
82+ import Rel8.Type.Information (parseTypeInformation )
83+ import qualified Rel8.Type.Information
7584import Rel8.Type.Name ( TypeName (.. ) )
7685
86+ -- semialign
87+ import Data.Semialign (align )
88+
7789-- these
7890import Data.These
7991
@@ -338,7 +350,7 @@ showCreateTable_helper name typeMap = "CREATE TABLE " <> show name <> " ("
338350 ++ " \n );"
339351 where
340352 go :: (String , TypeInfo ) -> String
341- go (name, typeInfo) = " \n " ++ show name ++ " " ++ showTypeInfo typeInfo
353+ go (name' , typeInfo) = " \n " ++ show name' ++ " " ++ showTypeInfo typeInfo
342354
343355
344356-- | @'showCreateTable'@ shows an example CREATE TABLE statement for the table.
@@ -378,17 +390,9 @@ checkTypeEquality env db hs
378390 sameMods = db. typeName. modifiers == hs. typeName. modifiers
379391 sameDims = db. typeName. arrayDepth == hs. typeName. arrayDepth
380392
381- sameName = equalName db. typeName. name hs. typeName. name
382-
383393 toName :: TypeInfo -> String
384394 toName typeInfo = case typeInfo. typeName. name of
385- QualifiedName name _ -> L. dropWhile (== ' _' ) name
386-
387- equalName :: QualifiedName -> QualifiedName -> Bool
388- equalName (QualifiedName a (Just b)) (QualifiedName a' (Just b'))
389- = L. dropWhile (== ' _' ) a == L. dropWhile (== ' _' ) a' && b == b'
390- equalName (QualifiedName a _) (QualifiedName a' _)
391- = dropWhile (== ' _' ) a == dropWhile (== ' _' ) a'
395+ QualifiedName name _ -> L. dropWhile (== ' _' ) name
392396
393397-- check types for a single table
394398compareTypes
@@ -430,7 +434,7 @@ compareTypes env attrMap typeMap = fmap (uncurry go) $ M.assocs (disjointUnion a
430434 (T. unpack attr. typ. typname)
431435 (Just $ T. unpack attr. namespace. nspname)
432436 , modifiers = toModifier
433- (T. dropWhile (== ' _' ) attr. typ. typname)
437+ (T. dropWhile (== ' _' ) attr. typ. typname)
434438 attr. attribute. atttypmod
435439 , arrayDepth = fromIntegral attr. attribute. attndims
436440 }
@@ -444,14 +448,10 @@ compareTypes env attrMap typeMap = fmap (uncurry go) $ M.assocs (disjointUnion a
444448 toModifier _ _ = []
445449
446450 disjointUnion :: Ord k => M. Map k a -> M. Map k b -> M. Map k (These a b )
447- disjointUnion a b = M. unionWith go (fmap This a) (fmap That b)
448- where
449- go :: These a b -> These a b -> These a b
450- go (This a) (That b) = These a b
451- go _ _ = undefined
451+ disjointUnion = align
452452
453453
454- -- | @pShowTable@ is a helper function which takes a grid of text and prints it
454+ -- | @pShowTable@ i's a helper f'unction which takes a grid of text and prints' it'
455455-- as a table, with padding so that cells are lined in columns, and a bordered
456456-- header for the first row
457457pShowTable :: [[Text ]] -> Text
@@ -464,7 +464,7 @@ pShowTable xs
464464 where
465465 addHeaderBorder :: [Text ] -> [Text ]
466466 addHeaderBorder [] = []
467- addHeaderBorder (x : xs ) = x : T. replicate (T. length x ) " -" : xs
467+ addHeaderBorder (a : as ) = a : T. replicate (T. length a ) " -" : as
468468
469469 xs' :: [[Text ]]
470470 xs' = L. transpose xs
@@ -489,8 +489,8 @@ pShowErrors = T.intercalate "\n\n" . fmap go
489489 [ " Table "
490490 , T. pack (show name)
491491 , " has multiple columns with the same name. This is an error with the Haskell code generating an impossible schema, rather than an error in your current setup of the database itself. Using 'namesFromLabels' can ensure each column has unique names, which is the easiest way to prevent this, but may require changing names in your database to match the new generated names."
492- , pShowTable ([" DB name" , " Haskell label" ] : (M. assocs duplicates <&> \ (name, typs) ->
493- [ T. pack name
492+ , pShowTable ([" DB name" , " Haskell label" ] : (M. assocs duplicates <&> \ (name' , typs) ->
493+ [ T. pack name'
494494 , T. intercalate " " $ fmap (\ typ -> T. intercalate " /" $ fmap T. pack typ. label) $ NonEmpty. toList typs
495495 ]))
496496 ]
@@ -531,8 +531,8 @@ showTypeInfo typeInfo = concat
531531 ]
532532 where
533533 name = case typeInfo. typeName. name of
534- QualifiedName a Nothing -> show (dropWhile (== ' _' ) a)
535- QualifiedName a (Just b) -> show b <> " ." <> show (dropWhile (== ' _' ) a)
534+ QualifiedName a Nothing -> show (dropWhile (== ' _' ) a)
535+ QualifiedName a (Just b) -> show b <> " ." <> show (dropWhile (== ' _' ) a)
536536
537537 modifiers :: [String ]
538538 modifiers = typeInfo. typeName. modifiers
0 commit comments