diff --git a/src/Database/Esqueleto.hs b/src/Database/Esqueleto.hs index ac9d2df..3681b87 100644 --- a/src/Database/Esqueleto.hs +++ b/src/Database/Esqueleto.hs @@ -47,7 +47,7 @@ module Database.Esqueleto , random_, round_, ceiling_, floor_ , min_, max_, sum_, avg_, castNum, castNumM , coalesce, coalesceDefault - , lower_, like, ilike, (%), concat_, (++.) + , lower_, like, ilike, (%), concat_, (++.), castString , subList_select, subList_selectDistinct, valList, justList , in_, notIn, exists, notExists , set, (=.), (+=.), (-=.), (*=.), (/=.) diff --git a/src/Database/Esqueleto/Internal/Language.hs b/src/Database/Esqueleto/Internal/Language.hs index 1eec827..b9a0751 100644 --- a/src/Database/Esqueleto/Internal/Language.hs +++ b/src/Database/Esqueleto/Internal/Language.hs @@ -422,6 +422,17 @@ class (Functor query, Applicative query, Monad query) => -- Haskell's '++' in order to avoid naming clash with '||.'). -- Supported by SQLite and PostgreSQL. (++.) :: SqlString s => expr (Value s) -> expr (Value s) -> expr (Value s) + -- | Cast a string type into 'Text'. This function + -- is very useful if you want to use @newtype@s, or if you want + -- to apply functions such as 'like' to strings of different + -- types. + -- + -- /Safety:/ This is a slightly unsafe function, especially if + -- you have defined your own instances of 'SqlString'. Also, + -- since 'Maybe' is an instance of 'SqlString', it's possible + -- to turn a nullable value into a non-nullable one. Avoid + -- using this function if possible. + castString :: (SqlString s, SqlString r) => expr (Value s) -> expr (Value r) -- | Execute a subquery @SELECT@ in an expression. Returns a -- list of values. @@ -800,6 +811,8 @@ instance SqlString B.ByteString where -- | /Since: 2.3.0/ instance SqlString Html where +-- | /Since: 2.4.0/ +instance SqlString a => SqlString (Maybe a) where -- | @FROM@ clause: bring entities into scope. diff --git a/src/Database/Esqueleto/Internal/Sql.hs b/src/Database/Esqueleto/Internal/Sql.hs index 2fb8dcf..d32ecb1 100644 --- a/src/Database/Esqueleto/Internal/Sql.hs +++ b/src/Database/Esqueleto/Internal/Sql.hs @@ -488,6 +488,7 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where (%) = unsafeSqlValue "'%'" concat_ = unsafeSqlFunction "CONCAT" (++.) = unsafeSqlBinOp " || " + castString = veryUnsafeCoerceSqlExprValue subList_select = EList . sub_select subList_selectDistinct = subList_select . distinct