Allow unsafeSqlFunction to take up to 10 args

... without needing to nest tuples. Fixes #162
This commit is contained in:
Harry Garrood 2019-11-21 23:05:14 +00:00
parent 04a73ed92d
commit d889476bdf
2 changed files with 89 additions and 2 deletions

View File

@ -1,7 +1,7 @@
cabal-version: 1.12 cabal-version: 1.12
name: esqueleto name: esqueleto
version: 3.2.2 version: 3.2.3
synopsis: Type-safe EDSL for SQL queries on persistent backends. synopsis: Type-safe EDSL for SQL queries on persistent backends.
description: @esqueleto@ is a bare bones, type-safe EDSL for SQL queries that works with unmodified @persistent@ SQL backends. Its language closely resembles SQL, so you don't have to learn new concepts, just new syntax, and it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked @esqueleto@ queries that fail at runtime. description: @esqueleto@ is a bare bones, type-safe EDSL for SQL queries that works with unmodified @persistent@ SQL backends. Its language closely resembles SQL, so you don't have to learn new concepts, just new syntax, and it's fairly easy to predict the generated SQL and optimize it for your backend. Most kinds of errors committed when writing SQL are caught as compile-time errors---although it is possible to write type-checked @esqueleto@ queries that fail at runtime.
. .

View File

@ -2068,6 +2068,13 @@ unsafeSqlCastAs t (ERaw p f) =
in ("CAST" <> parens ( parensM p b <> " AS " <> TLB.fromText t), v ) in ("CAST" <> parens ( parensM p b <> " AS " <> TLB.fromText t), v )
unsafeSqlCastAs _ (ECompositeKey _) = throw (CompositeKeyErr SqlCastAsError) unsafeSqlCastAs _ (ECompositeKey _) = throw (CompositeKeyErr SqlCastAsError)
-- | (Internal) This class allows 'unsafeSqlFunction' to work with different
-- numbers of arguments; specifically it allows providing arguments to a sql
-- function via an n-tuple of @SqlExpr (Value _)@ values, which are not all
-- necessarily required to be the same type. There are instances for up to
-- 10-tuples, but for sql functions which take more than 10 arguments, you can
-- also nest tuples, as e.g. @toArgList ((a,b),(c,d))@ is the same as
-- @toArgList (a,b,c,d)@.
class UnsafeSqlFunctionArgument a where class UnsafeSqlFunctionArgument a where
toArgList :: a -> [SqlExpr (Value ())] toArgList :: a -> [SqlExpr (Value ())]
@ -2097,7 +2104,69 @@ instance ( UnsafeSqlFunctionArgument a
, UnsafeSqlFunctionArgument d , UnsafeSqlFunctionArgument d
) => UnsafeSqlFunctionArgument (a, b, c, d) where ) => UnsafeSqlFunctionArgument (a, b, c, d) where
toArgList = toArgList . from4 toArgList = toArgList . from4
-- | @since 3.2.3
instance ( UnsafeSqlFunctionArgument a
, UnsafeSqlFunctionArgument b
, UnsafeSqlFunctionArgument c
, UnsafeSqlFunctionArgument d
, UnsafeSqlFunctionArgument e
) => UnsafeSqlFunctionArgument (a, b, c, d, e) where
toArgList = toArgList . from5
-- | @since 3.2.3
instance ( UnsafeSqlFunctionArgument a
, UnsafeSqlFunctionArgument b
, UnsafeSqlFunctionArgument c
, UnsafeSqlFunctionArgument d
, UnsafeSqlFunctionArgument e
, UnsafeSqlFunctionArgument f
) => UnsafeSqlFunctionArgument (a, b, c, d, e, f) where
toArgList = toArgList . from6
-- | @since 3.2.3
instance ( UnsafeSqlFunctionArgument a
, UnsafeSqlFunctionArgument b
, UnsafeSqlFunctionArgument c
, UnsafeSqlFunctionArgument d
, UnsafeSqlFunctionArgument e
, UnsafeSqlFunctionArgument f
, UnsafeSqlFunctionArgument g
) => UnsafeSqlFunctionArgument (a, b, c, d, e, f, g) where
toArgList = toArgList . from7
-- | @since 3.2.3
instance ( UnsafeSqlFunctionArgument a
, UnsafeSqlFunctionArgument b
, UnsafeSqlFunctionArgument c
, UnsafeSqlFunctionArgument d
, UnsafeSqlFunctionArgument e
, UnsafeSqlFunctionArgument f
, UnsafeSqlFunctionArgument g
, UnsafeSqlFunctionArgument h
) => UnsafeSqlFunctionArgument (a, b, c, d, e, f, g, h) where
toArgList = toArgList . from8
-- | @since 3.2.3
instance ( UnsafeSqlFunctionArgument a
, UnsafeSqlFunctionArgument b
, UnsafeSqlFunctionArgument c
, UnsafeSqlFunctionArgument d
, UnsafeSqlFunctionArgument e
, UnsafeSqlFunctionArgument f
, UnsafeSqlFunctionArgument g
, UnsafeSqlFunctionArgument h
, UnsafeSqlFunctionArgument i
) => UnsafeSqlFunctionArgument (a, b, c, d, e, f, g, h, i) where
toArgList = toArgList . from9
-- | @since 3.2.3
instance ( UnsafeSqlFunctionArgument a
, UnsafeSqlFunctionArgument b
, UnsafeSqlFunctionArgument c
, UnsafeSqlFunctionArgument d
, UnsafeSqlFunctionArgument e
, UnsafeSqlFunctionArgument f
, UnsafeSqlFunctionArgument g
, UnsafeSqlFunctionArgument h
, UnsafeSqlFunctionArgument i
, UnsafeSqlFunctionArgument j
) => UnsafeSqlFunctionArgument (a, b, c, d, e, f, g, h, i, j) where
toArgList = toArgList . from10
-- | (Internal) Coerce a value's type from 'SqlExpr (Value a)' to -- | (Internal) Coerce a value's type from 'SqlExpr (Value a)' to
@ -2805,6 +2874,9 @@ instance ( SqlSelect a ra
from5P :: Proxy (a,b,c,d,e) -> Proxy ((a,b),(c,d),e) from5P :: Proxy (a,b,c,d,e) -> Proxy ((a,b),(c,d),e)
from5P = const Proxy from5P = const Proxy
from5 :: (a,b,c,d,e) -> ((a,b),(c,d),e)
from5 (a,b,c,d,e) = ((a,b),(c,d),e)
to5 :: ((a,b),(c,d),e) -> (a,b,c,d,e) to5 :: ((a,b),(c,d),e) -> (a,b,c,d,e)
to5 ((a,b),(c,d),e) = (a,b,c,d,e) to5 ((a,b),(c,d),e) = (a,b,c,d,e)
@ -2831,6 +2903,9 @@ instance ( SqlSelect a ra
from6P :: Proxy (a,b,c,d,e,f) -> Proxy ((a,b),(c,d),(e,f)) from6P :: Proxy (a,b,c,d,e,f) -> Proxy ((a,b),(c,d),(e,f))
from6P = const Proxy from6P = const Proxy
from6 :: (a,b,c,d,e,f) -> ((a,b),(c,d),(e,f))
from6 (a,b,c,d,e,f) = ((a,b),(c,d),(e,f))
to6 :: ((a,b),(c,d),(e,f)) -> (a,b,c,d,e,f) to6 :: ((a,b),(c,d),(e,f)) -> (a,b,c,d,e,f)
to6 ((a,b),(c,d),(e,f)) = (a,b,c,d,e,f) to6 ((a,b),(c,d),(e,f)) = (a,b,c,d,e,f)
@ -2859,6 +2934,9 @@ instance ( SqlSelect a ra
from7P :: Proxy (a,b,c,d,e,f,g) -> Proxy ((a,b),(c,d),(e,f),g) from7P :: Proxy (a,b,c,d,e,f,g) -> Proxy ((a,b),(c,d),(e,f),g)
from7P = const Proxy from7P = const Proxy
from7 :: (a,b,c,d,e,f,g) -> ((a,b),(c,d),(e,f),g)
from7 (a,b,c,d,e,f,g) = ((a,b),(c,d),(e,f),g)
to7 :: ((a,b),(c,d),(e,f),g) -> (a,b,c,d,e,f,g) to7 :: ((a,b),(c,d),(e,f),g) -> (a,b,c,d,e,f,g)
to7 ((a,b),(c,d),(e,f),g) = (a,b,c,d,e,f,g) to7 ((a,b),(c,d),(e,f),g) = (a,b,c,d,e,f,g)
@ -2889,6 +2967,9 @@ instance ( SqlSelect a ra
from8P :: Proxy (a,b,c,d,e,f,g,h) -> Proxy ((a,b),(c,d),(e,f),(g,h)) from8P :: Proxy (a,b,c,d,e,f,g,h) -> Proxy ((a,b),(c,d),(e,f),(g,h))
from8P = const Proxy from8P = const Proxy
from8 :: (a,b,c,d,e,f,g,h) -> ((a,b),(c,d),(e,f),(g,h))
from8 (a,b,c,d,e,f,g,h) = ((a,b),(c,d),(e,f),(g,h))
to8 :: ((a,b),(c,d),(e,f),(g,h)) -> (a,b,c,d,e,f,g,h) to8 :: ((a,b),(c,d),(e,f),(g,h)) -> (a,b,c,d,e,f,g,h)
to8 ((a,b),(c,d),(e,f),(g,h)) = (a,b,c,d,e,f,g,h) to8 ((a,b),(c,d),(e,f),(g,h)) = (a,b,c,d,e,f,g,h)
@ -2920,6 +3001,9 @@ instance ( SqlSelect a ra
from9P :: Proxy (a,b,c,d,e,f,g,h,i) -> Proxy ((a,b),(c,d),(e,f),(g,h),i) from9P :: Proxy (a,b,c,d,e,f,g,h,i) -> Proxy ((a,b),(c,d),(e,f),(g,h),i)
from9P = const Proxy from9P = const Proxy
from9 :: (a,b,c,d,e,f,g,h,i) -> ((a,b),(c,d),(e,f),(g,h),i)
from9 (a,b,c,d,e,f,g,h,i) = ((a,b),(c,d),(e,f),(g,h),i)
to9 :: ((a,b),(c,d),(e,f),(g,h),i) -> (a,b,c,d,e,f,g,h,i) to9 :: ((a,b),(c,d),(e,f),(g,h),i) -> (a,b,c,d,e,f,g,h,i)
to9 ((a,b),(c,d),(e,f),(g,h),i) = (a,b,c,d,e,f,g,h,i) to9 ((a,b),(c,d),(e,f),(g,h),i) = (a,b,c,d,e,f,g,h,i)
@ -2953,6 +3037,9 @@ instance ( SqlSelect a ra
from10P :: Proxy (a,b,c,d,e,f,g,h,i,j) -> Proxy ((a,b),(c,d),(e,f),(g,h),(i,j)) from10P :: Proxy (a,b,c,d,e,f,g,h,i,j) -> Proxy ((a,b),(c,d),(e,f),(g,h),(i,j))
from10P = const Proxy from10P = const Proxy
from10 :: (a,b,c,d,e,f,g,h,i,j) -> ((a,b),(c,d),(e,f),(g,h),(i,j))
from10 (a,b,c,d,e,f,g,h,i,j) = ((a,b),(c,d),(e,f),(g,h),(i,j))
to10 :: ((a,b),(c,d),(e,f),(g,h),(i,j)) -> (a,b,c,d,e,f,g,h,i,j) to10 :: ((a,b),(c,d),(e,f),(g,h),(i,j)) -> (a,b,c,d,e,f,g,h,i,j)
to10 ((a,b),(c,d),(e,f),(g,h),(i,j)) = (a,b,c,d,e,f,g,h,i,j) to10 ((a,b),(c,d),(e,f),(g,h),(i,j)) = (a,b,c,d,e,f,g,h,i,j)