ihp-1.4.0: Haskell Web Framework
Safe HaskellNone
LanguageGHC2021

IHP.QueryBuilder.HasqlHelpers

Description

Shared helpers for building dynamic hasql queries.

These are used by both ihp-datasync (DynamicQuery) and ihp-ide (Controller) for executing dynamically-constructed SQL through hasql.

Synopsis

Documentation

wrapDynamicQuery :: Snippet -> Snippet Source #

Wraps a SQL query snippet so that each row is returned as a JSON object.

This is needed because hasql decoders are positional and don't provide column name metadata. By wrapping with row_to_json, we get column names in the JSON keys, which we can then decode into a list of fields.

Uses a CTE (Common Table Expression) which works for both SELECT queries and DML statements (INSERT, UPDATE, DELETE) with RETURNING:

WITH _ihp_dynamic_result AS (...original query...) SELECT row_to_json(t)::jsonb FROM _ihp_dynamic_result AS t

quoteIdentifier :: Text -> Snippet Source #

Quote a SQL identifier (table name, column name) to prevent SQL injection.

Wraps the identifier in double quotes and escapes any embedded double quotes by doubling them, following the SQL standard.