3 ms·
> The third approach is to safely wrap the database and its columns with code in a way that is composable. Like Haskell's Esqueleto[1] which lets you work with
by jolmg 8y ago
> The third approach is to safely wrap the database and its columns with code in a way that is composable.
Like Haskell's Esqueleto[1] which lets you work with SQL code as Haskell functions and values. As an example from the documentation:
select $ from $ \p -> do
where_ (p ^. PersonAge >=. just (val 18))
return p
results in roughly:
SELECT * FROM Person
WHERE Person.age >= 18
This lets you define new functions to use in your queries that use SQL functions. For example, you can define `isPrefixOf` for SQL using the SQL functions `char_length()` and `left()`. The use of the function will be expanded into the corresponding SQL code when the query runs. Like,
select $ from $ \p -> do
where_ $ val "John" `isPrefixOfE` p ^. PersonName
return p
could result in:
SELECT * FROM Person
WHERE LEFT(Person.name, CHAR_LENGTH('John')) = 'John'
[1] https://www.stackage.org/haddock/nightly-2019-05-07/esqueleto-3.0.0/Database-Esqueleto.html https://www.stackage.org/haddock/nightly-2019-05-07/esquelet...