renderQueryToText

This commit is contained in:
parsonsmatt 2019-08-28 09:40:01 -06:00 committed by Chris Allen
parent 3db6361d2c
commit a36f3f7bfe
5 changed files with 100 additions and 5 deletions

View File

@ -1,6 +1,9 @@
Unreleased Unreleased (3.1.1)
======== ========
- @parsonsmatt
- [](): Added `renderQueryToText` and related functions.
3.1.0 3.1.0
======= =======

View File

@ -86,6 +86,12 @@ module Database.Esqueleto
, insertSelectCount , insertSelectCount
, (<#) , (<#)
, (<&>) , (<&>)
-- ** Rendering Queries
, renderQueryToText
, renderQuerySelect
, renderQueryUpdate
, renderQueryDelete
, renderQueryInsertInto
-- * Internal.Language -- * Internal.Language
, From , From
-- * RDBMS-specific modules -- * RDBMS-specific modules

View File

@ -2031,6 +2031,90 @@ toRawSql mode (conn, firstIdentState) query =
, makeLocking lockingClause , makeLocking lockingClause
] ]
-- | Renders a 'SqlQuery' into a 'Text' value along with the list of
-- 'PersistValue's that would be supplied to the database for @?@ placeholders.
--
-- You must ensure that the 'Mode' you pass to this function corresponds with
-- the actual 'SqlQuery'. If you pass a query that uses incompatible features
-- (like an @INSERT@ statement with a @SELECT@ mode) then you'll get a weird
-- result.
--
-- @since 3.1.1
renderQueryToText
:: (SqlSelect a r, BackendCompatible SqlBackend backend, Monad m)
=> Mode
-- ^ Whether to render as an 'SELECT', 'DELETE', etc.
-> SqlQuery a
-- ^ The SQL query you want to render.
-> R.ReaderT backend m (T.Text, [PersistValue])
renderQueryToText mode query = do
backend <- R.ask
let (builder, pvals) = toRawSql mode (backend, initialIdentState) query
pure (builderToText builder, pvals)
-- | Renders a 'SqlQuery' into a 'Text' value along with the list of
-- 'PersistValue's that would be supplied to the database for @?@ placeholders.
--
-- You must ensure that the 'Mode' you pass to this function corresponds with
-- the actual 'SqlQuery'. If you pass a query that uses incompatible features
-- (like an @INSERT@ statement with a @SELECT@ mode) then you'll get a weird
-- result.
--
-- @since 3.1.1
renderQuerySelect
:: (SqlSelect a r, BackendCompatible SqlBackend backend, Monad m)
=> SqlQuery a
-- ^ The SQL query you want to render.
-> R.ReaderT backend m (T.Text, [PersistValue])
renderQuerySelect = renderQueryToText SELECT
-- | Renders a 'SqlQuery' into a 'Text' value along with the list of
-- 'PersistValue's that would be supplied to the database for @?@ placeholders.
--
-- You must ensure that the 'Mode' you pass to this function corresponds with
-- the actual 'SqlQuery'. If you pass a query that uses incompatible features
-- (like an @INSERT@ statement with a @SELECT@ mode) then you'll get a weird
-- result.
--
-- @since 3.1.1
renderQueryDelete
:: (SqlSelect a r, BackendCompatible SqlBackend backend, Monad m)
=> SqlQuery a
-- ^ The SQL query you want to render.
-> R.ReaderT backend m (T.Text, [PersistValue])
renderQueryDelete = renderQueryToText DELETE
-- | Renders a 'SqlQuery' into a 'Text' value along with the list of
-- 'PersistValue's that would be supplied to the database for @?@ placeholders.
--
-- You must ensure that the 'Mode' you pass to this function corresponds with
-- the actual 'SqlQuery'. If you pass a query that uses incompatible features
-- (like an @INSERT@ statement with a @SELECT@ mode) then you'll get a weird
-- result.
--
-- @since 3.1.1
renderQueryUpdate
:: (SqlSelect a r, BackendCompatible SqlBackend backend, Monad m)
=> SqlQuery a
-- ^ The SQL query you want to render.
-> R.ReaderT backend m (T.Text, [PersistValue])
renderQueryUpdate = renderQueryToText UPDATE
-- | Renders a 'SqlQuery' into a 'Text' value along with the list of
-- 'PersistValue's that would be supplied to the database for @?@ placeholders.
--
-- You must ensure that the 'Mode' you pass to this function corresponds with
-- the actual 'SqlQuery'. If you pass a query that uses incompatible features
-- (like an @INSERT@ statement with a @SELECT@ mode) then you'll get a weird
-- result.
--
-- @since 3.1.1
renderQueryInsertInto
:: (SqlSelect a r, BackendCompatible SqlBackend backend, Monad m)
=> SqlQuery a
-- ^ The SQL query you want to render.
-> R.ReaderT backend m (T.Text, [PersistValue])
renderQueryInsertInto = renderQueryToText INSERT_INTO
-- | (Internal) Mode of query being converted by 'toRawSql'. -- | (Internal) Mode of query being converted by 'toRawSql'.
data Mode = data Mode =

View File

@ -61,6 +61,11 @@ module Database.Esqueleto.Internal.Sql
, veryUnsafeCoerceSqlExprValue , veryUnsafeCoerceSqlExprValue
, veryUnsafeCoerceSqlExprValueList , veryUnsafeCoerceSqlExprValueList
-- * Helper functions -- * Helper functions
, renderQueryToText
, renderQuerySelect
, renderQueryUpdate
, renderQueryDelete
, renderQueryInsertInto
, makeOrderByNoNewline , makeOrderByNoNewline
, uncommas' , uncommas'
, parens , parens

View File

@ -1437,10 +1437,6 @@ testCountingRows run = do
[Value n] <- select $ from $ return . countKind [Value n] <- select $ from $ return . countKind
liftIO $ (n :: Int) `shouldBe` expected liftIO $ (n :: Int) `shouldBe` expected
tests :: Run -> Spec tests :: Run -> Spec
tests run = do tests run = do
describe "Tests that are common to all backends" $ do describe "Tests that are common to all backends" $ do
@ -1460,6 +1456,7 @@ tests run = do
testMathFunctions run testMathFunctions run
testCase run testCase run
testCountingRows run testCountingRows run
testRenderSql run