Broke out the error types into three seperate sum types with one sum type enumerating them
Identified and rewrote all error sections
This commit is contained in:
parent
b77a0c3e71
commit
431080611d
@ -76,16 +76,46 @@ import qualified Data.Text.Lazy.Builder as TLB
|
|||||||
|
|
||||||
import Database.Esqueleto.Internal.Language
|
import Database.Esqueleto.Internal.Language
|
||||||
|
|
||||||
-- | Exception data type for @esqueleto@ internal problems
|
-- | Exception data type for @esqueleto@ internal errors
|
||||||
data EsqueletoProblem =
|
data EsqueletoError =
|
||||||
UnexpectedCompositeKeyError String -- | Unexpected composite key error
|
CompositeKeyErr CompositeKeyError
|
||||||
| UnexpectedCase String -- | Unexpected function case encountered
|
| UnexpectedCaseErr UnexpectedCaseError
|
||||||
| EmptySqlExprValueList -- | EEmptyList found for value list
|
| SqlBinOpCompositeErr SqlBinOpCompositeError
|
||||||
| UnsupportedSqlInsertIntoType -- | Default Exception for sqlInsertInto
|
|
||||||
| UnsafeSqlBinOpComposite String -- | Error in unsafeSqlBinOpComposite
|
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
instance Exception EsqueletoProblem
|
data CompositeKeyError =
|
||||||
|
NotError
|
||||||
|
| ToInsertionError
|
||||||
|
| CombineInsertionError
|
||||||
|
| FoldHelpError
|
||||||
|
| SqlCaseError
|
||||||
|
| SqlBinOpError
|
||||||
|
| MakeOnClauseError
|
||||||
|
| MakeExcError
|
||||||
|
| MakeSetError
|
||||||
|
| MakeWhereError
|
||||||
|
| MakeHavingError
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
|
data UnexpectedCaseError =
|
||||||
|
EmptySqlExprValueList
|
||||||
|
| MakeFromError
|
||||||
|
| UnsupportedSqlInsertIntoType
|
||||||
|
| InsertionFinalError
|
||||||
|
| NewIdentForError
|
||||||
|
| UnsafeSqlCaseError
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
|
data SqlBinOpCompositeError =
|
||||||
|
MismatchingLengthsError
|
||||||
|
| NullPlaceholdersError
|
||||||
|
| DeconstructionError
|
||||||
|
deriving (Show)
|
||||||
|
|
||||||
|
instance Exception SqlBinOpCompositeError
|
||||||
|
instance Exception UnexpectedCaseError
|
||||||
|
instance Exception CompositeKeyError
|
||||||
|
instance Exception EsqueletoError
|
||||||
|
|
||||||
|
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
@ -253,7 +283,7 @@ newIdentFor = Q . lift . try . unDBName
|
|||||||
s <- S.get
|
s <- S.get
|
||||||
let go (t:ts) | t `HS.member` inUse s = go ts
|
let go (t:ts) | t `HS.member` inUse s = go ts
|
||||||
| otherwise = use t
|
| otherwise = use t
|
||||||
go [] = unexpectedCase "Esqueleto/Sql/newIdentFor: never here"
|
go [] = throw NewIdentForError
|
||||||
go (possibilities orig)
|
go (possibilities orig)
|
||||||
|
|
||||||
possibilities t = t : map addNum [2..]
|
possibilities t = t : map addNum [2..]
|
||||||
@ -463,7 +493,7 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
|
|||||||
|
|
||||||
not_ (ERaw p f) = ERaw Never $ \info -> let (b, vals) = f info
|
not_ (ERaw p f) = ERaw Never $ \info -> let (b, vals) = f info
|
||||||
in ("NOT " <> parensM p b, vals)
|
in ("NOT " <> parensM p b, vals)
|
||||||
not_ (ECompositeKey _) = unexpectedCompositeKeyError "not_"
|
not_ (ECompositeKey _) = throw (CompositeKeyErr NotError)
|
||||||
|
|
||||||
(==.) = unsafeSqlBinOpComposite " = " " AND "
|
(==.) = unsafeSqlBinOpComposite " = " " AND "
|
||||||
(!=.) = unsafeSqlBinOpComposite " != " " OR "
|
(!=.) = unsafeSqlBinOpComposite " != " " OR "
|
||||||
@ -529,13 +559,13 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
|
|||||||
field /=. expr = setAux field (\ent -> ent ^. field /. expr)
|
field /=. expr = setAux field (\ent -> ent ^. field /. expr)
|
||||||
|
|
||||||
(<#) _ (ERaw _ f) = EInsert Proxy f
|
(<#) _ (ERaw _ f) = EInsert Proxy f
|
||||||
(<#) _ (ECompositeKey _) = unexpectedCompositeKeyError "(<#)"
|
(<#) _ (ECompositeKey _) = throw (CompositeKeyErr ToInsertionError)
|
||||||
|
|
||||||
(EInsert _ f) <&> (ERaw _ g) = EInsert Proxy $ \x ->
|
(EInsert _ f) <&> (ERaw _ g) = EInsert Proxy $ \x ->
|
||||||
let (fb, fv) = f x
|
let (fb, fv) = f x
|
||||||
(gb, gv) = g x
|
(gb, gv) = g x
|
||||||
in (fb <> ", " <> gb, fv ++ gv)
|
in (fb <> ", " <> gb, fv ++ gv)
|
||||||
(EInsert _ _) <&> (ECompositeKey _) = unexpectedCompositeKeyError "(<&>)"
|
(EInsert _ _) <&> (ECompositeKey _) = throw (CompositeKeyErr CombineInsertionError)
|
||||||
|
|
||||||
case_ = unsafeSqlCase
|
case_ = unsafeSqlCase
|
||||||
toBaseId = veryUnsafeCoerceSqlExprValue
|
toBaseId = veryUnsafeCoerceSqlExprValue
|
||||||
@ -593,7 +623,7 @@ unsafeSqlCase when (ERaw p1 f1) = ERaw Never buildCase
|
|||||||
in ( "CASE" <> b2 <> " ELSE " <> parensM p1 b1 <> " END", vals2 <> vals1)
|
in ( "CASE" <> b2 <> " ELSE " <> parensM p1 b1 <> " END", vals2 <> vals1)
|
||||||
|
|
||||||
mapWhen :: [(SqlExpr (Value Bool), SqlExpr (Value a))] -> IdentInfo -> (TLB.Builder, [PersistValue])
|
mapWhen :: [(SqlExpr (Value Bool), SqlExpr (Value a))] -> IdentInfo -> (TLB.Builder, [PersistValue])
|
||||||
mapWhen [] _ = unexpectedCase "unsafeSqlCase: empty when list."
|
mapWhen [] _ = throw (UnexpectedCaseErr UnsafeSqlCaseError)
|
||||||
mapWhen when' info = foldl (foldHelp info) (mempty, mempty) when'
|
mapWhen when' info = foldl (foldHelp info) (mempty, mempty) when'
|
||||||
|
|
||||||
foldHelp :: IdentInfo -> (TLB.Builder, [PersistValue]) -> (SqlExpr (Value Bool), SqlExpr (Value a)) -> (TLB.Builder, [PersistValue])
|
foldHelp :: IdentInfo -> (TLB.Builder, [PersistValue]) -> (SqlExpr (Value Bool), SqlExpr (Value a)) -> (TLB.Builder, [PersistValue])
|
||||||
@ -601,8 +631,8 @@ unsafeSqlCase when (ERaw p1 f1) = ERaw Never buildCase
|
|||||||
let (b1, vals1) = f1' info
|
let (b1, vals1) = f1' info
|
||||||
(b2, vals2) = f2 info
|
(b2, vals2) = f2 info
|
||||||
in ( b0 <> " WHEN " <> parensM p1' b1 <> " THEN " <> parensM p2 b2, vals0 <> vals1 <> vals2 )
|
in ( b0 <> " WHEN " <> parensM p1' b1 <> " THEN " <> parensM p2 b2, vals0 <> vals1 <> vals2 )
|
||||||
foldHelp _ _ _ = unexpectedCompositeKeyError "unsafeSqlCase/foldHelp"
|
foldHelp _ _ _ = throw (CompositeKeyErr FoldHelpError)
|
||||||
unsafeSqlCase _ (ECompositeKey _) = unexpectedCompositeKeyError "unsafeSqlCase"
|
unsafeSqlCase _ (ECompositeKey _) = throw (CompositeKeyErr SqlCaseError)
|
||||||
|
|
||||||
|
|
||||||
-- | (Internal) Create a custom binary operator. You /should/
|
-- | (Internal) Create a custom binary operator. You /should/
|
||||||
@ -624,7 +654,7 @@ unsafeSqlBinOp op (ERaw p1 f1) (ERaw p2 f2) = ERaw Parens f
|
|||||||
(b2, vals2) = f2 info
|
(b2, vals2) = f2 info
|
||||||
in ( parensM p1 b1 <> op <> parensM p2 b2
|
in ( parensM p1 b1 <> op <> parensM p2 b2
|
||||||
, vals1 <> vals2 )
|
, vals1 <> vals2 )
|
||||||
unsafeSqlBinOp _ _ _ = unexpectedCompositeKeyError "unsafeSqlBinOp"
|
unsafeSqlBinOp _ _ _ = throw (CompositeKeyErr SqlBinOpError)
|
||||||
{-# INLINE unsafeSqlBinOp #-}
|
{-# INLINE unsafeSqlBinOp #-}
|
||||||
|
|
||||||
|
|
||||||
@ -662,20 +692,17 @@ unsafeSqlBinOpComposite op sep a b = ERaw Parens $ compose (listify a) (listify
|
|||||||
deconstruct :: (TLB.Builder, [PersistValue]) -> ([TLB.Builder], [PersistValue])
|
deconstruct :: (TLB.Builder, [PersistValue]) -> ([TLB.Builder], [PersistValue])
|
||||||
deconstruct ("?", [PersistList vals]) = (replicate (length vals) "?", vals)
|
deconstruct ("?", [PersistList vals]) = (replicate (length vals) "?", vals)
|
||||||
deconstruct (b', []) = (TLB.fromLazyText <$> TL.splitOn "," (TLB.toLazyText b'), [])
|
deconstruct (b', []) = (TLB.fromLazyText <$> TL.splitOn "," (TLB.toLazyText b'), [])
|
||||||
deconstruct x = err $ "cannot deconstruct " ++ show x ++ "."
|
deconstruct _ = throw (SqlBinOpCompositeErr DeconstructionError)
|
||||||
|
|
||||||
compose f1 f2 info
|
compose f1 f2 info
|
||||||
| not (null v1 || null v2) = err' "one side needs to have null placeholders"
|
| not (null v1 || null v2) = throw (SqlBinOpCompositeErr NullPlaceholdersError)
|
||||||
| length b1 /= length b2 = err' "mismatching lengths"
|
| length b1 /= length b2 = throw (SqlBinOpCompositeErr MismatchingLengthsError)
|
||||||
| otherwise = (bc, vc)
|
| otherwise = (bc, vc)
|
||||||
where
|
where
|
||||||
(b1, v1) = f1 info
|
(b1, v1) = f1 info
|
||||||
(b2, v2) = f2 info
|
(b2, v2) = f2 info
|
||||||
bc = intersperseB sep [x <> op <> y | (x, y) <- zip b1 b2]
|
bc = intersperseB sep [x <> op <> y | (x, y) <- zip b1 b2]
|
||||||
vc = v1 <> v2
|
vc = v1 <> v2
|
||||||
err' = err . (++ (", " ++ show ((b1, v1), (b2, v2))))
|
|
||||||
|
|
||||||
err = throw . UnsafeSqlBinOpComposite . ("unsafeSqlBinOpComposite: " ++)
|
|
||||||
|
|
||||||
-- | (Internal) A raw SQL value. The same warning from
|
-- | (Internal) A raw SQL value. The same warning from
|
||||||
-- 'unsafeSqlBinOp' applies to this function as well.
|
-- 'unsafeSqlBinOp' applies to this function as well.
|
||||||
@ -754,7 +781,7 @@ veryUnsafeCoerceSqlExprValue (ECompositeKey f) = ECompositeKey f
|
|||||||
-- a)' to 'SqlExpr (Value a)'. Does not work with empty lists.
|
-- a)' to 'SqlExpr (Value a)'. Does not work with empty lists.
|
||||||
veryUnsafeCoerceSqlExprValueList :: SqlExpr (ValueList a) -> SqlExpr (Value a)
|
veryUnsafeCoerceSqlExprValueList :: SqlExpr (ValueList a) -> SqlExpr (Value a)
|
||||||
veryUnsafeCoerceSqlExprValueList (EList v) = v
|
veryUnsafeCoerceSqlExprValueList (EList v) = v
|
||||||
veryUnsafeCoerceSqlExprValueList EEmptyList = throw EmptySqlExprValueList
|
veryUnsafeCoerceSqlExprValueList EEmptyList = throw (UnexpectedCaseErr EmptySqlExprValueList)
|
||||||
|
|
||||||
|
|
||||||
----------------------------------------------------------------------
|
----------------------------------------------------------------------
|
||||||
@ -1069,7 +1096,7 @@ makeFrom info mode fs = ret
|
|||||||
, mk Parens rhs
|
, mk Parens rhs
|
||||||
, maybe mempty makeOnClause monClause
|
, maybe mempty makeOnClause monClause
|
||||||
]
|
]
|
||||||
mk _ (OnClause _) = unexpectedCase "Esqueleto/Sql/makeFrom: never here (is collectOnClauses working?)"
|
mk _ (OnClause _) = throw (UnexpectedCaseErr MakeFromError)
|
||||||
|
|
||||||
base ident@(I identText) def =
|
base ident@(I identText) def =
|
||||||
let db@(DBName dbText) = entityDB def
|
let db@(DBName dbText) = entityDB def
|
||||||
@ -1085,31 +1112,25 @@ makeFrom info mode fs = ret
|
|||||||
fromKind FullOuterJoinKind = " FULL OUTER JOIN "
|
fromKind FullOuterJoinKind = " FULL OUTER JOIN "
|
||||||
|
|
||||||
makeOnClause (ERaw _ f) = first (" ON " <>) (f info)
|
makeOnClause (ERaw _ f) = first (" ON " <>) (f info)
|
||||||
makeOnClause (ECompositeKey _) = unexpectedCompositeKeyError "makeFrom/makeOnClause"
|
makeOnClause (ECompositeKey _) = throw (CompositeKeyErr MakeOnClauseError)
|
||||||
|
|
||||||
mkExc :: SqlExpr (Value Bool) -> OnClauseWithoutMatchingJoinException
|
mkExc :: SqlExpr (Value Bool) -> OnClauseWithoutMatchingJoinException
|
||||||
mkExc (ERaw _ f) =
|
mkExc (ERaw _ f) =
|
||||||
OnClauseWithoutMatchingJoinException $
|
OnClauseWithoutMatchingJoinException $
|
||||||
TL.unpack $ TLB.toLazyText $ fst (f info)
|
TL.unpack $ TLB.toLazyText $ fst (f info)
|
||||||
mkExc (ECompositeKey _) = unexpectedCompositeKeyError "makeFrom/mkExc"
|
mkExc (ECompositeKey _) = throw (CompositeKeyErr MakeExcError)
|
||||||
|
|
||||||
unexpectedCompositeKeyError :: String -> a
|
|
||||||
unexpectedCompositeKeyError w = throw $ UnexpectedCompositeKeyError (w ++ ": non-id/composite keys not expected here")
|
|
||||||
|
|
||||||
unexpectedCase :: String -> a
|
|
||||||
unexpectedCase = throw . UnexpectedCase
|
|
||||||
|
|
||||||
makeSet :: IdentInfo -> [SetClause] -> (TLB.Builder, [PersistValue])
|
makeSet :: IdentInfo -> [SetClause] -> (TLB.Builder, [PersistValue])
|
||||||
makeSet _ [] = mempty
|
makeSet _ [] = mempty
|
||||||
makeSet info os = first ("\nSET " <>) . uncommas' $ concatMap mk os
|
makeSet info os = first ("\nSET " <>) . uncommas' $ concatMap mk os
|
||||||
where
|
where
|
||||||
mk (SetClause (ERaw _ f)) = [f info]
|
mk (SetClause (ERaw _ f)) = [f info]
|
||||||
mk (SetClause (ECompositeKey _)) = unexpectedCompositeKeyError "makeSet" -- FIXME
|
mk (SetClause (ECompositeKey _)) = throw (CompositeKeyErr MakeSetError) -- FIXME
|
||||||
|
|
||||||
makeWhere :: IdentInfo -> WhereClause -> (TLB.Builder, [PersistValue])
|
makeWhere :: IdentInfo -> WhereClause -> (TLB.Builder, [PersistValue])
|
||||||
makeWhere _ NoWhere = mempty
|
makeWhere _ NoWhere = mempty
|
||||||
makeWhere info (Where (ERaw _ f)) = first ("\nWHERE " <>) (f info)
|
makeWhere info (Where (ERaw _ f)) = first ("\nWHERE " <>) (f info)
|
||||||
makeWhere _ (Where (ECompositeKey _)) = unexpectedCompositeKeyError "makeWhere"
|
makeWhere _ (Where (ECompositeKey _)) = throw (CompositeKeyErr MakeWhereError)
|
||||||
|
|
||||||
|
|
||||||
makeGroupBy :: IdentInfo -> GroupByClause -> (TLB.Builder, [PersistValue])
|
makeGroupBy :: IdentInfo -> GroupByClause -> (TLB.Builder, [PersistValue])
|
||||||
@ -1126,7 +1147,7 @@ makeGroupBy info (GroupBy fields) = first ("\nGROUP BY " <>) build
|
|||||||
makeHaving :: IdentInfo -> WhereClause -> (TLB.Builder, [PersistValue])
|
makeHaving :: IdentInfo -> WhereClause -> (TLB.Builder, [PersistValue])
|
||||||
makeHaving _ NoWhere = mempty
|
makeHaving _ NoWhere = mempty
|
||||||
makeHaving info (Where (ERaw _ f)) = first ("\nHAVING " <>) (f info)
|
makeHaving info (Where (ERaw _ f)) = first ("\nHAVING " <>) (f info)
|
||||||
makeHaving _ (Where (ECompositeKey _)) = unexpectedCompositeKeyError "makeHaving"
|
makeHaving _ (Where (ECompositeKey _)) = throw (CompositeKeyErr MakeHavingError)
|
||||||
|
|
||||||
-- makeHaving, makeWhere and makeOrderBy
|
-- makeHaving, makeWhere and makeOrderBy
|
||||||
makeOrderBy :: IdentInfo -> [OrderByClause] -> (TLB.Builder, [PersistValue])
|
makeOrderBy :: IdentInfo -> [OrderByClause] -> (TLB.Builder, [PersistValue])
|
||||||
@ -1188,7 +1209,7 @@ class SqlSelect a r | a -> r, r -> a where
|
|||||||
|
|
||||||
-- | Create @INSERT INTO@ clause instead.
|
-- | Create @INSERT INTO@ clause instead.
|
||||||
sqlInsertInto :: IdentInfo -> a -> (TLB.Builder, [PersistValue])
|
sqlInsertInto :: IdentInfo -> a -> (TLB.Builder, [PersistValue])
|
||||||
sqlInsertInto = throw UnsupportedSqlInsertIntoType
|
sqlInsertInto = throw (UnexpectedCaseErr UnsupportedSqlInsertIntoType)
|
||||||
|
|
||||||
|
|
||||||
-- | @INSERT INTO@ hack.
|
-- | @INSERT INTO@ hack.
|
||||||
@ -1202,9 +1223,7 @@ instance SqlSelect (SqlExpr InsertFinal) InsertFinal where
|
|||||||
in ("INSERT INTO " <> table <> parens fields <> "\n", [])
|
in ("INSERT INTO " <> table <> parens fields <> "\n", [])
|
||||||
sqlSelectCols info (EInsertFinal (EInsert _ f)) = f info
|
sqlSelectCols info (EInsertFinal (EInsert _ f)) = f info
|
||||||
sqlSelectColCount = const 0
|
sqlSelectColCount = const 0
|
||||||
sqlSelectProcessRow = const (Right (throw $ UnexpectedCase msg))
|
sqlSelectProcessRow = const (Right (throw InsertionFinalError))
|
||||||
where
|
|
||||||
msg = "sqlSelectProcessRow/SqlSelect/InsertionFinal: never here"
|
|
||||||
|
|
||||||
|
|
||||||
-- | Not useful for 'select', but used for 'update' and 'delete'.
|
-- | Not useful for 'select', but used for 'update' and 'delete'.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user